(function($){var favouritesFontList=null;var standardFontList=null;var googleFontList=null;var allFonts=null;$.widget("lws.lwss_fontselect",{options:{icon:"",cssColor:"",properties:["font-family","font-weight"],values:[],api:'https://fonts.googleapis.com/css?family=',},_create:function(){this.wrapper=this.element.addClass("lwss-fontselect-wrapper");this.shadow=$("<div>").addClass("lwss-fontselect-shadow").appendTo(this.wrapper);this.left=$("<div>").addClass("lwss-fontselect-left").appendTo(this.wrapper);this.right=$("<div>").addClass("lwss-fontselect-right").appendTo(this.wrapper);this.icon=$("<div>").addClass("lwss-fontselect-icon "+this.options.icon+" "+this.options.cssColor).appendTo(this.left);this.label=$("<div>").addClass("lwss-fontselect-label").html("Font").appendTo(this.right);this.group=$("<div>").addClass("lwss-fontselect-group").appendTo(this.right);this.input=$("<div>").addClass("lwss-fontselect-input").html("Choose a font...").appendTo(this.group);this.inputbtn=$("<div>").addClass("lwss-fontselect-btn lws-icon lws-icon-small-down "+this.options.cssColor).appendTo(this.group);this.family=$("<input>").addClass("lwss-fontselect-family lwss_value").data("css",this.options.properties[0]).attr("type","hidden").prependTo(this.wrapper);this.weight=$("<input>").addClass("lwss-fontselect-weight lwss_value").data("css",this.options.properties[1]).attr("type","hidden").prependTo(this.wrapper);this.mainColor=this.inputbtn.css("color");this.activeFont=null;this.rgba=this.mainColor.replace(')',', 0.1)').replace('rgb','rgba');var me=this;this._buildFontLists();this._buildPopUp();this._manageEvents();this._setTab("Fav")},_bind:function(fn,me){return function(){return fn.apply(me,arguments)}},_chooseFont:function(event){var myFont=allFonts[$(event.delegateTarget).css("font-family")];this.family.val(myFont.Name).trigger("change");this.weight.val("normal").trigger("change")},_setFavourite:function(font){if(favouritesFontList==undefined){favouritesFontList=[]}
var alreadyThere=favouritesFontList.indexOf(font.Name);if(alreadyThere!=-1){favouritesFontList.splice(alreadyThere,1)}
favouritesFontList.unshift(font.Name)
if(favouritesFontList.length>=7){favouritesFontList.pop()}
localStorage.setItem('lws-favouriteFonts',lwsBase64.fromObj(favouritesFontList))},_setFont:function(event){var myFont=allFonts[event.delegateTarget.value];if(myFont!=undefined){this.input.html(myFont.Name).css("font-family",myFont.Name);this._setFavourite(myFont);this.element.closest(".lwss_editor").find(".lwss-popup-shadow").hide();this.popup.hide()}else{this.input.html("Choose a font...").css("font-family","Gravity").css("font-weight","300")}},_setWeight:function(event){var weight=$(event.delegateTarget).val();this.input.html(myFont.Name).css("font-weight",weight)},_weightTr:function(weight){if(lws_adminpanel.fontWeightTr[weight.toLowerCase()]!=undefined)
return lws_adminpanel.fontWeightTr[weight.toLowerCase()];return weight},_selectVariant:function(event){var variant=$(event.delegateTarget);this.family.val(variant.data("family")).trigger("change");this.weight.val(variant.data("weight")).trigger("change")},_showVariants:function(event){$('.lwss-fontselect-variants').remove();var me=this;var variantsButton=$(event.delegateTarget);var variantsDiv=$("<div>",{'class':'lwss-fontselect-variants','style':'display:none;'}).appendTo(variantsButton);variantsDiv.offset({left:(variantsButton.offset().left+variantsButton.outerWidth()+1),top:variantsButton.offset().top});var variants=variantsButton.data('variants').split(',');for(var j=0;j<variants.length;++j){this._addFontLink(variantsButton.data('family'),variants[j]);var variantLine=$("<div>").addClass('lwss-fontselect-variantline').appendTo(variantsDiv);variantLine.html(this._weightTr(variants[j])).css("font-family",variantsButton.data('family')).css("font-weight",variants[j]);variantLine.data('family',variantsButton.data('family')).data('weight',variants[j]);variantLine.on('mouseover',function(){$(this).addClass(me.options.cssColor)}).mouseout(function(){$(this).removeClass(me.options.cssColor)});variantLine.on('click',this._bind(this._selectVariant,this))}
variantsDiv.fadeIn()},_setTab:function(tabName){this.activeTab=tabName;var me=this;this.element.find(".lwss_fontselect_tsel").each(function(){if($(this).data("Tab")==me.activeTab){$(this).css("background-color",me.mainColor).css("color","#ffffff")}else{$(this).css("background-color","transparent").css("color","#444444")}});this.element.find(".lwss-fontselect-tab").each(function(){if($(this).data("Tab")==me.activeTab){$(this).show()}else{$(this).hide()}})},_openPopup:function(){var me=this;this.gglTab.empty();this.stdTab.empty();this.favTab.empty();$.each(allFonts,function(index,element){var display=element.Display;me._setFontEvents(display,me);if(favouritesFontList.indexOf(element.Name)!=-1){display.appendTo(me.favTab)}else{if(element.Type=="Standard Font"){display.appendTo(me.stdTab)}else{display.appendTo(me.gglTab)}}});if(favouritesFontList!=undefined){this._setTab("Fav")}else{this._setTab("Ggl")}
var shadow=this.element.closest(".lwss_editor").find(".lwss-popup-shadow").show();this.popup.show();return!1},_showShadow:function(){this.shadow.show()},_hideShadow:function(){this.shadow.hide()},_manageEvents:function(){var me=this;this.element.on('mouseenter',this._bind(this._showShadow,this));this.element.on('mouseleave',this._bind(this._hideShadow,this));this.element.on('focusin',function(){me.shadow.addClass("lwss-active")});this.element.on('focusout',function(){me.shadow.removeClass("lwss-active")});this.family.on('change',this._bind(this._setFont,this));this.weight.on('change',this._bind(this._setWeight,this));this.group.on('click',this._bind(this._openPopup,this))},_buildPopUp:function(){this.popup=$("<div>").addClass("lwss-fontselect-popup").appendTo(this.right).hide();this.tabSelectors=$("<div>").addClass("lwss-fontselect-tab-selectors").appendTo(this.popup);this.tabs=$("<div>").addClass("lwss-fontselect-tabs").appendTo(this.popup);this._addFavTab();this._addStandardTab();this._addGoogleTab()},_addFavTab:function(){this.favSelector=$("<div>").addClass("lwss-fontselect-fav-selector lwss_fontselect_tsel").data("Tab","Fav").html("My Favourites").appendTo(this.tabSelectors);var me=this;this.favSelector.on('click',function(){me._setTab($(this).data("Tab"))});this.favTab=$("<div>").addClass("lwss-fontselect-tab").data("Tab","Fav").appendTo(this.tabs).hide()},_addStandardTab:function(){this.stdSelector=$("<div>").addClass("lwss-fontselect-std-selector lwss_fontselect_tsel").data("Tab","Std").html("Standard Fonts").appendTo(this.tabSelectors);var me=this;this.stdSelector.on('click',function(){me._setTab($(this).data("Tab"))});this.stdTab=$("<div>").addClass("lwss-fontselect-tab").data("Tab","Std").appendTo(this.tabs).hide()},_addGoogleTab:function(){this.gglSelector=$("<div>").addClass("lwss-fontselect-ggl-selector lwss_fontselect_tsel").data("Tab","Ggl").html("Google Fonts").appendTo(this.tabSelectors);var me=this;this.gglSelector.on('click',function(){me._setTab($(this).data("Tab"))});this.gglTab=$("<div>").addClass("lwss-fontselect-tab").data("Tab","Ggl").appendTo(this.tabs)},_addFontLink:function(family,weight){var link=this.options.api+family;if(weight!=undefined&&weight.length>0)
link+=(":"+weight);if($("link[href='"+link+"']").length===0){$('link:last').after('<link href="'+link+'" rel="stylesheet" type="text/css">')}},_setFontEvents:function(display,sp){var title=display.find(".lwss-fontselect-font-title");title.off().on('click',sp._bind(sp._chooseFont,sp));title.mouseover(function(){$(this).addClass(sp.options.cssColor)}).on('mouseout',function(){$(this).removeClass(sp.options.cssColor)});var variants=display.find(".lwss-fontselect-variant-btn");variants.off().on('click',sp._bind(sp._showVariants,sp))},_createFontDisplay:function(font,sp){var display=$("<div>").addClass("lwss-fontselect-font-container")
var title=$("<div>").addClass("lwss-fontselect-font-title").html(font.Name).css("font-family",font.Name).appendTo(display);var category=$("<div>").addClass("lwss-fontselect-font-cat").html(font.Cat).appendTo(display);var type=$("<div>").addClass("lwss-fontselect-font-type").html(font.Type).appendTo(display);if(font.Variants.length>1){var btn=$("<div>",{'class':'lwss-fontselect-variant-btn '+((sp.options.cssColor!=undefined)?" "+sp.options.cssColor:""),'html':font.Variants.length+" variants",'data-family':font.Name,'data-variants':font.Variants.join(',')}).appendTo(display)}
return display},_addStandardFonts:function(){var me=this;for(var i=0;i<standardFontList.length;i++){var myFont={};myFont.Variants=[];myFont.Name=standardFontList[i].family;myFont.Cat=standardFontList[i].category;myFont.Type="Standard Font"
for(var j=0;j<standardFontList[i].variants.length;j++){if(standardFontList[i].variants[j].indexOf('italic')<0){myFont.Variants.push(standardFontList[i].variants[j])}}
myFont.Display=me._createFontDisplay(myFont,me);allFonts[myFont.Name]=myFont}},_addGoogleFonts:function(){var me=this;for(var i=0;i<googleFontList.length;i++){if(googleFontList[i].family.substring(0,5)!="Slabo"){var myFont={};myFont.Variants=[];myFont.Name=googleFontList[i].family;myFont.Cat=googleFontList[i].category;myFont.Type="Google Font"
for(var j=0;j<googleFontList[i].variants.length;j++){if(googleFontList[i].variants[j].indexOf('italic')<0){myFont.Variants.push(googleFontList[i].variants[j])}}
me._addFontLink(myFont.Name,"normal");myFont.Display=me._createFontDisplay(myFont,me);allFonts[myFont.Name]=myFont}}},_buildFontLists:function(){var me=this;if(allFonts==null){allFonts={};$.getJSON(lws_ajax.url,{action:'lws_adminpanel_googlefontlist'},function(response){if(null==response)console.log("Font ressource error.");else if(0==response)console.log("Ajax action not found.");else{googleFontList=response.items;me._addGoogleFonts()}}).fail(function(d,textStatus,error){console.log("Ajax failed with status "+textStatus+", error: "+error)});$.getJSON(lws_ajax.url,{action:'lws_adminpanel_standardfontlist'},function(response){if(null==response)console.log("Font ressource error.");else if(0==response)console.log("Ajax action not found.");else{standardFontList=response.items;me._addStandardFonts()}}).fail(function(d,textStatus,error){console.log("Ajax failed with status "+textStatus+", error: "+error)});favouritesFontList=[];if(localStorage.getItem('lws-favouriteFonts'))
favouritesFontList=lwsBase64.toObj(localStorage.getItem('lws-favouriteFonts'));}}})})(jQuery)
jQuery(function($){$(document).on('click',function(e){if($(e.target).closest(".lwss-fontselect-popup").length==0){$(".lwss-fontselect-popup").hide()}});$(document).on('keyup',function(e){if((e.keyCode||e.which||e.charCode||0)===27){$(".lwss-fontselect-popup").hide()}})})