//-- copyright
// Copyright (C)2013-2014 the OpenProject Foundation (OPF)
// Copyright (C)2011 Stephan Eckardt, Tim Felgentreff, Marnen Laibow-Koser, Sandro Munda
// Copyright (C)2010-2011 friflaj
// Copyright (C)2010 Maxime Guilbot, Andrew Vit, Joakim Kolsjö, ibussieres, Daniel Passos, Jason Vasquez, jpic, Emiliano Heyns
// Copyright (C)2009-2010 Mark Maglana
// Copyright (C)2009 Joe Heck, Nate Lowrie
// The copyright follows:
// Copyright (C) 2010-2011 - Emiliano Heyns, Mark Maglana, friflaj
// Copyright (C) 2011 - Jens Ulferts, Gregor Schmidt - Finn GmbH - Berlin, Germany
// See doc/COPYRIGHT.rdoc for more details.
RB.Backlog=function(e){return RB.Object.create({initialize:function(t){this.$=e(t),this.el=t,this.$.data("this",this),this.getList().sortable({connectWith:".stories",dropOnEmpty:!0,start:this.dragStart,stop:this.dragStop,update:this.dragComplete,receive:this.dragChanged,remove:this.dragChanged,containment:e("#backlogs_container"),scroll:!0,helper:function(t,n){var i=e(n).clone();return i.css("position","absolute"),i.get(0)}}),this.$.find(".add_new_story").click(this.handleNewStoryClick),this.isSprintBacklog()&&(RB.Factory.initialize(RB.Sprint,this.getSprint()),this.burndown=RB.Factory.initialize(RB.Burndown,this.$.find(".show_burndown_chart")),this.burndown.setSprintId(this.getSprint().data("this").getID())),this.getStories().each(function(){RB.Factory.initialize(RB.Story,this)}),this.isSprintBacklog()&&this.refresh()},dragChanged:function(){e(this).parents(".backlog").data("this").refresh()},dragComplete:function(e,t){var n=null===t.sender||void 0===t.sender;n&&t.item.data("this").saveDragResult()},dragStart:function(e,t){t.item.addClass("dragging")},dragStop:function(t,n){n.item.removeClass("dragging"),e.browser.msie&&e.browser.version<=7&&n.item.css("z-index",0)},getSprint:function(){return e(this.el).find(".model.sprint").first()},getStories:function(){return this.getList().children(".story")},getList:function(){return this.$.children(".stories").first()},handleNewStoryClick:function(t){var n=e(this).parents(".header").find(".toggler");n.hasClass("closed")&&n.click(),t.preventDefault(),e(this).parents(".backlog").data("this").newStory()},isSprintBacklog:function(){return 1===e(this.el).find(".sprint").length},newStory:function(){var t,n;t=e("#story_template").children().first().clone(),this.getList().prepend(t),n=RB.Factory.initialize(RB.Story,t[0]),n.edit(),t.find(".editor").first().focus()},refresh:function(){this.recalcVelocity(),this.recalcOddity()},recalcVelocity:function(){var t;return this.isSprintBacklog()?(t=0,this.getStories().each(function(){t+=e(this).data("this").getPoints()}),this.$.children(".header").children(".velocity").text(t),void 0):!0},recalcOddity:function(){this.$.find(".story:even").removeClass("odd").addClass("even"),this.$.find(".story:odd").removeClass("even").addClass("odd")}})}(jQuery);