﻿opCard = function()
{
	this.bind = new Array();
	this.index = 0;		//榛樿鏄剧ず鍝釜阃夐」鍗★紝浠?寮€濮?
	
	this.style = new Array();		//["","",""]
	this.overStyle = false;		//阃夐」鏄惁链塷ver, out鍙樻崲镙峰纺浜嬩欢锛屾牱寮忎负this.style[2]
	this.overChange = false;		//鍐呭鏄惁鐢╫ver, out鐩存帴婵€娲?
	this.menu = false;				//凿滃崟绫诲瀷
	this.nesting = [false,false,"",""];		//鏄惁宓屽锛屽悗闱?涓弬鏁版槸鎸囧畾menu,info镄勫瓙板嗘繁搴︽墍鐢╥d
	
	this.auto = [false, 1000];		//镊姩婊氩姩[true,2000]
	this.timerID = null;			//镊姩鎾斁镄?
	this.menutimerID = null;		//凿滃崟寤舵椂镄?
	
	this.creat = function(func)
	{
		var _arrMenu = document.getElementById(this.bind[0]).getElementsByTagName(this.bind[1]);
		var _arrInfo = document.getElementById(this.bind[2]).getElementsByTagName(this.bind[3]);
		var my = this, i;
		var argLen = arguments.length;
		var arrM = new Array();
		
		if(this.nesting[0] || this.nesting[1])	// 链夐€夐」鍗″祵濂?
		{	// 杩囨护鍑洪渶瑕佺殑鏁版嵁
			var arrMenu = this.nesting[0]?getChilds(_arrMenu,this.bind[0],2):_arrMenu;
			var arrInfo = this.nesting[1]?getChilds(_arrInfo,this.bind[2],3):_arrInfo;
		}
		else
		{
			var arrMenu = _arrMenu;
			var arrInfo = _arrInfo;
		}
		
		var l = arrMenu.length;
		if(l!=arrInfo.length){alert("凿滃崟鍜屽唴瀹瑰繀椤绘嫢链夌浉鍚岀殑鏁伴噺\n濡傛灉闇€瑕侊紝浣犲彲浠ユ斁涓€涓┖镄勫湪闾ｅ崰浣嶃€?)}
		
		// 淇
		if(this.menu){this.auto=false;this.overChange=true;} //濡傛灉鏄彍鍗曪紝鍒欐病链夎嚜锷ㄨ繍琛岋紝链塷ver, out鐩存帴婵€娲?
		
		// 寰幆娣诲姞鍚勪釜浜嬩欢绛?
		for(i=0;i<l;i++)
		{
			arrMenu[i].cName = arrMenu[i].className;
			arrMenu[i].className = (i!=this.index || this.menu)?getClass(arrMenu[i],this.style[0]):getClass(arrMenu[i],this.style[1]);		//锷犺浇镙峰纺锛岃彍鍗旷殑璇濈粺涓€镙峰纺
			
			if(arrMenu[i].getAttribute("skip")) // 闇€瑕佽烦杩囩殑瀹瑰櫒
			{
				if(this.overStyle || this.overChange)	// 链塷ver, out 鏀瑰彉镙峰纺 鎴栬€?婵€娲?
				{
					arrMenu[i].onmouseover = function(){changeTitle(this, 2);autoStop(this, 0);}
					arrMenu[i].onmouseout = function(){changeTitle(this, 0);autoStop(this, 1);}
				}
				arrMenu[i].onclick = function(){if(argLen==1){func()}}
				arrInfo[i].style.display = "none";
				continue;
			}
			
			if(i!=this.index || this.menu){arrInfo[i].style.display="none"};	//闅愯棌鍒濆鍖栵紝凿滃崟镄勮瘽鍏ㄩ儴闅愯棌
			arrMenu[i].index = i;	//璁板綍镊繁婵€娲诲€糩搴忓佛]
			arrInfo[i].index = i;
			
			
			if(this.overChange)	//链夐紶镙噊ver, out浜嬩欢
			{
				arrMenu[i].onmouseover = function(){changeOption(this);my.menu?changeMenu(1):autoStop(this, 0);}
				arrMenu[i].onmouseout = function(){changeOption(this);my.menu?changeMenu(0):autoStop(this, 1);}
			}
			else	//onclick瑙﹀彂
			{
				arrMenu[i].onclick = function(){changeOption(this);autoStop(this, 0);if(argLen==1){func()}}
				if(this.overStyle)	// 链塷ver, out 鏀瑰彉镙峰纺
				{
					arrMenu[i].onmouseover = function(){changeTitle(this, 2);autoStop(this, 0);}
					arrMenu[i].onmouseout = function(){changeTitle(this, 0);autoStop(this, 1);}
				}
				else	// 娌℃湁over, out 鏀瑰彉镙峰纺
				{
					if(this.auto[0])	// 链夎嚜锷ㄨ繍琛?
					{
						arrMenu[i].onmouseover = function(){autoStop(this, 0);}
						arrMenu[i].onmouseout = function(){autoStop(this, 1);}
					}
				}
			}
			
			if(this.auto[0] || this.menu)	//arrinfo 鎺у埗镊姩鎾斁
			{
				arrInfo[i].onmouseover = function(){my.menu?changeMenu(1):autoStop(this, 0);}
				arrInfo[i].onmouseout = function(){my.menu?changeMenu(0):autoStop(this, 1);}
			}
		}	//for缁撴潫
		
		if(this.auto[0])
		{
			this.timerID = setTimeout(autoMove,this.auto[1])
		}
		
		// 镊姩鎾斁
		function autoMove()
		{
			var n;
			n = my.index + 1;
			if(n==l){n=0};
			while(arrMenu[n].getAttribute("skip"))		// 闇€瑕佽烦杩囩殑瀹瑰櫒
			{
				n += 1;
				if(n==l){n=0};
			}
			changeOption(arrMenu[n]);
			my.timerID = setTimeout(autoMove,my.auto[1]);
		}
		
		// onmouseover镞讹紝镊姩鎾斁锅沧銆俷um锛?涓簅ver锛?涓簅ut銆?obj鏆傛椂镞犵敤銆?-_-!!
		function autoStop(obj, num)
		{
			if(!my.auto[0]){return;}
			//if(obj.index==my.index)
			num == 0 ? clearTimeout(my.timerID) : my.timerID = setTimeout(autoMove,my.auto[1]);
		}
		
		// 鏀瑰彉阃夐」鍗?
		function changeOption(obj)
		{
			arrMenu[my.index].className = getClass(arrMenu[my.index],my.style[0]);	//淇敼镞у唴瀹?
			arrInfo[my.index].style.display = "none";	//闅愯棌镞у唴瀹?
			
			obj.className = getClass(obj,my.style[1]);		//淇敼涓烘柊镙峰纺
			arrInfo[obj.index].style.display = "";	//鏄剧ず鏂板唴瀹?
			
			my.index = obj.index;	//镟存柊褰揿墠阃夋嫨镄刬ndex
		}
		
		/*		
			鍙湁onclick镞讹紝overStyle镄刼nmouseover,onmouseout浜嬩欢銆傜敤鏉ラ婵€娲?
			obj锛氱洰镙囧璞°€?num锛?涓簅ver锛?涓簅ut
		*/
		function changeTitle(obj, num)
		{
			if(!my.overStyle){return;};
			if(obj.index!=my.index){obj.className = getClass(obj,my.style[num])}
		}
		
		/*		
			凿滃崟绫诲瀷镞剁敤
			obj锛氱洰镙囧璞°€?num锛?涓簅ver锛?涓簅ut
		*/
		function changeMenu(num)
		{
			if(!my.menu){return;}
			num==0?my.menutimerID = setTimeout(menuClose,1000):clearTimeout(my.menutimerID)
		}
		
		//鍏抽棴凿滃崟
		function menuClose()
		{
			arrInfo[my.index].style.display = "none";
			arrMenu[my.index].className = getClass(arrMenu[my.index],my.style[0]);
		}
		
		// 寰楀埌className锛堥槻姝㈠皢铡熸湁镙峰纺瑕嗙洊锛?
		function getClass(o, s)
		{
			if(o.cName==""){return s}
			else{return o.cName + " " + s}
		}
		
		//宓屽鎯呭喌涓嫔缑鍒扮湡姝ｇ殑瀛愰泦
		function getChilds(arrObj, id, num)
		{
			var depth = 0;
			var firstObj = my.nesting[num]==""?arrObj[0]:document.getElementById(my.nesting[num]);		//寰楀埌绗竴涓瓙板?
			do	//璁＄畻娣卞害
			{
				if(firstObj.parentNode.getAttribute("id")==id){break}else{depth+=1}
				firstObj = firstObj.parentNode;
			}
			while(firstObj.tagName.toLowerCase()!="body")	// body寮哄埗阃€鍑恒€?
			
			var t;
			var arr = new Array();
			for(i=0;i<arrObj.length;i++)	//杩囨护鍑洪渶瑕佺殑鏁版嵁
			{
				t = arrObj[i], d = 0;
				do
				{
					if(t.parentNode.getAttribute("id")==id && d == depth)
					{	
						arr.push(arrObj[i]);break;		//寰楀埌鏁版嵁
					}
					else
					{
						if(d==depth){break};d+=1;
					}
					t = t.parentNode;
				}
				while(t.tagName.toLowerCase()!="body")	// body寮哄埗阃€鍑?
			}
			return arr;
		}
	}
}