/* MainMenu Definite Start */
function ZKNicMainMenu() {
	if (window._ZKNicMainMenus == null) {
		window._ZKNicMainMenus = new Array();
		window._ZKNicMenuIDSeed = 0;
	}
	this.index = window._ZKNicMainMenus.length;
	window._ZKNicMainMenus[this.index] = this;
	this.width = 826;
	this.subMenus = new Array();
	this.activeSubMenu = null;
	this.sideImage = 'images/menu_button.gif';
	this.backgroundImage = 'images/menu_back.gif';
	this.seprateImage = 'images/menu_sep.gif';
	this.menuItemLetterWidth = 14;
	this.menuItemHeight = 22
}

ZKNicMainMenu.prototype.addSubMenu = function(title, link) {
	var subMenu =  new ZKNicSubMenu(title, link);
	subMenu.parent = this;
	subMenu.id = window._ZKNicMenuIDSeed++;
	subMenu.index = this.subMenus.length;
	this.subMenus[subMenu.index] = subMenu;
	return subMenu;
}

ZKNicMainMenu.prototype.output = function() {
	var index, subMenu;
	var totalWidth = this.width;
	var itemWidth = Math.floor((totalWidth - 1 - this.subMenus.length) / (2 + this.subMenus.length));
	var sideWidth = Math.floor((totalWidth - 1 - this.subMenus.length - itemWidth * this.subMenus.length) / 2);
	document.writeln('<table border="0" width="' + totalWidth + '" cellspacing="0" cellpadding="0" background="' + this.backgroundImage + '">');
	document.writeln('<tr>');
	document.writeln('<td width="' + sideWidth + '" align="center"><img src="' + this.sideImage + '" width="43" height="14"/></td>');
	document.writeln('<td width="1"><img src="' + this.seprateImage + '" width="1" height="24"/></td>');
	totalWidth -= sideWidth;
	for (index in this.subMenus) {
		subMenu = this.subMenus[index];
		document.writeln('<td width="' + itemWidth + '" align="center">');
		subMenu.width = itemWidth - 8;
		subMenu.output();
		document.writeln('</td>');
		document.writeln('<td width="1"><img src="' + this.seprateImage + '" width="1" height="24"/></td>');
		totalWidth -= itemWidth;
	}
	document.writeln('<td width="' + totalWidth + '" align="center"><img src="' + this.sideImage + '" width="43" height="14"/></td>');
	document.writeln('</tr>');
	document.writeln('</table>');
}
/* MainMenu Definite End */

/* SubMenu Definite Start */
function ZKNicSubMenu(title, link) {
	this.title = title;
	this.link = link;
	this.closeWhenMouseUp = false;
}

ZKNicSubMenu.prototype.addMenuItem = function(title, link) {
	var menuItem =  new ZKNicMenuItem(title, link);
	menuItem.parent = this;
	menuItem.id = window._ZKNicMenuIDSeed++;
	if (this.menuItems == null) this.menuItems = new Array();
	menuItem.index = this.menuItems.length;
	this.menuItems[menuItem.index] = menuItem;
	return menuItem;
}

ZKNicSubMenu.prototype.onMouseOver = function() {
	if (this.parent.activeSubMenu == null) {
		this.setState('hover');
	} else {
		this.parent.activeSubMenu.setState('normal');
		this.setState('active');
		this.parent.activeSubMenu = this;
	}
}

ZKNicSubMenu.prototype.onMouseOut = function() {
	if (this.parent.activeSubMenu == null) {
		this.setState('normal');
	}
}

ZKNicSubMenu.prototype.onMouseDown = function() {
	if (this.parent.activeSubMenu == null) {
		this.setState('active');
		this.parent.activeSubMenu = this;
	} else {
		this.closeWhenMouseUp = true;
	}
}

ZKNicSubMenu.prototype.onMouseUp = function() {
	if (this.menuItems == null) {
		this.setState('normal');
		this.parent.activeSubMenu = null;
		gotoLink(this.link);
		return;
	}
	if (this.closeWhenMouseUp) {
		this.setState('hover');
		this.parent.activeSubMenu = null;
	}
}

ZKNicSubMenu.prototype.setState = function(strState) {
	var htmlObj = this.getHtmlObj();
	switch (strState) {
	case 'normal':
		htmlObj.className = 'normalSubMenu';
		this.hideDropDown();
		break;
	case 'hover':
		htmlObj.className = 'hoverSubMenu';
		this.hideDropDown();
		break;
	case 'active':
		if (this.menuItems == null) {
			htmlObj.className = 'activeSubMenuItem';
		} else {
			htmlObj.className = 'activeSubMenu';
			this.showDropDown();
		}
		break;
	}
	this.closeWhenMouseUp = false;
}

ZKNicSubMenu.prototype.getHtmlObj = function() {
	if (this.htmlObj == null) this.htmlObj = document.getElementById('_ZKNIC_M' + this.id);
	return this.htmlObj;
}

