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

Zoo.ElementRepeatable=new Class({initialize:function(a){this.setOptions({element:"",msgDeleteElement:null,msgSortElement:null},a);this.element=$(this.options.element);this.list=this.element.getElement("ul.repeatable-list");this.hidden=this.list.getElement("li.hidden").remove();this.count=this.list.getElements("li.repeatable-element").length;this.list.getElements("li.repeatable-element").each(function(b){this.attachEvents(b)}.bind(this));this.sort=new SortableList({handle:"div.sort",opacity:0.5,onClone:function(b){b.getElements("input").each(function(c){c.setProperty("name",
"")})},onDragStart:function(b,c){c.addClass("ghost")}});this.sort.addList(this.list);this.element.getElement("div.add").addEvent("click",function(){this.addElement()}.bind(this))},attachEvents:function(a){var b=new Element("div",{"class":"repeatable-content"});a.getChildren().each(function(c){c.injectInside(b)});b.injectInside(a);(new Element("div",{"class":"sort",title:this.options.msgSortElement})).injectInside(a);(new Element("div",{"class":"delete",title:this.options.msgDeleteElement})).injectInside(a).addEvent("click",
function(){this.deleteElement(a)}.bind(this))},addElement:function(){var a=(new Element("li",{"class":"repeatable-element"})).setHTML(this.hidden.innerHTML.replace(/(elements\[\S+])\[(-?\d+)\]/g,"$1["+this.count++ +"]"));this.attachEvents(a);this.sort.addItems(new Array(a));a.injectInside(this.list);a.getElements("*").each(function(b){if(b.value)b.value=null});a.getElements("script").each(function(b){eval(b.innerHTML)});this.highlight(a)},deleteElement:function(a){this.sort&&this.sort.removeItems(new Array(a));
a.effects({duration:100,transition:Fx.Transitions.linear}).start({height:0,opacity:0}).chain(function(){a.remove()})},highlight:function(a){a.setStyle("background-color","#ffffff").effects({duration:100,transition:Fx.Transitions.linear}).start({"background-color":"#ffffaa"}).chain(function(){this.setOptions({duration:700});this.start({"background-color":"#ffffff"})})}});Zoo.ElementRepeatable.implement(new Options); 
