ACMS.Dispatch.Layout=function(){ACMS.Dispatch.Layout.load=true;var $container=$(".js-acms_layout_container"),$menu=$(".js-acms_layout_menu");var timer;var $layoutBox=$(".acms-admin-layout-unit-menu");$(window).resize(function(){clearTimeout(timer);timer=setTimeout(function(){$layoutBox.css("max-height",window.innerHeight-250)},300)}).trigger("resize");setAction(document);layoutSortable($(".js-acms_layout, .js-acms_layout .js-acms_layout_contents"));$menu.draggable({handle:".js-acms_layout_menu_handle",
stop:function(event,ui){var top=getTop(ui.helper);ui.helper.css("position","fixed");ui.helper.css("top",top+"px")}});$(".js-layout_edit_form .js-acms_layout_label").hide();$(".js-layout_edit_form .js-acms_layout_container").css("min-height",200);$(".js-acms_layout_list .js-acms_layout_row").draggable({connectToSortable:".js-acms_layout",revert:"invalid",zIndex:999,helper:function(){var $helper=$("<div></div>").append($(this).find(".js-acms_layout_label").clone());$helper.addClass("js-acms_layout_draggable_helper");
return $helper},drag:function(event,ui){ui.helper.find(".js-acms_layout_menu_label").hide();ui.helper.width(200)},start:function(event,ui){$container.addClass("js-acms_layout_frame")},stop:function(event,ui){layoutSortable($(".js-acms_layout .js-acms_layout_contents"));$container.removeClass("js-acms_layout_frame")}});$(".js-layout_preview").bind("click",function(){var $container=$(".preview-container"),$form=$(".js-layout_edit_form"),$btn=$form.find("[name^=ACMS_POST_Layout_Save]"),name=$btn.attr("name"),
nameAry=name.split(":"),url=$(this).attr("href");nameAry[0]="ACMS_POST_Layout_Preview";$btn.attr("name",nameAry.join(":"));$.ajax({url:$form.attr("action"),type:"post",data:$form.serialize(),success:function(){$btn.attr("name",name);$.get(url,function(res){$container.html(res);$container.animate({width:"show",opacity:"show"},"fast");$(".js-preview_close").bind("click",function(){$container.animate({width:"hide",opacity:"hide"},"fast")})})},error:function(){}});return false});$(".js-layout_save").unbind("click").bind("click",
function(){$(".js-layout_edit_form").submit();return false});function layoutSortable($sortableContents){$sortableContents.sortable({items:".js-acms_layout_row",cursor:"move",connectWith:".js-acms_layout_contents:not(.js-acms_layout_module_box)",placeholder:{element:function(currentItem){if($(currentItem).find(".js-acms_layout_module_box").length>0)return $('<div class="acms-admin-module-placeholder">')[0];else return $('<div class="acms-admin-layout-placeholder">')[0]},update:function(){return}},
cursorAt:{top:40,left:100},forceHelperSize:true,forcePlaceholderSize:true,zIndex:999,scroll:true,opacity:0.5,start:function(event,ui){var $helper=$(ui.helper);$helper.find(".js-acms_layout_label").show();$helper.find(".js-acms_layout-menu-inner").show();$helper.find(".js-acms_layout_menu_label").hide();$helper.find(".js-acms_layout-unit").hide();ui.helper.width(200)},stop:function(event,ui){var $item=$(ui.item);$parent=$item.parent().closest(".js-acms_layout_row"),$column=$item.closest(".js-acms_layout_contents"),
$ids=$item.find("[name^=ids]:first"),$pid=$item.find("[name^=parent_]:first"),$col=$item.find("[name^=col_]:first"),$row=$item.find("[name^=row_]:first"),$class=$item.find("[name^=class_]:first"),$mid=$item.find("[name^=mid_]:first"),$tpl=$item.find("[name^=tpl_]:first"),pid=0,col=1,row=1;setAction(ui.item);$item.removeClass("js-acms_layout_list");$item.find(".js-acms_layout_label").hide();$item.find(".js-acms_layout-menu-inner").hide();$item.find(".js-acms_layout-unit").show();if(!$ids.val())$ids.val(ACMS.Dispatch.Utility.random(32));
if($parent.length){pid=$parent.find("[name^=ids]:first").val();col=Number($column.data("col"));_.each($column.children(),function(v,k){row=String(++k);$(v).find("[name^=row]:first").val(row)})}else{var $rows=$(".js-acms_layout").children(".js-acms_layout_row");_.each($rows,function(v,k){var $r=$(v);row=String(++k);$r.find("[name^=row]:first").val(row)})}$pid.val(pid);$col.val(col);$pid.attr("name","parent_"+$ids.val());$col.attr("name","col_"+$ids.val());$row.attr("name","row_"+$ids.val());$class.attr("name",
"class_"+$ids.val());$mid.attr("name","mid_"+$ids.val());$tpl.attr("name","tpl_"+$ids.val())}})}function setAction(elm){$(".js-module_management",elm).remove();$(".acms-admin-module-edit",elm).remove();$(".acms-admin-module-edit-wrapper").removeClass("acms-admin-module-edit-wrapper");$(".js-acms_layout_delete_unit",elm).unbind("click").bind("click",function(){var $box=$(this).closest(".js-acms_layout_row");$box.fadeOut("fast",function(){$box.remove()});return false});$(".js-acms_layout_select_module",
elm).unbind("click").bind("click",function(){var $unit=$(this).closest(".js-acms_layout_row"),$box=$unit.find(".js-acms_layout_contents"),$mid=$unit.find("[name^=mid]"),$tpl=$unit.find("[name^=tpl]");ACMS.Dispatch.showModuleDialog("index",$mid.val(),$tpl.val(),function(res,mid,tpl){$box.empty();$box.append(res);$mid.val(mid);$tpl.val(tpl);ACMS.Dispatch($box);setAction($box)});return false})}function getTop(ele){var eTop=ele.offset().top;var wTop=$(window).scrollTop();var top=eTop-wTop;return top}
};
