// alib Vmenu object
// 20080427 - 3.0
// Recode and fixed by ThienVC(a)natech.vn
function Vmenu(r) {Vmenu.prototype.root=r};
//------------------------------------
//Modified by ThienVC@natech.vn - 06/03/2008
//------------------------------------
Vmenu.prototype.root = '';
Vmenu.prototype.addBehavior = function() {
	// get menu roots
	/*
	var node;
	var i = 0;
	var k = 1;
	while((node = document.getElementsByTagName('UL').item(i++))) {
		if (node.className == 'vmenu') {
			node.className = 'vmenuALIB';
			i = 0; k = 1;
			for (var i = 0; i < node.childNodes.length;i++) {
				if (node.childNodes[i].tagName == 'LI') {
					Vmenu.prototype.initMenuItem(node.childNodes[i], k++);
					// compute the height of whole block
					// cos we float top-level li`s we loose the height 
					// beware, we suppose that all are one-liners :(
					node.style.height = k*1.1+'em';
				}
			}
		} 
	}*/
	var li = Vmenu.prototype.activeTab(); //Lấy li của tab hiện thời

	if(li){
		//Scan child to expande child
		for(i=0;i<li.childNodes.length; i++){
			if(li.childNodes[i].tagName == 'UL')Vmenu.prototype.UL2Div(li.childNodes[i]);
		}
		var ul = li;
		var _p;
		//Scan parent to expande
		while (ul && ul!=Vmenu.prototype.getROOT()) {
			_debug(ul.tagName + '=' + ul.id);
			if(ul.tagName =='UL'){
				_p = ul.previousSibling;
				Vmenu.prototype.UL2Div(ul);
				ul = _p;
			}		
			ul = ul.parentNode;
		}
	}
	//SubMenu on selected style
	//all current style is inherit from css.
	//elmA.style.fontWeight = 'normal';
	//elmA.style.fontStyle = 'italic';
	//set any more private style here
	
	//Init the ROOT Menu
	Vmenu.prototype.initROOT(Vmenu.prototype.root);
}
Vmenu.prototype.getROOT = function(){
	return document.getElementById(Vmenu.prototype.root);
}
Vmenu.prototype.initROOT = function(root){
	var i=0,k=1;
	var node = document.getElementById(root);
	if(node){
		i = 0; k = 1;
		for (var i = 0; i < node.childNodes.length;i++) {
			if (node.childNodes[i].tagName == 'LI') {Vmenu.prototype.initMenuItem(node.childNodes[i], k++);node.style.height = k*1.1+'em';}
		}	
	}	
}
//Convert LI to new LI with new ID
Vmenu.prototype.RenewLIWithID = function(li,id) {
}
//Convert UL to DIV
Vmenu.prototype.UL2Div = function(ul) {
	_debug('UL2DIV:'+ul.tagName + '=' + ul.id);
	var _div = document.createElement('div');
	_div.id = ul.id;
	_div.innerHTML = ul.innerHTML;
	var _parent = ul.parentNode;
	//_debug('_parent='+_parent.id);
	var _next = _parent.nextSibling;
	if(_next) {
		_parent.parentNode.insertBefore(_div, _next);
	} else {
		_parent.appendChild(_div);
	}
	_parent.removeChild(ul);
	Vmenu.prototype.initROOT(ul.id);
}
Vmenu.prototype.initMenuItem = function(li, num) {
	// to emulate :first-child
	if (num == 1) {
		li.style.borderTopWidth = '0px';
	}
	//li.style.backgroundColor = 'red';
	for (var i = 0; i < li.childNodes.length;i++) {
		if (li.childNodes[i].tagName == 'UL') { //&& li.id != vMenuActive   || li.childNodes[i].tagName == 'DIV'
			//alert(activeTab);
			var nodeUL = li.childNodes[i];
			li.cachedUL = nodeUL;
			// 1 + n-level submenu
			if (li.parentNode.className != 'vmenuALIB') {
				// ul position -
				// TODO - i am unable to find its width
				// anyway it is a bad idea, cos it changes with zoom
				// nodeUL.style.left = li.style.pixelWidth;
				// background arrow
				li.style.backgroundRepeat = 'no-repeat';
				//li.style.backgroundImage = 'url("menu.vertical/sub.gif")';
				li.style.backgroundColor = 'white';
				li.style.backgroundPosition = '99%';
			}
			alib.addListener(li, 'mouseover', Vmenu.prototype.showSubmenu);
			alib.addListener(li, 'mouseout',  Vmenu.prototype.hideSubmenu);
			var k = 1;
			for (var j = 0; j < nodeUL.childNodes.length;j++) {
				if (nodeUL.childNodes[j].tagName == 'LI')
					Vmenu.prototype.initMenuItem(nodeUL.childNodes[j], k++);
			}
		}/* else if (li.childNodes[i].tagName == 'UL' && li.id == activeTab) {
			//Modified by ThienVC@natech.vn
			//If menu is selected and has SubMenu
			//Replace UL by Div to fixed position
			_debug(activeTab);
			var vMenuChild = document.createElement('div');
			vMenuChild.id = 'div_Active';
			vMenuChild.innerHTML = li.childNodes[i].innerHTML;
			var vNextMenu = li.nextSibling;
			if(vNextMenu) {
				li.parentNode.insertBefore(vMenuChild, vNextMenu);
			} else {
				li.parentNode.appendChild(vMenuChild);
			}
			li.removeChild(li.childNodes[i]);
			_debug(vMenuChild.id);
			var k = 1;
			for (var j = 0; j < vMenuChild.childNodes.length;j++) {
				if (vMenuChild.childNodes[j].tagName == 'LI')
					Vmenu.prototype.initMenuItem(vMenuChild.childNodes[j], k++);
			}
			//li.id = 'li_vMenuActive';
			//li.className = 'li_vMenuActive';
		}*/
	}
}

