window.CMB=function(window,document,$){"use strict";var l10n=window.cmb_l10,setTimeout=window.setTimeout,cmb={formfield:"",idNumber:!1,file_frames:{},repeatEls:'input:not([type="button"]),select,textarea,.cmb_media_status'};return cmb.metabox=function(){return cmb.$metabox?cmb.$metabox:(cmb.$metabox=$("table.cmb_metabox"),cmb.$metabox)},cmb.init=function(){var $metabox=cmb.metabox(),$repeatGroup=$metabox.find(".repeatable-group");l10n.new_admin_style&&$metabox.find(".cmb-spinner img").hide(),cmb.initPickers($metabox.find("input:text.cmb_timepicker"),$metabox.find("input:text.cmb_datepicker"),$metabox.find("input:text.cmb_colorpicker")),$("#ui-datepicker-div").wrap('
'),$(''+l10n.check_toggle+"
").insertBefore("ul.cmb_checkbox_list"),$metabox.on("change",".cmb_upload_file",function(){cmb.formfield=$(this).attr("id"),$("#"+cmb.formfield+"_id").val("")}).on("click",".cmb-multicheck-toggle",cmb.toggleCheckBoxes).on("click",".cmb_upload_button",cmb.handleMedia).on("click",".cmb_remove_file_button",cmb.handleRemoveMedia).on("click",".add-group-row",cmb.addGroupRow).on("click",".add-row-button",cmb.addAjaxRow).on("click",".remove-group-row",cmb.removeGroupRow).on("click",".remove-row-button",cmb.removeAjaxRow).on("keyup paste focusout",".cmb_oembed",cmb.maybeOembed).on("cmb_remove_row",".repeatable-group",cmb.resetTitlesAndIterator),$repeatGroup.length&&$repeatGroup.filter(".sortable").each(function(){$(this).find(".remove-group-row").before(''+l10n.up_arrow+' '+l10n.down_arrow+"")}).on("click",".shift-rows",cmb.shiftRows).on("cmb_add_row",cmb.emptyValue),setTimeout(cmb.resizeoEmbeds,500),$(window).on("resize",cmb.resizeoEmbeds)},cmb.resetTitlesAndIterator=function(){$(".repeatable-group").each(function(){var $table=$(this);$table.find(".repeatable-grouping").each(function(rowindex){var $row=$(this);$row.data("iterator",rowindex),$row.find(".cmb-group-title h4").text($table.find(".add-group-row").data("grouptitle").replace("{#}",rowindex+1))})})},cmb.toggleCheckBoxes=function(event){event.preventDefault();var $self=$(this),$multicheck=$self.parents("td").find("input[type=checkbox]");$self.data("checked")?($multicheck.prop("checked",!1),$self.data("checked",!1)):($multicheck.prop("checked",!0),$self.data("checked",!0))},cmb.handleMedia=function(event){if(wp){event.preventDefault();var $metabox=cmb.metabox(),$self=$(this);cmb.formfield=$self.prev("input").attr("id");var $formfield=$("#"+cmb.formfield),formName=$formfield.attr("name"),uploadStatus=!0,attachment=!0,isList=$self.hasClass("cmb_upload_list");if(cmb.formfield in cmb.file_frames)return void cmb.file_frames[cmb.formfield].open();cmb.file_frames[cmb.formfield]=wp.media.frames.file_frame=wp.media({title:$metabox.find("label[for="+cmb.formfield+"]").text(),button:{text:l10n.upload_file},multiple:isList?!0:!1});var handlers={list:function(selection){attachment=selection.toJSON(),$formfield.val(attachment.url),$("#"+cmb.formfield+"_id").val(attachment.id);var fileGroup=[];$(attachment).each(function(){uploadStatus=this.type&&"image"===this.type?''+l10n.remove_image+'
':""+l10n.file+" "+this.filename+' ('+l10n.download+' / '+l10n.remove_file+')',fileGroup.push(uploadStatus)}),$(fileGroup).each(function(){$formfield.siblings(".cmb_media_status").slideDown().append(this)})},single:function(selection){attachment=selection.first().toJSON(),$formfield.val(attachment.url),$("#"+cmb.formfield+"_id").val(attachment.id),uploadStatus=attachment.type&&"image"===attachment.type?'":l10n.file+" "+attachment.filename+' ('+l10n.download+' / '+l10n.remove_file+")",$formfield.siblings(".cmb_media_status").slideDown().html(uploadStatus)}};cmb.file_frames[cmb.formfield].on("select",function(){var selection=cmb.file_frames[cmb.formfield].state().get("selection"),type=isList?"list":"single";handlers[type](selection)}),cmb.file_frames[cmb.formfield].open()}},cmb.handleRemoveMedia=function(event){event.preventDefault();var $self=$(this);if($self.is(".attach_list .cmb_remove_file_button"))return $self.parents("li").remove(),!1;cmb.formfield=$self.attr("rel");var $container=$self.parents(".img_status");return cmb.metabox().find("input#"+cmb.formfield).val(""),cmb.metabox().find("input#"+cmb.formfield+"_id").val(""),$container.length?$container.html(""):$self.parents(".cmb_media_status").html(""),!1},$.fn.replaceText=function(b,a,c){return this.each(function(){var g,e,f=this.firstChild,d=[];if(f)do 3===f.nodeType&&(g=f.nodeValue,e=g.replace(b,a),e!==g&&(!c&&/=0;i--){var id=cmb.neweditor_id[i].id,old=cmb.neweditor_id[i].old;if("undefined"==typeof tinyMCEPreInit.mceInit[id]){var newSettings=jQuery.extend({},tinyMCEPreInit.mceInit[old]);for(_prop in newSettings)"string"==typeof newSettings[_prop]&&(newSettings[_prop]=newSettings[_prop].replace(new RegExp(old,"g"),id));tinyMCEPreInit.mceInit[id]=newSettings}if("undefined"==typeof tinyMCEPreInit.qtInit[id]){var newQTS=jQuery.extend({},tinyMCEPreInit.qtInit[old]);for(_prop in newQTS)"string"==typeof newQTS[_prop]&&(newQTS[_prop]=newQTS[_prop].replace(new RegExp(old,"g"),id));tinyMCEPreInit.qtInit[id]=newQTS}tinyMCE.init({id:tinyMCEPreInit.mceInit[id]})}}cmb.initPickers($row.find("input:text.cmb_timepicker"),$row.find("input:text.cmb_datepicker"),$row.find("input:text.cmb_colorpicker"))},cmb.updateNameAttr=function(){var $this=$(this),name=$this.attr("name");if("undefined"==typeof name)return!1;var prevNum=parseInt($this.parents(".repeatable-grouping").data("iterator")),newNum=prevNum-1,$newName=name.replace("["+prevNum+"]","["+newNum+"]");$this.attr("name",$newName)},cmb.emptyValue=function(event,row){$('input:not([type="button"]), textarea',row).val("")},cmb.addGroupRow=function(event){event.preventDefault();var $self=$(this),$table=$("#"+$self.data("selector")),$oldRow=$table.find(".repeatable-grouping").last(),prevNum=parseInt($oldRow.data("iterator"));cmb.idNumber=prevNum+1;var $row=$oldRow.clone();$row.data("title",$self.data("grouptitle")).newRowHousekeeping().cleanRow(prevNum,!0);var $newRow=$(''+$row.html()+"
");$oldRow.after($newRow),cmb.afterRowInsert($newRow),$table.find(".repeatable-grouping").length<=1?$table.find(".remove-group-row").prop("disabled",!0):$table.find(".remove-group-row").removeAttr("disabled"),$table.trigger("cmb_add_row",$newRow)},cmb.addAjaxRow=function(event){event.preventDefault();var $self=$(this),tableselector="#"+$self.data("selector"),$table=$(tableselector),$emptyrow=$table.find(".empty-row"),prevNum=parseInt($emptyrow.find("[data-iterator]").data("iterator"));cmb.idNumber=prevNum+1;var $row=$emptyrow.clone();$row.newRowHousekeeping().cleanRow(prevNum),$emptyrow.removeClass("empty-row").addClass("repeat-row"),$emptyrow.after($row),cmb.afterRowInsert($row),$table.trigger("cmb_add_row",$row)},cmb.removeGroupRow=function(event){event.preventDefault();var $self=$(this),$table=$("#"+$self.data("selector")),$parent=$self.parents(".repeatable-grouping"),noRows=$table.find(".repeatable-grouping").length;$parent.nextAll(".repeatable-grouping").find(cmb.repeatEls).each(cmb.updateNameAttr),noRows>1&&($parent.remove(),3>noRows?$table.find(".remove-group-row").prop("disabled",!0):$table.find(".remove-group-row").prop("disabled",!1),$table.trigger("cmb_remove_row"))},cmb.removeAjaxRow=function(event){event.preventDefault();var $self=$(this),$parent=$self.parents("tr"),$table=$self.parents(".cmb-repeat-table");$table.find("tr").length>1&&($parent.hasClass("empty-row")&&$parent.prev().addClass("empty-row").removeClass("repeat-row"),$self.parents(".cmb-repeat-table tr").remove(),$table.trigger("cmb_remove_row"))},cmb.shiftRows=function(event){event.preventDefault();var $self=$(this),$parent=$self.parents(".repeatable-grouping"),$goto=$self.hasClass("move-up")?$parent.prev(".repeatable-grouping"):$parent.next(".repeatable-grouping");if($goto.length){var inputVals=[];$parent.find(cmb.repeatEls).each(function(){var val,$element=$(this);$element.hasClass("cmb_media_status")?val=$element.html():"checkbox"===$element.attr("type")?(val=$element.is(":checked"),cmb.log("checked",val)):"select"===$element.prop("tagName")?(val=$element.is(":selected"),cmb.log("checked",val)):val=$element.val(),inputVals.push({val:val,$:$element})}),$goto.find(cmb.repeatEls).each(function(index){var val,$element=$(this);$element.hasClass("cmb_media_status")?(val=$element.html(),$element.html(inputVals[index].val),inputVals[index].$.html(val)):"checkbox"===$element.attr("type")?(inputVals[index].$.prop("checked",$element.is(":checked")),$element.prop("checked",inputVals[index].val)):"select"===$element.prop("tagName")?(inputVals[index].$.prop("selected",$element.is(":selected")),$element.prop("selected",inputVals[index].val)):(inputVals[index].$.val($element.val()),$element.val(inputVals[index].val))})}},cmb.initPickers=function($timePickers,$datePickers,$colorPickers){cmb.initTimePickers($timePickers),cmb.initDatePickers($datePickers),cmb.initColorPickers($colorPickers)},cmb.initTimePickers=function($selector){$selector.length&&$selector.timePicker({startTime:"00:00",endTime:"23:59",show24Hours:!1,separator:":",step:30})},cmb.initDatePickers=function($selector){$selector.length&&($selector.datepicker("destroy"),$selector.datepicker())},cmb.initColorPickers=function($selector){$selector.length&&("object"==typeof jQuery.wp&&"function"==typeof jQuery.wp.wpColorPicker?$selector.wpColorPicker():$selector.each(function(i){$(this).after(''),$("#picker-"+i).hide().farbtastic($(this))}).focus(function(){$(this).next().show()}).blur(function(){$(this).next().hide()}))},cmb.maybeOembed=function(evt){var $self=$(this),type=evt.type,m={focusout:function(){setTimeout(function(){cmb.spinner(".postbox table.cmb_metabox",!0)},2e3)},keyup:function(){var betw=function(min,max){return evt.which<=max&&evt.which>=min};(betw(48,90)||betw(96,111)||betw(8,9)||187===evt.which||190===evt.which)&&cmb.doAjax($self,evt)},paste:function(){setTimeout(function(){cmb.doAjax($self)},100)}};m[type]()},cmb.resizeoEmbeds=function(){cmb.metabox().each(function(){var $self=$(this),$tableWrap=$self.parents(".inside");if(!$tableWrap.length)return!0;var newWidth=Math.round(.82*$tableWrap.width()*.97)-30;if(newWidth>639)return!0;var $embeds=$self.find(".cmb-type-oembed .embed_status"),$children=$embeds.children().not(".cmb_remove_wrapper");return $children.length?void $children.each(function(){var $self=$(this),iwidth=$self.width(),iheight=$self.height(),_newWidth=newWidth;$self.parents(".repeat-row").length&&(_newWidth=newWidth-91);var newHeight=Math.round(_newWidth*iheight/iwidth);$self.width(_newWidth).height(newHeight)}):!0})},cmb.log=function(){l10n.script_debug&&console&&"function"==typeof console.log&&console.log.apply(console,arguments)},cmb.spinner=function($context,hide){hide?$(".cmb-spinner",$context).hide():$(".cmb-spinner",$context).show()},cmb.doAjax=function($obj){var oembed_url=$obj.val();if(!(oembed_url.length<6)){var field_id=$obj.attr("id"),$context=$obj.parents(".cmb-repeat-table tr td");$context=$context.length?$context:$obj.parents(".cmb_metabox tr td");var embed_container=$(".embed_status",$context),oembed_width=$obj.width(),child_el=$(":first-child",embed_container);cmb.log("oembed_url",oembed_url,field_id),oembed_width=embed_container.length&&child_el.length?child_el.width():$obj.width(),cmb.spinner($context),$(".embed_wrap",$context).html(""),setTimeout(function(){$(".cmb_oembed:focus").val()===oembed_url&&$.ajax({type:"post",dataType:"json",url:l10n.ajaxurl,data:{action:"cmb_oembed_handler",oembed_url:oembed_url,oembed_width:oembed_width>300?oembed_width:300,field_id:field_id,object_id:$obj.data("objectid"),object_type:$obj.data("objecttype"),cmb_ajax_nonce:l10n.ajax_nonce},success:function(response){cmb.log(response),"undefined"!=typeof response.id&&(cmb.spinner($context,!0),$(".embed_wrap",$context).html(response.result))}})},500)}},$(document).ready(cmb.init),cmb}(window,document,jQuery);