function initMenuA(name, containerClass) {
	this.name = name;
	this.containerclass = containerClass;
	
	this.root_level = new Array();
	this.structure = new Array();
	this.nodes = new Array();
	this.menuOpened = new Array();

	this.tm = '';
	this.timeOut = 10;
	this.timeOutPeriod = 2000;
	this.addnode = addNode;
	this.showMenu = showRootLevel;
	this.showsubmenu = showSubMenu;
	this.prehidesubmenu = preHideSubMenu;
	this.hidesubmenu = hideSubMenu;
	this.drawcontainer = drawContainer;
	this.containerover = containerOver;
	this.containerout = containerOut;
	this.hidenodechilds = hideNodeChilds;
	this.cleared = 1;
	this.overMenu = 0;

	this.navVersion = new String(navigator.appVersion);
	this.navUserAgent = new String(navigator.userAgent);
	this.navProduct = new String(navigator.product);
	this.msie6 =(this.navVersion.indexOf("MSIE 6")>-1&&this.navUserAgent)?1:0;
	this.msie5 =(this.navVersion.indexOf("MSIE 5")>-1&&this.navUserAgent)?1:0;
	this.opera = (this.navUserAgent.indexOf("Opera")>-1)?1:0;
	this.mac = (this.navVersion.indexOf("Mac")>-1)?1:0;
	this.mozilla = (this.navProduct.indexOf("Gecko")>-1)?1:0;
	
	return this;
}

//name, id, parent, normalClass, normalStyle, selClass, selStyle, title, action, 
//subMenuDirection, subMenuExtraOffsetX, subMenuExtraOffsetY, selCode, normalCode

function addNode(name, id, parent, normalClass, normalStyle, selClass, selStyle, title, action, subMenuDirection, subMenuExtraOffsetX, subMenuExtraOffsetY, selCode, normalCode) {
	this.structure[name]=new Object();
	var node = this.structure[name];
	this.nodes[name] = node;

	node.name = name;
	node.id = id;
	node.childs = new Array();
	node.parent = parent;
	node.normclassname = normalClass;
	node.normstyle = normalStyle;
	node.selclassname = selClass;
	node.selstyle = selStyle;
	node.title = title;
	node.action = action;
	node.setselected = setSelected;
	node.setnormal = setNormal;
	node.submenudirection = subMenuDirection;
	node.submenuextraoffsetx = subMenuExtraOffsetX;
	node.submenuextraoffsety = subMenuExtraOffsetY;
	node.selcode = new Function(selCode);
	node.normalcode = new Function(normalCode);
	
	node.level = 0;

	node.object = '';
	node.show = setVisible;
	
    if (typeof(this.structure[parent]) != 'object') {
		this.root_level[this.root_level.length] = name;
	} else {
		this.structure[parent].childs[this.structure[parent].childs.length] = name;
		//detecting Level
		tmp = this.structure[node.parent];
		while (typeof(tmp) == 'object') {
			if (node.submenudirection == "") 
				node.submenudirection = tmp.submenudirection;

			tmp = this.structure[tmp.parent];
			node.level = node.level + 1;
		}
	}
}

function setVisible(){
	this.object.style.visibility = "visible";
}

function showRootLevel(){
	var node;
	for (i=0; i<this.root_level.length; i++) {
		node = this.nodes[this.root_level[i]];
		var div = document.createElement('DIV');
		div.setAttribute('id', node.id);
		div.innerHTML = node.title;

		document.getElementById(node.parent).appendChild(div);
		node.object = document.getElementById(node.id);

		node.object.className = node.normclassname;
		node.object.style.cssText = node.normstyle;
		node.object.onmouseover = new Function(this.name+".showsubmenu('"+node.name+"')");
		node.object.onmouseout = new Function(this.name+".prehidesubmenu('"+node.name+"')");
		node.show();
	}

	return;
}

function containerOver(name) {
	clearTimeout(this.tm);
	this.cleared = 0;
	this.overMenu = 1;
	
	var node = this.structure[name];
	var parent = this.structure[node.parent];
	if (typeof(parent) == 'object') {
		parent.setselected();
	}
}

function containerOut(name) {
	this.tm = setTimeout(this.name+".hidesubmenu()", this.timeOutPeriod);
	this.overMenu = 0;
}