Vmenu.prototype.showSubmenu = function(e) {
	if (!e) var e = window.event;
	var target = alib.getTarget(e);
	if (target.tagName == 'A')
		target = target.parentNode;
	if (target.cachedUL)
		target.cachedUL.style.display = 'block';
}
function pausecomp(Amount)
{
d = new Date() //today's date
while (1<2)
{
mill=new Date() // Date Now
diff = mill-d //difference in milliseconds
if( diff > Amount ) {break;}
}
}
Vmenu.prototype.hideSubmenu = function(e) {
	if (!e) var e = window.event;
	var target = alib.getTarget(e);
	var related = alib.getRelatedTarget(e);
	if (!related || related.tagName == 'HTML')
		related = document.getElementsByTagName('body')[0];
	Vmenu.prototype.report('out '+target.tagName+'->'+related.tagName);
	Vmenu.prototype.report("\n");
	// maybe mouse left only into successor, handle
	if (target.tagName == 'A')
		target = target.parentNode;
	if (Vmenu.prototype.isAncestor(target, related))
		return;
	// it left into ancestor
	// find ul upon related node or body
	var commonUL = related;
	while (commonUL.tagName != 'UL' && commonUL.tagName != 'BODY')
		commonUL = commonUL.parentNode;
	// iterate up the tree from target till commonUL and close submenu
	//pausecomp(300);
	Vmenu.prototype.report('coUL:'+commonUL.tagName+'::');
	while (target != commonUL && target.tagName != 'BODY') {
		Vmenu.prototype.report(target.tagName+'~');
		if (target.cachedUL)
		{
			
			target.cachedUL.style.display = 'none';
		}
		target = target.parentNode;
	}
	Vmenu.prototype.report("\n");
}


Vmenu.prototype.debug = 0;

Vmenu.prototype.report = function(txt) {
	var report = document.getElementById('report');
	if (report && Vmenu.prototype.debug) {
//		report.value += txt;
	}
}
Vmenu.prototype.reportln = function(txt) {
	var report = document.getElementById('report');
	if (report && debug) {
//		report.value += txt + "\n";
	}
}
Vmenu.prototype.isAncestor = function(ancestor, child) {
	while (child != ancestor && child.nodeName != 'BODY')
		child = child.parentNode;
	return child == ancestor ? 1 : 0;
}
Vmenu.prototype.activeTab = function(){
	var vl=alib.getURLParam('tid');
	if(!vl)vl=alib.getURLParam('tabid');
	return document.getElementById('NatechMenu'+ vl);
};
/* INITS */
/* var vmenu1 = new Vmenu('vmenuROOT');
//register onload init
alib.addListener(window, 'load', vmenu.addBehavior); */

