/**
 * This a drop down menu
 * 二级下拉菜单
 * @author Mizi
 * @version ver1.1.20090510 | 2009-05-09
 * ***********/
jQuery.fn.dropdownmenu = function( opts ){
	opts = jQuery.extend ({
		fclass: "", //父菜单样式
		cclass: "", //子菜单样式
		fcurrentclass: "currentMenu", //父菜单当选选中样式
		cid: "_", //子菜单规则
		delaytime: 150, //滑动菜单延迟时间
		hidetime: 300, //菜单展开延迟时间
		showtime: 300, //菜单关闭延迟时间
		adjust: 22, //子菜单调整高度
		psettime: "", //settimeout id
		pid: "", //上一次样式
		currentid: "", //当前菜单
		width:""
	}, opts || {} );
	
	return this.each( function(){
		
		function mouseover(f,c){
			var offset = f.offset();
			c.css( {
				width: (opts.width?opts.width:f.width())+ "px",
				left: offset.left + "px",
				top: offset.top + opts.adjust + "px"
			} );
			c.show( opts.showtime );
		}
		
		function mouseout(f,c){
			opts.psettime = setTimeout( function(){
				c.hide( opts.hidetime );
				f.removeClass( opts.fcurrentclass );
				currentMenu();
				}, opts.delaytime ); 
		}
		
		function currentMenu(){
			if( opts.currentid !="" ){
				getFdom( "#"+ opts.currentid ).addClass( opts.fcurrentclass );
			}
		}
		
		function getFdom( dom ) {
			return jQuery( dom );
		}
		
		currentMenu();
		
		//菜单滑入事件
		this.onmouseover = function() {
			
			var f = getFdom("#"+this.id);
			var c = getFdom("#"+opts.cid+this.id);
			var d = getFdom("#" + opts.cid + this.id + " li");
			
			//滑动到此menu下的菜单,不消失
			if( opts.pid == this.id ) { 
				clearTimeout( opts.psettime ); 
			}else{
				if( opts.pid != "" ) {
					mouseout( getFdom( "#"+ opts.pid ), getFdom( "#"+ opts.cid+ opts.pid ) );
				}
			}
			
			//存储id
			opts.pid = this.id;
			
			//移动到父菜单,调出子菜单
			mouseover(f,c);
			
			//滑到子菜单
			c.mouseover( function() {   clearTimeout( opts.psettime ); } );
			
			//子菜单事件
			d.each( function(){
				
				this.onmouseover = function(){
					clearTimeout( opts.psettime );
					f.addClass( opts.fcurrentclass );
					c.show();
				}
				
				this.onmouseout = function() {
					mouseout( f,c );
				}
				
			} );
			
		}
		
		//菜单滑出事件
		this.onmouseout = function() {
			
			var f = getFdom("#"+this.id);
			var c = getFdom("#"+opts.cid+this.id);
			
			mouseout( f,c );
		}
		
	})
}
