<%--
 ~
 ~ Copyright (c) 2020, WSO2 Inc. (http://www.wso2.com). All Rights Reserved.
 ~
 ~ This software is the property of WSO2 Inc. and its suppliers, if any.
 ~ Dissemination of any information or reproduction of any material contained
 ~ herein in any form is strictly forbidden, unless permitted by WSO2 expressly.
 ~ You may not alter or remove any copyright or other notice from copies of this content."
 ~
--%>


<%-- localize.jsp MUST already be included in the calling script --%>
<%@ page import="org.apache.commons.text.StringEscapeUtils" %>
<%@ page import="org.wso2.carbon.identity.mgt.endpoint.util.IdentityManagementEndpointUtil" %>
<%@ page import="org.owasp.encoder.Encode" %>
<%@ page import="java.io.File" %>

<%-- Include tenant context --%>
<jsp:directive.include file="../tenant-resolve.jsp"/>

<%-- Branding Preferences --%>
<jsp:directive.include file="./branding-preferences.jsp"/>

<% 
    String isCookieproEnabled = System.getenv("is_cookiepro_enabled");
    String initialScriptType = (Boolean.TRUE.toString()).equals(isCookieproEnabled) ? "text/plain" : "text/javascript";
%>

<!-- Extract the name of the stylesheet-->
<%
    String themeName = "asgardio";
    File themeDir = new File(request.getSession().getServletContext().getRealPath("/")
        + "/" + "libs/themes/" + themeName + "/");
    String[] fileNames = themeDir.list();
    String themeFileName = "";

    for(String file: fileNames) {
        if(file.endsWith("min.css")) {
            themeFileName = file;
        }
    }
%>

<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<%-- Updates the favicon with the URL resolved in branding-preferences --%>
<link rel="icon" href="<%= StringEscapeUtils.escapeHtml4(faviconURL) %>" type="image/x-icon"/>

<%-- Load the base theme --%>
<link href="libs/themes/asgardio/<%= themeFileName %>" rel="stylesheet">

<%-- Load Default Theme Skeleton --%>
<jsp:include page="./theme-skeleton.jsp"/>

<%-- If an override stylesheet is defined in branding-preferences, applying it... --%>
<% if (overrideStylesheet != null && !StringUtils.equals(layout, "custom-" + tenantForTheming)) { %>
<link rel="stylesheet" href="<%= StringEscapeUtils.escapeHtml4(overrideStylesheet) %>">
<% } %>

<%-- Layout specific styleSheet --%>
<%
    String styleFilePath;
    if (StringUtils.equals(layout, "custom-" + tenantForTheming)) {
        styleFilePath = application.getInitParameter("LayoutStoreURL").replace("${tenantDomain}", tenantForTheming) + "/styles.css";
%>
    <link rel="stylesheet" href="<%= styleFilePath %>">
<%
    } else {
        styleFilePath = "extensions/layouts/" + layout + "/styles.css";
        if (config.getServletContext().getResource(styleFilePath) != null) {
%>
    <link rel="stylesheet" href="<%= styleFilePath %>">
<%
        }
    }
%>

<%-- Updates the site tile with the text resolved in branding-preferences --%>
<title><%= StringEscapeUtils.escapeHtml4(siteTitle) %></title>

<%
  String vwo_ac_id = System.getenv("vwo_account_id");
%>
<!-- Start VWO Async SmartCode -->
<script type="<%=initialScriptType%>" class="optanon-category-C0002">
    var vwo_ac_id = "<%= Encode.forJavaScriptBlock(vwo_ac_id) %>";
    if (vwo_ac_id !== null && vwo_ac_id) {
        window._vwo_code = window._vwo_code || (function(){
        var account_id=vwo_ac_id,
        settings_tolerance=2000,
        library_tolerance=2500,
        use_existing_jquery=false,
        is_spa=1,
        hide_element='body',

        /* DO NOT EDIT BELOW THIS LINE */
        f=false,d=document,code={use_existing_jquery:function(){return use_existing_jquery;},library_tolerance:function(){return library_tolerance;},finish:function(){if(!f){f=true;var a=d.getElementById('_vis_opt_path_hides');if(a)a.parentNode.removeChild(a);}},finished:function(){return f;},load:function(a){var b=d.createElement('script');b.src=a;b.type='text/javascript';b.innerText;b.onerror=function(){_vwo_code.finish();};d.getElementsByTagName('head')[0].appendChild(b);},init:function(){
        window.settings_timer=setTimeout(function () {_vwo_code.finish() },settings_tolerance);var a=d.createElement('style'),b=hide_element?hide_element+'{opacity:0 !important;filter:alpha(opacity=0) !important;background:none !important;}':'',h=d.getElementsByTagName('head')[0];a.setAttribute('id','_vis_opt_path_hides');a.setAttribute('type','text/css');if(a.styleSheet)a.styleSheet.cssText=b;else a.appendChild(d.createTextNode(b));h.appendChild(a);this.load('https://dev.visualwebsiteoptimizer.com/j.php?a='+account_id+'&u='+encodeURIComponent(d.URL)+'&f='+(+is_spa)+'&r='+Math.random());return settings_timer; }};window._vwo_settings_timer = code.init(); return code; }());
    } else {
        console.warn("VWO is disabled");
    }
</script>
<%-- End VWO Async SmartCode --%>
<%-- Begin Hotjar Tracking Code --%>
<% String hotjar_track_code_system_var = System.getenv().getOrDefault("hotjar_tracking_code", null);
   String hotjar_track_code = StringUtils.isNotBlank(hotjar_track_code_system_var) ? hotjar_track_code_system_var : null;
   String tenantName = request.getParameter("t");
   if (tenantName != null && tenantName.equalsIgnoreCase("carbon.super")) { %>
<script type="<%=initialScriptType%>" class="optanon-category-C0002">
    var hotjar_id = <%= hotjar_track_code %>;
    if (hotjar_id !== null && hotjar_id !== "") {
        (function(h,o,t,j,a,r){
            h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
            h._hjSettings={hjid:hotjar_id,hjsv:6};
            a=o.getElementsByTagName('head')[0];
            r=o.createElement('script');r.async=1;
            r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
            a.appendChild(r);
        })(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');
    } else {
        console.warn("Hotjar feedback is disabled");
    };
</script>
<% } %>
<%-- End Hotjar Tracking Code --%>

<%-- Downtime banner --%>
<jsp:include page="planned-downtime-banner.jsp"/>
