/*


 AbsolutelySuperList version 0.1.
 http://github.com/AvocadoCorp/absolutely-super-list

 (c) 2012 Avocado Software, Inc.
 AbsolutelySuperList is freely distributable under the MIT license.
*/
(function(){function c(a){if(1<a.length||!a.is("ol")&&!a.is("ul"))throw"Selector should only contain one <ul> or <ol>.";this.$list=a;a=a[0];a.style.position="relative";$.browser.webkit&&!/chrome/i.test(window.navigator.userAgent)&&(a.style.WebkitPerspective="1000",a.style.WebkitBackfaceVisibility="hidden");this.$list.delegate(h,c.SUPER_LIST_RESIZED_EVENT,g(this.onChildListResized_,this));this.$list.delegate(n,"mousedown",g(this.onDraggableMousedown_,this));this.reload()}var h=".absolutely-super-item",
n=".absolutely-super-draggable",f=function(){};$.browser.webkit?f=function(a,b){a.style.WebkitTransition=b}:$.browser.mozilla?f=function(a,b){a.style.MozTransition=b}:$.browser.msie&&10<=parseInt($.browser.version,10)&&(f=function(a,b){a.style.msTransition=b});var g=function(a,b){return function(){return a.apply(b,arguments)}},j=function(a){if(a.is(h))return a;var b=$('<div class="absolutely-super-item"/>');b.insertBefore(a);a.appendTo(b);a=b[0];a.style.position="absolute";a.style.left="0px";a.style.right=
"0px";a.style.overflow="hidden";return b},k=function(a){var b=a[0],e=b.style.height;e&&(f(b,"top 0.2s ease"),b.style.height="");var d=a.height();e?(b.style.height=e,setTimeout(function(){f(b,"top 0.2s ease, height 0.2s ease");b.style.height=d+"px"},0)):(b.style.height=d+"px",setTimeout(function(){f(b,"top 0.2s ease, height 0.2s ease")},0));return d};c.ITEM_DRAGGED_EVENT="absolutely-super-dragged";c.SUPER_LIST_RESIZED_EVENT="super-list-resized";c.prototype.getItemPositions_=function(){var a=0,b=[];
this.$list.children().each(function(e,d){var c=$(d);c.find(".absolutely-super-drag-boundary").length?b.push(NaN):b.push(a);a+=c.data("height")});return b};c.prototype.onDraggableMousedown_=function(a){a.preventDefault();a.stopPropagation();var b=$(a.currentTarget).closest(h);this.$list.addClass("absolutely-super-dragging");b.children().addClass("absolutely-super-dragging-item");var e=b[0];e.style.zIndex="100000";e.style.overflow="visible";f(e,"height 0.2s ease");b.data("dragging",!0);var e=b.index(),
d=b.position().top,c=this.getItemPositions_();this.dragInfo_={item:b,boundDrag:g(this.onDrag_,this),boundMouseup:g(this.onDraggableMouseup_,this),currentTop:d,offsetY:a.pageY-d,startIndex:e,currentIndex:e,boundaries:c};$(document.body).bind("mousemove",this.dragInfo_.boundDrag).bind("mouseup",this.dragInfo_.boundMouseup)};c.prototype.onDrag_=function(a){var b=a.pageY-this.dragInfo_.offsetY;this.dragInfo_.item[0].style.top=b+"px";for(var a=this.dragInfo_.boundaries,e=this.dragInfo_.currentIndex,d=
0,c=e+1,f=a.length;!isNaN(a[c])&&c<f&&b>a[c];c++)d++;for(c=e-1;!isNaN(a[c])&&0<=c&&b<a[c];c--)d--;0!=d&&(b=e+d,this.moveItem(e,b),this.dragInfo_.currentIndex=b,this.dragInfo_.currentTop=a[b],this.dragInfo_.boundaries=this.getItemPositions_())};c.prototype.onDraggableMouseup_=function(){$(document.body).unbind("mousemove",this.dragInfo_.boundDrag).unbind("mouseup",this.dragInfo_.boundMouseup);var a=this.dragInfo_.item,b=this.dragInfo_.startIndex,e=this.dragInfo_.currentIndex;delete this.dragInfo_;
this.$list.removeClass("absolutely-super-dragging");a.children().removeClass("absolutely-super-dragging-item");a.data("dragging",null);var d=a[0];f(d,"top 0.2s ease, height 0.2s ease");d.style.overflow="hidden";setTimeout(function(){d.style.zIndex=""},250);this.repositionElements_();b!=e&&this.$list.trigger(c.ITEM_DRAGGED_EVENT,[b,e])};c.prototype.onChildListResized_=function(a,b){var e=$(a.currentTarget),d=e.data("height")-b;e.data("height",d);e[0].style.height=d+"px";this.repositionElements_()};
c.prototype.reload=function(){for(var a=this.$list.children(),b=0,e=a.length;b<e;b++){var d=a.eq(b),d=j(d),c=k(d);d.data("height",c)}this.repositionElements_()};c.prototype.repositionElements_=function(){for(var a=0,b=this.$list.children(),e=0;e<b.length;e++){var d=b.eq(e);d.data("dragging")||(d[0].style.top=a+"px");a+=d.data("height")}this.$list[0].style.height=a+"px";b=parseInt(this.$list.data("height"),10);!isNaN(b)&&b!=a&&this.$list.parent().trigger(c.SUPER_LIST_RESIZED_EVENT,b-a);this.$list.data("height",
a)};c.prototype.length=function(){return this.$list.children().length};c.prototype.isEmpty=function(){return 0==this.length()};c.prototype.html=function(a){this.$list.html(a);this.reload()};c.prototype.insertAtIndex=function(a,b){var c=j($(a)),d=this.$list.children().eq(b);d.length?c.insertBefore(d):c.appendTo(this.$list);this.reload()};c.prototype.indexBySelector=function(a){return this.find(a).closest(h).index()};c.prototype.removeAtIndex=function(a){this.$list.children().eq(a).remove();this.repositionElements_()};
c.prototype.removeBySelector=function(a){this.removeAtIndex(this.indexBySelector(a))};c.prototype.moveItem=function(a,b){var c=this.$list.children(),d=c.length;a==b||(a>=d||b>=d)||(d=c.eq(a),c=c.eq(b),d.detach(),a>b?d.insertBefore(c):d.insertAfter(c),setTimeout(g(function(){this.repositionElements_()},this),50))};c.prototype.sort=function(a){for(var b=this.$list.children(),c=b.length,d=[],f=0;f<c;f++)d.push(b.eq(f).children());d.sort(a||function(a,b){return a.text().localeCompare(b.text())});for(f=
0;f<c;f++)a=d[f].parent(),a.detach(),a.appendTo(this.$list);setTimeout(g(function(){this.repositionElements_()},this),50)};c.prototype.refreshAtIndex=function(a){var a=this.$list.children().eq(a),b=k(a);a.data("height")!=b&&(a.data("height",b),this.repositionElements_())};c.prototype.refreshBySelector=function(a){this.refreshAtIndex(this.indexBySelector(a))};for(var l="empty addClass removeClass toggleClass find bind unbind delegate".split(" "),i=0;i<l.length;i++){var m=l[i];c.prototype[m]=function(a){return function(){return this.$list[a].apply(this.$list,
arguments)}}(m)}window.require&&window.define?define([],function(){return c}):window.AbsolutelySuperList=c})();