function showSubMenu(name) {
	clearTimeout(this.tm);
	var node = this.structure[name];
	var brother;
	var container;

	this.overMenu = 1;
	node.setselected();

	this.cleared = 0;

	//HighLite Choosen Path
	var parent = this.structure[node.parent];
	while (typeof(parent) == 'object') {
			parent.setselected();
			parent = this.structure[parent.parent];
	}

	//Unmark other oh this level
	if (node.level > 0) {
		for (var i=0; i<this.structure[node.parent].childs.length; i++) {
			brother = this.structure[this.structure[node.parent].childs[i]];
			if (brother.name != name) {
				brother.setnormal();
			}
		}
	}

	//Lets Hide all menus that we dont need to show;
	if (node.level > 0) {
		for (var i=0; i<this.structure[node.parent].childs.length; i++) {
			brother = this.structure[this.structure[node.parent].childs[i]];
			if (brother.name != node.name) {
				var cid = 'level_'+brother.name+'_'+brother.level
				if (this.menuOpened[cid]) {
					document.getElementById(cid).style.visibility = 'hidden';
					this.menuOpened[cid] = 0;
				}
				this.hidenodechilds(brother.name);
			}
		}
	} else {
		for (var i=0; i<this.root_level.length; i++) {
			brother = this.nodes[this.root_level[i]];
			if (brother.name != node.name) {
				var cid = 'level_'+brother.name+'_'+brother.level;
				if (this.menuOpened[cid]) {
					document.getElementById(cid).style.visibility = 'hidden';
					this.menuOpened[cid] = 0;
				}
				this.hidenodechilds(brother.name);
			}
		}
	}

	//Show subMenu if such exists
	if (this.structure[name].childs.length) {
		//Maybe we already have this object
		if (document.getElementById('level_'+node.name+'_'+node.level) == null)	{
			container = this.drawcontainer(name);
		} else {
			container = document.getElementById('level_'+node.name+'_'+node.level);
			//Need to clear previously choosen elements
			for (var i=0; i<this.structure[name].childs.length; i++) {
				var tmpnode = this.structure[this.structure[name].childs[i]];
				tmpnode.setnormal();
			}
		}
		container.style.visibility = 'visible';
		this.menuOpened[container.id] = 1;
	}
	return;
}

function preHideSubMenu(name){
	this.overMenu = 0;
	var node = this.structure[name];
	node.setnormal();

	this.tm = setTimeout(this.name+".hidesubmenu()", this.timeOutPeriod);

	return;
}


function hideSubMenu(toHide) {
	var tmp = new Array();
	var rootNode;
	if (this.cleared == 0) {
		if (this.overMenu == 0) {
			//kill them all
			for (var a in this.menuOpened) {
				//UnHighLite Root Element in Path 
				tmp = a.split('_');
				if (tmp[tmp.length-1] == '0') {
					var rootNode = this.structure[a.substr(6, a.length-8)];
					rootNode.setnormal();
				}

				this.menuOpened[a] = 0;
				document.getElementById(a).style.visibility = 'hidden';
			}
			clearTimeout(this.tm);
			this.cleared = 1;
		}
	}
}

function hideNodeChilds(name) {
	for (var i=0; i<this.structure[name].childs.length; i++) {
		child = this.structure[this.structure[name].childs[i]];
		var cid = 'level_'+child.name+'_'+child.level;
		if (this.menuOpened[cid]) {
			document.getElementById(cid).style.visibility = 'hidden';
			this.menuOpened[cid] = 0;
			this.hidenodechilds(child.name);
		}
	}
	this.structure[name].setnormal();
}