ZKNicSubMenu.prototype.showDropDown = function() {
	var htmlObj = this.getHtmlObj();
	if (this.dropDownMenu == null) this.constructDropDownMenu();
	var left = getOffsetLeftToBody(htmlObj);
	var top = getOffsetTopToBody(htmlObj) + htmlObj.offsetHeight - 1;
	this.dropDownMenu.style.left = left + 'px';
	this.dropDownMenu.style.top = top + 'px';
	this.dropDownMenu.style.display = 'block';
	hideComboBox(this.dropDownMenu);
}

ZKNicSubMenu.prototype.hideDropDown = function() {
	if (this.dropDownMenu != null) {
		this.dropDownMenu.style.display = 'none';
		showComboBox(this.dropDownMenu);
	}
}

ZKNicSubMenu.prototype.constructDropDownMenu = function() {
	var maxItemLength = 0;
	for (index in this.menuItems) {
		var l = this.menuItems[index].title.length;
		if (l > maxItemLength) maxItemLength = l;
	}
	var width = 24 + (maxItemLength + 1) * this.parent.menuItemLetterWidth + 20;
	if (width < this.width + 30) width = this.width + 30;
	var height = 3 + this.menuItems.length * this.parent.menuItemHeight;
	var htmlObj = document.createElement('div');
	htmlObj.className = 'outBoxMenuItems';
	htmlObj.style.width = width + 'px';
	htmlObj.innerHTML = '<div style="height:1px"><span style="background-color:#666666"><img src="images/spacer.gif" width="1" height="1"/></span></div>' +
						'<div style="height:1px;background-color:#666666"><img src="images/spacer.gif" width="1" height="1"/><span style="background-color:#dbd8d1"><img src="images/spacer.gif" width="' + (this.getHtmlObj().offsetWidth - 2) + '" height="1"/></span></div>';
	var panelSubMenu = document.createElement('div');
	panelSubMenu.className = 'inBoxMenuItems';
	var menuItemTemplate = document.createElement('div');
	menuItemTemplate.className = 'normalMenuItem';
	menuItemTemplate.innerHTML = '<div class="normalMenuItemTitle"><div style="margin: 5px 0px 0px ' + Math.floor(this.parent.menuItemLetterWidth / 2) + 'px"></div></div>';
	for (index in this.menuItems) {
		var menuItem = this.menuItems[index];
		var itemHtmlObj = menuItemTemplate.cloneNode(true);
		var textNode = document.createTextNode(menuItem.title);
		itemHtmlObj.id = '_ZKNIC_M' + menuItem.id;
		itemHtmlObj.firstChild.firstChild.appendChild(textNode);
		if (menuItem.link == null) {
			itemHtmlObj.firstChild.firstChild.style.color='#808080';
		}
		itemHtmlObj.firstChild.style.height = this.parent.menuItemHeight + 'px';
		itemHtmlObj._ZKNICData = menuItem;
		itemHtmlObj.onmouseover = ZKNicMenuItemEvent;
		itemHtmlObj.onmouseout = ZKNicMenuItemEvent;
		itemHtmlObj.onmousedown = ZKNicMenuItemEvent;
		itemHtmlObj.onclick = ZKNicMenuItemEvent;
		itemHtmlObj.onselectstart = ZKNicMenuItemEvent;
		panelSubMenu.appendChild(itemHtmlObj);
	}
	htmlObj.appendChild(panelSubMenu);
	this.dropDownMenu = document.body.appendChild(htmlObj);
}

ZKNicSubMenu.prototype.output = function() {
	document.writeln('<div class="normalSubMenu" id="_ZKNIC_M' + this.id + '" style="height:' + (this.parent.menuItemHeight + 0) + 'px;width:' + this.width + 'px" onmouseover="ZKNicSubMenuEvent(event, ' + this.parent.index + ', ' + this.index + ', \'mouseover\');" onmouseout="ZKNicSubMenuEvent(event, ' + this.parent.index + ', ' + this.index + ', \'mouseout\');" onmousedown="ZKNicSubMenuEvent(event, ' + this.parent.index + ', ' + this.index + ', \'mousedown\');" onmouseup="ZKNicSubMenuEvent(event, ' + this.parent.index + ', ' + this.index + ', \'mouseup\');" onselectstart="ZKNicSubMenuEvent(event, ' + this.parent.index + ', ' + this.index + ', \'selectstart\');"><div style="padding-top:5px">');
	document.writeln(this.title);
	document.writeln('</div></div');
}
/* SubMenu Definite End */

/* MenuItem Definite Start */
function ZKNicMenuItem(title, link) {
	this.title = title;
	this.link = link;
}

ZKNicMenuItem.prototype.setState = function(strState) {
	var htmlObj = this.getHtmlObj();
	switch (strState) {
	case 'normal':
		htmlObj.className = 'normalMenuItem';
		htmlObj.firstChild.className = 'normalMenuItemTitle';
		htmlObj.firstChild.style.height = this.parent.parent.menuItemHeight + 'px';
		break;
	case 'hover':
		htmlObj.className = 'hoverMenuItem';
		htmlObj.firstChild.className = 'hoverMenuItemTitle';
		htmlObj.firstChild.style.height = (this.parent.parent.menuItemHeight - 1) + 'px';
		break;
	}
}

