function megaHoverOver(){
    $(this).find(".sub").stop().fadeTo('fast', 1).show();
    $(this).find(".edge").stop().fadeTo('fast', 1).show();
		
    //Calculate width of all ul's
    (function($) { 
	    jQuery.fn.calcSubWidth = function() {
		    rowWidth = 0;
		    //Calculate row
		    $(this).find("ul").each(function() {	
		        var ulWidth = $(this).width();			
		    	if ($(this).css('padding-left') != undefined) { // Include left padding if it exists
                    ulWidth += parseInt($(this).css('padding-left').replace('px',''));
                }
                if ($(this).css('padding-right') != undefined) { // Include right padding if it exists
                    ulWidth += parseInt($(this).css('padding-right').replace('px',''));
                }	
			    rowWidth += ulWidth;
		    });	
	    };
    })(jQuery); 
	
    $(this).calcSubWidth();
    
    //Set Width
    $(this).find(".sub").css({'width' : rowWidth});
	
	iframeWidth = 0;
	
	if (rowWidth > 0)
	{
		iframeWidth = rowWidth + 12;
		$(this).find("#navframe").css({'width' : iframeWidth});
		$(this).find("#navframe").css({'display' : 'block'});
	}
    
    if ($(this).find(".sub").css('padding-left') != undefined) {
        rowWidth += parseInt($(this).find(".sub").css('padding-left').replace('px',''));
    }
    if ($(this).find(".sub").css('padding-right') != undefined) {
        rowWidth += parseInt($(this).find(".sub").css('padding-right').replace('px',''));
    }
    
    // Reposition .sub if it would go over the edge of the nav
    var subSpace = $(this).position().left + rowWidth;
  
	var navTotalWidth = $(this).parent().width(); // Get the total nav width from the parent (#topnav)
  
    if (subSpace > navTotalWidth) {
        $(this).find(".sub").css({'left' : navTotalWidth - subSpace - ($(this).find(".edge").width() - edgeOffset)});
    }
    
    // Set .edge position
    if ($(this).find(".sub").css('left') != undefined) {
        $(this).find(".edge").css({'left' : parseInt($(this).find(".sub").css('left').replace('px','')) + rowWidth - edgeOffset});
    }
}

function megaHoverOut(){ 
  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
      $(this).hide(); 
  });
  $(this).find(".edge").stop().fadeTo('fast', 0, function() {
      $(this).hide(); 
  });
  
  $(this).find("#navframe").css({'display' : 'none'});
}

function fixMainNavHighlight(){
	var currentUrl = document.URL;
	currentUrl = currentUrl.replace("http://", "");
	currentUrl = currentUrl.substr(currentUrl.indexOf("/"));
	
	if (currentUrl != "/")
	{
		var navElements = document.getElementById("topnav").getElementsByTagName("li");
		
		var i = 0;
		
		while (i < navElements.length)
		{
			if (navElements[i].innerHTML.indexOf('<div class="sub">') != -1 || navElements[i].innerHTML.indexOf('<DIV class=sub>') != -1)
			{
				if (navElements[i].innerHTML.indexOf(currentUrl) != -1)
				{
					navElements[i].innerHTML = navElements[i].innerHTML.replace('title=', 'class="currentCategory" title=');
				}
			}
			
			i = i + 1;
		}
	}
}
