﻿/**
 * Menu Top
 * 
 * @author     banhthidiem <banhthidiem@gmail.com>
 * @copyright  2007 Bach Khoa Computer Inc.
 * @version    $Id: MenuTop.js, v2.0 2007/10/3
 */


function MMItem(id, name, url, action, isReload)
{
	this.id = id;
	this.name = name;
	this.action = action;
	this.isReload = false;
	if (typeof(isReload) != "undefined") this.isReload = isReload;
	this.url = url;
	this.level = 0;
	this.isChild = true;
	this.parent = null;
	this.listChild = [];
	
	this.addChild = function(item)
	{
		if(item.isAncestor(this)) return;
		var parent = item.parent;
		if (parent != null)
		{
			parent.removeChild(item);
		}
		item.parent = this;
		item.increaseLevel(this.level);
		this.isChild = false;
		this.listChild[this.listChild.length] = item;
	};
	
	this.removeChild = function(item)
	{
		for (var i = 0; i < this.listChild.length; i++)
		{
			if (item == this.listChild[i])
			{
				this.listChild.splice(i, 1);
				break;
			}
		}
		if (this.listChild.length == 0) this.isChild = true;
	};
	
	this.increaseLevel = function(level)
	{
		this.level = level + 1;
		if (this.isChild) return;
		for (var i = 0; i < this.listChild.length; i++)
		{
			this.listChild[i].increaseLevel(this.level);
		}
	};
	
	this.isAncestor = function(item)
	{
		if (this == item) return true;
		if (this.isChild) return false;
		var chk = false;
		for (var i = 0; i < this.listChild.length; i++)
		{
			chk = this.listChild[i].isAncestor(item);
			if(chk) break;
		}
		return chk;
	};
};

/*
 * Menu Top
 */
