dropDownMenu.menuPadding = 4;
dropDownMenu.menuPaddingImg = "images/transparent.gif";
dropDownMenu.itemPadding = 4;
dropDownMenu.itemPaddingHTML = "|";
dropDownMenu.shadowSize = 1;
dropDownMenu.shadowOffset = 0;
dropDownMenu.shadowColor = "";
dropDownMenu.backgroundColor = "";
dropDownMenu.backgroundImg = "";
dropDownMenu.hideDelay = 200;
dropDownMenu.slideTime = 200;
dropDownMenu.menuOffsetTop = 4;
dropDownMenu.rightMargin = 112;
dropDownMenu.reference = {topLeft:1,topRight:2,bottomLeft:3,bottomRight:4};
dropDownMenu.direction = {down:1,right:2};
dropDownMenu.menuDirection = 1;
dropDownMenu.registry = [];
dropDownMenu._maxZ = 100;
dropDownMenu.completed = false;
dropDownMenu.isSupported = function() {
	if (typeof dropDownMenu.isSupported.r == "boolean") 
		return dropDownMenu.isSupported.r;
	var ua = navigator.userAgent.toLowerCase();
	var an = navigator.appName;
	var r = false;
	if (ua.indexOf("gecko") > -1) r = true; 
	else if (an == "Microsoft Internet Explorer") {
		if (document.getElementById) r = true; 
	}
	dropDownMenu.isSupported.r = r;
	return r;
}

dropDownMenu.initialize = function() {
	if(dropDownMenu.completed)
		return;
	for (var i = 0, menu = null; menu = this.registry[i]; i++) {
		menu.initialize();
	}
	if( window.screen.width<900 ){
		dropDownMenu.rightMargin = 0;
	}
}

dropDownMenu.renderAll = function(obj) {
    if(obj==null){
        alert("ShowMenu Parameter Error");
        return;
    }
	wirteCss();
	var aMenuHtml = [];
	for (var i = 0, menu = null; menu = this.registry[i]; i++) {
		aMenuHtml[i] = menu.toString();
	}
	//document.writeln(aMenuHtml.join(""));
	eval(obj+".innerHTML+=aMenuHtml.join(\"\");")
	//divMenu.innerHTML+=aMenuHtml.join("");
}

