﻿if (typeof (useClientPositioning) != "undefined" && useClientPositioning == true) {
    //window.onresize = LayoutPage;
    //window.onload = LayoutPage;
    //window.onblur = LayoutPage;
}

function Today() {
    var date = new window.Date();

    return date.toLocaleDateString();
}

function LayoutPage() {
    var oSection = window.document.getElementById("PageSection");
    var oHeader = window.document.getElementById("HeaderSection");
    var oContentSection = window.document.getElementById("ContentSection");
    var oContentBlock = window.document.getElementById("ContentBlock");
    var oContent = window.document.getElementById("Content");
    var oFooter = window.document.getElementById("FooterSection");

    var clientHeight = window.document.body.offsetHeight;
    var clientWidth = window.document.body.offsetWidth;

    // Exit on unsupported browser

    if (typeof (oFooter.offsetHeight) == "undefined") return;
    if (typeof (verticalPositioning) == "undefined") verticalPositioning = "default";
    if (typeof (showCommercial) == "undefined") showCommercial = false;

    // Hide while positioning

    window.document.body.style.visibility = "hidden";

    // Check commercial

    if (showCommercial && (commercialPresentationCount < 4)) {
        OpenCommercial();
    }

    // Center page section

    if (verticalPositioning == "fill") {
        oSection.style.height = "100%";
    }
    else {
        oSection.style.height = "auto";
    }

    var newTop = Math.max((clientHeight - oSection.offsetHeight) / 2, 0);

    if (verticalPositioning == "center") {
        oSection.style.top = newTop.toString() + "px"
    }

    oSection.style.margin = "auto";
    oSection.style.width = pageWidth + "px";

    if (horizontalPositioning == "default") {
        oSection.style.margin = "5px";
    }
    else if (horizontalPositioning == "fill") {
        oSection.style.margin = "0px";
        oSection.style.width = "100%";
    }

    // Optionally fill page

    if (verticalPositioning == "fill") {
        var verticalSpace = 20;
        var minimumContentHeight = oContentBlock.offsetHeight;
        var desiredContentHeight = clientHeight - oHeader.offsetHeight - oFooter.offsetHeight - verticalSpace;

        oSection.style.height = "100%";
        oContentSection.style.height = Math.max(minimumContentHeight, desiredContentHeight) + "px";

        var footerTop = 0; Math.max((clientHeight - oContentSection.offsetHeight - oHeader.offsetHeight - 5), 0);
        oFooter.style.top = footerTop + "px";
    }

    // Center and show/hide modal panel

    var oModalPanel = window.document.getElementById("ModalFrame");

    if (oModalPanel != null) {
        oModalPanel.style.top = Math.max((clientHeight - oModalPanel.offsetHeight) / 2, 0) + "px";
        oModalPanel.style.left = Math.max((clientWidth - oModalPanel.offsetWidth) / 2, 0) + "px";

        if (typeof (showModalSection) != "undefined") {
            if (showModalSection) {
                ShowModalPanel();
            }
            else {
                HideModalPanel();
            }
        }
    }

    // Show after positioning is complete

    window.document.body.style.visibility = "visible";
}

function ShowModalPanel() {
    var oModalSection = window.document.getElementById("ModalSection");

    if (oModalSection != null) {
        showModalSection = true;
        oModalSection.style.zIndex = "999";
        oModalSection.style.visibility = "visible";
    }
}

function HideModalPanel() {
    var oModalSection = window.document.getElementById("ModalSection");

    if (oModalSection != null) {
        showModalSection = false;
        oModalSection.style.zIndex = "-999";
        oModalSection.style.visibility = "hidden";
    }
}

function ShowLoginPanel() {
    var oModalSection = window.document.getElementById("LoginSection");

    if (oModalSection != null) {
        showModalSection = true;
        oModalSection.style.zIndex = "9999";
        oModalSection.style.visibility = "visible";
    }
}

function HideLoginPanel() {
    var oModalSection = window.document.getElementById("LoginSection");

    if (oModalSection != null) {
        showModalSection = false;
        oModalSection.style.zIndex = "-999";
        oModalSection.style.visibility = "hidden";
    }
}

function CloseCommercial() {
    var oCommercial = window.document.getElementById("Commercial");

    if (oCommercial != null) {
        oCommercial.style.zIndex = -99;
        oCommercial.style.visibility = "hidden";
    }
}

function OpenCommercial() {
    var oCommercial = window.document.getElementById("Commercial");
    var oSection = window.document.getElementById("PageSection");

    if (oCommercial != null && oSection != null) {
        var clientHeight = window.document.body.offsetHeight;
        var clientWidth = window.document.body.offsetWidth;
        var commercialHtml;

        oCommercial.style.top = Math.max((oSection.offsetHeight - oCommercial.offsetHeight) / 2, 0);
        oCommercial.style.left = Math.max((oSection.offsetWidth - oCommercial.offsetWidth) / 2, 0);
        oCommercial.align = "center";

        if (oCommercial.currentStyle.visibility == "hidden") {
            commercialPresentationCount++;
            oCommercial.style.zIndex = 99;
            oCommercial.style.visibility = "visible";
        }

        // TODO: Remove testing logic from commercial

        commercialHtml = "<p>Commercial presented " + commercialPresentationCount + " times.</p>";
        commercialHtml = commercialHtml + "<p><a href=\"javascript: CloseCommercial();\">Close</a></p>";
        oCommercial.innerHTML = commercialHtml;
    }
}