//** Smooth Navigational Menu- By Dynamic Drive DHTML code library: http://www.dynamicdrive.com
//** Script Download/ instructions page: http://www.dynamicdrive.com/dynamicindex1/ddlevelsmenu/
//** Menu created: Nov 12, 2008
//** Download by http://sc.xueit.com
//** Dec 12th, 08" (v1.01): Fixed Shadow issue when multiple LIs within the same UL (level) contain sub menus: http://www.dynamicdrive.com/forums/showthread.php?t=39177&highlight=smooth

//** Feb 11th, 09" (v1.02): The currently active main menu item (LI A) now gets a CSS class of ".selected", including sub menu items.

//** May 1st, 09" (v1.3):
//** 1) Now supports vertical (side bar) menu mode- set "orientation" to 'v'
//** 2) In IE6, shadows are now always disabled

//** July 27th, 09" (v1.31): Fixed bug so shadows can be disabled if desired.


var EIMS_C_22_1_ddsmoothmenu={

//Specify full URL to down and right arrow images (23 is padding-right added to top level LIs with drop downs):
arrowimages: {down:['EIMS_C_22_1_downarrowclass', 'images/control/EIMS_CI_22_down.gif'], right:['EIMS_C_22_1_rightarrowclass', 'images/control/EIMS_CI_22_right.gif']},

transition: {overtime:300, outtime:300}, //duration of slide in/ out animation, in milliseconds
shadow: {enable:true, offsetx:5, offsety:5},

///////Stop configuring beyond here///////////////////////////

detectwebkit: navigator.userAgent.toLowerCase().indexOf("applewebkit")!=-1, //detect WebKit browsers (Safari, Chrome etc)
detectie6: document.all && !window.XMLHttpRequest,

getajaxmenu:function($, setting){ //function to fetch external page containing the panel DIVs
	var $menucontainer=$('#'+setting.contentsource[0]) //reference empty div on page that will hold menu
	$menucontainer.html("Loading Menu...")
	$.ajax({
		url: setting.contentsource[1], //path to external menu file
		async: true,
		error:function(ajaxrequest){
			$menucontainer.html('Error fetching content. Server Response: '+ajaxrequest.responseText)
		},
		success:function(content){
			$menucontainer.html(content)
			EIMS_C_22_1_ddsmoothmenu.buildmenu($, setting)
		}
	})
},


buildmenu:function($, setting){
	var smoothmenu=EIMS_C_22_1_ddsmoothmenu
	var $mainmenu=$("#"+setting.mainmenuid+">ul") //reference main menu UL
	$mainmenu.parent().get(0).className=setting.classname || "EIMS_C_22_1_ddsmoothmenu"
	var $headers=$mainmenu.find("ul").parent()
	
	$headers.hover(//鼠标经过选中状态
	function(e){
		$(this).children('a:eq(0)').addClass('selected')
	},
	function(e){
		$(this).children('a:eq(0)').removeClass('selected')
	})
	
	$headers.each(function(i){ //loop through each LI header
		var $curobj=$(this).css({zIndex: 100-i}) //reference current LI header
		var $subul=$(this).find('ul:eq(0)').css({display:'block'})
		this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()}
		this.istopheader=$curobj.parents("ul").length==1? true : false //is top level header?
		$subul.css({top:this.istopheader && setting.orientation!='v'? this._dimensions.h+"px" : 0})
		$curobj.children("a:eq(0)").css(this.istopheader? {paddingRight: smoothmenu.arrowimages.down[2]} : {}).append( //add arrow images
			'<img src="'+ (this.istopheader && setting.orientation!='v'? smoothmenu.arrowimages.down[1] : smoothmenu.arrowimages.right[1])
			+'" class="' + (this.istopheader && setting.orientation!='v'? smoothmenu.arrowimages.down[0] : smoothmenu.arrowimages.right[0])
			+ '" style="border:0;" />'
		)
		if (smoothmenu.shadow.enable){
			this._shadowoffset={x:(this.istopheader?$subul.offset().left+smoothmenu.shadow.offsetx : this._dimensions.w), y:(this.istopheader? $subul.offset().top+smoothmenu.shadow.offsety : $curobj.position().top)} //store this shadow's offsets
			if (this.istopheader)
				$parentshadow=$(document.body)
			else{
				var $parentLi=$curobj.parents("li:eq(0)")
				$parentshadow=$parentLi.get(0).$shadow
			}
			this.$shadow=$('<div class="EIMS_C_22_1_ddshadow'+(this.istopheader? ' EIMS_C_22_1_toplevelshadow' : '')+'"></div>').prependTo($parentshadow).css({left:this._shadowoffset.x+'px', top:this._shadowoffset.y+'px'})  //insert shadow DIV and set it to parent node for the next shadow div
		}
		$curobj.hover(
			function(e){
				var $targetul=$(this).children("ul:eq(0)")
				this._offsets={left:$(this).offset().left, top:$(this).offset().top}
				var menuleft=this.istopheader && setting.orientation!='v'? 0 : this._dimensions.w
				menuleft=(this._offsets.left+menuleft+this._dimensions.subulw>$(window).width())? (this.istopheader && setting.orientation!='v'? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft //calculate this sub menu's offsets from its parent
				if ($targetul.queue().length<=1){ //if 1 or less queued animations
					$targetul.css({left:menuleft+"px", width:this._dimensions.subulw+'px'}).animate({height:'show',opacity:'show'}, EIMS_C_22_1_ddsmoothmenu.transition.overtime)
					if (smoothmenu.shadow.enable){
						var shadowleft=this.istopheader? $targetul.offset().left+EIMS_C_22_1_ddsmoothmenu.shadow.offsetx : menuleft
						var shadowtop=this.istopheader?$targetul.offset().top+smoothmenu.shadow.offsety : this._shadowoffset.y
						if (!this.istopheader && EIMS_C_22_1_ddsmoothmenu.detectwebkit){ //in WebKit browsers, restore shadow's opacity to full
							this.$shadow.css({opacity:1})
						}
						this.$shadow.css({overflow:'', width:this._dimensions.subulw+'px', left:shadowleft+'px', top:shadowtop+'px'}).animate({height:this._dimensions.subulh+'px'}, EIMS_C_22_1_ddsmoothmenu.transition.overtime)
					}
				}
			},
			function(e){
				var $targetul=$(this).children("ul:eq(0)")
				$targetul.animate({height:'hide', opacity:'hide'}, EIMS_C_22_1_ddsmoothmenu.transition.outtime)
				if (smoothmenu.shadow.enable){
					if (EIMS_C_22_1_ddsmoothmenu.detectwebkit){ //in WebKit browsers, set first child shadow's opacity to 0, as "overflow:hidden" doesn't work in them
						this.$shadow.children('div:eq(0)').css({opacity:0})
					}
					this.$shadow.css({overflow:'hidden'}).animate({height:0}, EIMS_C_22_1_ddsmoothmenu.transition.outtime)
				}
			}
		) //end hover
		

	}) //end $headers.each()
	
	//Click Event
	$mainmenu.find("li").each(function(i){
		$(this)
			.click(function(){
					var NaviIndex=0
					//取消先前激活的菜单组
					$("#"+setting.mainmenuid+">ul")
						.children("li")
						.each(function(){
								if($(this).attr("Index")!=i){
									$(this).attr("id",$(this).find("li:eq(0)").attr("Parent"))
								}
					})
					//激活当前焦点栏目菜单组
					if(typeof $(this).attr("Index")!="undefined"){
						NaviIndex=$(this).attr("Index")
						$(this).attr("id","EIMS_C_22_1_Menu"+NaviIndex)
					}else{
						NaviIndex=$("."+$(this).attr("Parent")+":eq(0)").attr("Index")
						$("."+$(this)
						.attr("Parent")+":eq(0)")
						.attr("id","EIMS_C_22_1_Menu"+NaviIndex)
					}
					
					document.cookie = "EIMS_C_22_1_LastNaviIndex="+NaviIndex
			})
	})
	
	$mainmenu.find("ul").css({display:'none', visibility:'visible'})
},


init:function(setting){
	var LastNaviIndex=null;
	var cookies=document.cookie.split(';')
	for(var a_i=0;a_i<cookies.length;a_i++){
		if(cookies[a_i].indexOf("EIMS_C_22_1_LastNaviIndex=")>=0){
			LastNaviIndex=cookies[a_i].split('=')[1]
			break;
		}
	}
	
	var $mainmenu=$("#"+setting.mainmenuid+">ul")
	$mainmenu.children("li").each(
				function(i){
					$(this)
					.find("li")
					.attr("Parent",$(this)
					.attr("id"))
					
					if(LastNaviIndex==i){//焦点
						$(this).attr("id","EIMS_C_22_1_Menu"+i)
					}
					$(this).attr("Index",i)
	})//With Click Event
	
	if (typeof setting.customtheme=="object" && setting.customtheme.length==2){ //override default menu colors (default/hover) with custom set?
		var mainmenuid='#'+setting.mainmenuid
		var mainselector=(setting.orientation=="v")? mainmenuid : mainmenuid+', '+mainmenuid
		document.write('<style type="text/css">\n'
			+mainselector+' ul li a {background:'+setting.customtheme[0]+';}\n'
			+mainmenuid+' ul li a:hover {background:'+setting.customtheme[1]+';}\n'
		+'</style>')
	}
	this.shadow.enable=(document.all && !window.XMLHttpRequest)? false : this.shadow.enable //in IE6, always disable shadow
	jQuery(document).ready(function($){ //ajax menu?
		if (typeof setting.contentsource=="object"){ //if external ajax menu
			EIMS_C_22_1_ddsmoothmenu.getajaxmenu($, setting)
		}
		else{ //else if markup menu
			EIMS_C_22_1_ddsmoothmenu.buildmenu($, setting)
		}
	})
}

} //end EIMS_C_22_1_ddsmoothmenu variable

