/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);



/**
 * 
 * init menu
 * 
 * @author Jun
 * 
 */
$(function() {

	var menuWidth = $("div.nav").innerWidth() - parseInt($("div.ssm").css("padding-left")) * 2;

	
	// show/hide menu when mouse hover
	$.each($("ul.navf a"), function(i) {
		if (this.rel == menuId) {
			initMenu(this, i);
		}

		$(this).hoverIntent(function() {
			var id = $(this).attr("id").replace("menu-","");
			var isHidden = $("div[id=sub-" + id + "]").is(":hidden");
			if(isHidden){
				initMenu(this, i);
			}
		}, function() {
		});
	});

	var config = {
	     interval: 200,
	     over: function(){},
	     timeout: 1000,
	     out: hideMenu
	};
	function hideMenu(){ 
		if (isHome) {
			$("ul.navf a").removeClass("on");
			$("div[id^=sub-]:visible").hide();
		} else {
			var menuAnchor = $("ul.navf a[rel=" + menuId + "]");
			if (menuAnchor.length == 1) {
				var id = menuAnchor.attr("id").replace("menu-", "");
				var isHidden = $("div[id=sub-" + id + "]").is(":hidden");
				if (isHidden) {
					initMenu(menuAnchor, id);
				}
			}
		}
	}
	
	// return to current submenu
	$("div.nav").hoverIntent(config);

	function initMenu(menu, subIndex) {
		var offsetWidth = 3;
		
		$("ul.navf a").removeClass("on");
		$("div[id^=sub-]:visible").hide();
		
		var $subMenu = $("#sub-" + subIndex);
		var $subTable = $("table", $subMenu);
		
		$(menu).addClass("on");
		$subMenu.show();
		
		if (subIndex == $("ul.navf a").length - 1) {
			width = Math.floor(menuWidth - $subTable.width()) - offsetWidth;
			$subTable.css("margin-left", width + "px");
		} else if (subIndex > 0) {
			var mw = 0;
			$("ul.navf a:lt(" + subIndex + ")").each(function(i) {
				mw += $(this).width();
			})
			mw += Math.floor($("ul.navf a:eq(" + subIndex + ")").width() / 2);
			sw = Math.floor($subTable.width() / 2);
			var offset = mw - sw - parseInt($("div.ssm").css("padding-left"));
			var padding = parseInt($("ul.navf").css('padding-left'));
			offset += padding;
			if (offset > 0) {
				var width = Math.floor(menuWidth - $subTable.width()) - offsetWidth;
				if(offset + $subTable.width() >= menuWidth){
					$subTable.css("margin-left", width + "px");
				}else{
					$subTable.css("margin-left", offset + "px");
				}
			}
		}
	} 
})