function dropDownMenu(oActuator, iDirection, iLeft, iTop, iReferencePoint, parentMenuSet) {
	this.addItem = addItem;
	this.addMenu = addMenu;
	this.toString = toString;
	this.initialize = initialize;
	this.isOpen = false;
	this.show = show;
	this.hide = hide;
	this.items = [];
	this.onactivate = new Function(); 
	this.ondeactivate = new Function(); 
	this.onmouseover = new Function(); 
	this.onqueue = new Function(); 
	this.index = dropDownMenu.registry.length;
	//---------
	dropDownMenu.registry[this.index] = this;
	var id = "dropDownMenu" + this.index;
	var contentHeight = null;
	var contentWidth = null;
	var childMenuSet = null;
	var animating = false;
	
	var childMenus = [];
	var slideAccel = -1;
	var elmCache = null;
	var ready = false;
	var _this = this;
	var a = null;
	var pos = iDirection == dropDownMenu.direction.down ? "top" : "left";
	var dim = null;
	function initialize() {
		initCache();
		initEvents();
		initSize();
		ready = true;
	}
		
	function initCache() {
		var menu = document.getElementById(id);
		var all = menu.all ? menu.all : menu.getElementsByTagName("*"); 
		elmCache = {};
		elmCache["menu"] = menu;
		elmCache["item"] = [];
		
		for (var i = 0, elm = null; elm = all[i]; i++) {
			switch (elm.className) {
				case "items":
				case "navContent":
				case "background":
				case "shadowRight":
				case "shadowBottom":
					elmCache[elm.className] = elm;
					break;
				case "item":
					elm._index = elmCache["item"].length;
					elmCache["item"][elm._index] = elm;
					break;
			}
		}

		_this.elmCache = elmCache;
	}
	
	function initSize() {
		var ow = elmCache["items"].offsetWidth;
		var oh = elmCache["items"].offsetHeight;
		if(!dropDownMenu.completed && ow>0 && oh>0){
			dropDownMenu.completed = true;
		}		
		var ua = navigator.userAgent.toLowerCase();
		
		if ( ow<80 ) ow=80;
		
		contentWidth = ow + dropDownMenu.shadowSize;
		contentHeight = oh + dropDownMenu.shadowSize;
		
		elmCache["menu"].style.width = ow + dropDownMenu.shadowSize + 2 + "px";
		elmCache["menu"].style.height = oh + dropDownMenu.shadowSize + 2 + "px";

		elmCache["navContent"].style.width = ow + dropDownMenu.shadowSize + "px";
		elmCache["navContent"].style.height = oh + dropDownMenu.shadowSize + "px";

		dim = iDirection == dropDownMenu.direction.down ? contentHeight : contentWidth;
		elmCache["navContent"].style[pos] = -dim - dropDownMenu.shadowSize + "px";

		elmCache["menu"].style.visibility = "hidden";
		
		if (ua.indexOf("mac") == -1 || ua.indexOf("gecko") > -1) {
			elmCache["background"].style.width = ow + "px";
			elmCache["background"].style.height = oh + "px";
			elmCache["background"].style.backgroundColor = dropDownMenu.backgroundColor;

			elmCache["shadowRight"].style.left = ow + "px";
			elmCache["shadowRight"].style.height = oh - (dropDownMenu.shadowOffset - dropDownMenu.shadowSize) + "px";
			elmCache["shadowRight"].style.backgroundColor = dropDownMenu.shadowColor;

			elmCache["shadowBottom"].style.top = oh + "px";
			elmCache["shadowBottom"].style.width = ow - dropDownMenu.shadowOffset + "px";
			elmCache["shadowBottom"].style.backgroundColor = dropDownMenu.shadowColor;
		}else{
			elmCache["background"].firstChild.src = dropDownMenu.backgroundImg;
			elmCache["background"].firstChild.width = ow;
			elmCache["background"].firstChild.height = oh;
            
			elmCache["shadowRight"].firstChild.src = dropDownMenu.shadowPng;
			elmCache["shadowRight"].style.left = ow + "px";
			elmCache["shadowRight"].firstChild.width = dropDownMenu.shadowSize;
			elmCache["shadowRight"].firstChild.height = oh - (dropDownMenu.shadowOffset - dropDownMenu.shadowSize);

			elmCache["shadowBottom"].firstChild.src = dropDownMenu.shadowPng;
			elmCache["shadowBottom"].style.top = oh + "px";
			elmCache["shadowBottom"].firstChild.height = dropDownMenu.shadowSize;
			elmCache["shadowBottom"].firstChild.width = ow - dropDownMenu.shadowOffset;
		}
	}

	function initEvents() {
		for (var i = 0, item = null; item = elmCache.item[i]; i++) {
			item.onmouseover = item_mouseover;
			item.onmouseout = item_mouseout;
		}
		if (typeof oActuator.tagName != "undefined") {
			oActuator.onmouseover = actuator_mouseover;
			oActuator.onmouseout = actuator_mouseout;
		}
		elmCache["navContent"].onmouseover = content_mouseover;
		elmCache["navContent"].onmouseout = content_mouseout;
	}

	function addItem(sText, sUrl, sTarget, sTitle) {
		var item = new dropDownMenuItem(sText, sUrl, sTarget, sTitle, this);
		item._index = this.items.length;
		this.items[item._index] = item;
	}
	
	function addMenu(oMenuItem) {
		if (!oMenuItem.parentMenu == this) throw new Error("不能添加菜单");
		if (childMenuSet == null) {
			if ( dropDownMenu.menuDirection==0 ){
				childMenuSet = new dropDownMenuSet(dropDownMenu.direction.right, -5, 2, dropDownMenu.reference.bottomLeft);
			}else{
				childMenuSet = new dropDownMenuSet(dropDownMenu.direction.right, -5, 2, dropDownMenu.reference.topRight);
			}
		}
		var m = childMenuSet.addMenu(oMenuItem);
		childMenus[oMenuItem._index] = m;
		m.onmouseover = child_mouseover;
		m.ondeactivate = child_deactivate;
		m.onqueue = child_queue;
		return m;
	}
		
	function show() {
		if (ready) {
			_this.isOpen = true;
			animating = true;
			setContainerPos();
			elmCache["menu"].style.visibility = "visible";
			elmCache["menu"].style.zIndex = dropDownMenu._maxZ++;

			slideStart();
			_this.onactivate();
		}
	}

	function hide() {
		if (ready) {
			_this.isOpen = false;
			animating = true;
			for (var i = 0, item = null; item = elmCache.item[i]; i++) 
				dehighlight(item);
			if (childMenuSet) childMenuSet.hide();
			slideStart();
			_this.ondeactivate();
		}
	}

	function setContainerPos() {
		var sub = oActuator.constructor == dropDownMenuItem; 
		var act = sub ? oActuator.parentMenu.elmCache["item"][oActuator._index] : oActuator; 
		var el = act;
		var x = 0;
		var y = 0;
		var minX = 0;
		var maxX = (window.innerWidth ? window.innerWidth : document.body.clientWidth-dropDownMenu.rightMargin) - parseInt(elmCache["menu"].style.width);
		var minY = 0;
		var maxY = (window.innerHeight ? window.innerHeight : document.body.clientHeight) - parseInt(elmCache["menu"].style.height);

		while (sub ? el.parentNode.className.indexOf("dropDownMenuMenu") == -1 : el.offsetParent) {
			x += el.offsetLeft;
			y += el.offsetTop;
			if (el.scrollLeft) x -= el.scrollLeft;
			if (el.scrollTop) y -= el.scrollTop;
			//--
			var pagewidth=780;
			var pageright=window.screen.width-parseInt((window.screen.width-pagewidth)/2)-10;
			if(x+elmCache["menu"].offsetWidth>pageright){
			    x=pageright-elmCache["menu"].offsetWidth;
			}
			el = el.offsetParent;
		}
		if (oActuator.constructor == dropDownMenuItem) {
			x += parseInt(el.parentNode.style.left);
			y += parseInt(el.parentNode.style.top);
		}

		switch (iReferencePoint) {
			case dropDownMenu.reference.topLeft:
				break;
			case dropDownMenu.reference.topRight:
				x += act.offsetWidth;
				break;
			case dropDownMenu.reference.bottomLeft:
				y += act.offsetHeight;
				break;
			case dropDownMenu.reference.bottomRight:
				x += act.offsetWidth;
				y += act.offsetHeight;
				break;
		}
		x += iLeft;
		y += iTop;
		x = Math.max(Math.min(x, maxX), minX);
		y = Math.max(Math.min(y, maxY), minY);
		
		elmCache["menu"].style.left = x + "px";
		elmCache["menu"].style.top = y + dropDownMenu.menuOffsetTop + 2 + "px";
	}

	function slideStart() {
		var x0 = parseInt(elmCache["navContent"].style[pos]);
		var x1 = _this.isOpen ? 0 : -dim;
		if (a != null) a.stop();
		a = new Accelimation(x0, x1, dropDownMenu.slideTime, slideAccel);
		a.onframe = slideFrame;
		a.onend = slideEnd;
		a.start();
	}

	function slideFrame(x) {
		elmCache["navContent"].style[pos] = x + "px";
	}

	function slideEnd() {
		if (!_this.isOpen) elmCache["menu"].style.visibility = "hidden";
		animating = false;
	}

	function highlight(oRow) {
		//oRow.className = "itemHover";		
		oRow.className="NavSubMenu";
		if (childMenus[oRow._index]){
			//oRow.lastChild.firstChild.src = dropDownMenu.dingbatOn;
		}
	}

	function dehighlight(oRow) {
		//oRow.className = "item";			
		oRow.className="NavSubMenu";
		if (childMenus[oRow._index]) {
			//oRow.lastChild.firstChild.src = dropDownMenu.dingbatOff;
		}
	}

	function actuator_mouseover() {
		parentMenuSet.showMenu(_this);
	}

	function actuator_mouseout() {
		parentMenuSet.hideMenu(_this);
	}

	function content_mouseover() {
		if (!animating) {
			parentMenuSet.showMenu(_this);
			_this.onmouseover();
		}
	}

	function content_mouseout() {
		if (!animating) {
			parentMenuSet.hideMenu(_this);
		}
	}	
	
	function item_mouseover() {
		if (!animating) {
			highlight(this);
			if (childMenus[this._index]) 
				childMenuSet.showMenu(childMenus[this._index]);
			else if (childMenuSet) childMenuSet.hide();
		}
	}

	function item_mouseout() {
		if (!animating) {
			if (childMenus[this._index])
				childMenuSet.hideMenu(childMenus[this._index]);
			else 
				dehighlight(this);
		}
	}
	
	function item_click() {
		if (!animating) {
			if (_this.items[this._index].url) 
				location.href = _this.items[this._index].url;
		}
	}

	function child_mouseover() {
		if (!animating) {
			parentMenuSet.showMenu(_this);
		}
	}

	function child_deactivate() {
		for (var i = 0; i < childMenus.length; i++) {
			if (childMenus[i] == this) {
				dehighlight(elmCache["item"][i]);
				break;
			}
		}
	}

	function child_queue() {
		parentMenuSet.hideMenu(_this);
	}
		
	function toString() {
		var aHtml = [];
		var sClassName = "dropDownMenuMenu" + "";//(oActuator.constructor != dropDownMenuItem ? " top" : "");
		for (var i = 0, item = null; item = this.items[i]; i++) {
			aHtml[i] = item.toString(childMenus[i]);
		}
		var sRtn = '';
		sRtn += '<div id="' + id + '" class="' + sClassName + '">';
		sRtn += '<div class="navContent"><table class="items" width=80px cellpadding="0" cellspacing="0" border="0" background="'+ dropDownMenu.backgroundImg +'"> ';	//菜单的最小宽度为80px
		sRtn += '<tr><td colspan="2"><img src="' + dropDownMenu.menuPaddingImg + '" width="1" height="' + dropDownMenu.menuPadding + '"></td></tr>';		//用于控制菜单上边缘的间隔
		
		if ( dropDownMenu.menuDirection==0 ){
			sRtn += '<tr><td nowrap>&nbsp;';
			sRtn += aHtml.join("&nbsp;" + dropDownMenu.itemPaddingHTML + "&nbsp;");	
			sRtn += '&nbsp;</td></tr>';
		}else{
			sRtn += aHtml.join('');
		}
    	sRtn += '<tr><td colspan="2"><img src="' + dropDownMenu.menuPaddingImg + '" width="1" height="' + dropDownMenu.menuPadding + '"></td></tr>';		//用于控制菜单下边缘的间隔
		sRtn += '</table>';
		sRtn += '<div class="shadowBottom"><img src="' + dropDownMenu.menuPaddingImg + '" width="1" height="1"></div>';		//用于显示菜单底部阴影
		sRtn += '<div class="shadowRight"><img src="' + dropDownMenu.menuPaddingImg + '" width="1" height="1"></div>';			//用于显示菜单右部阴影
		sRtn += '<div class="background"><img src="' + dropDownMenu.menuPaddingImg + '" width="1" height="1"></div>';			//用于显示菜单背景
		sRtn += '</div>';
		sRtn += '</div>';
		return sRtn
	}
}

