﻿function initPage()
{
    //Get the current size of the client window
    var htmlheight = document.documentElement.clientHeight;
    var htmlwidth = document.documentElement.clientWidth;
    
    //Constants from the CSS sheet
    var contentMargin = 10;
    var shadowOffset = 2;
    var paddingLeft = 5;

    //Fetch all the layers that require some sort of transformation
    var headerbackgroundItem = document.getElementById("headerbackground")
    var contentShadowBottomItem = document.getElementById("contentShadowBottom")
    var contentShadowRightItem = document.getElementById("contentShadowRight")
    var rightopacityItem = document.getElementById("optionalMenuOpacityBackground");
    var mainMenuenuItem = document.getElementById("mainMenu");
    var optionalMenuItem = document.getElementById("optionalMenu");
    var quicklinksItem = document.getElementById("quicklinks");
    var navigationpathItem = document.getElementById("navigationpath");
    var contentItem = document.getElementById("content");
    var footerbackgroundItem = document.getElementById("footerbackground")
    
    //Fix the maxHeight of the main and optional Menu to prevent them from going through the footer
    //or leaving the page window. CSS will draw a scrollbar when maxHeight is reached.
    mainMenuenuItem.style.maxHeight = htmlheight - headerbackgroundItem.offsetHeight - footerbackgroundItem.offsetHeight + "px";
    optionalMenuItem.style.maxHeight = htmlheight - headerbackgroundItem.offsetHeight + "px";
    contentItem.style.minHeight = htmlheight - headerbackgroundItem.offsetHeight - quicklinksItem.offsetHeight - navigationpathItem.offsetHeight - (2 * contentMargin) + "px";
    contentItem.style.maxHeight = htmlheight - headerbackgroundItem.offsetHeight - quicklinksItem.offsetHeight - navigationpathItem.offsetHeight - (2 * contentMargin) + "px";
    
    quicklinksItem.style.maxWidth = htmlwidth - mainMenuenuItem.offsetWidth + "px";
    navigationpathItem.style.maxWidth = htmlwidth - mainMenuenuItem.offsetWidth + "px";
    contentItem.style.maxWidth = htmlwidth - mainMenuenuItem.offsetWidth - (2 * contentMargin) + "px";
    
    //Set the height of the optional menu background to the exact size of the optional menu
    rightopacityItem.style.minHeight = optionalMenuItem.offsetHeight + "px";
    rightopacityItem.style.Height = optionalMenuItem.offsetHeight + "px";
    
    //match the shadow layers to the content window for that authentic shdows style
    contentShadowBottomItem.style.minHeight = contentItem.offsetHeight + "px";
    contentShadowBottomItem.style.Height = contentItem.offsetHeight + "px";

    contentShadowRightItem.style.minHeight = contentItem.offsetHeight - shadowOffset + "px";
    contentShadowRightItem.style.Height = contentItem.offsetHeight - shadowOffset + "px";
    
    contentShadowBottomItem.style.width = htmlwidth - mainMenuenuItem.offsetWidth - (2 * contentMargin) - shadowOffset + "px";
    contentShadowRightItem.style.width = htmlwidth - mainMenuenuItem.offsetWidth - (2 * contentMargin) + "px";

    //Stretch the quicklink, navigation and content div to the full width of the client
    quicklinksItem.style.width = htmlwidth - mainMenuenuItem.offsetWidth + "px";
    navigationpathItem.style.width = htmlwidth - mainMenuenuItem.offsetWidth + "px";
    contentItem.style.width = htmlwidth - mainMenuenuItem.offsetWidth - (2 * contentMargin) - paddingLeft + "px";
    
}

function toggleSearch()
{
    var rightopacityItem = document.getElementById("optionalMenuOpacityBackground").style;
    var optionalMenuItem = document.getElementById("optionalMenu").style;
    
    rightopacityItem.display = (optionalMenuItem.display =='' || optionalMenuItem.display=='none')? 'block':'none';
    optionalMenuItem.display = (optionalMenuItem.display =='' || optionalMenuItem.display=='none')? 'block':'none';

    
    initPage();
}

function getUrlParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function UpdateFieldsetFieldValue(fieldsetId, fieldname, dataValue) {
    $(fieldsetId).find("label").each(
	function(i, data) {
	    if ($(data).attr("for") == (fieldname)) {
	        var tmpLabel = $(data).clone(true);
	        var tmpField = $(data).parent();
	        tmpField.empty();
	        tmpField.append(tmpLabel);
	        tmpLabel.after(dataValue);
	    }
	});
}

function GetContentPaneHeight() {
    var contentPane = document.getElementById("content");
    var maxHeight = contentPane.style.maxHeight;
    var index = maxHeight.search("px");
    if (index != -1) {
        return maxHeight.substring(0, index);
    }
    else {
        alert("Can not determine the height of the contentpane");
        return "";
    };
}

function GetContentPaneWidth() {
    var contentPane = document.getElementById("content");
    var width = contentPane.style.Width;
    var index = width.search("px");
    if (index != -1) {
        return width.substring(0, index);
    }
    else {
        alert("Can not determine the width of the contentpane");
        return "";
    };
}