//Initialize Menu instance(s):

var EIMS_C_38_1Menu = ["EIMS_C_38_1_menu"];
function EIMS_C_38_1_InitailMenu() {
    var li = document.getElementById(EIMS_C_38_1Menu).getElementsByTagName("li");
    for (var i = 0; i < li.length; i++) {
        var li_ul = li[i].getElementsByTagName("ul");
        if (li_ul.length) {
            li[i].onmouseout = function() {
                this.getElementsByTagName("a")[0].style.backgroundColor = "";
                this.getElementsByTagName("ul")[0].style.display = "none";
            }
            if (li[i].parentNode.id != EIMS_C_38_1Menu) {
                li[i].onmouseover = function() {
                    this.getElementsByTagName("a")[0].style.backgroundColor = "#F00";
                    this.getElementsByTagName("ul")[0].style.display = "block";
                }
            }
        }
    }
}
function EIMS_C_38_1_ShowMenu(obj) {
    var li = document.getElementById(EIMS_C_38_1Menu).getElementsByTagName("li");
    for (var c = 0; c < li.length; c++) {
        if (li[c].parentNode.id == EIMS_C_38_1Menu) {
            li[c].getElementsByTagName("a")[0].className = "";
        }
    }
    obj.getElementsByTagName("a")[0].className = "ok";
    obj.getElementsByTagName("a")[0].style.backgroundColor = "#F00";
    obj.getElementsByTagName("ul")[0].style.display = "block";
}
function EIMS_C_38_1_SetMouseOver(ulId) {
    var obj = document.getElementById(ulId);
    obj.style.display = "block";
}
function EIMS_C_38_1_SetMouseOut(ulId) {
    var obj = document.getElementById(ulId);
    obj.style.display = "none";
}
if (! +[1, ]) {
    window.attachEvent('onload', EIMS_C_38_1_InitailMenu);
}
else {
    window.addEventListener('load', EIMS_C_38_1_InitailMenu, false);
}
