/* Copyright (C) 2007 - 2010 YOOtheme GmbH, YOOtheme License (http://www.yootheme.com/license) */

var SortableTree=new Class({options:{onStart:Class.empty,onComplete:Class.empty,onBeforeDrop:Class.empty,onAfterDrop:Class.empty,childTag:"li",parentTag:"ul",markPrefix:"drop-",clone:true,cloneOpacity:0.3,elementOpacity:0.5,handle:false,snap:5,constrain:false},initialize:function(a,b){this.setOptions(b);this.tree=$(a);this.elements=[];this.idle=true;this.attachEvents()},attachEvents:function(){this.tree.getElementsBySelector(this.options.childTag).each(function(a){this.elements.push(a);if(!a._startsort)a._startsort=
this.start.bindWithEvent(this,a);(this.options.handle?a.getElement(this.options.handle)||a:a).addEvent("mousedown",a._startsort)},this)},getClone:function(a,b){if(!this.options.clone)return(new Element("div")).inject(document.body);var c=b.getPosition();this.offset=a.page.y-c.y;return b.clone().setStyles({margin:"0px",position:"absolute",left:c.x,top:a.page.y-this.offset,width:b.getStyle("width"),opacity:this.options.cloneOpacity}).inject(this.tree)},start:function(a,b){if(this.idle){this.idle=false;
this.dropPosition=null;this.clone=this.getClone(a,b);this.element=b;this.element.setStyle("opacity",this.options.elementOpacity);this.drag=new Drag.Element(this.clone,{snap:this.options.snap,container:this.options.constrain&&this.tree,droppables:this.elements,onDrag:this.hover.bind(this),onDrop:this.drop.bind(this),onComplete:this.stop.bind(this)});this.clone.injectBefore(this.element);this.drag.start(a)}},hover:function(){var a=this.droppable(this.drag.overed);if(a){var b=a.getCoordinates();b=(b.top+
b.height-this.drag.mouse.now.y)/b.height;this.dropPosition=b<0.33?"bottom":b>0.77?"top":"insert"}this.mark(a)},droppable:function(a){if(a){if(a==this.element)return null;for(var b=a.parentNode;b!=this.tree;b=b.parentNode)if(b==this.element)return null}return a},mark:function(a){var b=this.options.markPrefix;this.elements.each(function(c){[b+"top",b+"bottom",b+"insert"].each(function(d){c.removeClass(d)})});a&&a.addClass(b+this.dropPosition)},drop:function(a,b){if(this.droppable(b)){this.fireEvent("onBeforeDrop",
[this.element,b,this.dropPosition]);switch(this.dropPosition){case "top":this.element.injectBefore(b);break;case "bottom":this.element.injectAfter(b);break;case "insert":var c=b.getElement(this.options.parentTag);c||(c=(new Element(this.options.parentTag)).inject(b));this.element.injectTop(c);break}this.fireEvent("onAfterDrop",[this.element,b,this.dropPosition])}},stop:function(){this.mark();this.drag.detach();var a=this.element.getCoordinates();(new Fx.Styles(this.clone,{duration:200,wait:false})).start({top:a.top,
left:a.left}).chain(function(){this.idle=true;this.clone.remove();this.element.setStyle("opacity",1);this.elements=this.tree.getElementsBySelector(this.options.childTag);this.fireEvent("onComplete",this.element)}.bind(this))}});SortableTree.implement(new Events,new Options);
Drag.Element=Drag.Base.extend({options:{droppables:[],container:false,overflown:[]},initialize:function(a,b){this.setOptions(b);this.element=$(a);this.droppables=$$(this.options.droppables);this.container=$(this.options.container);this.position={element:this.element.getStyle("position"),container:false};if(this.container)this.position.container=this.container.getStyle("position");if(!["relative","absolute","fixed"].contains(this.position.element))this.position.element="absolute";var c=this.element.getStyle("top").toInt(),
d=this.element.getStyle("left").toInt();if(this.position.element=="absolute"&&!["relative","absolute","fixed"].contains(this.position.container)){c=$chk(c)?c:this.element.getTop(this.options.overflown);d=$chk(d)?d:this.element.getLeft(this.options.overflown)}else{c=$chk(c)?c:0;d=$chk(d)?d:0}this.element.setStyles({top:c,left:d,position:this.position.element});this.parent(this.element)},start:function(a){this.overed=null;if(this.container){var b=this.container.getCoordinates(),c=this.element.getCoordinates();
this.options.limit=this.position.element=="absolute"&&!["relative","absolute","fixed"].contains(this.position.container)?{x:[b.left,b.right-c.width],y:[b.top,b.bottom-c.height]}:{y:[0,b.height-c.height],x:[0,b.width-c.width]}}this.parent(a)},drag:function(a){this.parent(a);a=this.out?false:this.droppables.filter(this.checkAgainst,this).getLast();if(this.overed!=a){if(this.overed){this.overed.fireEvent("leave",[this.element,this]);this.fireEvent("onLeave",[this.element,this.overed])}a&&this.fireEvent("onEnter",
[this.element,a]);this.overed=a?a.fireEvent("over",[this.element,this]):null}return this},checkAgainst:function(a){a=a.getCoordinates(this.options.overflown);var b=this.mouse.now;return b.x>a.left&&b.x<a.right&&b.y<a.bottom&&b.y>a.top},stop:function(){this.overed&&!this.out?this.overed.fireEvent("drop",[this.element,this]):this.element.fireEvent("emptydrop",this);this.fireEvent("onDrop",[this.element,this.overed]);this.parent();return this}}); 
