text: $(function(){var t=0,e=0,i=!0,n={};"undefined"!=typeof jQuery&&(jQuery.fn.sortable=function(t){return this.each(function(){var e=$(this),i=e.data("sortable");!i&&t instanceof Object&&(i=new Sortable(this,t),e.data("sortable",i)),i&&t in i&&i[i].apply(i,[].slice.call(arguments,1))})});var a=function(t){$('.main div[item="'+t+'"] input.one').each(function(){var t=$(this).parents("div.row"),e=$(this).attr("item"),i=$(this).attr("property"),n=$(this).outerWidth()-2;$(this).autocomplete({serviceUrl:"/moonlight/elements/autocomplete",params:{item:e},formatResult:function(t,e){return t.value+" <small>("+t.id+")</small>"},onSelect:function(e){t.find('input:hidden[name="'+i+'"]').val(e.id),t.find('span[container][name="'+i+'"]').html(e.value)},width:n,minChars:0})}),$('.main div[item="'+t+'"] table.elements tbody').each(function(){$(this).sortable({handle:".drag",chosenClass:"chosen",dragClass:"dragging",onEnd:function(e){if(e.newIndex===e.oldIndex)return!1;var i=[];$(e.to).find("tr").each(function(){var t=$(this).attr("elementId");i.push(t)}),$.blockUI(),$.post("/moonlight/elements/order",{item:t,elements:i},function(t){$.unblockUI()})}})})},o=function(n,r){$.getJSON("/moonlight/elements/list",{item:n,classId:r},function(r){r.html&&r.html.length&&($('.main div[item="'+n+'"]').hide().html(r.html).fadeIn(200),a(n),i=!1),++e==t?i&&$("div.empty").show():o(d[e].item,d[e].classId)}).fail(function(){$.alertDefaultError()})},r=function(t,i,n){var o={item:t,classId:i};if(n)for(var r in n)o[r]=n[r];$.blockUI(),$.getJSON("/moonlight/elements/list",o,function(i){$.unblockUI(),i.html&&i.html.length?($('.main div[item="'+t+'"]').html(i.html),a(t)):$('.main div[item="'+t+'"]').fadeOut(200,function(){--e||$("div.empty").show()})}).fail(function(){$.unblockUI(),$.alertDefaultError()})},d=[];$(".main div[item]").each(function(){var e=$(this).attr("item"),i=$(this).attr("classId");d.push({item:e,classId:i}),t++}),t&&o(d[0].item,d[0].classId),$("body").on("click",".main div[item] ul.header > li.h2",function(){var t=$(this),e=t.attr("display"),i=t.parents("div[item]"),n=i.find("div[list]"),o=i.attr("item"),r=i.attr("classId");"show"==e?(t.attr("display","hide"),n.hide(),$.post("/moonlight/elements/close",{item:o,classId:r})):"hide"==e?(t.attr("display","show"),n.show(),$.post("/moonlight/elements/open",{item:o,classId:r})):($.blockUI(),$.getJSON("/moonlight/elements/list",{item:o,classId:r,open:!0},function(t){$.unblockUI(),t.html&&($('.main div[item="'+o+'"]').html(t.html),a(o))}))}),$("body").on("click","table.elements th span[resetorder]",function(){var t=$(this).parents("div[item]"),e=t.attr("item"),i=t.attr("classId");r(e,i,{resetorder:!0})}),$("body").on("click","table.elements th span[order][direction]",function(){var t=$(this).parents("div[item]"),e=t.attr("item"),i=t.attr("classId"),n=$(this).attr("order"),a=$(this).attr("direction");r(e,i,{order:n,direction:a})}),$("body").on("click","table.elements td.editable",function(){var t=$(this),e=t.parent(),i=$(this).parents("div[item]"),n=(i.attr("item"),t.attr("mode"));e.attr("elementId");"edit"==n?(t.attr("mode","view"),t.find(".view-container").show(),t.find(".edit-container").hide(),t.find(".edit-container").find("input,textarea").attr("disabled","disabled")):(t.attr("mode","edit"),t.find(".view-container").hide(),t.find(".edit-container").show(),t.find(".edit-container").find("input,textarea").removeAttr("disabled").focus()),i.find('td.editable[mode="edit"]').length?i.find(".button.save:not(.disabled)").addClass("enabled"):i.find(".button.save:not(.disabled)").removeClass("enabled")}),$("body").on("click","table.elements td.editable input",function(t){t.stopPropagation()}),$("body").on("click","table.elements td.editable textarea",function(t){t.stopPropagation()}),$("body").on("click","table.elements td.editable div.checkbox",function(t){var e=$(this),i=e.parents("td"),n=(i.parent(),e.attr("name"),i.find("input:hidden"));1==n.val()?($(this).removeClass("checked"),n.val(0)):($(this).addClass("checked"),n.val(1)),t.stopPropagation()}),$("body").on("submit",'form[name="save"]',function(){var t=$(this).parents("div[item]");t.attr("item"),t.attr("classId");return!!t.find('td.editable[mode="edit"]').length&&(t.find("td.editable.invalid").removeClass("invalid"),$(this).ajaxSubmit({url:this.action,dataType:"json",success:function(e){if($.unblockUI(),e.error&&$.alert(e.error),e.errors)for(var i in e.errors)for(var n in e.errors[i])t.find('table.elements tr[elementId="'+i+'"] td.editable[name="'+n+'"]').addClass("invalid");if(e.views){for(var i in e.views)for(var n in e.views[i])t.find('table.elements tr[elementId="'+i+'"] td.editable[name="'+n+'"]').replaceWith(e.views[i][n]);t.find('td.editable[mode="edit"]').length||t.find(".button.save:not(.disabled)").removeClass("enabled")}},error:function(t){$.unblockUI(),$.alert(t.statusText)}}),!1)}),$("body").on("click",".button.save.enabled",function(){return $(this).parents("div[item]").find('form[name="save"]').submit(),!1}),$("body").on("mouseover","table.elements td.check",function(){$(this).parent().addClass("hover")}),$("body").on("mouseout","table.elements td.check",function(){$(this).parent().removeClass("hover")}),$("body").on("click","th.check",function(){var t=$(this).parent(),e=t.parents("table"),i=$(this).parents("div[item]"),a=i.attr("item");void 0===n[a]&&(n[a]=[]),t.hasClass("checked")?(n[a]=[],t.removeClass("checked"),e.find("tbody tr").each(function(){$(this).removeClass("checked")})):(t.addClass("checked"),e.find("tbody tr").each(function(){var t=$(this).attr("elementId");-1===n[a].indexOf(t)&&n[a].push(t),$(this).addClass("checked")})),n[a].length?(i.find(".button.copy:not(.disabled)").addClass("enabled"),i.find(".button.move:not(.disabled)").addClass("enabled"),i.find(".button.bind:not(.disabled)").addClass("enabled"),i.find(".button.unbind:not(.disabled)").addClass("enabled"),i.find(".button.favorite:not(.disabled)").addClass("enabled"),i.find(".button.delete:not(.disabled)").addClass("enabled")):(i.find(".button.copy:not(.disabled)").removeClass("enabled"),i.find(".button.move:not(.disabled)").removeClass("enabled"),i.find(".button.bind:not(.disabled)").removeClass("enabled"),i.find(".button.unbind:not(.disabled)").removeClass("enabled"),i.find(".button.favorite:not(.disabled)").removeClass("enabled"),i.find(".button.delete:not(.disabled)").removeClass("enabled"))}),$("body").on("click","td.check",function(){var t=$(this).parent(),e=$(this).parents("div[item]"),i=e.attr("item"),a=t.attr("elementId");void 0===n[i]&&(n[i]=[]);var o=n[i].indexOf(a);t.hasClass("checked")?(o>-1&&n[i].splice(o,1),t.removeClass("checked")):(-1===o&&n[i].push(a),t.addClass("checked")),n[i].length?(e.find(".button.copy:not(.disabled)").addClass("enabled"),e.find(".button.move:not(.disabled)").addClass("enabled"),e.find(".button.bind:not(.disabled)").addClass("enabled"),e.find(".button.unbind:not(.disabled)").addClass("enabled"),e.find(".button.favorite:not(.disabled)").addClass("enabled"),e.find(".button.delete:not(.disabled)").addClass("enabled")):(e.find(".button.copy:not(.disabled)").removeClass("enabled"),e.find(".button.move:not(.disabled)").removeClass("enabled"),e.find(".button.bind:not(.disabled)").removeClass("enabled"),e.find(".button.unbind:not(.disabled)").removeClass("enabled"),e.find(".button.favorite:not(.disabled)").removeClass("enabled"),e.find(".button.delete:not(.disabled)").removeClass("enabled"))}),$("body").on("click",".addition.unset[property]",function(){var t=$(this).parents("div.row"),e=$(this).attr("property");t.find('input:hidden[name="'+e+'"]').val(""),t.find('input:text[name="'+e+'_autocomplete"]').val(""),t.find('span[container][name="'+e+'"]').html("Не определено")}),$("body").on("click",".button.copy.enabled",function(){var t=$(this).parents("div[item]").attr("item");$.confirm(null,'.confirm[id="'+t+'_copy"]')}),$("body").on("click",".button.move.enabled",function(){var t=$(this).parents("div[item]").attr("item");$.confirm(null,'.confirm[id="'+t+'_move"]')}),$("body").on("click",".button.bind.enabled",function(){var t=$(this).parents("div[item]").attr("item");$.confirm(null,'.confirm[id="'+t+'_bind"]')}),$("body").on("click",".button.unbind.enabled",function(){var t=$(this).parents("div[item]").attr("item");$.confirm(null,'.confirm[id="'+t+'_unbind"]')}),$("body").on("click",".button.favorite.enabled",function(){var t=$(this).parents("div[item]"),e=t.attr("item");t.find('div[name="add"], .favorite-list.add').hide(),t.find('div[name="remove"], .favorite-list.remove').hide(),t.find(".favorite-list.add div[rubric]").hide(),t.find(".favorite-list.remove div[rubric]").hide();for(var i in n[e]){var a=n[e][i],o=t.find('table.elements tr[elementId="'+a+'"]').attr("rubrics").split(",");t.find(".favorite-list.add div[rubric]").each(function(){var e=$(this).attr("rubric");-1===o.indexOf(e)&&($(this).show(),t.find('div[name="add"], .favorite-list.add').show())});for(var r in o){var d=o[r];t.find('.favorite-list.remove div[rubric="'+d+'"]').show(),d&&t.find('div[name="remove"], .favorite-list.remove').show()}}$.confirm(null,'.confirm[id="'+e+'_favorite"]')}),$("body").on("click",".button.delete.enabled",function(){var t=$(this).parents("div[item]").attr("item");$.confirm(null,'.confirm[id="'+t+'_delete"]')}),$("body").on("click",".confirm .btn.copy",function(){var t,e,i=$(this).parents("div[item]"),a=$(this).parents(".confirm"),o=i.attr("item");if(a.find('input[type="radio"]:checked:not(:disabled), input[type="hidden"]').each(function(){t=$(this).attr("property"),e=$(this).val()}),!t)return!1;$.confirmClose(),$.blockUI(),$.post("/moonlight/elements/copy",{item:o,checked:n[o],name:t,value:e},function(t){$.unblockUI(function(){t.error?$.alert(t.error):t.copied&&(e?t.url&&(location.href=t.url):location.reload())})})}),$("body").on("click",".confirm .btn.move",function(){var t=$(this).parents("div[item]"),e=$(this).parents(".confirm"),i=t.attr("item"),a=t.attr("classId"),o=null;if(e.find('input[type="radio"]:checked:not(:disabled), input[type="hidden"]').each(function(){var t=$(this).attr("property"),e=$(this).val();o={name:t,value:e}}),!o)return!1;$.confirmClose(),$.blockUI(),$.post("/moonlight/elements/move",{item:i,classId:a,checked:n[i],name:o.name,value:o.value},function(t){$.unblockUI(function(){t.error?$.alert(t.error):t.moved&&t.url&&(location.href=t.url)})})}),$("body").on("click",".confirm .btn.bind",function(){var t=$(this).parents("div[item]"),e=$(this).parents(".confirm"),i=t.attr("item"),a=t.attr("classId"),o={},d=0;if(e.find('input[type="radio"]:checked:not(:disabled), input[type="hidden"]').each(function(){var t=$(this).attr("property"),e=$(this).val();e&&(o[t]=e,d++)}),!d)return!1;$.confirmClose(),$.blockUI(),$.post("/moonlight/elements/bind",{item:i,checked:n[i],ones:o},function(t){t.error?$.unblockUI(function(){$.alert(t.error)}):t.attached&&r(i,a)})}),$("body").on("click",".confirm .btn.unbind",function(){var t=$(this).parents("div[item]"),e=$(this).parents(".confirm"),i=t.attr("item"),a=t.attr("classId"),o={},d=0;if(e.find('input[type="radio"]:checked:not(:disabled), input[type="hidden"]').each(function(){var t=$(this).attr("property"),e=$(this).val();e&&(o[t]=e,d++)}),!d)return!1;$.confirmClose(),$.blockUI(),$.post("/moonlight/elements/unbind",{item:i,checked:n[i],ones:o},function(t){t.error?$.unblockUI(function(){$.alert(t.error)}):t.detached&&r(i,a)})}),$("body").on("click",".confirm .favorite-list.add div[rubric]",function(){var t=$(this).parents(".confirm"),e=$(this).parents("div[item]"),i=e.attr("classId"),a=e.attr("item"),o=t.attr("url"),d=$(this).attr("rubric");return!!o&&(!!d&&($.confirmClose(),$.blockUI(),void $.post(o,{item:a,checked:n[a],add_favorite_rubric:d},function(t){$.unblockUI(function(){t.error?$.alert(t.error):t.saved&&r(a,i)})}).fail(function(){$.unblockUI(),$.alertDefaultError()})))}),$("body").on("click",".confirm .favorite-list.remove div[rubric]",function(){var t=$(this).parents(".confirm"),e=$(this).parents("div[item]"),i=e.attr("classId"),a=e.attr("item"),o=t.attr("url"),d=$(this).attr("rubric");return!!o&&(!!d&&($.confirmClose(),$.blockUI(),void $.post(o,{item:a,checked:n[a],remove_favorite_rubric:d},function(t){$.unblockUI(function(){t.error?$.alert(t.error):t.saved&&r(a,i)})}).fail(function(){$.unblockUI(),$.alertDefaultError()})))}),$("body").on("keypress",'.confirm .favorite-new input[type="text"]',function(t){if(t||(t=window.event),t.keyCode)var e=t.keyCode;else if(t.which)e=t.which;13==e&&$(this).parents(".confirm").find(".btn.favorite").click()}),$("body").on("click",".confirm .btn.favorite",function(){var t=$(this).parents(".confirm"),e=$(this).parents("div[item]"),i=e.attr("classId"),a=e.attr("item"),o=t.attr("url"),d=t.find('.favorite-new input[type="text"]').val();return!!o&&(!!d&&($.confirmClose(),$.blockUI(),void $.post(o,{item:a,checked:n[a],new_favorite_rubric:d},function(t){$.unblockUI(function(){t.error?$.alert(t.error):t.saved&&r(a,i)})}).fail(function(){$.unblockUI(),$.alertDefaultError()})))}),$("body").on("click",".confirm .btn.remove",function(){var t=$(this).parents("div[item]"),e=t.attr("classId"),i=t.attr("item");$.confirmClose(),$.blockUI(),$.post("/moonlight/elements/delete",{item:i,checked:n[i]},function(t){t.error?$.unblockUI(function(){$.alert(t.error)}):t.deleted&&r(i,e)})}),$("body").on("click","ul.pager > li[prev].active",function(){var t=$(this).parent(),e=t.attr("classId"),i=t.attr("item"),n=parseInt(t.attr("page"))-1;n<1&&(n=1),r(i,e,{page:n})}),$("body").on("click","ul.pager > li[first].active",function(){var t=$(this).parent(),e=t.attr("classId"),i=t.attr("item");r(i,e,{page:1})}),$("body").on("keydown","ul.pager > li.page > input",function(t){var e=$(this).parents("ul.pager"),i=e.attr("classId"),n=e.attr("item"),a=parseInt($(this).val()),o=parseInt(e.attr("last"));13===(t.keyCode||t.which)&&(a<1&&(a=1),a>o&&(a=o),r(n,i,a))}),$("body").on("click","ul.pager > li[last].active",function(){var t=$(this).parent(),e=t.attr("classId"),i=t.attr("item"),n=t.attr("last");r(i,e,{page:n})}),$("body").on("click","ul.pager > li[next].active",function(){var t=$(this).parent(),e=t.attr("classId"),i=t.attr("item"),n=parseInt(t.attr("page"))+1,a=parseInt(t.attr("last"));n>a&&(n=a),r(i,e,{page:n})}),$(".sidebar .elements .h2 span").click(function(){var t=$(this).parents(".elements"),e=t.attr("rubric"),i=t.attr("display"),n=t.find("ul").first();"show"==i?(t.attr("display","hide"),n.hide(),$.post("/moonlight/rubrics/close",{rubric:e})):"hide"==i?(t.attr("display","show"),n.show(),$.post("/moonlight/rubrics/open",{rubric:e})):($.blockUI(),$.getJSON("/moonlight/rubrics/get",{rubric:e},function(e){$.unblockUI(),e.html&&(t.append(e.html),t.attr("display","show"))}))}),$("body").on("click",".sidebar .elements span.open",function(){var t=$(this),e=t.parents("li").first(),i=t.attr("rubric"),n=t.attr("bind"),a=t.attr("classId"),o=t.attr("display");"show"==o?($('.sidebar .elements ul[node="'+a+'"]').slideUp(200),t.attr("display","hide"),$.post("/moonlight/rubrics/node/close",{rubric:i,classId:a})):"hide"==o?($('.sidebar .elements ul[node="'+a+'"]').slideDown(200),t.attr("display","show"),$.post("/moonlight/rubrics/node/open",{rubric:i,classId:a})):($.blockUI(),$.getJSON("/moonlight/rubrics/node/get",{rubric:i,bind:n,classId:a},function(i){$.unblockUI(),i.html&&($(i.html).hide().appendTo(e).slideDown(200),t.attr("display","show"))}))}),$("body").on("contextmenu",".sidebar .elements a",function(t){t.preventDefault(),t.stopPropagation();var e=$(this),i=$(".sidebar"),n=$(".sidebar .contextmenu"),a=e.offset().left,o=e.offset().top-i.offset().top+e.height()+2;n.find("li.title span").html(e.text()),n.find("li.title small").html(e.attr("item")),n.find("li.edit a").attr("href",e.attr("href")+"/edit"),n.find("li.browse a").attr("href",e.attr("href")),o+n.height()>$(window).height()&&(o=o-n.height()-e.height()-4<0?0:o-n.height()-e.height()-4),n.css({left:a+"px",top:o+"px"}).fadeIn(200)}),$("body").on("click",".sidebar .contextmenu",function(t){t.stopPropagation()}),$("body").on("click",".sort-toggler",function(){var t=$(this).parents("div[item]"),e=t.find("th.browse");"true"==e.attr("sort")?(e.attr("sort","false"),t.find("td.browse a").show(),t.find("td.browse .drag").hide()):(e.attr("sort","true"),t.find("td.browse a").hide(),t.find("td.browse .drag").show())}),$("body").on("click","li.column-toggler",function(){var t=$(this),e=t.find(".dropdown");"show"==t.attr("display")?(t.attr("display","hide"),e.fadeOut(200)):(t.attr("display","show"),e.fadeIn(200))}),$("body").on("click","li.column-toggler .dropdown",function(t){t.stopPropagation()}),$("body").on("click","li.column-toggler .dropdown ul > li[show]",function(t){var e=$(this),i=e.attr("name"),n=e.attr("show"),a=e.parents("div[item]").attr("item");n="true"==n?"false":"true",e.attr("show",n),$.post("/moonlight/column",{item:a,name:i,show:n})}),$("body").on("click","li.column-toggler .dropdown .btn",function(t){var e=$(this).parents("div[item]"),i=$(this).parents("li.column-toggler"),n=i.find(".dropdown"),a=e.attr("classId"),o=e.attr("item");i.attr("display","hide"),n.fadeOut(200,function(){r(o,a)})})});