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

Zoo.BrowseCategories=new Class({initialize:function(a){this.setOptions({url:"index.php"},a);this.oldParent=null;if($("categories"))this.tree=new SortableTree("categories",{handle:".handle",onBeforeDrop:this.beforeDrop.bind(this),onAfterDrop:this.afterDrop.bind(this),onComplete:this.save.bind(this)})},beforeDrop:function(a){this.oldParent=a.getParent()},afterDrop:function(a,b,c){var d=null;switch(c){case "top":case "bottom":a.getElement("input[name^=parent]").setProperty("value",b.getElement("input[name^=parent]").getProperty("value"));
d=b.getParent();break;case "insert":a.getElement("input[name^=parent]").setProperty("value",b.getElement("input[name^=category]").getProperty("value"));d=a.getParent();break}this.updateOrdering(d);this.updateOrdering(this.oldParent)},updateOrdering:function(a){var b=this.tree,c=1;a.getChildren().each(function(d){d!=b.clone&&d.getElement("input[name^=ordering]").setProperty("value",c++)})},save:function(a){var b=new Hash;$("categories-default").getElementsBySelector("input[name^=parent], input[name^=ordering], input[type=hidden]").each(function(c){var d=
c.name,e=c.getValue();e===false||!d||c.disabled||b.set(d,e)});(new Ajax(this.options.url,{method:"post",data:b.merge({task:"saveorder",format:"raw"}).obj,onRequest:function(){a.addClass("loader")},onComplete:function(c){a.removeClass("loader");Message.show(c,true)}})).request()}});Zoo.BrowseCategories.implement(new Options);
Zoo.EditCategory=new Class({initialize:function(){var a=this;(new Array("apply","save")).each(function(b){$("toolbar-"+b).getElement("a").removeProperty("onclick").addEvent("click",function(){a.submit(b)})})},submit:function(a){$("name-edit").getElement('input[name="name"]').value==""?$("name-edit").getElement("span.message-name").setStyle("display","inline"):submitbutton(a)}}); 
