var highlightMenu = true;
var textResizing = true;
var expandingMenu = false;

$(document).ready(function(){
    // <li> Hover for IE6 and below
	if($.browser.msie && $.browser.version < "7") {
	    $("#nav li").hover(function() {
	        $(this).addClass("hover");
	    }, function(){
	        $(this).removeClass("hover");
	    });
	}
	
	$("ul#nav > li.navdrop > a").click(function(e){
        e.preventDefault();
        return false;	    
	});
	
	$("#nav a").each(function(){
        // Add active class to current page link in the menu
        if (highlightMenu) {
            if (location.href.indexOf(this.href) != -1) $(this).addClass("active");
        }
        
        $(this).click(function(e){
            $(this).parents('li').toggleClass('hover');
        });
        
        // Collapse lists again when the link loses focus
        /* The blur executes before the other link click event, thus preventing 
           the click event by hiding the link too quickly, the ul has to be 
           hidden with opacity for half a second, long enough to register the 
           click event, and then raise the opacity and remove the hover class. */
        $(this).blur(function() {
            var lnk = $(this);
            lnk.closest('li.navdrop').children('ul.subnav').css({'opacity' : 0});
            var to = setTimeout(function(){
                lnk.parents('#nav li').removeClass('hover');
                lnk.closest('li.navdrop').children('ul.subnav').css({'opacity' : 1});
            }, 500);
        });
    });
    
    // Text resizing
    if(textResizing){
        changeTextSize(readCookie('style'));
        $('#widgets a').click(function(event){
            event.preventDefault();
            changeTextSize(this.id);
        });
    }
    
    if(expandingMenu){
        var navid = "" + $.query.get('n');    
        $("#vNav ul.subnav").hide();    

        if(navid != ""){
            $("#vNav li#" + navid).parents("ul.subnav").show();
            $("#vNav li#" + navid).children("ul.subnav").show();
        }
           
        $("#vNav li.navdrop > a").next("ul.subnav").children("li").children("a").each(function(){      
            $(this).attr("href", $(this).attr("href") + "?n=" + $(this).parent("li").parent("ul").parent("li").attr("id"));
        });
        
        $("#vNav li.navdrop > a").click(function() {
            $(this).next("ul.subnav").slideToggle();
            return false;
        });
    }    
});