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") {
                    Result = (Result == "1") || (Result == "true") || Result;
                }
                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");
    },
    imageCache: new Array(),
    buildImageCache: function(imageNames) {
        if (!imageNames || !imageNames.length || (imageNames.length == 0)) return;
        this.imageCache = new Array();
        for (var i = 0; i < imageNames.length; i++) {
            this.imageCache[imageNames[i]] = new Image();
            this.imageCache[imageNames[i]].src = "images/" + imageNames[i] + ".jpg";
            this.imageCache[imageNames[i] + "-sm"] = new Image();
            this.imageCache[imageNames[i] + "-sm"].src = "images/" + imageNames[i] + "-sm.jpg";
            this.imageCache[imageNames[i] + "-bw"] = new Image();
            this.imageCache[imageNames[i] + "-bw"].src = "images/" + imageNames[i] + "-bw.jpg";
            this.imageCache[imageNames[i]].next = (i < (imageNames.length - 1)) ? imageNames[i + 1] : imageNames[0];
        }
    },
    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);
    },
    setOpacity: function(elem, opacityAsInt) {
        var opacityAsDecimal = opacityAsInt;
        if (opacityAsInt > 100) opacityAsInt = opacityAsDecimal = 100;
        else if (opacityAsInt < 0) opacityAsInt = opacityAsDecimal = 0;
        opacityAsDecimal /= 100;
        //MSIE7 bug:
        if (opacityAsInt < 1) opacityAsInt = 1;
        elem.style.opacity = opacityAsDecimal;
        elem.style.filter = "alpha(opacity=" + opacityAsInt + ")";
    },
    fadeOpacityStep: function(elemId, stepNum, steps, fromOpacity, delta, timePerStep) {
        this.setOpacity(document.getElementById(elemId), Math.round(parseInt(fromOpacity) + (delta * stepNum)));
        if (stepNum < steps) setTimeout("HJP.fadeOpacityStep('" + elemId + "', " + (stepNum + 1) + ", " + steps + ", " + fromOpacity + ", " + delta + ", " + timePerStep + ");", timePerStep);
    },
    fadeOpacity: function(elemId, fromOpacity, toOpacity, time, fps) {
        var steps = Math.ceil(fps * (time / 1000));
        var delta = (toOpacity - fromOpacity) / steps;
        this.fadeOpacityStep(elemId, 0, steps, fromOpacity, delta, (time / steps));
    },
    showInLightbox: function(Thumb, Next) {
        if (!Thumb || !Thumb.src) return;
        Thumb.parentNode.blur();
        if (!this.PhotoChain) {
            this.PhotoChain = new Array();
            for (var i in document.images) {
                if (document.images[i].src && (document.images[i].src.indexOf("images/galleries") != -1)) {
                    this.PhotoChain.push(document.images[i].src);
                }
            }
            for (var i = 0; i < this.PhotoChain.length; i++) {
                //Map every photo URL to index of following image
                this.PhotoChain[this.PhotoChain[i]] = (i + 1) % this.PhotoChain.length;
            }
        }
        var Lightbox = document.getElementById("lightboxpicture");
        if (!(Lightbox.className == "framed")) {
            Lightbox.className = "framed";
        } else {
            this.agentMapper.setOpacity(Lightbox, 0);
        }
        if (typeof this.PhotoChain[Thumb.src] == "undefined") this.PhotoChain[Thumb.src] = 0;
        Lightbox.src = Next ? this.PhotoChain[this.PhotoChain[Thumb.src]] : Thumb.src;
        this.fadeOpacity("lightboxpicture", 20, 100, 250, 30);
    },
    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();
    }
}