function drawContainer(name) {
	var maxWidth = 0;
	var offsetY;
	var offsetX;
	var tmp;

	var node = this.structure[name];
	var container = document.createElement('DIV');
	var idContainer = 'level_'+node.name+'_'+node.level;
	container.setAttribute('id', idContainer);

	container.className = this.containerclass;
	document.body.appendChild(container);
	//we need to highlite parent element - to prevent blinking
	if (this.structure[name].childs.length > 0) {
		container.onmouseover = new Function(this.name+".containerover('"+this.structure[this.structure[name].childs[0]].name+"')");
		container.onmouseout = new Function(this.name+".containerout('"+this.structure[this.structure[name].childs[0]].name+"')");
	}

	for (var i=0; i<this.structure[name].childs.length; i++) {
		node = this.structure[this.structure[name].childs[i]];
		var div = document.createElement('DIV');
		div.setAttribute('id', node.id);
		div.innerHTML = node.title;

		container.appendChild(div);
		node.object = document.getElementById(node.id);
		
		node.object.className = node.normclassname;
		node.object.style.cssText = node.normstyle + '; position: relative';
		node.object.onmouseover = new Function(this.name+".showsubmenu('"+node.name+"')");
		node.object.onmouseout = new Function(this.name+".prehidesubmenu('"+node.name+"')");
		if (node.action.length > 1) {
			node.object.onclick = new Function(node.action);
		}
		if (this.mozilla) {
			tmp = container.offsetWidth - 10
			node.object.style.width = tmp + 'px';

			tmp = node.object.offsetHeight;
			node.object.style.height = tmp + 'px';
		}

		//Lets find Out Max Length
		if (this.opera) {
			var tt = document.createElement('DIV');
			tt.style.visibility = "hidden";
			var c = document.createElement('TABLE')
			c.setAttribute('border', "1");
			var c1 = document.createElement('TBODY')
			var c2 = document.createElement('TR');
			c3 = document.createElement('TD');
			c3.className = "menu_item";
			var tt1 =  document.createTextNode(node.title);
			c3.appendChild(tt1);
			c2.appendChild(c3);
			c1.appendChild(c2);
			c.appendChild(c1);
			tt.appendChild(c);
			document.body.appendChild(tt);
			
			if (maxWidth < c3.offsetWidth) {
				maxWidth = c3.offsetWidth
			}
			document.body.removeChild(tt);
		} else {
			if (maxWidth < div.offsetWidth) {
				maxWidth = div.offsetWidth
			}
		}
	}
	if (maxWidth > container.offsetWidth) {
		tmp = maxWidth;
		if (this.opera) 
			tmp = tmp + 5

		container.style.width = tmp + 'px';
	}
	
	//all divs must have same width
	if (this.opera && maxWidth < container.offsetWidth) 
		maxWidth = container.offsetWidth - 6
	
	if (this.mozilla && maxWidth < container.offsetWidth) 
		maxWidth = maxWidth - 4

	for (i=0; i < container.childNodes.length; i++) {
		if (typeof(container.childNodes[i]) == "object") {
			container.childNodes[i].style.width = maxWidth + "px";
		}
	}

	offsetY = getOffset(this.structure[node.parent].id, 'offsetTop');
	offsetX = getOffset(this.structure[node.parent].id, 'offsetLeft');
	if (node.submenudirection == 'left' && node.level == 1) {
		offsetX = offsetX - (container.offsetWidth - document.getElementById(this.structure[node.parent].id).offsetWidth);
	}
	if (node.level > 1)	{
		if (node.submenudirection == 'left') {
			offsetX = offsetX - (container.offsetWidth - document.getElementById(this.structure[node.parent].id).offsetWidth) - 100;
		} else {
			offsetX = offsetX + 100;
		}
	}
	
	//Lets add special offset that client asked for
	offsetX = offsetX + this.structure[node.parent].submenuextraoffsetx;
	offsetY = offsetY + this.structure[node.parent].submenuextraoffsety;

	container.style.top = offsetY;
	container.style.left = offsetX;

	return container;
}

function getOffset(id, off) {
	var myElement = document.getElementById(id);
	var offset;
	eval("offset = myElement."+off);

	while (myElement.parentNode.nodeName != 'HTML') {
		myElement  = myElement.parentNode
		eval("offset = offset + myElement."+off);
	}
	
	return offset;
}

//Set element normal class & style 
function setNormal(){
	if (this.normclassname.length > 1) {
		this.object.className = this.normclassname;
	}
	if (this.normstyle.length > 1) {
		this.object.style.cssText = this.object.style.cssText + this.normstyle;
	}
	this.normalcode();

	return;
}

//Set element normal class & style 
function setSelected(){
	if (this.selclassname.length > 1) {
		this.object.className = this.selclassname;
	}
	if (this.selstyle.length > 1) {
		this.object.style.cssText = this.object.style.cssText + this.selstyle;
	}
	this.selcode();

	return;
}