dropDownMenuSet.registry = [];
function dropDownMenuSet(iDirection, iLeft, iTop, iReferencePoint) {
	this.addMenu = addMenu;
	this.showMenu = showMenu;
	this.hideMenu = hideMenu;
	this.hide = hide;
	this.renderAll = renderAll;
	
	var menus = [];
	var _this = this;
	var current = null;
	this.index = dropDownMenuSet.registry.length;
	dropDownMenuSet.registry[this.index] = this;
	
	//------
	if ( iDirection==null ) iDirection = dropDownMenu.direction.down;
	if ( iLeft==null ) iLeft = 0;
	if ( iTop==null ) iTop = 0;
	if ( iReferencePoint==null ) iReferencePoint = dropDownMenu.reference.bottomLeft;
	//------

	function addMenu(oActuator) {
		var m = new dropDownMenu(oActuator, iDirection, iLeft, iTop, iReferencePoint, this);
		menus[menus.length] = m;
		return m;
	}

	function showMenu(oMenu) {
		if (oMenu != current) {
			if (current != null) hide(current); 
			current = oMenu;
			oMenu.show();
		}else{
			cancelHide(oMenu);
		}
	}

	function hideMenu(oMenu) {
		if (current == oMenu && oMenu.isOpen) {
			if (!oMenu.hideTimer) scheduleHide(oMenu);
		}
	}

	function scheduleHide(oMenu) {
		oMenu.onqueue();
		oMenu.hideTimer = window.setTimeout("dropDownMenuSet.registry[" + _this.index + "].hide(dropDownMenu.registry[" + oMenu.index + "])", dropDownMenu.hideDelay);
	}

	function cancelHide(oMenu) {
		if (oMenu.hideTimer) {
			window.clearTimeout(oMenu.hideTimer);
			oMenu.hideTimer = null;
		}
	}

	function hide(oMenu) { 
		if (!oMenu && current) oMenu = current;
		if (oMenu && current == oMenu && oMenu.isOpen) {
			cancelHide(oMenu);
			current = null;
			oMenu.hideTimer = null;
			oMenu.hide();
		}
	}
	
	function renderAll(obj){
		dropDownMenu.renderAll(obj);
	}
}

