var strCurrPageURL = GetCurrentPageURL(); // current page URL (everthing after the last '/' ex. 'index.asp' not 'dirctory/index.asp')
var resetSubNavFlag = 0;		// DO NOT EDIT: flag that controls when the menus can be reset
var timeOutCounter  = 0;		// DO NOT EDIT: Control to decide which timeout can reset the menus
$(document).ready(function(){		

    /***** Site Map *****/
    $('#innerfooter div ul:first li:first').unwrap().remove(); // get rid of the surounding "<ul>" wrapper and the first "<li>" element which is the "HOME" link
    $('#innerfooter div ul ul').addClass('submenu');		   // add "submenu" to all sub sub menus
    $('#innerfooter div ul:not(ul.submenu)').parent().wrap('<div>'); // wrap the top level 'li' elements with '<div>' tags
    $('#innerfooter div div').addClass('sitemapcolumns'); // style the div tags
    $('#innerfooter div div:first').addClass('padleft'); // style the first div tag
    $('#innerfooter div ul:not(ul.submenu)').prev().unwrap().addClass('coral').wrap("<h2>"); // style the top level link and wrap with '<h2>' tags
    $('#innerfooter div ul a').not('#innerfooter ul ul a').wrapInner("<strong>");
    // experimental (talk with marketing about what to do with the extra site-map entries)
    //$('#innerfooter div div:last').remove();
    //$('#innerfooter div div:contains("Languages other than English")').remove();
    $("#innerfooter div#sitemap_container").hide(); // hide the sitemap on page load
	$("#sitemap-link").click(function() {		 // toggle the visibility of the sitemap
	    $("#innerfooter div#sitemap_container").slideToggle(800);
        return false; // returning false makes sure there is no jerkyness to the transition.
    });


    /***** Text size funcionality *****/
    // first look for cookie with font-size info
	if($.cookie('fontsize') == "bigfont") {
        // first add the 'decrease font size' link
		$('#headercontact').html($('#headercontact').html() + ' |  <a id="fontsize" class="decreasefont" href="javascript:void(0);">Decrease font size</a>');
        toggleSetFontSize("increasefont"); // will set it to the larger size
	} else {
        //  add the 'decrease font size' link since there's no cookie
		$('#headercontact').html($('#headercontact').html() + ' |  <a id="fontsize" class="increasefont" href="javascript:void(0);">Increase font size</a>');
	}
    // change the font size when clicked
    $('#fontsize').click(function() {
        toggleSetFontSize($(this).attr('class'));
    });


    /***** Top Nav *****/
    // add <span> tags inside the outermost <a> top nav tabs so that the CSS can add a background image

	$('#nav ul li a.active').addClass("main-nav-hover");

    /* due to IE6 not supporting the ":hover" pseudo class, we have the following work-around: 
	 * 1) add the class "hoverable" to all items (the top menu li elements) that should be hoverable
	 * 2) when an item with class "hoverable" is hovered over then add the class "hovered" to it (and remove it upon the mouse moving on)
	 * 3) change instances of ":hover" in layout.css to ".hovered"
	 */
    $('#nav li').addClass("hoverable");
    $('.hoverable').hover(
        function() {
            $(this).addClass('hovered');     /* On hover add the class 'hovered' and apply the hovered styles */
        }, function() {
            $(this).removeClass('hovered');  /* On mouseout remove the class 'hovered' and reset the styles   */
		}
    );
    $('#nav ul li a').not('#nav ul li ul a').wrapInner('<span />');

    $('#nav ul li a').mouseover(function(e) {
        resetSubNavFlag = 1;					  // subnav shouldn't reset when the mouse is hovering over the main nav so set the flag
		$('#nav ul li a').removeClass("main-nav-hover"); // remove the highlighting from all the main navs
		$(this).addClass("main-nav-hover");				 // set the currently moused over nav to be highlighted
    });

    $('#nav ul li a').mouseout(function() {
		$("#nav ul li a.active").addClass("active");
		resetSubNavFlag = 0;				// mouse is not hovering over the subnav so set the flag to indicate that it can change			
        setTimeout("ShowActiveMenu(" + (++timeOutCounter) + ");",1600); // reset the subnav after the timeout (increment timeOutCounter before passing its value)
    });

    $('#nav li li ul').prev().addClass("navarrow"); // add the indicator that there is a sublevel

    // add the 'active' class to all links that match the current page url
	// TO FIX this doesn't seem to add active to anything!
    $("a[href=" + strCurrPageURL + "]").addClass('active');

    // add active class to the top level nav if a 2nd or 3rd level nav item is the active item
    $('#nav ul ul a.active').parent().parent().prev().addClass('active');
    $('#nav ul ul ul a.active').parent().parent().parent().parent().prev().addClass('active');

    if ($.browser.msie) {
		// removes extra spaces in the drop-down menu for IE browsers
		$('#nav ul li ul li').css('margin', '0 0 -2px 0;');
	}

    /***** LeftNav *****/

    // upon first loading the page collapse all the left-nav third level navigation
	$('#leftmenu ul li ul').addClass("hidden").prev().prev().addClass("twisty_close parent"); // collapse third level nav

    $('#leftmenu ul#menuElem a').filter(function() {
        if ($(this).siblings().length > 0) {
			// if it has siblings then it must be a parent so add the appropriate twisty link and class 
            $("<a class='twisty_close parent category' href='javascript:void(0);'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>").insertBefore($(this));
		} else {
			// link has no siblings so is not a parent... make it's enclosing <li> a class="leaf"
			$(this).parent().addClass("leaf"); 
		}
    });

	$('#leftmenu ul li a.parent').click(function() {
        return ToggleLeftNav($(this), "slow");
    });

    // if the currepage matches a 3rd level nav element then open the 2nd level nav element containing this element
    $("#leftmenu ul li ul li a").filter(function() {
        if ($(this).attr('href') == strCurrPageURL) {
			ToggleLeftNav($(this).parent().parent().prev().prev(), "fast");
		}
    });

    // if this is a second level selected nav then open the third level nav elements if they exist
    $("#leftmenu ul li a").filter(function() {
        if ($(this).attr('href') == strCurrPageURL && $(this).prev().hasClass('parent')) {
			ToggleLeftNav($(this).prev(), "fast"); // open the hidden child unordered list
		}
    });

    // if the currepage matches a 4th level nav element then open the 3rd level nav elements containing this element
    $("#leftmenu ul li ul li ul li a").filter(function() {
        if ($(this).attr('href') == strCurrPageURL) {
			ToggleLeftNav($(this).parent().parent().parent().parent().prev().prev(), "fast");
		}
    });

    /***** Search Results Page *****/
    $('.SearchDialog td').css('padding','5px'); // add in a bit of padding
    $('.SearchDialog td').last().css('padding','0 0 15px 0'); // add more bottom padding
    $('.SearchDialog td input.ContentButton').addClass('button'); // make the search button look like the other search buttons
});

