/**************************************************************
	2009.1, Ye Wang
	DHTML menu:
	- resolution 1024 
	- resolution 1024+
	- IE6, 7, 8
	- Firefox 2/3
	- Safari
**************************************************************/

// Detect IE 6 & 7
function isMSIE6(){
	if(jQuery.browser.msie && jQuery.browser.version.substr(0,1)!=8) {
		return true;
	} else {
		return false;
	}
}
// Detect Safari
function isSafari(){
	if (jQuery.browser.safari) {
		return true;
	} else {
		return false;
	}
}

function mainmenu(){
	msie6 = isMSIE6();
	safari = isSafari();
	$("#mainNavBanner ul").css({display: "none"}); // Opera Fix
	$("#mainNavBanner li").hover(  
		function(){     
			var thisMenu = $(this); 
			if (!msie6) {	// IE8 and Firefox fix: add drop-shadow image and positioning adjustment
				thisMenu.find("ul:first li:first").css({borderTop: "1px solid #999933"});
				thisMenu.find("ul:first li:last-child").css({borderBottom: "1px solid #CCCC99"});
			}  
			if (safari) { // Safari for James!!! 
				$(this).find("br").css({display:"block"});
			}
			thisMenu.find('ul:first').css({visibility: "visible", display: "none"}).show(); 
			thisMenu.find("a:first").addClass("on"); 
		},function(){
			$(this).find("a:first").removeClass("on");
			$(this).find('ul:first').css({visibility: "hidden", display: ""});
		} 
	);  
	// submenu repositioning
	$("#mainNavBanner li ul li a").hover( 
		function(e){  
			var curSubMenu = $(this).parent("li").find("ul:first");
 
			if (curSubMenu.find("li").length) {
				curHeight = $(this).height();
				wHeight = $(window).height() - $("#mainNavBanner").position().top ;
				mHeight = curSubMenu.outerHeight();  
				
				if (msie6) {	// IE6 
					curTop = $(this).parent().position().top;  
					wHeight = wHeight - curHeight;
				} else {
					curTop = $(this).position().top;
				}
				//$(this).text($(this).parent().offset().top + "|" + $(this).offset().top + "|" + e.pageY + '|' + e.clientY + '|' + curSubMenu.find("li:first").scrollTop());
				if (curTop + mHeight < wHeight) {
					newTop =  mHeight + curTop - wHeight + 5;
				} else {
					newTop = mHeight;
				}
				// re-adjust top margin based on available space: resolution, broswer size and etc;
				if (newTop >= e.clientY) {
					newTop = e.clientY - curHeight;
				}				
				
				// re-adjust top margin based on parent menu item hight;
				if (curHeight > 20 && curHeight > newTop) {
					newTop = curHeight + 10;
				}
				
				// apply top margin  
				if (newTop > 0) {
					curSubMenu.css({marginTop: newTop *(-1)}); 
				} else if (curHeight > 20) {
					curSubMenu.css({marginTop: -(curHeight+10)});
				}
			}
		},function(){
		}
	);
} 

$(document).ready(function(){ 
	mainmenu(); 
});
 

