﻿function getWindowHeight() {
    var windowHeight = 0;
    if (typeof (window.innerHeight) == 'number') {
        windowHeight = window.innerHeight;
    }
    else {
        if (document.documentElement && document.documentElement.clientHeight) {
            windowHeight = document.documentElement.clientHeight;
        }
        else {
            if (document.body && document.body.clientHeight) {
                windowHeight = document.body.clientHeight;
            }
        }
    }
    return windowHeight;
}
function getWindowWidth() {
    var windowWidth = 0;
    if (typeof (window.innerWidth) == 'number') {
        // firefox scrollbar is 18px width
        windowWidth = window.innerWidth - 18;
    }
    else {
        // internet explorer
        if (document.documentElement && document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        }
        else {
            if (document.body && document.body.clientWidth) {
                windowWidth = document.body.clientWidth;
            }
        }
        windowWidth -= 1;
    }
    return windowWidth;
}
function SetFooter() {
    if (document.getElementById('FeaturedCaseStudy')) {
        AdjustLeftMainRightHeight();
    }

    if (document.getElementById) {
        var windowHeight = getWindowHeight();
        var bodyHeight = document.getElementById('Content').offsetHeight;
        var headerElement = document.getElementById('Header');
        var footerElement = document.getElementById('Footer');
        if (document.getElementById('FooterRelatedCategories')) {
            var footerRelatedCategories = document.getElementById('FooterRelatedCategories').offsetHeight;
        }
        else {
            var footerRelatedCategories = 90;
        }
        bodyHeight -= 55; //Add padding which is not determined by .offsetHeight

        if (document.getElementById('ctl00_pnlRelated')) {
            var varRelatedHeight = document.getElementById('ctl00_pnlRelated').offsetHeight;
        }
        else {
            var varRelatedHeight = 0;
        }

        var middleElement = document.getElementById('Content');
        var footerHeight = footerElement.offsetHeight;
        if (BrowserDetect.browser == 'Explorer' && (BrowserDetect.version == 7 || BrowserDetect.version == 6)) {
            footerHeight = 60;
        }
        var headerHeight = headerElement.offsetHeight;
        bodyHeight += headerHeight + footerHeight + footerRelatedCategories + varRelatedHeight + 55; //adding menu and breadcrumbs' height
        
        //home page
        if (bodyHeight <= windowHeight) {
            if (document.getElementById('LatestNews')) {
                
                middleElement.style.height = (windowHeight - footerHeight - footerRelatedCategories - headerHeight + 25) + "px"
            }
            else {
                middleElement.style.height = (windowHeight - footerHeight - footerRelatedCategories - varRelatedHeight - headerHeight - 0) + "px"
            }
            //alert("bodyHeight " + windowHeight + ", varRelatedHeight " + varRelatedHeight + ", footerRelatedCategories " + footerRelatedCategories + ", headerHeight " + headerHeight + ", footerHeight " + footerHeight + " = " + middleElement.style.height)
            //alert("middleElement" + middleElement.offsetHeight);
        }
        else {
            
        }
    }
    //Execute below function only for the home page.
    if (document.getElementById('FeaturedCaseStudy')) {
        foo(1); OnMouseOverFeatureLoop(1);
    }
    var varContentHeight = document.getElementById('Content').offsetHeight;
    document.getElementById('ContentLeft').style.height = varContentHeight + 'px';
    document.getElementById('ContentRight').style.height = varContentHeight + 'px';

    var varHeaderHeight = document.getElementById('Header').offsetHeight;
    document.getElementById('HeaderLeftBackground').style.height = varHeaderHeight + 'px';
    document.getElementById('HeaderRightBackgroud').style.height = varHeaderHeight + 'px';

    if (document.getElementById('ctl00_pnlRelated')) {
        var varRelatedHeight = document.getElementById('ctl00_pnlRelated').offsetHeight;
        document.getElementById('PublicRelatedLeft').style.height = varRelatedHeight + 'px';
        document.getElementById('PublicRelatedRight').style.height = varRelatedHeight + 'px';
    }

    
}
function SetMainRightWidth() {

    var BodyWidth = getWindowWidth();

    var LeftContentElement = document.getElementById('PublicLeftContent');
    var MainRightContentElement = document.getElementById('PublicMainRightContent');

    var LeftContentWidth = LeftContentElement.offsetWidth;

    //alert("bodyWidth: " + BodyWidth + " Left: " + LeftContentWidth)

    MainRightContentElement.style.width = (BodyWidth - LeftContentWidth) + "px";
}
function SetBorderHeights() {
    /*
    - Enforces min height
    - Adjusts the height of left, main, and right section of the page to be the same.
    */

    var arrContainerNames = new Array("FeaturedCategory", "FeaturedCaseStudy", "StaticHomePageText");

    var intMininumHeightAllowed = 0;
    var intMinimumHeight = intMininumHeightAllowed; // the mininum height out of the three sections
    var intMaximumHeight = intMininumHeightAllowed; // the maximum height out of the three sections

    for (i = 0; i < arrContainerNames.length; i++) {
        var obj = document.getElementById(arrContainerNames[i]);
        if (obj != null) {
            var intCurrentHeight = obj.offsetHeight;
            //alert(intCurrentHeight);

            // find minimum height
            if (intCurrentHeight > intMininumHeightAllowed // must be at least greater than the height allowed
                && intCurrentHeight < intMinimumHeight) {
                intMinimumHeight = intCurrentHeight;
            }

            // find maximum height
            if (intCurrentHeight > intMaximumHeight) {
                intMaximumHeight = intCurrentHeight;
            }
        }
    }

    // by now, min and max height are found    

    for (i = 0; i < arrContainerNames.length; i++) {
        var obj = document.getElementById(arrContainerNames[i]);
        if (obj != null) {
            obj.style.height = intMaximumHeight + "px";
            //alert(arrContainerNames[i] + intMaximumHeight);
        }
    }
}

//Detect the Browser
var BrowserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
    dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

};
BrowserDetect.init();
