var CofinaSSOApi=function(){var _defaultValues={SSOSiteUrl:"https://aminhaconta.xl.pt/",SSOLayer:"sso_layer",AppID:"",SiteHost:window.location.host,SSORootDivID:"cofinasso-root",SSORootIframe:"cofinasso-arbitation",SSOFavouriteNewsIframe:"cofinasso-favourite-news",SSORootIframeCommunication:"cofinasso-arbitation-communication",SSOLoginCookieName:"cof_site_user",SSORedirectProtectionCookieName:"cof_redir_protect",DonAskNickSelectionCookieName:"cof_dontask_nick_cookie",ThirdpartyScriptUrl:"//aminhaconta.xl.pt/Api/Js/ThirdpartySSOApi.js",UserAgentValidatorUrl:"//aminhaconta.xl.pt/Api/Js/UserAgentValidator.js",UseThirdparty:!0,AlwaysShowLoginInLayer:!0,SSOHost:"aminhaconta.xl.pt",LogToConsole:!1},_communicationMethods={CloseCurrentLayer:"CLOSE_CURRENT_LAYER",CloseCurrentLayerWithCheckLogin:"CLOSE_CURRENT_LAYER_WITH_CHECK_LOGIN",CloseNickLayerAndSetDontAskCookie:"CLOSE_NICK_LAYER_AND_SEND_DONT_ASK_COOKIE",OpenNickSelectionFrame:"OPEN_SITE_NICK_NAME_SELECTION_FRAME",EndedSettingDefaultVars:"LOAD_DEFAULT_VARS_ENDED",GetCampaign:"GET_CAMPAIGN",AddFavouriteSucess:"ADD_FAVOURITE_SUCESS",AddFavouriteError:"ADD_FAVOURITE_ERROR",OpenCofinaConversionLayer:"OPEN_COFINA_CONVERSION_LAYER"},_log=function(e){_defaultValues.LogToConsole&&console.warn(e)},_registerUserAgentValidatorScript=function(){if(!window.UserAgentValidator){var e=document.createElement("script"),t=_defaultValues.UserAgentValidatorUrl+(_defaultValues.UserAgentValidatorUrl.indexOf("?")>-1?"&":"?")+"_="+String(1e5*Math.random());e.src=t,e.type="text/javascript",e.async="async",document.getElementsByTagName("head")[0].appendChild(e)}},_registerThirdparty=function(){var e=document.createElement("script"),t=_defaultValues.ThirdpartyScriptUrl+(_defaultValues.ThirdpartyScriptUrl.indexOf("?")>-1?"&":"?")+"_="+String(1e5*Math.random());e.src=t,e.type="text/javascript",e.async="async",document.getElementsByTagName("head")[0].appendChild(e)},_thirpartySessionValidation=function(e){_isNullOrEmpty(e)&&(e=""),_thirdpartyUid=e;var t=_checkCookie(_defaultValues.SSOLoginCookieName);if(_isNullOrEmpty(t))t="";else try{if(-1!=document.referrer.toLowerCase().indexOf("check_login")){var o=_checkCookie(_defaultValues.SSORedirectProtectionCookieName);_isNullOrEmpty(o)?_createCookieWithMinutes(_defaultValues.SSORedirectProtectionCookieName,"1",.3):"1"==o?_createCookieWithMinutes(_defaultValues.SSORedirectProtectionCookieName,"2",.3):(t="",_createCookie(_defaultValues.SSOLoginCookieName,t,365))}}catch(e){_log(e)}_setArbitationFrame(_defaultValues.SSOSiteUrl+"Api/Handlers/Arbitration/CHECK_LOGIN_FROM_THIRDPARTY?returnUrl="+encodeURIComponent(window.top.location.href)+"&siteLoginToken="+t+"&thirdpartyUID="+e)},_openCofinaConversionLayer=function(){_createFullScreenLayer(600,400,_defaultValues.SSOSiteUrl+"conversions/cofina.aspx?isLayer=1&site="+encodeURIComponent(window.top.location.hostname)+"&returnUrl="+encodeURIComponent(window.top.location.href)+"&thirdpartyUserId="+_thirdpartyUid)},_thirdpartyUid="",_rootFrame=null,_favouritesFrame=null,_rootDiv=null,_mustCheckLogin=!1,_favouriteNewsCallback=null,_favouriteNewsAddCallback=null,_favouriteNewsRemoveCallback=null,_getScrollXY=function(){var e=0,t=0;return"number"==typeof window.pageYOffset?(t=window.pageYOffset,e=window.pageXOffset):document.body&&(document.body.scrollLeft||document.body.scrollTop)?(t=document.body.scrollTop,e=document.body.scrollLeft):document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop)&&(t=document.documentElement.scrollTop,e=document.documentElement.scrollLeft),[e,t]},_isNullOrEmpty=function(e){return null==e||("undefined"==e||""==e)},_setBrowserScroll=function(e){},_getDocumentMaxHeight=function(){var e=document.body,t=document.documentElement;return Math.max(e.scrollHeight,e.offsetHeight,t.clientHeight,t.scrollHeight,t.offsetHeight)},_closeLayer=function(){var e=document.getElementById(_defaultValues.SSOLayer);null!=e&&document.getElementsByTagName("body")[0].removeChild(e),_setBrowserScroll(!1)},_createIframe=function(e,t,o,i,a,n){if(!_isNullOrEmpty(e)){var r=document.createElement("iframe");return n||(-1!=e.indexOf("?")?e+="&":e+="?",e+="_="+String(Math.random())),-1!=e.indexOf("?")?e+="&":e+="?",e+="siteHost="+_defaultValues.SiteHost,r.src=e,null!=t&&(r.style.width=String(t)+"px"),null!=o&&(r.style.height=String(o)+"px"),i?r.setAttribute("scrolling","auto"):r.setAttribute("scrolling","no"),a||(r.style.display="none"),r.setAttribute("frameBorder","0"),r}},_appendTo=function(e,t){document.getElementsByTagName(e)[0].appendChild(t)},_checkCookie=function(e){for(var t="",o=e+"=",i=document.cookie.split(";"),a=0;an&&(t=n-20);var l="innerHeight"in window?window.innerHeight:document.documentElement.offsetHeight,c=Math.ceil((l-t)/2);c<0&&(c=0);var s="innerWidth"in window?window.innerWidth:document.documentElement.offsetWidth,u=Math.ceil((s-e)/2);u<0&&(u=0);var d=document.createElement("div");d.style.marginLeft=String(u+"px"),d.style.width=e+"px",d.style.marginTop=String(c+"px"),d.style.display="block",r.appendChild(d);var _=document.createElement("div");_.style.width=e+"px",_.style.height=t+"px",_.style.position="fixed",d.appendChild(_);var p=document.createElement("img");p.title="Fechar",p.alt="Fechar",p.src=_defaultValues.SSOSiteUrl+"/img/bt_fechar.png",p.style.position="absolute",p.style.top="10px",p.style.cursor="pointer",p.style.right="10px",p.onclick=function(){CofinaSSOApi.closeLayer(),i&&i()},_.appendChild(p);var f=_createIframe(o,e,t,!a,!0,!0);_.appendChild(f),f.style.background="#fff"},_openNickSelectionLayer=function(){_isNullOrEmpty(_checkCookie(_defaultValues.DonAskNickSelectionCookieName))&&(_createCookie(_defaultValues.DonAskNickSelectionCookieName,"1",1e3),_createFullScreenLayer(600,400,_defaultValues.SSOSiteUrl+"api/layers/nickselection.aspx"))},_createFrames=function(){(_rootDiv=document.createElement("div")).style.position="absolute",_rootDiv.style.top="-10000px",_rootDiv.style.height="0px",_rootDiv.style.width="0px",_rootDiv.setAttribute("id",_defaultValues.SSORootDivID),(_rootFrame=document.createElement("iframe")).setAttribute("id",_defaultValues.SSORootIframe),_rootFrame.setAttribute("name",_defaultValues.SSORootIframe),_rootFrame.setAttribute("title","Cofina SSO Arbitration"),_rootDiv.appendChild(_rootFrame),(_favouritesFrame=document.createElement("iframe")).setAttribute("id",_defaultValues.SSOFavouriteNewsIframe),_favouritesFrame.setAttribute("name",_defaultValues.SSOFavouriteNewsIframe),_favouritesFrame.setAttribute("title","Cofina SSO Favourite News"),_rootDiv.appendChild(_favouritesFrame),_appendTo("body",_rootDiv)},_setArbitationFrame=function(e){var t=1e10*Math.random();e+=(-1==e.indexOf("?")?"?":"&")+"ord="+t;var o=_rootFrame.cloneNode(!0);o.setAttribute("src",e),_rootFrame.parentNode.replaceChild(o,_rootFrame),_rootFrame=o},_init=function(){_createFrames(),_setArbitationFrame(_defaultValues.SSOSiteUrl+"Api/Handlers/Arbitration/SET_DEFAULT_VARS?host="+_defaultValues.SiteHost);try{window.addEventListener?window.addEventListener("message",CofinaSSOApi.messageHandler,!1):window.attachEvent("onmessage",CofinaSSOApi.messageHandler)}catch(e){_log("Unable to register message handler"),_log(e)}},_messageHandler=function(event){try{if(null==event||null==event.origin||null==event.data||-1==event.origin.toLowerCase().indexOf(_defaultValues.SSOHost))return;var data=event.data,msgIsString="string"==typeof data;msgIsString?(_log(data),_handleCommunication(data)):data.methodCall&&(_log(data.methodCall),eval(data.methodCall))}catch(e){_log(e)}},_initApplication=function(e,t,o){if(_isNullOrEmpty(e))throw"Must set AppID";if(_defaultValues.AppID=e,window.location.href.indexOf("https://")>-1&&(_defaultValues.SSOSiteUrl=_defaultValues.SSOSiteUrl.replace(/http:/gi,"https:")),_createFrames(),o){var i=_checkCookie(_defaultValues.SSOLoginCookieName);_isNullOrEmpty(i)&&(i=""),_setArbitationFrame(_defaultValues.SSOSiteUrl+"Api/Handlers/Arbitration/SET_APP?appID="+_defaultValues.AppID+"&loginCheck=1&returnUrl="+encodeURIComponent(window.top.location.href)+"&siteLoginToken="+i)}else _setArbitationFrame(_defaultValues.SSOSiteUrl+"Api/Handlers/Arbitration/SET_APP?appID="+_defaultValues.AppID)},_initCommunication=function(){var e=String(document.URL).toUpperCase();if(!(_isNullOrEmpty(e)||-1==e.indexOf("#")&&e.length>e.indexOf("#"))){var t=e.substring(e.indexOf("#")+1);_handleCommunication(t)}},_handleCommunication=function(e){try{switch(e){case _communicationMethods.OpenNickSelectionFrame:window.top.CofinaSSOApi.openNickSelectionLayer();break;case _communicationMethods.CloseCurrentLayer:window.top.CofinaSSOApi.closeLayer();break;case _communicationMethods.CloseCurrentLayerWithCheckLogin:window.top.CofinaSSOApi.closeLayer(),setTimeout((function(){window.top.CofinaSSOApi.checkLogin()}),500);break;case _communicationMethods.CloseNickLayerAndSetDontAskCookie:_createCookie(_defaultValues.DonAskNickSelectionCookieName,"1",30),window.top.CofinaSSOApi.closeLayer();break;case _communicationMethods.EndedSettingDefaultVars:window.top.CofinaSSOApi.checkLogin();break;case _communicationMethods.GetCampaign:window.top.CofinaSSOApi.getCampaign();break;case _communicationMethods.AddFavouriteSucess:window.top.CofinaSSOApi.callFavouriteAddCallback(!0);break;case _communicationMethods.AddFavouriteError:window.top.CofinaSSOApi.callFavouriteAddCallback(!1);break;case _communicationMethods.RemoveFavouriteSucess:window.top.CofinaSSOApi.callFavouriteRemoveCallback(!0);break;case _communicationMethods.RemoveFavouriteError:window.top.CofinaSSOApi.callFavouriteRemoveCallback(!1);break;case _communicationMethods.OpenCofinaConversionLayer:window.top.CofinaSSOApi.openCofinaConversionLayer()}}catch(t){_log(t),_log(e)}},_getAppLoginLayer=function(){if(_isNullOrEmpty(_defaultValues.AppID))throw"Must set appID, use the initApplication method first with current app ID";_createFullScreenLayer(380,600,_defaultValues.SSOSiteUrl+"Login?appID="+_defaultValues.AppID)},_getAppLogin=function(){window.top.location=_defaultValues.SSOSiteUrl+"Login?appID="+_defaultValues.AppID+"&returnUrl="+encodeURIComponent(window.top.location.href)},_getLoginLayer=function(){_createFullScreenLayer(380,600,_defaultValues.SSOSiteUrl+"Login?returnUrl="+encodeURIComponent(window.top.location.href)+"&isLayer=1")},_gotoLogin=function(){var e="innerHeight"in window?window.innerHeight:document.documentElement.offsetHeight,t=/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent),o=/Firefox/i.test(navigator.userAgent);_defaultValues.AlwaysShowLoginInLayer&&e>620&&!o&&!t&&"https:"==location.protocol?_createFullScreenLayer(340,700,_defaultValues.SSOSiteUrl+"Login?returnUrl="+encodeURIComponent(window.top.location.href)+"&isLayer=1",!0):window.top.location=_defaultValues.SSOSiteUrl+"Login?returnUrl="+encodeURIComponent(window.top.location.href)},_gotoLogout=function(){window.top.location=_defaultValues.SSOSiteUrl+"Logout?returnUrl="+encodeURIComponent(window.top.location.href)},_gotoAccount=function(){window.top.location=_defaultValues.SSOSiteUrl+"PersonalArea/PersonalInfo/PersonalInfo"},_gotoAccountFavouriteNews=function(){window.top.location=_defaultValues.SSOSiteUrl+"PersonalArea/Activity/FavouriteNews"},_gotoPasswordRecover=function(){window.top.location=_defaultValues.SSOSiteUrl+"PasswordRecover"},_gotoRegistration=function(){window.top.location=_defaultValues.SSOSiteUrl+"Registration/NewRegistration"},_gotoAppRegistrationLayer=function(){if(_isNullOrEmpty(_defaultValues.AppID))throw"Must set appID, use the initApplication method first with current app ID";_createFullScreenLayer(380,600,_defaultValues.SSOSiteUrl+"Registration/NewRegistration?appID="+_defaultValues.AppID)},_checkLogin=function(){if(_mustCheckLogin){var e=_checkCookie(_defaultValues.SSOLoginCookieName);if(_isNullOrEmpty(e))e="";else try{if(-1!=document.referrer.toLowerCase().indexOf("check_login")){var t=_checkCookie(_defaultValues.SSORedirectProtectionCookieName);_isNullOrEmpty(t)?_createCookieWithMinutes(_defaultValues.SSORedirectProtectionCookieName,"1",.3):"1"==t?_createCookieWithMinutes(_defaultValues.SSORedirectProtectionCookieName,"2",.3):(e="",_createCookie(_defaultValues.SSOLoginCookieName,e,365))}}catch(e){_log(e)}if(_setArbitationFrame(_defaultValues.SSOSiteUrl+"Api/Handlers/Arbitration/CHECK_LOGIN?returnUrl="+encodeURIComponent(window.top.location.href)+"&siteLoginToken="+e),!_isNullOrEmpty(e)&&null!=_favouriteNewsCallback){var o=_defaultValues.SSOSiteUrl+"Api/Handlers/Arbitration/GET_FAVOURITE_NEWS?siteUrl= "+encodeURIComponent(window.top.location.href)+"&callback="+_favouriteNewsCallback+"&_="+String(1e10*Math.random());try{var i=document.createElement("script");i.src=o,i.type="text/javascript",document.getElementsByTagName("head")[0].appendChild(i)}catch(e){_log(e)}}}},_getCampaign=function(){try{window.$&&window.$.getScript&&$.getScript(_defaultValues.SSOSiteUrl+"Api/Handlers/Arbitration/GET_CAMPAIGN?ord="+String(1e10*Math.random()))}catch(e){_log(e)}},_getCampaignById=function(e){try{window.$&&window.$.getScript&&$.getScript(_defaultValues.SSOSiteUrl+"Api/Handlers/Arbitration/GET_CAMPAIGN_BY_ID?cId="+e+"&ord="+String(1e10*Math.random()))}catch(e){_log(e)}},_addNewsToFavourites=function(e){if(null!=_rootFrame&&!_isNullOrEmpty(e)){var t=_checkCookie(_defaultValues.SSOLoginCookieName);if(!_isNullOrEmpty(t)){if((e=String(e).toLowerCase()).indexOf("://")<0){var o="http://"+window.top.location.host;"/"!=e[0]&&(e="/"+e),e=o+e}var i="0";String(e).toLowerCase()==String(window.top.location.href).toLowerCase()&&(i="1");var a=_defaultValues.SSOSiteUrl+"Api/Handlers/Arbitration/ADD_NEWS_TO_FAVOURITES?readed="+i+"&url="+encodeURIComponent(e)+"&_="+String(1e10*Math.random());_setFavouritesFrame(a)}}},_removeNewsFromFavourites=function(e){if(null!=_rootFrame&&!_isNullOrEmpty(e)){var t=_checkCookie(_defaultValues.SSOLoginCookieName);if(!_isNullOrEmpty(t)){if((e=String(e).toLowerCase()).indexOf("://")<0){var o="http://"+window.top.location.host;"/"!=e[0]&&(e="/"+e),e=o+e}var i=_defaultValues.SSOSiteUrl+"Api/Handlers/Arbitration/REMOVE_NEWS_FROM_FAVOURITES?siteUrl= "+encodeURIComponent(window.top.location.href)+"&url="+encodeURIComponent(e)+"&_="+String(1e10*Math.random());_setFavouritesFrame(i)}}},_setFavouritesFrame=function(e){var t=1e10*Math.random();e+=(-1==e.indexOf("?")?"?":"&")+"ord="+t;var o=_favouritesFrame.cloneNode(!0);o.setAttribute("src",e),_favouritesFrame.parentNode.replaceChild(o,_favouritesFrame),_favouritesFrame=o};return{init:function(){_registerUserAgentValidatorScript(),_init(),_defaultValues.UseThirdparty&&_registerThirdparty()},initWithCheckLogin:function(){_mustCheckLogin=!0,_registerUserAgentValidatorScript(),_init(),_defaultValues.UseThirdparty&&_registerThirdparty()},initApplication:function(e,t,o){_registerUserAgentValidatorScript(),_initApplication(e,t,o)},initCommunication:function(){_registerUserAgentValidatorScript(),_initCommunication()},checkLogin:function(){_checkLogin()},closeLayer:function(){_closeLayer()},getAppLoginLayer:function(){_getAppLoginLayer()},getAppLogin:function(){_getAppLogin()},gotoLogin:function(){_gotoLogin()},getLoginLayer:function(){_getLoginLayer()},gotoLogout:function(){_gotoLogout()},gotoAccount:function(){_gotoAccount()},gotoAccountFavouriteNews:function(){_gotoAccountFavouriteNews()},gotoPasswordRecover:function(){_gotoPasswordRecover()},gotoRegistration:function(){_gotoRegistration()},gotoAppRegistrationLayer:function(){_gotoAppRegistrationLayer()},openNickSelectionLayer:function(){_openNickSelectionLayer()},getCampaign:function(){_getCampaign()},getCampaignById:function(e){_getCampaignById(e)},checkCookie:function(e){return _checkCookie(e)},createCookie:function(e,t,o){_createCookie(e,t,o)},createFullScreenLayer:function(e,t,o,i){_createFullScreenLayer(e,t,o,i)},addNewsToFavourites:function(e){_addNewsToFavourites(e)},removeNewsFromFavourites:function(e){_removeNewsFromFavourites(e)},callFavouriteRemoveCallback:function(val){if(!_isNullOrEmpty(_favouriteNewsRemoveCallback)){var scriptCode="try{if(window."+_favouriteNewsRemoveCallback+" != null){ window."+_favouriteNewsRemoveCallback+"("+String(val)+"); }}catch(errCallFavouriteNewsRemoveCallback){}";eval(scriptCode)}},setFavouriteNewsCallback:function(e,t){_isNullOrEmpty(e)||(_favouriteNewsCallback=e),_isNullOrEmpty(t)||(_favouriteNewsAddCallback=t)},callFavouriteAddCallback:function(val){if(!_isNullOrEmpty(_favouriteNewsAddCallback)){var scriptCode="try{if(window."+_favouriteNewsAddCallback+" != null){ window."+_favouriteNewsAddCallback+"("+String(val)+"); }}catch(errCallFavouriteAddCallback){}";eval(scriptCode)}},thirpartySessionValidation:function(e){_thirpartySessionValidation(e)},openCofinaConversionLayer:function(){_openCofinaConversionLayer()},messageHandler:function(e){_messageHandler(e)},enableDebug:function(){_defaultValues.LogToConsole=!0,_log("Debug enabled")}}}();