var wpWidgets;(function(a){wpWidgets={init:function(){var e,c=a("div.widgets-sortables"),b=!!("undefined"!=typeof isRtl&&isRtl),d=(isRtl?"marginRight":"marginLeft");a("#widgets-right").children(".widgets-holder-wrap").children(".sidebar-name").click(function(){var g=a(this).siblings(".widgets-sortables"),f=a(this).parent();if(!f.hasClass("closed")){g.sortable("disable");f.addClass("closed")}else{f.removeClass("closed");g.sortable("enable").sortable("refresh")}});a("#widgets-left").children(".widgets-holder-wrap").children(".sidebar-name").click(function(){a(this).parent().toggleClass("closed")});c.each(function(){if(a(this).parent().hasClass("inactive")){return true}var g=50,f=a(this).children(".widget").length;g=g+parseInt(f*48,10);a(this).css("minHeight",g+"px")});a("a.widget-action").live("click",function(){var h={},i=a(this).closest("div.widget"),f=i.children(".widget-inside"),g=parseInt(i.find("input.widget-width").val(),10);if(f.is(":hidden")){if(g>250&&f.closest("div.widgets-sortables").length){h.width=g+30+"px";if(f.closest("div.widget-liquid-right").length){h[d]=235-g+"px"}i.css(h)}wpWidgets.fixLabels(i);f.slideDown("fast")}else{f.slideUp("fast",function(){i.css({width:"",margin:""})})}return false});a("input.widget-control-save").live("click",function(){wpWidgets.save(a(this).closest("div.widget"),0,1,0);return false});a("a.widget-control-remove").live("click",function(){wpWidgets.save(a(this).closest("div.widget"),1,1,0);return false});a("a.widget-control-close").live("click",function(){wpWidgets.close(a(this).closest("div.widget"));return false});c.children(".widget").each(function(){wpWidgets.appendTitle(this);if(a("p.widget-error",this).length){a("a.widget-action",this).click()}});a("#widget-list").children(".widget").draggable({connectToSortable:"div.widgets-sortables",handle:"> .widget-top > .widget-title",distance:2,helper:"clone",zIndex:5,containment:"document",start:function(g,f){wpWidgets.fixWebkit(1);f.helper.find("div.widget-description").hide()},stop:function(g,f){if(e){a(e).hide()}e="";wpWidgets.fixWebkit()}});c.sortable({placeholder:"widget-placeholder",items:"> .widget",handle:"> .widget-top > .widget-title",cursor:"move",distance:2,containment:"document",start:function(g,f){wpWidgets.fixWebkit(1);f.item.children(".widget-inside").hide();f.item.css({margin:"",width:""})},stop:function(h,f){if(f.item.hasClass("ui-draggable")&&f.item.data("draggable")){f.item.draggable("destroy")}if(f.item.hasClass("deleting")){wpWidgets.save(f.item,1,0,1);f.item.remove();return}var g=f.item.find("input.add_new").val(),k=f.item.find("input.multi_number").val(),j=f.item.attr("id"),i=a(this).attr("id");f.item.css({margin:"",width:""});wpWidgets.fixWebkit();if(g){if("multi"==g){f.item.html(f.item.html().replace(/<[^<>]+>/g,function(l){return l.replace(/__i__|%i%/g,k)}));f.item.attr("id",j.replace(/__i__|%i%/g,k));k++;a("div#"+j).find("input.multi_number").val(k)}else{if("single"==g){f.item.attr("id","new-"+j);e="div#"+j}}wpWidgets.save(f.item,0,0,1);f.item.find("input.add_new").val("");f.item.find("a.widget-action").click();return}wpWidgets.saveOrder(i)},receive:function(h,g){var f=a(g.sender);if(!a(this).is(":visible")||this.id.indexOf("orphaned_widgets")!=-1){f.sortable("cancel")}if(f.attr("id").indexOf("orphaned_widgets")!=-1&&!f.children(".widget").length){f.parents(".orphan-sidebar").slideUp(400,function(){a(this).remove()})}}}).sortable("option","connectWith","div.widgets-sortables").parent().filter(".closed").children(".widgets-sortables").sortable("disable");a("#available-widgets").droppable({tolerance:"pointer",accept:function(f){return a(f).parent().attr("id")!="widget-list"},drop:function(g,f){f.draggable.addClass("deleting");a("#removing-widget").hide().children("span").html("")},over:function(g,f){f.draggable.addClass("deleting");a("div.widget-placeholder").hide();if(f.draggable.hasClass("ui-sortable-helper")){a("#removing-widget").show().children("span").html(f.draggable.find("div.widget-title").children("h4").html())}},out:function(g,f){f.draggable.removeClass("deleting");a("div.widget-placeholder").show();a("#removing-widget").hide().children("span").html("")}})},saveOrder:function(c){if(c){a("#"+c).closest("div.widgets-holder-wrap").find("img.ajax-feedback").css("visibility","visible")}var b={action:"widgets-order",savewidgets:a("#_wpnonce_widgets").val(),sidebars:[]};a("div.widgets-sortables").each(function(){if(a(this).sortable){b["sidebars["+a(this).attr("id")+"]"]=a(this).sortable("toArray").join(",")}});a.post(ajaxurl,b,function(){a("img.ajax-feedback").css("visibility","hidden")});this.resize()},save:function(g,d,e,b){var h=g.closest("div.widgets-sortables").attr("id"),f=g.find("form").serialize(),c;g=a(g);a(".ajax-feedback",g).css("visibility","visible");c={action:"save-widget",savewidgets:a("#_wpnonce_widgets").val(),sidebar:h};if(d){c.delete_widget=1}f+="&"+a.param(c);a.post(ajaxurl,f,function(i){var j;if(d){if(!a("input.widget_number",g).val()){j=a("input.widget-id",g).val();a("#available-widgets").find("input.widget-id").each(function(){if(a(this).val()==j){a(this).closest("div.widget").show()}})}if(e){b=0;g.slideUp("fast",function(){a(this).remove();wpWidgets.saveOrder()})}else{g.remove();wpWidgets.resize()}}else{a(".ajax-feedback").css("visibility","hidden");if(i&&i.length>2){a("div.widget-content",g).html(i);wpWidgets.appendTitle(g);wpWidgets.fixLabels(g)}}if(b){wpWidgets.saveOrder()}})},appendTitle:function(b){var c=a('input[id*="-title"]',b);if(c=c.val()){c=c.replace(/<[^<>]+>/g,"").replace(//g,">");a(b).children(".widget-top").children(".widget-title").children().children(".in-widget-title").html(": "+c)}},resize:function(){a("div.widgets-sortables").each(function(){if(a(this).parent().hasClass("inactive")){return true}var c=50,b=a(this).children(".widget").length;c=c+parseInt(b*48,10);a(this).css("minHeight",c+"px")})},fixWebkit:function(b){b=b?"none":"";a("body").css({WebkitUserSelect:b,KhtmlUserSelect:b})},fixLabels:function(b){b.children(".widget-inside").find("label").each(function(){var c=a(this).attr("for");if(c&&c==a("input",this).attr("id")){a(this).removeAttr("for")}})},close:function(b){b.children(".widget-inside").slideUp("fast",function(){b.css({width:"",margin:""})})}};a(document).ready(function(b){wpWidgets.init()})})(jQuery);