ZKNicMenuItem.prototype.getHtmlObj = function() {
	if (this.htmlObj == null) this.htmlObj = document.getElementById('_ZKNIC_M' + this.id);
	return this.htmlObj;
}

ZKNicMenuItem.prototype.onMouseOver = function() {
	this.setState('hover');
}

ZKNicMenuItem.prototype.onMouseOut = function() {
	this.setState('normal');
}

ZKNicMenuItem.prototype.onClick = function() {
	this.setState('normal');
	this.parent.setState('normal');
	this.parent.parent.activeSubMenu = null;
	gotoLink(this.link);	
}
/* MenuItem Definite End */

/* Global Event Handle Definite Start */
function ZKNicSubMenuEvent(e, indexMainMenu, indexSubMenu, strEvent) {
	e = e || window.event;
	var menu = window._ZKNicMainMenus[indexMainMenu].subMenus[indexSubMenu];
	switch (e.type) {
	case 'mouseover':
		menu.onMouseOver();
		break;
	case 'mouseout':
		menu.onMouseOut();
		break;
	case 'mousedown':
		menu.onMouseDown();
		break;
	case 'mouseup':
		menu.onMouseUp();
		break;
	case 'selectstart':
		e.returnValue = false;
		break;
	}
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function ZKNicMenuItemEvent(e) {
	e = e || window.event;
	var menu = this._ZKNICData;
	if (menu == null) return;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	switch (e.type) {
	case 'mouseover':
		menu.onMouseOver();
		break;
	case 'mouseout':
		menu.onMouseOut();
		break;
	case 'click':
		menu.onClick();
		break;
	case 'selectstart':
		e.returnValue = false;
		break;
	}
}

function ZKNicClearMenu() {
	var index;
	if (window._ZKNicMainMenus != null) {
		for (index in window._ZKNicMainMenus) {
			if (window._ZKNicMainMenus[index].activeSubMenu != null) {
				window._ZKNicMainMenus[index].activeSubMenu.setState('normal');
				window._ZKNicMainMenus[index].activeSubMenu = null;
			}
		}
	}
}

var old_onmousedown = document.onmousedown;
document.onmousedown = function() {
	if (old_onmousedown) old_onmousedown();
	ZKNicClearMenu();
}

/* Global Event Handle Definite End */

/* Global Helper Function Start */
function getOffsetLeftToBody(obj) {
	if (obj.offsetParent == null) return obj.offsetLeft;
	return obj.offsetLeft + getOffsetLeftToBody(obj.offsetParent);
}

function getOffsetTopToBody(obj) {
	if (obj.offsetParent == null) return obj.offsetTop;
	return obj.offsetTop + getOffsetTopToBody(obj.offsetParent);
}

function gotoLink(link) {
	if (link == null) return;
	window.location.href = link;
}

function hideComboBox(menu){
	if (menu) {
		if (menu.intersectCombos == null) {
			menu.intersectCombos = new Array();
			caculatePosition(menu);
			var Combos = document.getElementsByTagName('SELECT');
			for (i = 0; i < Combos.length; i++) {
				if (Combos[i].style.visibility != 'hidden') {
					caculatePosition(Combos[i]);
					if (isIntersect(menu, Combos[i])) menu.intersectCombos[menu.intersectCombos.length] = Combos[i];
				}
			}
		}
		for (i = 0; i < menu.intersectCombos.length; i++) {
			menu.intersectCombos[i].style.visibility = 'hidden';
		}
	}
}

function showComboBox(menu){
	if (menu) {
		if (menu.intersectCombos) {
			for (i = 0; i < menu.intersectCombos.length; i++) {
				menu.intersectCombos[i].style.visibility = 'visible';
			}
		}
	}
}

function caculatePosition(obj) {
	if (obj) {
		if (obj.offsetTopToBody == null) obj.offsetTopToBody= getOffsetTopToBody(obj)
		if (obj.offsetLeftToBody == null) obj.offsetLeftToBody = getOffsetLeftToBody(obj)
	}
}

function isIntersect(objA, objB) {
	var AX1 = objA.offsetLeftToBody;
	var AX2 = AX1 + objA.offsetWidth;
	var AY1 = objA.offsetTopToBody;
	var AY2 = AY1 + objA.offsetHeight;
	var BX1 = objB.offsetLeftToBody;
	var BX2 = BX1 + objB.offsetWidth;
	var BY1 = objB.offsetTopToBody;
	var BY2 = BY1 + objB.offsetHeight;
	//A at left side of B
	if (AX2 <= BX1) return false;
	//A at right side of B
	if (AX1 >= BX2) return false;
	//A at top side of B
	if (AY2 <= BY1) return false;
	//A at bottom side of B
	if (AY1 >= BY2) return false;
	//A intersect with B
	return true;
}

/* Global Helper Function End */

