/*!
 * ExpressionEngine - by EllisLab
 *
 * @package		ExpressionEngine
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2003 - 2011, EllisLab, Inc.
 * @license		http://expressionengine.com/user_guide/license.html
 * @link		http://expressionengine.com
 * @since		Version 2.0
 * @filesource
 */

EE.publish=EE.publish||{};
EE.publish.category_editor=function(){var b=[],c=$("<div />"),g=$('<div id="cat_modal_container" />').appendTo(c),f={},k={},j=EE.BASE+"&C=admin_content&M=category_editor&group_id=",l,i,e,m={};c.dialog({autoOpen:!1,height:475,width:600,modal:!0,resizable:!1,open:function(){$(".ui-dialog-content").css("overflow","hidden");$(".ui-dialog-titlebar").focus();$("#cat_name").focus()}});$(".edit_categories_link").each(function(){var a=this.href.substr(this.href.lastIndexOf("=")+1);$(this).data("gid",a);b.push(a)});
for(e=0;e<b.length;e++)f[b[e]]=$("#cat_group_container_"+[b[e]]),f[b[e]].data("gid",b[e]),k[b[e]]=$("#cat_group_container_"+[b[e]]).find(".cat_action_buttons").remove();l=function(a){f[a].text("loading...").load(j+a+"&timestamp="+ +new Date+" .pageContents table",function(){i.call(f[a],f[a].html(),!1)})};i=function(a,d){var h=$(this),o=h.data("gid"),a=$.trim(a);h.hasClass("edit_categories_link")&&(h=$("#cat_group_container_"+o));if(a.charAt(0)!=="<"&&d)return l(o);h.closest(".cat_group_container").find("#refresh_categories").show();
var b=$(a),n,f,e;if(b.find("form").length){g.html(b);b=g.find("input[type=submit]");n=g.find("form");f=n.find("#cat_name");e=n.find("#cat_url_title");f.keyup(function(){f.ee_url_title(e)});var m=function(a){var d=a||$(this),a=d.serialize(),d=d.attr("action");$.ajax({url:d,type:"POST",data:a,dataType:"html",beforeSend:function(){h.html(EE.lang.loading)},success:function(a){a=$.trim(a);c.dialog("close");a[0]=="<"?(a=$(a).find(".pageContents"),a.find("form").length==0&&h.html(a),a=a.wrap("<div />").parent(),
i.call(h,a.html(),!0)):i.call(h,a,!0)},error:function(a){a=$.parseJSON(a.responseText);c.html(a.error)}});return!1};n.submit(m);var j={};j[b.remove().attr("value")]=function(){m(n)};c.dialog("open");c.dialog("option","buttons",j);c.one("dialogclose",function(){l(o)})}else k[o].clone().appendTo(h).show();return!1};e=function(a){a.preventDefault();$(this).hide();var d=$(this).data("gid"),h=".pageContents";if($(this).hasClass("edit_cat_order_trigger")||$(this).hasClass("edit_categories_link"))h+=" table";
d||(d=$(this).closest(".cat_group_container").data("gid"));m[d]=f[d].find("input:checked").map(function(){return this.value}).toArray();f[d].text(EE.lang.loading);$.ajax({url:$(this).attr("href")+"&timestamp="+ +new Date+h,dataType:"html",success:function(a){var b="",a=$.trim(a);a.charAt(0)=="<"&&(a=$(a).find(h),b=$("<div />").append(a).html(),a.find("form").length==0&&f[d].html(b));i.call(f[d],b,!0)},error:function(a){a=$.parseJSON(a.responseText);f[d].html(a.error);i.call(f[d],a.error,!0)}})};$(".edit_categories_link").click(e);
$(".cat_group_container a:not(.cats_done)").live("click",e);$(".cats_done").live("click",function(){var a=$(this).closest(".cat_group_container"),d=a.data("gid");$(".edit_categories_link").each(function(){$(this).data("gid")==d&&$(this).show()});a.text("loading...").load(EE.BASE+"&C=content_publish&M=category_actions&group_id="+a.data("gid")+"&timestamp="+ +new Date,function(h){a.html($(h).html());$.each(m[d],function(d,h){a.find("input[value="+h+"]").attr("checked","checked")})});return!1})};
Number.prototype.is_integer=String.prototype.is_integer=function(){var b=parseInt(this,10);return isNaN(b)?!1:this==b&&this.toString()==b.toString()};EE.publish.get_percentage_width=function(b){return b.attr("data-width")&&b.attr("data-width").slice(0,-1).is_integer()?parseInt(b.attr("data-width"),10):Math.round(b.width()/b.parent().width()*10)*10};
EE.publish.save_layout=function(){var b=0,c={},g={},f=0,k=!1,j=$("#tab_menu_tabs li.current").attr("id");$(".main_tab").show();$("#tab_menu_tabs a:not(.add_tab_link)").each(function(){if($(this).parent("li").attr("id")&&$(this).parent("li").attr("id").substring(0,5)=="menu_"){var e=$(this).parent("li").attr("id").substring(5),a=$(this).parent("li").attr("id").substring(5),d=$(this).parent("li").attr("title");f=0;visible=!0;$(this).parent("li").is(":visible")?(lay_name=e,c[lay_name]={},c[lay_name]._tab_label=
d):(k=!0,visible=!1);$("#"+a).find(".publish_field").each(function(){var a=$(this),d=this.id.replace(/hold_field_/,""),a=EE.publish.get_percentage_width(a),b=$("#sub_hold_field_"+d+" .markItUp ul li:eq(2)");a>100&&(a=100);b=b.html()!=="undefined"&&b.css("display")!=="none"?!0:!1;a={visible:$(this).css("display")==="none"||visible===!1?!1:!0,collapse:$("#sub_hold_field_"+d).css("display")==="none"?!0:!1,htmlbuttons:b,width:a+"%"};visible===!0?(a.index=f,c[lay_name][d]=a,f+=1):g[d]=a});visible===!0&&
b++}});if(k==!0){var l,i,e=0;for(darn in c){i=darn;for(l in c[i])c[i][l].index>e&&(e=c[i][l].index);break}$.each(g,function(){this.index=++e});jQuery.extend(c[i],g)}EE.tab_focus(j.replace(/menu_/,""));b===0?$.ee_notice(EE.publish.lang.tab_count_zero,{type:"error"}):$("#layout_groups_holder input:checked").length===0?$.ee_notice(EE.publish.lang.no_member_groups,{type:"error"}):$.ajax({type:"POST",dataType:"json",url:EE.BASE+"&C=content_publish&M=save_layout",data:"XID="+EE.XID+"&json_tab_layout="+
JSON.stringify(c)+"&"+$("#layout_groups_holder input").serialize()+"&channel_id="+EE.publish.channel_id,success:function(b){b.messageType==="success"?$.ee_notice(b.message,{type:"success"}):b.messageType==="failure"&&$.ee_notice(b.message,{type:"error"})}})};
EE.publish.remove_layout=function(){if($("#layout_groups_holder input:checked").length===0)return $.ee_notice(EE.publish.lang.no_member_groups,{type:"error"});$.ajax({type:"POST",url:EE.BASE+"&C=content_publish&M=save_layout",data:"XID="+EE.XID+"&json_tab_layout={}&"+$("#layout_groups_holder input").serialize()+"&channel_id="+EE.publish.channel_id+"&field_group="+EE.publish.field_group,success:function(){$.ee_notice(EE.publish.lang.layout_removed+' <a href="javascript:location=location">'+EE.publish.lang.refresh_layout+
"</a>",{duration:0,type:"success"});return!0}});return!1};EE.publish.change_preview_link=function(){$select=$("#layout_preview select");$link=$("#layout_group_preview");base=$link.attr("href").split("layout_preview")[0];$link.attr("href",base+"layout_preview="+$select.val());$.ajax({url:EE.BASE+"&C=content_publish&M=preview_layout",type:"POST",dataType:"json",data:{XID:EE.XID,member_group:$select.find("option:selected").text()}})};
EE.date_obj_time=function(){var b=new Date,c=b.getHours(),b=b.getMinutes(),g="";b<10&&(b="0"+b);EE.date.format=="us"&&(g=c<12?" AM":" PM",c!=0&&(c=(c+11)%12+1));c<10&&(c="0"+c);return" '"+c+":"+b+g+"'"}();file_manager_context="";
function disable_fields(b){var c=$(".main_tab input, .main_tab textarea, .main_tab select, #submit_button"),g=$("#submit_button"),f=$("#holder").find("a");b?(disabled_fields=c.filter(":disabled"),c.attr("disabled",!0),g.addClass("disabled_field"),f.addClass("admin_mode"),$("#holder div.markItUp, #holder p.spellcheck").each(function(){$(this).before('<div class="cover" style="position:absolute;width:100%;height:50px;z-index:9999;"></div>').css({})}),$(".contents, .publish_field input, .publish_field textarea").css("-webkit-user-select",
"none")):(c.removeAttr("disabled"),g.removeClass("disabled_field"),f.removeClass("admin_mode"),$(".cover").remove(),disabled_fields.attr("disabled",!0),$(".contents, .publish_field input, .publish_field textarea").css("-webkit-user-select","auto"))}
$(document).ready(function(){function b(a){return a?(a=a.toString(),a=a.replace(/\(\!\(([\s\S]*?)\)\!\)/g,function(a,b){var c=b.split("|!|");return altKey===!0?c[1]!==void 0?c[1]:c[0]:c[1]===void 0?"":c[0]}),a=a.replace(/\[\!\[([\s\S]*?)\]\!\]/g,function(a,b){var c=b.split(":!:");if(e===!0)return!1;value=prompt(c[0],c[1]?c[1]:"");value===null&&(e=!0);return value})):""}function c(a,d){var b=$("input[name="+d+"]").closest(".publish_field");a.is_image==!1?b.find(".file_set").show().find(".filename").html('<img src="'+
EE.PATH_CP_GBL_IMG+'default.png" alt="'+EE.PATH_CP_GBL_IMG+'default.png" /><br />'+a.file_name):b.find(".file_set").show().find(".filename").html('<img src="'+a.thumb+'" /><br />'+a.file_name);$("input[name="+d+"_hidden]").val(a.file_name);$("select[name="+d+"_directory]").val(a.upload_location_id)}var g,f;$("#layout_group_submit").click(function(){EE.publish.save_layout();return!1});$("#layout_group_remove").click(function(){EE.publish.remove_layout();return!1});$("#layout_preview select").change(function(){EE.publish.change_preview_link()});
$("a.reveal_formatting_buttons").click(function(){$(this).parent().parent().children(".close_container").slideDown();$(this).hide();return!1});$("#write_mode_header .reveal_formatting_buttons").hide();$("a.glossary_link").click(function(){$(this).parent().siblings(".glossary_content").slideToggle("fast");$(this).parent().siblings(".smileyContent .spellcheck_content").hide();return!1});EE.publish.smileys===!0&&($("a.smiley_link").toggle(function(){$(this).parent().siblings(".smileyContent").slideDown("fast",
function(){$(this).css("display","")})},function(){$(this).parent().siblings(".smileyContent").slideUp("fast")}),$(this).parent().siblings(".glossary_content, .spellcheck_content").hide(),$(".glossary_content a").click(function(){var a=$(this).closest(".publish_field"),d=a.attr("id").replace("hold_field_","field_id_");a.find("#"+d).insertAtCursor($(this).attr("title"));return!1}));if(EE.publish.autosave&&EE.publish.autosave.interval){var k=!1;f=function(){k||(k=!0,setTimeout(g,1E3*EE.publish.autosave.interval))};
g=function(){var a;$("#tools:visible").length===1?f():(a=$("#publishForm").serialize(),$.ajax({type:"POST",dataType:"json",url:EE.BASE+"&C=content_publish&M=autosave",data:a,success:function(a){a.error?console.log(a.error):a.success?(a.autosave_entry_id&&$("input[name=autosave_entry_id]").val(a.autosave_entry_id),$("#autosave_notice").text(a.success)):console.log("Autosave Failed");k=!1}}))};var j=$("textarea, input").not(":password,:checkbox,:radio,:submit,:button,:hidden"),l=$("select, :checkbox, :radio, :file");
j.bind("keypress change",f);l.bind("change",f)}if(EE.publish.pages){var j=$("#pages__pages_uri"),i=EE.publish.pages.pagesUri;j.val()||j.val(i);j.focus(function(){this.value===i&&$(this).val("")}).blur(function(){this.value===""&&$(this).val(i)})}EE.publish.markitup.fields!==void 0&&$.each(EE.publish.markitup.fields,function(a){$("#"+a).markItUp(mySettings)});EE.publish.setup_writemode=function(){var a=$("#write_mode_writer"),b=$("#write_mode_textarea"),c,f,e;b.markItUp(myWritemodeSettings);$(window).resize(function(){var b=
$(this).height()-117;a.css("height",b+"px").find("textarea").css("height",b-67-17+"px")}).triggerHandler("resize");$(".write_mode_trigger").overlay({closeOnEsc:!1,closeOnClick:!1,top:"center",target:"#write_mode_container",mask:{color:"#262626",loadSpeed:200,opacity:0.85},onBeforeLoad:function(){var a=this.getTrigger()[0].id;e=a.match(/^id_\d+$/)?$("#field_"+a):$("#"+a.replace(/id_/,""));c=e.getSelectedRange();b.val(e.val())},onLoad:function(){b.focus();b.createSelection(c.start,c.end);var a=this;
a.getClosers().unbind("click").click(function(b){b.srcElement=this;a.close(b)})},onBeforeClose:function(a){a=$(a.srcElement).closest(".close");a.hasClass("publish_to_field");a.hasClass("publish_to_field")&&(f=b.getSelectedRange(),e.val(b.val()),e.createSelection(f.start,f.end));e.focus()}})};EE.publish.show_write_mode===!0&&EE.publish.setup_writemode();var e=!1;$.ee_filebrowser();$.ee_filebrowser.add_trigger(".btn_img a, .file_manipulate",function(a){var d,c="",f="",e="",g="";textareaId=$(this).closest("#markItUpWrite_mode_textarea").length?
"write_mode_textarea":$(this).closest(".publish_field").attr("id").replace("hold_field_","field_id_");textareaId!=void 0&&(d=$("#"+textareaId),d.focus());a.is_image?(f=EE.upload_directories[a.upload_location_id].properties,e=EE.upload_directories[a.upload_location_id].pre_format,g=EE.upload_directories[a.upload_location_id].post_format,c=EE.filebrowser.image_tag.replace(/src="(.*)\[!\[Link:!:http:\/\/\]!\](.*)"/,'src="$1{filedir_'+a.upload_location_id+"}"+a.file_name+'$2"'),dimensions="",typeof a.file_hw_original!=
"undefined"&&a.file_hw_original!=""&&(dimensions=a.file_hw_original.split(" "),dimensions='height="'+dimensions[0]+'" width="'+dimensions[1]+'"'),c=c.replace(/\/?>$/,dimensions+" "+f+" />"),c=e+c+g):(f=EE.upload_directories[a.upload_location_id].file_properties,e=EE.upload_directories[a.upload_location_id].file_pre_format,e+='<a href="{filedir_'+a.upload_location_id+"}"+a.file_name+'" '+f+" >",g="</a>",g+=EE.upload_directories[a.upload_location_id].file_post_format);d.is("textarea")?(d.is(".markItUpEditor")||
(d.markItUp(myNobuttonSettings),d.closest(".markItUpContainer").find(".markItUpHeader").hide(),d.focus()),a.is_image?$.markItUp({replaceWith:c}):$.markItUp({key:"L",name:"Link",openWith:e,closeWith:g,placeHolder:a.file_name})):d.val(function(a,d){d+=e+c+g;return b(d)})});$("input[type=file]","#publishForm").each(function(){var a=$(this).closest(".publish_field"),b=a.find(".choose_file"),e=$(this).data("content-type"),f=$(this).data("directory"),e={content_type:e,directory:f};$.ee_filebrowser.add_trigger(b,
$(this).attr("name"),e,c);a.find(".remove_file").click(function(){a.find("input[type=hidden]").val("");a.find(".file_set").hide();return!1})});$(".hide_field span").click(function(){var a=$(this).parent().parent().attr("id").substr(11),b=$("#hold_field_"+a),a=$("#sub_hold_field_"+a);a.css("display")=="block"?(a.slideUp(),b.find(".ui-resizable-handle").hide(),b.find(".field_collapse").attr("src",EE.THEME_URL+"images/field_collapse.png")):(a.slideDown(),b.find(".ui-resizable-handle").show(),b.find(".field_collapse").attr("src",
EE.THEME_URL+"images/field_expand.png"));return!1});$(".close_upload_bar").toggle(function(){$(this).parent().children(":not(.close_upload_bar)").hide();$(this).children("img").attr("src",EE.THEME_URL+"publish_plus.png")},function(){$(this).parent().children().show();$(this).children("img").attr("src",EE.THEME_URL+"publish_minus.gif")});$(".ping_toggle_all").toggle(function(){$("input.ping_toggle").each(function(){this.checked=!1})},function(){$("input.ping_toggle").each(function(){this.checked=!0})});
EE.user.can_edit_html_buttons&&($(".markItUp ul").append('<li class="btn_plus"><a title="'+EE.lang.add_new_html_button+'" href="'+EE.BASE+"&C=myaccount&M=html_buttons&id="+EE.user_id+'">+</a></li>'),$(".btn_plus a").click(function(){return confirm(EE.lang.confirm_exit,"")}));$(".markItUpHeader ul").prepend('<li class="close_formatting_buttons"><a href="#"><img width="10" height="10" src="'+EE.THEME_URL+'images/publish_minus.gif" alt="Close Formatting Buttons"/></a></li>');$(".close_formatting_buttons a").toggle(function(){$(this).parent().parent().children(":not(.close_formatting_buttons)").hide();
$(this).parent().parent().css("height","13px");$(this).children("img").attr("src",EE.THEME_URL+"images/publish_plus.png")},function(){$(this).parent().parent().children().show();$(this).parent().parent().css("height","auto");$(this).children("img").attr("src",EE.THEME_URL+"images/publish_minus.gif")});$(".tab_menu li:first").addClass("current");EE.publish.title_focus==!0&&$("#title").focus();EE.publish.which=="new"&&$("#title").bind("keyup blur",function(){$("#title").ee_url_title($("#url_title"))});
EE.publish.versioning_enabled=="n"?$("#revision_button").hide():$("#versioning_enabled").click(function(){$(this).attr("checked")?$("#revision_button").show():$("#revision_button").hide()});EE.publish.category_editor();if(EE.publish.hidden_fields){EE._hidden_fields=[];var m=$("input");$.each(EE.publish.hidden_fields,function(a){EE._hidden_fields.push(m.filter("[name="+a+"]")[0])});$(EE._hidden_fields).after('<p class="hidden_blurb">This module field only shows in certain circumstances. This is a placeholder to let you define it in your layout.</p>')}});
