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

var ElementSelect=new Class({initialize:function(a){this.setOptions({element:null,variable:null},a);var b=this;this.element=$(this.options.element);this.options_list=this.element.getElement("ul");this.hidden=this.options_list.getElement("li.hidden").remove();this.sortlist=new SortableList({handle:"div.sort-handle",opacity:"0.5",onDragStart:function(c,e){e.setStyle("display","none")},onComplete:function(){b.orderOptions()}});this.sortlist.addList(this.options_list);this.options_list.getElements("li").each(function(c){b.attachOptionEvents(c)});
this.element.getElement("div.add").addEvent("click",function(){b.addOption()})},attachOptionEvents:function(a){var b=this,c=a.getElement("a.trigger"),e=a.getElement('div.panel input[type="text"]');a.getElement("div.delete").addEvent("click",function(){b.deleteOption(a)});a.getElement("div.name-input input").addEvent("blur",function(){if(this.getValue()!=""&&e.getValue()==""){var d=Zoo.String.slugify(this.getValue());e.value=d;c.setText(d);this.removeEvents()}});c.addEvent("click",function(){this.setStyle("display",
"none");a.getElement("div.panel").addClass("active");e.focus();e.addEvent("keydown",function(d){d=new Event(d);d.key=="enter"&&b.setOptionValue(a);d.key=="esc"&&b.removeOptionPanel(a)});a.getElement("input.accept").addEvent("click",function(){b.setOptionValue(a)});a.getElement("a.cancel").addEvent("click",function(){b.removeOptionPanel(a)})});this.sortlist.addItems(new Array(a))},setOptionValue:function(a){var b=a.getElement('div.panel input[type="text"]'),c=Zoo.String.slugify(b.getValue());if(c==
"")c=(c=Zoo.String.slugify(a.getElement("div.name-input input").getValue()))?c:"42";b.value=c;a.getElement("a.trigger").setText(c);this.removeOptionPanel(a)},removeOptionPanel:function(a){a.getElement("a.trigger").removeProperty("style");a.getElement("div.panel").removeClass("active")},addOption:function(){var a=this.hidden.clone().removeClass("hidden").injectInside(this.options_list);this.attachOptionEvents(a);this.highlight(a);this.orderOptions()},deleteOption:function(a){this.sortlist.removeItems(new Array(a));
a.effects({duration:100,transition:Fx.Transitions.linear}).start({height:0,opacity:0}).chain(function(){a.remove();this.orderOptions()}.bind(this))},orderOptions:function(){var a=/^(\S+\[options\])\[\d+\](\[name\]|\[value\])$/;this.options_list.getElements("li").each(function(b,c){b.getElements("input").each(function(e){var d=e.getProperty("name");d&&e.setProperty("name",d.replace(a,"$1["+c+"]$2"))})})},highlight:function(a){a.setStyle("background-color","#f6f6f6").effects({duration:100,transition:Fx.Transitions.linear}).start({"background-color":"#ffffaa"}).chain(function(){this.setOptions({duration:700});
this.start({"background-color":"#f6f6f6"})})}});ElementSelect.implement(new Options); 
