﻿/**
* MenuBTD
* 
* @author		banhthidiem <banhthidiem@gmail.com>
* @copyright	2008 Bach Khoa Computer Inc.
* @version		1.0 
* @date			2010/7/26
*/

var MenuType = {
	vertical: 1,
	horizontal: 2
};

var ItemMenuType = {
	normal: 1,
	line: 2,
	check: 3
};

var EventNameMenu =
{
	clickItem: "clickItem"
};

function MenuBTD(idContainer, listChildren, menuType, isShowArrowFirstMenu, style) {
	if (idContainer == null) return;
	this.listEvent = new Object();
	this.itemRoot = new Object();
	this.isBTD = true;
	this.timerClose = null;
	this.stopClose = false;
	this.style = style == null ? "" : style;
	this.init(idContainer, listChildren, menuType, isShowArrowFirstMenu);
}

MenuBTD.prototype = {
	menuType: MenuType.horizontal,
	isShowArrowFirstMenu: true,
	isNotHide: false,
	isActive: false,
	listMenu: new Object(), // là biết static
	myName: "",

	/****************************************************************
	************************Create Event*****************************
	****************************************************************/
	addEvent: function (eventName, func) {
		if (this.listEvent[eventName] == null) {
			this.listEvent[eventName] = [];
		}
		this.listEvent[eventName].push(func);
	},

	removeEvent: function (eventName, func) {
		if (this.listEvent[eventName] == null) {
			return;
		}
		for (var i = 0; i < this.listEvent[eventName].length; i++) {
			if (func == this.listEvent[eventName][i]) {
				this.listEvent[eventName].splice(i, 1);
				break;
			}
		}
	},

	executeEvent: function (e, eventName) {
		if (e == null) {
			e = utilObj.getWindowEvent();
		}
		if (this.listEvent[eventName] != null) {
			for (var i = 0; i < this.listEvent[eventName].length; i++) {
				this.listEvent[eventName][i](e);
			}
		}
	},

	/****************************************************************
	**************************Create Menu****************************
	****************************************************************/

	hide: function (item) {
		item.isShowChildren = false;
		item.elItem.className = "item" + (item.elItem.isCheck == true ? " check" : "");
		if (item.elItem.elContainerItem != null) {
			item.elItem.elContainerItem.style.display = "none";
		}
		if (item.itemShow != null) {
			item.itemShow.elItem.className = "item" + (item.itemShow.elItem.isCheck == true ? " check" : "");
		}
	},

	hideAllChildren: function (item) {
		if (this.stopClose) return;
		var itemHide = item.parent.itemShow;
		while (itemHide != null) {
			this.hide(itemHide);
			itemHide = itemHide.itemShow;
			if (itemHide != null && !itemHide.isShowChildren) return;
		}
	},

	hideAllChildrenMenu: function () {
		for (var key in this.listMenu) {
			if (this.listMenu[key] != this && this.listMenu[key].itemRoot.itemShow != null) {
				this.hideAllChildren(this.listMenu[key].itemRoot.itemShow);
			}
		}
	},

	showMenuChildren: function (elItem) {
		if (elItem.item.listChildren.length == 0) return;
		elItem.item.isShowChildren = true;
		var pos = utilObj.coordsElChildByParentIntelligent(elItem.elContainerItem, elItem);
		var size = utilObj.getElementSize(elItem);
		elItem.elContainerItem.style.display = "block";
		if (elItem.item.isFirst == true && this.menuType == MenuType.horizontal) {
			if (pos.posCase == "bottomRight" || pos.posCase == "bottomLeft") {
				pos.Y -= 1;
			}
			if (pos.posCase == "bottomRight" || pos.posCase == "topRight") {
				pos.X -= size.width;
			}
			else if (pos.posCase == "bottomLeft" || pos.posCase == "topLeft") {
				pos.X += size.width;
			}
		}
		else {
			if (pos.posCase != "topRight") {
				pos.Y -= size.height;
			}
			else {
				pos.Y += size.height;
			}
		}
		this.elItemOver = elItem;
		elItem.elContainerItem.style.left = pos.X + "px";
		elItem.elContainerItem.style.top = pos.Y + "px";
	},

	item_OnClick: function (e, elItem) {
		e.elItem = elItem;
		if (elItem.item.type == ItemMenuType.check) {
			elItem.isCheck = elItem.isCheck == null ? true : !elItem.isCheck;
			elItem.className = "item" + (elItem.className.indexOf("over") != -1 ? " over" : "") + (elItem.isCheck == true ? " check" : "");
		}
		if (elItem.item.listChildren.length == 0) {
			this.stopClose = false;
			this.hideAllChildren(this.itemRoot.itemShow);
		}
		this.executeEvent(e, EventNameMenu.clickItem);
	},

	item_OnMouseOver: function (e, elItem) {
		clearTimeout(this.timerClose);
		this.stopClose = false;
		this.hideAllChildren(elItem.item);
		this.hideAllChildrenMenu();

		elItem.className = "item over" + (elItem.isCheck == true ? " check" : "");
		elItem.item.parent.itemShow = elItem.item;
		this.showMenuChildren(elItem);
	},

	item_OnMouseOut: function (e, elItem) {
		if (elItem.item.isShowChildren == false) {
			elItem.className = "item" + (elItem.isCheck == true ? " check" : "");
		}
	},

	createItemLine: function (elContainerItem, item) {
		var elLine = utilObj.createEl("DIV");
		elLine.className = "bgLine";
		item.elItem = elLine;
		elContainerItem.appendChild(elLine);
	},

	createDetailItem: function (item) {
		var elBgL = utilObj.createEl("DIV");
		elBgL.className = "bgL";
		item.elItem.appendChild(elBgL);
		var elBgR = utilObj.createEl("DIV");
		elBgR.className = "bgR";
		elBgL.appendChild(elBgR);
		var elBgC = utilObj.createEl("DIV");
		elBgC.className = "bgC";
		elBgR.appendChild(elBgC);

		var elBgArrow = utilObj.createEl("DIV");
		elBgArrow.className = "bgArrow";
		if (item.isFirst == true) {
			if (this.isShowArrowFirstMenu) {
				elBgArrow.className = this.menuType == MenuType.horizontal ? "bgArrowH" : "bgArrow";
			}
			else {
				elBgArrow.className = "";
			}
		}
		if (item.listChildren.length == 0) {
			elBgArrow.style.backgroundImage = "none";
			elBgArrow.style.paddingRight = "0px";
		}
		elBgC.appendChild(elBgArrow);

		var elBgIcon = utilObj.createEl("DIV");
		elBgIcon.className = "bgIcon";
		if (item.pathIcon != null && item.pathIcon != "") {
			elBgIcon.style.backgroundImage = "url(" + item.pathIcon + ")";
		}
		else if (item.isFirst == true) {
			elBgIcon.style.paddingLeft = "0px";
		}
		elBgArrow.appendChild(elBgIcon);

		var elContent = utilObj.createEl("DIV");
		elContent.className = "contentMenu";
		elBgIcon.appendChild(elContent);

		var elTitle = utilObj.createEl("DIV");
		elTitle.className = "title";
		elTitle.innerHTML = item.text;
		elContent.appendChild(elTitle);

		// Là Root thì không tạo shortcut
		if (item.isFirst == true) return;
		var elShortcut = utilObj.createEl("DIV");
		elShortcut.className = "shortcut";
		elShortcut.innerHTML = item.shortcut;
		elContent.appendChild(elShortcut);
	},

	createItem: function (elContainerItem, item) {
		if (item.type == ItemMenuType.line) {
			this.createItemLine(elContainerItem, item);
			return;
		}
		item.type = item.type != null ? item.type : ItemMenuType.normal;
		item.shortcut = item.shortcut != null ? item.shortcut : "";
		var me = this;
		var elItem = utilObj.createEl("DIV");
		elItem.item = item;
		item.elItem = elItem;
		elItem.className = "item";
		this.createDetailItem(item);
		utilObj.addEvent(elItem, "click", function (e) { me.item_OnClick(e, elItem); });
		utilObj.addEvent(elItem, "mouseover", function (e) { me.item_OnMouseOver(e, elItem); });
		utilObj.addEvent(elItem, "mouseout", function (e) { me.item_OnMouseOut(e, elItem); });
		elContainerItem.appendChild(elItem);

		if (item.listChildren.length > 0) {
			var elStyle = utilObj.createEl("DIV");
			elStyle.className = this.style;
			utilObj.addChildToBody(elStyle);
			elItem.elStyle = elStyle;
			elItem.elContainerItem = utilObj.createEl("DIV");
			elItem.elContainerItem.className = "containerMenuBTD";
			elItem.elContainerItem.myName = this.myName;
			elItem.elContainerItem.style.display = "none";
			elStyle.appendChild(elItem.elContainerItem);
			for (var i = 0; i < item.listChildren.length; i++) {
				item.listChildren[i].parent = item;
				this.createItem(elItem.elContainerItem, item.listChildren[i]);
			}
		}
	},

	createListItemFirst: function () {
		var elStyle = utilObj.createEl("DIV");
		elStyle.className = this.style;
		this.itemRoot.elItem.appendChild(elStyle);

		var containerMenu = utilObj.createEl("DIV");
		containerMenu.myName = this.myName;
		containerMenu.className = "containerMenuBTDFirst";
		elStyle.appendChild(containerMenu);
		this.itemRoot.elItem.elStyle = elStyle;
		this.itemRoot.elItem.elContainerItem = containerMenu;
		for (var i = 0; i < this.itemRoot.listChildren.length; i++) {
			var item = this.itemRoot.listChildren[i];
			item.isFirst = true;
			item.parent = this.itemRoot;
			this.createItem(containerMenu, item);
			if (this.menuType == MenuType.horizontal) {
				item.elItem.style.display = "block";
				if (item.elItem.style.cssFloat != null) {
					item.elItem.style.cssFloat = "left";
				}
				else {
					item.elItem.style.styleFloat = "left";
				}
			}
		}
	},

	getElContent: function (idContainer) {
		var el = idContainer;
		if (typeof idContainer == "string") {
			el = utilObj.getElById(idContainer);
		}
		return el;
	},

	onMouseMove: function (e) {
		if (this.itemRoot.itemShow == null ||
			this.itemRoot.itemShow.isShowChildren == null ||
			this.itemRoot.itemShow.isShowChildren == false) {
			return;
		}

		var elTarget = utilObj.getElement(e);
		var elTarget = utilObj.getTargetElement(e);
		while (elTarget != null) {
			if (elTarget.myName == this.myName) {
				this.stopClose = true;
				clearTimeout(this.timerClose);
				return;
			}
			elTarget = elTarget.parentNode;
		}
		var self = this;
		if (this.itemRoot.itemShow != null) {
			this.stopClose = false;
			this.timerClose = setTimeout(function () {
				self.hideAllChildren(self.itemRoot.itemShow);
			}, 3000);
		}
	},

	onMouseDown: function (e) {
		if (this.itemRoot.itemShow == null ||
			this.itemRoot.itemShow.isShowChildren == null ||
			this.itemRoot.itemShow.isShowChildren == false) {
			return;
		}

		var elTarget = utilObj.getElement(e);
		var elTarget = utilObj.getTargetElement(e);
		while (elTarget != null) {
			if (elTarget.myName == this.myName) {
				this.stopClose = true;
				clearTimeout(this.timerClose);
				return;
			}
			elTarget = elTarget.parentNode;
		}
		if (this.itemRoot.itemShow != null) {
			this.stopClose = false;
			this.hideAllChildren(this.itemRoot.itemShow);
		}
	},

	init: function (idContainer, listChildren, menuType, isShowArrowFirstMenu) {
		this.myName = idContainer;
		this.itemRoot.elItem = this.getElContent(idContainer);
		this.itemRoot.listChildren = listChildren;
		this.isShowArrowFirstMenu = isShowArrowFirstMenu;
		this.menuType = menuType;
		this.createListItemFirst();
		this.listMenu[this.myName] = this;
		var self = this;
		utilObj.addEvent(document, "mousedown", function (e) {
			self.onMouseDown(e);
		});
		utilObj.addEvent(document, "mousemove", function (e) {
			self.onMouseMove(e);
		});
	}
};
