var Url = {

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

$(function () {
	$("#image-fader").innerfade ({
			speed: 2000,
			timeout: 5000
		}
	),
	$("#navigation ul li a").hover (function () {
			$("#navigation ul li a").removeClass ("nav-active-item");
			$(this).addClass("nav-active-item")
		},
		function () {
			$(this).removeClass("nav-active-item")
			$("#nav-active-item").addClass ("nav-active-item");
		}
	),
	$("a").click (function () {
		$(this).blur();
	}),
	$("#lhs-bar ul li a.initiate-slide").click (function (event) {
		if (!checkSafari())
		{
			event.preventDefault(); /* bug fix for safari */
			$("#lhs-bar ul li div").each ( function () {
				if ($(this).is(":visible"))
				{
					$(this).slideUp();
				}
			})
			if ($(this).parent().find("div").is(":hidden"))
			{
				$(this).parent().find("div").slideDown();
			}
			//return false;
		}
	}),
	/* search to see which page we are on, then
	 * display (open) the appropriate menu item
	 */
	$("#lhs-bar div a").each(function () {
		if (Url.decode(location.href).indexOf ($(this).attr("href"))!=-1)
		{
			$(this).parent().parent().parent().css("display", "block");
			$(this).addClass ("active-sub-menu-item");
			$(this).attr("id","active-sub-menu-item");
		}
	}),
	/** remove the active-sub-menu-item class from #active-sub-menu-item
	 * when another button is hovered over, then add it again when mouseout
	 */
	$("#lhs-bar div a").hover(function () { /* hover function */
			$("#active-sub-menu-item").removeClass("active-sub-menu-item");
		},
		function () { /* out function */
			$("#active-sub-menu-item").addClass ("active-sub-menu-item");
		}
	),
	/** open external lhs-nav links in new window **/
	$("#lhs-bar div a").click(function () {
		loc = Url.decode(location.href).split("/",3);
		hname = loc[2];
		/* can only be another hostname if it has http:// or https:// at the start */
		if (($(this).attr("href").indexOf("http://")==0
						|| $(this).attr("href").indexOf("https://")==0)
				/* if has http:// check to see if the hostname is the same as current */
				&& $(this).attr("href").indexOf("http://"+hname)==-1)
		{
			$(this).attr("target","_blank");
		}
	});
});

if (checkSafari())
{
	$("#lhs-bar ul li div").css("display", "block");
}

function checkSafari ()
{
	return ($.browser.safari && navigator.userAgent.toLowerCase().indexOf('chrome') == -1);
}