$(document).ready( function() {		
	/* Hover events for sponsor images	*/
	$('#sponsorImages a').hover( function() {
		var imageElem = $(this).find('img');
		imageElem.attr("src", imageElem.attr("src").split(".").join("_hover."));
	}, function() {
		var imageElem = $(this).find('img');
		imageElem.attr("src", imageElem.attr("src").split("_hover.").join("."));
	});
	
	$('#dataLinks a').hover( function() {
		var imageElem = $(this).find('.rolloverContainer');
		if (imageElem.size() > 0) {
			//imageElem.attr("src", imageElem.attr("src").split(".").join("_hover."));
			imageElem.css('margin-left', '-160px');
		} 
	}, function() {
		var imageElem = $(this).find('.rolloverContainer');
		if (imageElem.size() > 0) {
			//imageElem.attr("src", imageElem.attr("src").split(".").join("_hover."));
			imageElem.css('margin-left', '0px');
		} 
	});
	
	$('#nav li:has(ul)').each ( function() {
		var submenuElement = $('ul', this);
		var highlightPosition = (submenuElement.width() / 2) - ($(this).width() / 2) - 2;
		var parentWidth = (-submenuElement.width() / 2) + ($(this).width() / 2);
		
		// Create hilight
		var menuHighlight = $("<li class='menuHilight'>&nbsp</li>");
		menuHighlight.css('width', $(this).width());
		
		// Set the position of the highlight
		if ($(this).hasClass('noCenter')) {
			menuHighlight.css('margin-left', '50px');	
		} else {
			menuHighlight.css('margin-left', highlightPosition + 'px');
		}
		
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			parentWidth = -($(this).width() / 2) - (submenuElement.width() / 2);
			if ($(this).hasClass('noCenter')) {
				submenuElement.css('margin-left', '-130px');	
			} else {
				submenuElement.css('margin-left', parentWidth + 'px');
			}
		} else {
			if ($(this).hasClass('noCenter')) {
				// This element has been marked as being too close to edge, so set a sane margin to keep it in bounds
				submenuElement.css('margin-left', '-50px');
			} else {
				// Set the margin of this element, so it lines up parent center to child center
				submenuElement.css('margin-left', parentWidth + 'px');
			}
		}
		
		// Add a border to the top of every child element, excluding the first
		submenuElement.children('li:not(:last)').addClass("menuMiddle");
		
		if(submenuElement.children().size() > 1) {
			submenuElement.children('li:first').addClass("menuTop");
			submenuElement.children('li:last').addClass("menuBottom");
		} else {
			submenuElement.children('li:last').addClass("menuSingle");
		}
		
		menuHighlight.prependTo(submenuElement);		
	
	});
	
	/* Animation and Hover events for menu */
	$('#nav li:has(ul)').hover(
		function () {
			var submenuElement = $('ul', this);
			// Add the selected class to the menu's parent element
			$(this).find('a:first').addClass("selected");
			
			// Put the current menu higher than the others, in case nav items have been pushed to next level
			$(this).css('z-index', 501);
			
			// Show the submenu (Fade if not IE, ie has issues with opacity and fading)
			if ($.browser.msie) {
				submenuElement.show();
			} else {
				submenuElement.stop(true, true).fadeIn(250);
			}
		}, 
		function () {
			// Hide the sub menu
			$(this).find('a:first').removeClass("selected");

			// Set back to "default" z-index for menu items
			$(this).css('z-index', 500);
			
			if ($.browser.msie) {
				$('ul', this).hide();
			} else {
				$('ul', this).stop(true, true).fadeOut(250);			
			}
			
		}
	);
});