function dropDownMenuItem(sText, sUrl, sTarget, sTitle, oParent) {
	this.toString = toString;
	this.text = sText;
	this.url = sUrl;
	this.parentMenu = oParent;
	
	function toString(bDingbat) {
		var sDingbat = bDingbat ? dropDownMenu.dingbatOff : dropDownMenu.menuPaddingImg;
		var iEdgePadding = dropDownMenu.itemPadding + dropDownMenu.menuPadding;
		var sPaddingLeft = "padding:" + dropDownMenu.itemPadding + "px; padding-left:" + iEdgePadding + "px;"
		var sPaddingRight = "padding:" + dropDownMenu.itemPadding + "px; padding-right:" + iEdgePadding + "px;"
		var sRtn = '';
		var menuClass="NavSubMenu";
		//--
		if ( sText==null || sText=="" ) return sRtn;
		if (dropDownMenu.menuDirection==1){
			sRtn += '<tr class="item"><td style="background-color:red" nowrap style="' + sPaddingLeft + '">' ;
		}else{
			sRtn += '<span class="item">';// style="' + sPaddingLeft + '">';
		}
		if ( sUrl!=null && sUrl!="" ){
			if ( sTitle==null || sTitle=='' ) sTitle = sText;
			if ( sTarget==null || sTarget=='' ) sTarget = '_blank';
			if ( sText.indexOf("<a href=") < 0 )
				sText = '<a href="' + sUrl + '" class="' + menuClass + '" target="' + sTarget + '" title="' + sTitle + '">' + sText + '</a>';
		}else{
			if ( sText.indexOf("label disabled") < 0 )
				sText = '<label disabled=true>' + sText + '</label>';
		}
		sRtn += sText;
		if (dropDownMenu.menuDirection == 1){
			sRtn += '</td></tr>';
		}else{
			sRtn += '</span>';
		}
		return sRtn;
	}
}