// increases or decreases the text size of the website
// pAction: either "increasefont" or "decreasefont"
function toggleSetFontSize(pAction) {
	pAction = $.trim(pAction);	// remove trailing/leading spaces

	if ( pAction == "increasefont" ) {
		// increase the text size, and change the wording of the link
		$('body').css('font-size', '70%');
		$("#fontsize").html('Decrease font size').removeClass("increasefont").addClass("decreasefont");

		// move the hidden second level nav closer to the top tabs so there's no gap for the mouse to find, which causes sub level nav to disappear.
		// Increasing the font size increases the all ems, but things defined in px aren't affected, which may cause the gap to appear.

		// fix the 1px gap that appears when the font size is increased (for FF & Chrome)
		$('#header').css('height', '122px');

		// set a cookie so all pages will behave the same
		$.cookie('fontsize', 'bigfont', {path: '/', expires: 10} );
	} else {
		// decrease the text size, and change the wording of the link
		$('body').css('font-size', '62.5%');
		$("#fontsize").html('Increase font size').removeClass("decreasefont").addClass("increasefont");

		// reset the 1px fix to normal
		$('#header').css('height', '121px'); // reset the height back to normal

		// we are at the default size, so delete the cookie
		$.cookie('fontsize', 'normalfont', {path: '/', expires: 10});
	}    
	return false;
}

function ToggleLeftNav(pThis, pSpeed) {
        pThis.next().next().slideToggle(pSpeed);	

        // toggle the twisty background image
		if (pThis.hasClass("twisty_close")) {
			pThis.removeClass("twisty_close").addClass("twisty_open");
		} else {
			pThis.removeClass("twisty_open").addClass("twisty_close");
		}
        return false;
}

function GetCurrentPageURL() {
	return window.location.pathname;
}

function ShowActiveMenu(pTimeOutCounter) {
	// only reset the subnav if 
    //     1) the reset flag is 0 (which means we haven't moused over the subnav or main nav since the timeout started) 
    //     2) the pTimeOutCounter param matches the global timeOutCounter (this means that only the last timeout has the power to reset the menus)
    if(resetSubNavFlag == 0 && pTimeOutCounter == timeOutCounter) {
		$('#nav ul li a.main-nav-hover').removeClass("main-nav-hover");
		$('#nav ul li a.active').addClass("main-nav-hover");
    }
}