var styleBtnMM = {
	
	isMapLink : false,
	oldObject : null,
	oDisplayChildMM : null,
	itemChoose : null,
	listItem : null,
	
	createMain : function(idContainerParent, list)
	{
		this.listItem = list;
		var oContainerParent = utilObj.getElById(idContainerParent);
		oContainerParent.appendChild(this.createElTop());
		oContainerParent.appendChild(this.createElSpacerLeft(260, 30));
		var o = this.createElCenterRight();
		this.createItemMM(o);
		oContainerParent.appendChild(o);
		this.oDisplayChildMM = this.createElBottom();
		oContainerParent.appendChild(this.oDisplayChildMM);
	},
	
	createItemMM : function(o)
	{
		for (var i = 0; i < this.listItem.length; i++)
		{
			var item = this.listItem[i];
			if (item.level == 0)
			{
				o.appendChild(this.createElTab(item));
			}
		}
	},
	
	showChildMM : function(item)
	{
		if (item.oDisplayChild == null)
		{
			var o = this.createElDiv();
			this.addChildItem(o, item.listChild);
			item.oDisplayChild = o;
			this.oDisplayChildMM.appendChild(o);
			
		}
		item.oDisplayChild.style.display = "";
		this.itemOver = item;
	},
	
	hideChildMM : function(item)
	{
		if (item.oDisplayChild != null)
		{
			item.oDisplayChild.style.display = "none";
		}
	},
	
	addChildItem : function(o, list)
	{
		o.appendChild(this.createElSpacerLeft(15, 30));
		for (var i = 0; i < list.length; i++)
		{
			var item = list[i];
			if (item.isChild)
			{
				if (i != 0)
				{
					o.appendChild(this.createElSep());
				}
				item.oElChild = this.createChildItem(item);
				o.appendChild(item.oElChild);
			}
		}
	},
	
	createElTop : function()
	{
		var oH61 = this.createElDiv();
		oH61.style.height = "61px";
		return oH61;
	},
	
	createElSep : function()
	{
		var o = this.createElDiv(null, "SepChildItemMM");
		o.innerHTML = "|";
		return o;
	},
	
	createTagA : function(content, url)
	{
		var o = utilObj.createEl("A");
		o.alt = content;
		o.innerHTML = content;
		o.href = url;
		o.onclick = "return false;";
		return o;
	},
	
	createElTab : function(item)
	{
		var o = this.createElDiv(item.id, "btn_main btn_main_out");
		var self = this;
		utilObj.addEvent(o, "mouseover", function() { self.over(o, item); });
		utilObj.addEvent(o, "mouseout", function() { self.out(o, item); });
		utilObj.addEvent(o, "click", function() { self.click(o, item); });
		o.appendChild(this.createElDiv(null, "btn_main_padding_top"));
		o.appendChild(utilObj.createElText(item.name));
		return o;
	},
	
	createChildItem : function(item)
	{
		var o = this.createElDiv("Child_" + item.id, "childItemMM_out");
		var self = this;
		utilObj.addEvent(o, "mouseover", function() { self.overChild(o, item); });
		utilObj.addEvent(o, "mouseout", function() { self.outChild(o, item); });
		utilObj.addEvent(o, "click", function() { self.clickChild(o, item); });
		o.appendChild(this.createTagA(item.name, item.url));
		return o;
	},

	overChild : function(o, item)
	{
		if (o.className == "childItemMM_down") return;
		o.className = "childItemMM_over";
	},
	
	outChild : function(o, item)
	{
		if (o.className == "childItemMM_down") return;
		o.className = "childItemMM_out";
	},
	
	clickChild : function(o, item)
	{
		this.createMapLink(item, false);
		// Change style when different Parent
		if (this.oldObject != utilObj.getElById("MM_" + item.parent.id))
		{
			this.changeStyle(item.parent.id);
		}
		if (!this.isLoadWebPage) item.action(item.url, item.isReload);
		if (o.className == "childItemMM_down") return;
		if (this.oldObjectChild != null) this.oldObjectChild.className = "childItemMM_out";
		o.className = "childItemMM_down";
		this.oldObjectChild = o;
	},
	
	createElSpacerLeft : function(w, h)
	{
		var o = this.createElDiv();
		this.setStyleFloat(o, "left");
		o.style.width = w + "px";
		o.style.height = h + "px";
		return o;
	},
	
	createElCenterRight : function()
	{
		var o = this.createElDiv();
		this.setStyleFloat(o, "right");
		o.style.width = "520px";
		o.style.height = "30px";
		return o;
	},
	
	createElBottom : function()
	{
		var o = this.createElDiv("displayChildTab");
		this.setStyleFloat(o, "none");
		o.style.height = "35px";
		return o;
	},
	
	setStyleFloat : function(o, pos)
	{
		if (utilObj.isIE)
			o.style.styleFloat = pos;
		else if (utilObj.isFF)
			o.style.cssFloat = pos;
		else
			o.setAttribute("style", "float:" + pos);
	},
	
	createElDiv : function(id, className)
	{
		var o = utilObj.createEl("DIV");
		if (id != null) o.id = "MM_" + id;
		if (className != null) o.className = className;
		return o;
	},

	over : function(o, item)
	{
		if (item.listChild.length > 0)
		{
			if (this.itemOver) this.hideChildMM(this.itemOver);
			this.showChildMM(item);
		}
		if (o.className == "btn_main btn_main_down") return;
		o.className = "btn_main btn_main_over";
	},
	
	out : function(o, item)
	{
		if (o.className == "btn_main btn_main_down") return;
		o.className = "btn_main btn_main_out";
		/* ***************************** When not return sub menu *************************************
		if (item.listChild.length > 0)
		{
			if (this.itemOver) this.hideChildMM(this.itemOver);
			if (this.itemChoose) this.showChildMM(this.itemChoose);
		}
		*/
	},
	
	/* ***************************** Create Map Link *************************************/
	createMapLink : function(item, isParentCall)
	{
		if (this.isMapLink)
		{
			MapLink.resetList();
			if (isParentCall)
			{
				MapLink.addNode(item.name, item.url);
				if (item.listChild.length > 0)
					MapLink.addNode(item.listChild[0].name, item.listChild[0].url);
			}
			else
			{
				while (item != null)
				{
					MapLink.addNode(item.name, item.url);
					item = item.parent;
				}
			}
			MapLink.buildMapLink();
		}
		else
		{
			MapLink.hide();
		}
	},
	/* ***************************** Create Map Link *************************************/
	
	click : function(o, item)
	{
		this.createMapLink(item, true);
		if (o.className == "btn_main btn_main_down") return;
		if (this.oldObject != null) this.oldObject.className = "btn_main btn_main_out";
		o.className = "btn_main btn_main_down";
		if (item.listChild.length > 0)
		{
			this.itemChoose = item;
			if (item.listChild[0].oElChild) this.clickChild(item.listChild[0].oElChild, item.listChild[0]);
		}
		else
		{
			item.action(item.url, item.isReload);
			if (this.itemChoose)
			{
				this.hideChildMM(this.itemChoose);
				if (this.itemChoose.listChild.length > 0) 
					this.itemChoose.listChild[0].oElChild.className = "childItemMM_out";
				this.itemChoose = null;
			}
		}
		this.oldObject = o;
	},
	
	resetAllStyle : function()
	{
		if (this.itemChoose)
		{
			this.hideChildMM(this.itemChoose);
			if (this.oldObjectChild)
			{
				this.oldObjectChild.className = "childItemMM_out";
				this.oldObjectChild = null;
			}
			this.itemChoose = null;
		}
		if (this.oldObject)
		{
			this.oldObject.className = "btn_main btn_main_out";
			this.oldObject = null;
		}
	},
	
	changeStyle : function(id)
	{
		if (this.oldObject != null) this.oldObject.className = "btn_main btn_main_out";
		if (!utilObj.getElById("MM_" + id)) return;
		this.oldObject = utilObj.getElById("MM_" + id);
		this.oldObject.className = "btn_main btn_main_down";
	},
	
	getItemWR : function(list, id)
	{
		for (var i = 0; i < list.length; i++)
		{
			if (list[i].id == id) return list[i];
		}
		return null;
	},
	
	changeStyleWhenReload : function(query, e)
	{
		var re = utilObj.analyseURL(query);
		var para, id;
		if (utilObj.getElById("MM_" + re.page))
		{
			this.isLoadWebPage = true;
			if (re.paramater["TabID"] != null)
			{
				para = re.paramater["TabID"].split("*");
				if (para.length == 1)
					this.changeStyleMM(re.page);
				else
				{
					id = re.page + "?TabID=" + para[0];
					this.changeStyleMM(re.page, id + "*" + para[1]);
				}
			}
			else
			{
				this.changeStyleMM(re.page);
			}
			this.isLoadWebPage = false;
		}
		else if (re.paramater["TreeID"] != null)
		{
			BKC.listObjPage[nameCat].findTargetChildEl(re.paramater["TreeID"], e);
		}
	},
	 
	changeStyleMM : function(id, childId)
	{
		this.oldObject = utilObj.getElById("MM_" + id);
		this.itemChoose = this.getItemWR(this.listItem, id);
		if (!this.itemChoose) return;
		this.over(this.oldObject, this.itemChoose);
		this.oldObject.className = "btn_main btn_main_down";
		if (this.itemChoose.listChild.length > 0)
		{
			var item = this.itemChoose.listChild[0];
			if (typeof(childId) != "undefined") item = this.getItemWR(this.itemChoose.listChild, childId);
			if (item) this.clickChild(item.oElChild, item);
		}
	}
};