function wirteCss(){
	var sCss = "\n<style>";
	sCss += "\n.dropDownMenuMenu {	LEFT: -1000px; OVERFLOW: hidden; POSITION: absolute; TOP: -1000px}";
	sCss += "\n.dropDownMenuMenu .navContent {	POSITION: absolute}";
	sCss += "\n.dropDownMenuMenu .items {	BORDER-RIGHT: #999 0px solid; BORDER-TOP: #999 0px solid; Z-INDEX: 2; LEFT: 0px; BORDER-LEFT: #999 0px solid; BORDER-BOTTOM: #999 0px solid; POSITION: relative; TOP: 0px}";
	sCss += "\n.dropDownMenuMenu .item {	BORDER-RIGHT: medium none; BORDER-TOP: medium none; FONT-SIZE: 9pt; BORDER-BOTTOM-WIDTH: medium; BORDER-LEFT: medium none; CURSOR: hand; COLOR: #000000; FONT-FAMILY:Verdana, Arial, Helvetica, sans-serif; TEXT-DECORATION: none; EXT-DECORATION: none}";
	sCss += "\n.dropDownMenuMenu .background {	Z-INDEX: 1; FILTER: alpha(opacity=60); LEFT: 0px; POSITION: absolute; TOP: 0px; moz-opacity: .8}";
	sCss += "\n.dropDownMenuMenu .shadowRight {Z-INDEX: 3; FILTER: alpha(opacity=30); WIDTH: 0px; POSITION: absolute; TOP: 0px; moz-opacity: .4}";
	sCss += "\n.dropDownMenuMenu .shadowBottom {Z-INDEX: 1; FILTER: alpha(opacity=30); LEFT: 0px; POSITION: absolute; HEIGHT: 0px; moz-opacity: .4}";
	sCss += "\n.dropDownMenuMenu .hover {BACKGROUND: #EFEFEF;}";
	sCss += "\n.dropDownMenuMenu .item IMG {MARGIN-LEFT: 10px}";
	sCss += "\n</style>";
	//document.writeln(sCss);
}

