$(document).ready(function() {
    globalsInit();
});

function globalsInit() {
    pngFix();
    rolloversInit();
    menuInit();
    languageInit();
    try {
        pageInit();
    } catch (e) {
    }
}

function pngFix() {
    if (/MSIE (5\.5|6\.)/.test(navigator.userAgent)) {
        $("div.alpha").each(function(i, alphaDiv) {
            pngFixBox(alphaDiv);
        });
    }
}

function pngFixBox(alphaDiv, debug) {
    var objDiv = $(alphaDiv);
    if (!objDiv.length) alert ("[pngFixBox] no jquery objDiv");
    var newImg = $("<img/>");

    //id + src attributes
    newImg.attr("id", objDiv.attr("id") + "_png");
    var newSrc = objDiv.css("backgroundImage").replace("url(\"", "").replace("\")", "");
    newImg.attr("src", newSrc);

    //width
    var divStyleWidth = isNaN(parseInt(objDiv.css("width").replace("px", ""))) ? 0 : parseInt(objDiv.css("width").replace("px", ""));
    var divStylePadLeft = isNaN(parseInt(objDiv.css("paddingLeft").replace("px", ""))) ? 0 : parseInt(objDiv.css("paddingLeft").replace("px", ""));
    var divStylePadRight = isNaN(parseInt(objDiv.css("paddingRight").replace("px", ""))) ? 0 : parseInt(objDiv.css("paddingRight").replace("px", ""));
    var imgTotalWidth = (divStyleWidth + divStylePadLeft + divStylePadRight) + "px";
    newImg.css("width", imgTotalWidth);

    //height
    var divStyleHeight = isNaN(parseInt(objDiv.css("height").replace("px", ""))) ? 0 : parseInt(objDiv.css("height").replace("px", ""));
    var divStylePadTop = isNaN(parseInt(objDiv.css("paddingTop").replace("px", ""))) ? 0 : parseInt(objDiv.css("paddingTop").replace("px", ""));
    var divStylePadBottom = isNaN(parseInt(objDiv.css("paddingBottom").replace("px", ""))) ? 0 : parseInt(objDiv.css("paddingBottom").replace("px", ""));
    var imgTotalHeight = (divStyleHeight + divStylePadTop + divStylePadBottom) + "px";
    newImg.css("height", imgTotalHeight);

    //positioning
    newImg.addClass("alpha");
    newImg.css("position", "absolute");
    newImg.css("top", objDiv.css("position") == "absolute" ? objDiv.css("top") : objDiv.get(0).offsetTop);
    newImg.css("left", objDiv.css("position") == "absolute" ? objDiv.css("left") : objDiv.get(0).offsetLeft);
    newImg.css("bottom", objDiv.css("position") == "absolute" ? objDiv.css("bottom") : "");
    newImg.css("right", objDiv.css("position") == "absolute" ? objDiv.css("right") : "");
    newImg.css("zIndex", (objDiv.attr("id") == "ui-datepicker-div" ? "0" : objDiv.css("zIndex") - 1));
    newImg.css("display", "block");
    objDiv.css("backgroundImage", "none");
    objDiv.get(0).onpropertychange = function() {
        handleBoxChange(this);
    }
    newImg.insertBefore(objDiv);
}

function handleBoxChange(objBox) {
    var objBoxPng = document.getElementById(objBox.id + "_png");
    if (objBoxPng) {
        objBoxPng.style.display = objBox.currentStyle["display"];
    }
}

function rolloversInit() {
    // preload all image rollovers
    var arrPreload = new Array();

    $("img.imgOver").each(function (i) {
        if (!/MSIE (5\.5|6\.)/.test(navigator.userAgent) || (/MSIE (5\.5|6\.)/.test(navigator.userAgent) && this.className.indexOf("alpha") !== -1)) {
            var src = this.src;
            var imgtype = src.substring(src.lastIndexOf("."), src.length);
            var altsrc = src.replace(imgtype, "_on" + imgtype);

            this.setAttribute("altsrc", altsrc);
            arrPreload[i] = new Image();
            arrPreload[i].setAttribute("src", altsrc);
        }
    });

    // set mouse events for everything that's not a main menu item)
    $("img.imgOver").filter(":not(.mainMenu)").each(function() {
        if (!/MSIE (5\.5|6\.)/.test(navigator.userAgent) || (/MSIE (5\.5|6\.)/.test(navigator.userAgent) && this.className.indexOf("alpha") !== -1)) {
            this.onmouseover = function() {
                handleImageMouseover(this);
            }

            this.onmouseout = function () {
                handleImageMouseout(this);
            }
        }
    });
}



// rollover event handlers
function handleImageMouseover(objImage) {
    if (!/MSIE (5\.5|6\.)/.test(navigator.userAgent)) {
        var strSrcTmp;
        strSrcTmp = objImage.getAttribute("src");
        objImage.setAttribute("src", objImage.getAttribute("altsrc"));
    }
}

