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

(function(a){var e=function(){};a.extend(e.prototype,{name:"ElementSelect",options:{element:null,variable:null},initialize:function(c,d){this.options=a.extend({},this.options,d);var b=this;this.element=c;this.list=c.children("ul");this.hidden=this.list.find("li.hidden").detach();c.delegate("div.delete","click",function(){a(this).parent("li").slideUp(400,function(){a(this).remove();b.orderOptions()})}).delegate("div.name-input input","blur",function(){var b=a(this).closest("li"),c=b.find("div.panel input:text");
if(a(this).val()!=""&&c.val()==""){var d=a.String.slugify(a(this).val());c.val(d);b.find("a.trigger").text(d)}}).delegate("div.panel input:text","keydown",function(c){c.stopPropagation();c.which==13&&b.setOptionValue(a(this).closest("li"));c.which==27&&b.removeOptionPanel(a(this).closest("li"))}).delegate("input.accept","click",function(){b.setOptionValue(a(this).closest("li"))}).delegate("a.cancel","click",function(){b.removeOptionPanel(a(this).closest("li"))}).delegate("a.trigger","click",function(){a(this).hide().closest("li").find("div.panel").addClass("active").find("input:text").focus()}).find("div.add").bind("click",
function(){b.hidden.clone().removeClass("hidden").appendTo(b.list).slideDown(200).effect("highlight",{},1E3);b.orderOptions()});this.list.sortable({handle:"div.sort-handle",containment:this.list.parent().parent(),placeholder:"dragging",axis:"y",opacity:1,revert:75,delay:100,tolerance:"pointer",zIndex:99,start:function(c,a){a.placeholder.height(a.helper.height());b.list.sortable("refreshPositions")},stop:function(){b.orderOptions()}})},setOptionValue:function(c){var d=c.find("div.panel input:text"),
b=a.String.slugify(d.val());b==""&&(b=(b=a.String.slugify(c.find("div.name-input input").val()))?b:"42");d.val(b);c.find("a.trigger").text(b);this.removeOptionPanel(c)},removeOptionPanel:function(a){a.find("div.panel input:text").val(a.find("a.trigger").show().text());a.find("div.panel").removeClass("active")},orderOptions:function(){var c=/^(\S+\[options\])\[\d+\](\[name\]|\[value\])$/;this.list.children("li").each(function(d){a(this).find("input").each(function(){a(this).attr("name")&&a(this).attr("name",
a(this).attr("name").replace(c,"$1["+d+"]$2"))})})}});a.fn[e.prototype.name]=function(){var c=arguments,d=c[0]?c[0]:null;return this.each(function(){var b=a(this);if(e.prototype[d]&&b.data(e.prototype.name)&&d!="initialize")b.data(e.prototype.name)[d].apply(b.data(e.prototype.name),Array.prototype.slice.call(c,1));else if(!d||a.isPlainObject(d)){var f=new e;e.prototype.initialize&&f.initialize.apply(f,a.merge([b],c));b.data(e.prototype.name,f)}else a.error("Method "+d+" does not exist on jQuery."+
e.name)})}})(jQuery);