function Accelimation(from, to, time, zip) {
	if (typeof zip == "undefined") zip = 0;
	if (typeof unit == "undefined") unit = "px";
	this.x0 = from;
	this.x1 = to;
	this.dt = time;
	this.zip = -zip;
	this.unit = unit;
	this.timer = null;
	this.onend = new Function();
	this.onframe = new Function();
}

Accelimation.prototype.start = function() {
	this.t0 = new Date().getTime();
	this.t1 = this.t0 + this.dt;
	var dx = this.x1 - this.x0;
	this.c1 = this.x0 + ((1 + this.zip) * dx / 3);
	this.c2 = this.x0 + ((2 + this.zip) * dx / 3);
	Accelimation._add(this);
}

Accelimation.prototype.stop = function() {
	Accelimation._remove(this);
}

Accelimation.prototype._paint = function(time) {
	if (time < this.t1) {
		var elapsed = time - this.t0;
		this.onframe(Accelimation._getBezier(elapsed/this.dt,this.x0,this.x1,this.c1,this.c2));
	}
	else this._end();
}

Accelimation.prototype._end = function() {
	Accelimation._remove(this);
	this.onframe(this.x1);
	this.onend();
}

Accelimation._add = function(o) {
	var index = this.instances.length;
	this.instances[index] = o;

	if (this.instances.length == 1) {
		this.timerID = window.setInterval("Accelimation._paintAll()", this.targetRes);
	}
}

Accelimation._remove = function(o) {
	for (var i = 0; i < this.instances.length; i++) {
		if (o == this.instances[i]) {
			this.instances = this.instances.slice(0,i).concat( this.instances.slice(i+1) );
			break;
		}
	}

	if (this.instances.length == 0) {
		window.clearInterval(this.timerID);
		this.timerID = null;
	}
}

Accelimation._paintAll = function() {
	var now = new Date().getTime();
	for (var i = 0; i < this.instances.length; i++) {
		this.instances[i]._paint(now);
	}
}

Accelimation._B1 = function(t) { return t*t*t }
Accelimation._B2 = function(t) { return 3*t*t*(1-t) }
Accelimation._B3 = function(t) { return 3*t*(1-t)*(1-t) }
Accelimation._B4 = function(t) { return (1-t)*(1-t)*(1-t) }

Accelimation._getBezier = function(percent,startPos,endPos,control1,control2) {
	return endPos * this._B1(percent) + control2 * this._B2(percent) + control1 * this._B3(percent) + startPos * this._B4(percent);
}

Accelimation.instances = [];
Accelimation.targetRes = 10;
Accelimation.timerID = null;

//-----------
if (dropDownMenu.isSupported()) {
    setTimeout("dropDownMenu.initialize();",1000);
    setTimeout("dropDownMenu.initialize();",5000);
    setTimeout("dropDownMenu.initialize();",15000);
}else{
    alert("don't support");
}
