String.prototype.trim = function() {
    return (this.replace(/\s+$/, "").replace(/^\s+/, ""));
}
var HJP = {
    urlParams: function() {
        var _URLPARAMS = new Array();
        if (document.location.href.indexOf("?") != -1) {
            var URLparamPairs = document.location.href.split("?")[1].split("&");
            for (var u in URLparamPairs) {
                var OneParamPair = URLparamPairs[u].split("=");
                if (OneParamPair.length > 1) {
                    _URLPARAMS[OneParamPair[0]] = OneParamPair[1];
                }
            }
        }
        return {
            getValue: function(ParameterName, DefaultValue) {
                var Result = (typeof _URLPARAMS[ParameterName] != "undefined") ? _URLPARAMS[ParameterName] : DefaultValue;
                if (typeof DefaultValue == "number") {
                    Result = parseFloat(Result)
                } else if (typeof DefaultValue == "boolean") {
                    if ((Result == "1") || (Result == "true")) {
                        Result = true;
                    } else if ((Result == "0") || (Result == "false")) {
                        Result = false;
                    }
                }
                return Result;
            }
        }
    }(),
    agentMapper: function() {
        var _isMSIE = (navigator.appName == "Microsoft Internet Explorer");
        return {
            cssRules: (_isMSIE) ? "rules" : "cssRules",
            isMSIE: _isMSIE,
            setOpacity: function(elem, opacity) {
                opacity = Math.min(Math.max(opacity, 0), 100);
                if (_isMSIE) {
                    //MSIE7 bug:
                    if (opacity < 1) opacity = 1;
                    elem.style.filter = "alpha(opacity=" + opacity + ")";
                } else {
                    elem.style.opacity = (opacity / 100);
                }
            }
        }
    }(),
    setCookieValue: function(Name, Value, StorageDays) {
        if (!navigator.cookieEnabled || !Name) return null;
        if (!StorageDays) StorageDays = 365;
        document.cookie = escape(Name.trim()) + "=" + escape(String(Value)) + ";expires=" + (new Date((new Date()).getTime() + (StorageDays * 86400000))).toGMTString() + ";";
        return Value;
    },
    getCookieValue: function(Name, DefaultValue) {
        var Result = (typeof DefaultValue != "undefined") ? DefaultValue : "";
        if (!navigator.cookieEnabled || !document.cookie || !Name) return Result;
        var Crumbs = document.cookie.split(";");
        for (var i = 0; i < Crumbs.length; i++) {
            var NameAndValue = Crumbs[i].split("=");
            if (NameAndValue[0].trim() == escape(Name.trim())) {
                Result = unescape(NameAndValue[1]);
                return (typeof DefaultValue == "number") ? parseInt(Result, 10) : Result;
            }
        }
        return Result;
    },
    ModifyStyle: function(Selector, Property, Value, StyleSheet) {
        if (!StyleSheet) StyleSheet = 0;
        if (!document.styleSheets[StyleSheet]) return;
        for (var i = 0; i < document.styleSheets[StyleSheet][this.agentMapper.cssRules].length; i++) {
            if (document.styleSheets[StyleSheet][this.agentMapper.cssRules][i].selectorText.toLowerCase().indexOf(Selector.toLowerCase()) != -1) {
                document.styleSheets[StyleSheet][this.agentMapper.cssRules][i].style[Property] = Value;
                break;
            }
        }
    },
    currentTextSize: 12,
    setTextSize: function(change, e) {
        var evt = e || window.event;
        if (evt.ctrlKey) {
            this.currentTextSize = 12;
        } else {
            this.currentTextSize += change;
        }
        this.setCookieValue("TextSize", this.currentTextSize);
        this.ModifyStyle("body", "font", this.currentTextSize + "px/18px Verdana,Geneva,Arial,Helvetica,sans-serif");
    },
    preloadList: new Array(),
    preloadCache: new Array(),
    preloadTimerID: 0,
    preloadCycle: function() {
        clearTimeout(this.preloadTimerID);
        if (!this.preloadList || !this.preloadList.length) return;
        var Index = this.getCookieValue("PreloadIndex", 0);
        if (Index > this.preloadList.length - 1) return;
        this.preloadCache[Index] = new Image();
        this.preloadCache[Index].onload = function() {
            //document.getElementById("logographics").src = HJP.preloadCache[Index - 1].src;
            clearTimeout(HJP.preloadTimerID);
            HJP.preloadTimerID = setTimeout("HJP.preloadCycle()", 500);
        }
        this.preloadCache[Index].onerror = function() {
            clearTimeout(HJP.preloadTimerID);
            HJP.preloadTimerID = setTimeout("HJP.preloadCycle()", 2000);
        }
        this.preloadCache[Index].src = this.preloadList[Index];
        HJP.setCookieValue("PreloadIndex", ++Index, 0.01);
        //Fallback in case of onload not firing (cached data may prevent preload of forthcoming images!)
        this.preloadTimerID = setTimeout("HJP.preloadCycle()", 3000);
    },
    newsDisplay: function(Show, PageID, Force) {
        if (!PageID) PageID = "default";
        Force = Force || HJP.urlParams.getValue("news", false);
        var DisplayNews = this.getCookieValue("DisplayNews-" + PageID, 1);
        if (DisplayNews || Force) {
            document.getElementById("newsblend").style.visibility = Show ? "visible" : "hidden";
            document.getElementById("newsdata").style.visibility = Show ? "visible" : "hidden";
            if (Show) this.setCookieValue("DisplayNews-" + PageID, 0, 0.05);
        }
    },
    init: function() {
        HJP.currentTextSize = HJP.getCookieValue("TextSize", 12);
        HJP.ModifyStyle("body", "font", HJP.currentTextSize + "px/18px Verdana,Geneva,Arial,Helvetica,sans-serif");
        HJP.pageInit();
    }
}