function handleImageMouseout(objImage) {
    if (!/MSIE (5\.5|6\.)/.test(navigator.userAgent)) {
        var strSrcTmp;
        var src = objImage.src;
        var imgtype = src.substring(src.lastIndexOf("."), src.length);
        if (!strSrcTmp) strSrcTmp = objImage.getAttribute("altsrc").replace("_on" + imgtype, imgtype);
        objImage.setAttribute("src", strSrcTmp);
    }    
}

// menu initialization
var menuTimeout;

function menuInit() {
    // mouse events for about button
    $("img#mainAbout").mouseover(function() {
        if ($("img#mainAbout").hasClass("imgOver")) {
            handleImageMouseover(this);
            handleMenuMouseover();
        }
    });

    $("img#mainAbout").mouseout(function() {
        if ($("img#mainAbout").hasClass("imgOver")) {
            handleMenuMouseout();
        }
    });

    // mouse events for other main items
    $("div#menuBox img.imgOver:not(#mainAbout)").mouseover(function() {
        if ($("img#mainAbout").hasClass("imgOver")) {
            hideSubmenu();
        }
        handleImageMouseover(this);
    });

    $("div#menuBox img.imgOver:not(#mainAbout)").mouseout(function() {
        handleImageMouseout(this);
    });

    // mouse events for submenu box
    if ($("div#menuBox img:first").hasClass("imgOver")) {
        $("div#submenuBox").mouseover(function() {
           handleSubmenuMouseover();
        });

        $("div#submenuBox").mouseout(function() {
           handleMenuMouseout();
        });
    } else {
        $("div#submenuBox").css("display", "block");
    }
}

// menu event handlers
function handleMenuMouseover() {
    clearTimeout(menuTimeout);
    $("div#submenuBox").css("display", "block");
}

function handleSubmenuMouseover() {
    clearTimeout(menuTimeout);
}

function handleMenuMouseout() {
    menuTimeout = setTimeout("hideSubmenu()", 500);
}

function hideSubmenu() {
    handleImageMouseout($("img#mainAbout").get(0));
    $("div#submenuBox").css("display", "none");
}

function cookieHandler() {
    //methods
    this.Create = createCookie;
    this.Read = readCookie;
    this.Erase = eraseCookie;

    //create or overwrite a value
    function createCookie(strName, strValue, intDays) {
        var skyExpires;
        if (intDays) {
            var skyDate = new Date();
            skyDate.setTime(skyDate.getTime() + (intDays * 24 * 60 * 60 * 1000));
            skyExpires = "; expires=" + skyDate.toGMTString();
        } else {
            skyExpires = "";
        }
        document.cookie = strName + "=" + strValue + skyExpires + "; path=/";
    }

    //retrieve a value
    function readCookie(strName) {
        var arrCookie = document.cookie.split('; ');
        for (var i=0; i<arrCookie.length; i++) {
            var nameValue = arrCookie[i].split('=');
            if (nameValue[0] == strName) {
                return nameValue[1];
            }
        }
        return null;
    }

    //erase a value
    function eraseCookie(strName) {
        this.Create(strName, "", -1);
    }
}

/**
 * Object Prototypes
 */
Array.prototype.search = arr_search;
Array.prototype.searchProperty = arr_searchProperty;
Array.prototype.getPropertyValue = arr_getPropValue;

/**
 * search an array for a given value and return the corresponding index if successful, otherwise return -1
 */
function arr_search(strValue) {
	var intFound = -1;
	for (var i=0; i<this.length; i++) {
		if (this[i] == strValue) {
			intFound=i;
			break;
		}
	}
	return intFound;
}

/**
 * search an associative array for a property value, return the index of that element
 */
function arr_searchProperty(strSearchPropName, strValue) {
	var intFound = -1;
	for (i=0; i<this.length; i++) {
		if (eval("this[i]." + strSearchPropName) == strValue) {
			intFound=i;
			break;
		}
	}
	return intFound;
}

/**
 * search an associative array for a property value, return another property value from the desired element
 */
function arr_getPropValue(strSearchPropName, strValue, strReturnPropName) {
	var intFound = -1;
	for (i=0; i<this.length; i++) {
		if (eval("this[i]." + strSearchPropName) == strValue) {
			intFound = i;
		}
	}
	if (intFound > -1) {
		return eval("this[intFound]." + strReturnPropName);
	} else {
		return intFound;
	}
}
function ptq(q)
{
var x = q.replace(/;/g, '&').split('&'), i, name, t;
/* q changes from string version of query to object */
for (q={}, i=0; i<x.length; i++)
{
t = x[i].split('=', 2);
name = unescape(t[0]);
if (!q[name])
q[name] = [];
if (t.length > 1)
{
q[name][q[name].length] = unescape(t[1]);
}
/* next two lines are nonstandard */
else
q[name][q[name].length] = true;
}
return q;
}

function param() {
return ptq(location.search.substring(1).replace(/\+/g, ' '));
}