/*
* Builds and visualise horizontal menu from XML file
* Max recurse level 1
* Require horizontal_menu.css style file attached to project
* HTMLDOM object can be acessed via DOMObject property
*/

function HorizontalMenu(xmlDoc){
	root=xmlDoc.documentElement;
	
	var UL=document.createElement('UL');
		UL.className="horizontal_menu";		
	this.DOMObject=UL;

	var child;
	for (var i=0;i<root.childNodes.length;i++){
		child=root.childNodes[i];
		if (child.nodeType==1){
			if (child.nodeName=="ITEM"){									
				this.DOMObject.appendChild(this.buildSubMenu(child));
			}
		}		
	}	
	
	if (window.attachEvent)
		this.menuhover(this.DOMObject);	
	return this;	
}

HorizontalMenu.prototype={
	buildSubMenu: function(parentNode){
		node=parentNode.getElementsByTagName("TEXT")[0];		
		text=node.text || node.textContent;	
		
		node=parentNode.getElementsByTagName("HREF")[0];
		href=node.text || node.textContent;	
		
		hasChilds=parentNode.getElementsByTagName("ITEM").length!=0;	
		
		var item=this.buildMenuItem(text, href, false);	

		if (hasChilds){	
			subMenu=document.createElement('UL');
			item.appendChild(subMenu);
			var child;		
			for (var i=0;i<parentNode.childNodes.length;i++){				
				child=parentNode.childNodes[i];
				if (child.nodeType==1){
					if (child.nodeName=="ITEM"){									
						subMenu.appendChild(this.buildSubMenu(child));
					}
				}		
			}		
		}	
		return item;
	},
	
	buildMenuItem: function(text, url,  isSelected){
		var li=document.createElement('LI');
		var a=document.createElement('A');
			//if (isSelected)
			//	a.className=selectedClass;
			a.setAttribute("href", url);
			a.innerHTML=text;			
		li.appendChild(a);	
		return li;
	},
	
	menuhover:function (UL){
		var lis = UL.getElementsByTagName("LI");
		for (var i=0;i<lis.length;i++){
			lis[i].onmouseover=function(){this.className+=" iehover";}
			lis[i].onmouseout=function() {this.className=this.className.replace(new RegExp(" iehover\\b"), "");}
		}
	}
}

