function SGPBMediaButton(){}
SGPBMediaButton.prototype.init=function()
{this.tinymceButton();this.openMediaButtonPopup();};SGPBMediaButton.prototype.tinymceButton=function()
{var that=this;jQuery(document).on('tinymce-editor-setup',function(event,editor){if('sgpb-newsletter-text'===editor.settings.id){return;}
if(editor.settings.toolbar1.indexOf('popupBuilderEditorButton')!==-1){return;}
editor.settings.toolbar1+=', popupBuilderEditorButton';editor.addButton('popupBuilderEditorButton',{icon:'wp-menu-image dashicons-before dashicons-menu-icon-sgpb sgpb-tinymce-button',onclick:function(){that.mediaButtonPopup();}});});};SGPBMediaButton.prototype.openMediaButtonPopup=function()
{var that=this;jQuery('.sgpb-tinymce-button, .sgpb-insert-media-button-js, .sgpb-insert-js-variable').unbind('click').bind('click',function(e){e.preventDefault();that.mediaButtonPopup(jQuery(this));});};SGPBMediaButton.prototype.mediaButtonPopup=function(currentTag)
{var select2Init=1;var that=this;var hiddenDivId=jQuery(currentTag).attr('data-id')||'sgpb-hidden-media-popup';var popupConfigObj=new PopupConfig();popupConfigObj.magicCall('setContentPadding',14);popupConfigObj.magicCall('setContentBorderRadius',4);popupConfigObj.magicCall('setContentBorderRadiusType','px');popupConfigObj.magicCall('setContentBorderWidth',0);popupConfigObj.magicCall('setContentBorderColor','#506274');popupConfigObj.magicCall('setShadowSpread',1);popupConfigObj.magicCall('setContentShadowBlur',4);popupConfigObj.magicCall('setContentShadowColor','#cccccc');popupConfigObj.magicCall('setMinWidth',400);popupConfigObj.magicCall('setSrcElement',hiddenDivId);popupConfigObj.magicCall('setOverlayColor','black');popupConfigObj.magicCall('setOverlayOpacity',40);var config=popupConfigObj.combineConfigObj();var popup=new SGPopup(config);popup.open();jQuery(window).bind('sgpbDidOpen',function(){jQuery('.sgpb-insert-popup').addClass('js-sg-select2');if(mediaButtonParams.currentPostType!==mediaButtonParams.popupBuilderPostType){jQuery('.sgpb-insert-popup-event').addClass('js-sg-select2');if(select2Init===1){that.popupSelect2();}
select2Init++;jQuery('.select2-container--below').remove();that.popupSelect2();}
else{that.popupSelect2();jQuery('.select2-container--below').remove();}
that.closeMediaButtonPopup(popup);});that.insertPopup(popup);};SGPBMediaButton.prototype.closeMediaButtonPopup=function(popup)
{jQuery('.sgpb-close-media-popup-js').on('click',function(){popup.close();});};SGPBMediaButton.prototype.insertPopup=function(popup)
{var insidePopup=false;if(mediaButtonParams.currentPostType===mediaButtonParams.popupBuilderPostType){insidePopup=true;}
jQuery('.sgpb-insert-popup-js').unbind('click').bind('click',function(){var selectedContent='';var selectedPopup=jQuery('.sgpb-insert-popup').val();var selectedPopupEvent=jQuery('.sgpb-insert-popup-event').val();if(typeof selectedPopupEvent!=='undefined'){selectedPopupEvent=' event="'+selectedPopupEvent+'"';}
else{selectedPopupEvent='';if(insidePopup){selectedPopupEvent=' insidePopup="on"';}}
if(selectedPopup===''||selectedPopup===null){popup.close();return;}
if(selectedPopupEvent==='onLoad'&&!insidePopup){window.send_to_editor('[sg_popup id="'+selectedPopup+'"'+selectedPopupEvent+'][/sg_popup]');popup.close();return;}
if(typeof tinyMCE.editors.content!=='undefined'&&(document.getElementById('content').offsetParent===null)){selectedContent=(tinyMCE.activeEditor.selection.getContent())?tinyMCE.activeEditor.selection.getContent():'';}
else{var content=document.getElementById('content');if(typeof document.selection!=='undefined'){content.focus();var sel=document.selection.createRange();selectedContent=sel.text;}
else if(typeof content!=='undefined'&&content!==null){if(typeof content.selectionStart!=='undefined'){var startPos=content.selectionStart;var endPos=content.selectionEnd;selectedContent=content.value.substring(startPos,endPos);}}}
window.send_to_editor('[sg_popup id="'+selectedPopup+'"'+selectedPopupEvent+']'+selectedContent+'[/sg_popup]');popup.close();});jQuery('.sgpb-insert-js-variable-to-editor').unbind('click').bind('click',function(){var jsVariableSelector=jQuery('.sgpb-popup-dialog-main-div-wrapper .sgpb-js-variable-selector').val();var jsVariableAttribute=jQuery('.sgpb-popup-dialog-main-div-wrapper .sgpb-js-variable-attribute').val();jQuery('.sgpb-js-variable-errors').addClass('sg-hide-element');var valid=true;if(jsVariableSelector===''){valid=false;jQuery('.sgpb-js-variable-selector-error').removeClass('sg-hide-element');}
if(jsVariableAttribute===''){valid=false;jQuery('.sgpb-js-variable-attribute-error').removeClass('sg-hide-element');}
if(!valid){return false;}
jQuery('.sgpb-js-variable-errors').addClass('sg-hide-element');window.send_to_editor('[pbvariable selector="'+jsVariableSelector+'" attribute="'+jsVariableAttribute+'"]');popup.close();});};SGPBMediaButton.prototype.popupSelect2=function()
{if(!jQuery('.js-sg-select2').length){return;}
jQuery('select.js-sg-select2').each(function(){var type=jQuery(this).attr('data-select-type');var className=jQuery(this).attr('data-select-class');var options={width:'100%'};if(type==='ajax'){if(typeof SGPB_JS_PARAMS==='undefined'){return;}
options=jQuery.extend(options,{minimumInputLength:1,ajax:{url:SGPB_JS_PARAMS.url,dataType:'json',delay:250,type:'POST',data:function(params){var searchKey=jQuery(this).attr('data-value-param');return{action:'select2_search_data',nonce_ajax:SGPB_JS_PARAMS.nonce,searchTerm:params.term,searchKey:searchKey};},processResults:function(data){return{results:jQuery.map(data.items,function(item){return{text:item.text,id:item.id}})};}}});}
jQuery(this).sgpbselect2(options);});};jQuery(document).ready(function(){var mediaButton=new SGPBMediaButton();jQuery(document).on('tinymce-editor-init',function(event,editor){mediaButton.init();});});