// 插件来源于互联网，henry@celay做了适当优化
// 2009年8月
// 2011年11月：更改主菜单当前背景，如果用css的hover，在IE下显示有Bug
// 子菜单位置稍有偏差，而且FF上效果还出不来,shit!!!
// 所以改用脚本实现
$(document).ready(function() {
	var menuBar = "ul#topnav";
	var menuStr = "ul#topnav li";
	var menuSubLeft = "div.menu1-level1-no";
	var currentpath = "/fileadmin/template/Celay2011_CN/Resources";

	//居中主菜单
	var menuWidth = ($(menuStr).length) * ($(menuStr).width());
	var totalWidth = $(menuBar).parent().width();
	var leftPos = (totalWidth - menuWidth)/2 ;
	$(menuBar).css("left", leftPos);

	//调整每个子菜单的位置
	$(menuStr).each(function(){
		var navMenu = $(menuBar)
		var navOffset = navMenu.offset();

		var upOffset = $(this).offset();
		var subSpan = $(this).find("span");
		var spanOffset = subSpan.offset();
		var leftPos = upOffset.left - (subSpan.width() - $(this).width())/2;
		if (leftPos < navOffset.left){
			leftPos = navOffset.left + 8;
		}else if ((leftPos + subSpan.width()) > (navOffset.left + navMenu.width())){
			leftPos = navOffset.left + navMenu.width() - subSpan.width();
		}else{
			leftPos = leftPos + 8
		}
		subSpan.css("left", leftPos - navOffset.left);
	}),
	$(menuStr).hover(function() { //Hover over event on list item
		$(this).css({
			background: "url('" + currentpath + "/menu_mainpointer.png')"
		});
		$(this).find("span").fadeIn('normal'); //Show the subnav
	},
	function() { //on hover out...
		$(this).css({
			background: "none"
		});
		$(this).find("span").fadeOut('normal'); //Hide the subnav
	});

	//调整左侧二级菜单样式
	$(menuSubLeft).hover(function() {
		$(this).css("background","url('" + currentpath + "/menu_subSelected.png')");
		$(this).css("background-repeat","no-repeat");
		$(this).css("background-position","center");
	},
	function() {
		$(this).css({
			background: "none"
		});
	});
});
