function Menu(divs){
	this.node=new Array();
	this.divs=divs;
	this.current=null;
	this.host=null;

	
	
	this.show=function(){

		if(this.node[0] && this.node[0].child){
			this.showMainMenu(this.node[0].child);
		}
				
		this.showContext(this.findNode(this.current,1));

	}

	this.replaceUrl=function(link,node){
		if(!this.host) this.host=window.location.host;

		link.href='http://'+this.host+'/heading/heading'+node.id+'.html';
		link.firstChild.nodeValue=node.name;
	}
	
	

	this.showMainMenu=function(nodes){
	
		var div=document.getElementById(this.divs['mainMenu']);
		var template=document.getElementById(this.divs['mainMenu']+'Template');
		var templateSelected=document.getElementById(this.divs['mainMenu']+'SelectedTemplate');
		div.removeChild(template);
		div.removeChild(templateSelected);
		template.style.display='';
		templateSelected.style.display='';
		
		for(var i in nodes){
			var node=nodes[i];
			var cell=(this.hasChildSelected(node))?templateSelected.cloneNode(true):template.cloneNode(true);
			cell.id='mainMenuRowTemplate';
			this.replaceUrl(cell.firstChild,node);
			if (i== 4 || i== 5 || i== 6 ){
				cell.style.marginTop='-5px';
      }
      if (i== 7){
        cell.firstChild.style.color='#F5A416';
      }
      if (i== 8){
        cell.firstChild.style.color='#F5A416';
        cell.style.marginTop='-15px';
      }
			div.appendChild(cell);
		}
	}

		
	
	this.showContext=function(main){
		var div=document.getElementById(this.divs['context']);
		if(!div) return;
		var title=document.getElementById(this.divs['context']+'Title');
		var template=document.getElementById(this.divs['context']+'Template');
		var selectedTemplate=document.getElementById(this.divs['context']+'SelectedTemplate');
		var div=template.parentNode;
		div.removeChild(template);
		div.removeChild(selectedTemplate);
		template.style.display='';
		selectedTemplate.style.display='';
		
		
		title.innerHTML=main.name;
		for(var i in main.child){
			var node=main.child[i];
			var cell=(this.hasChildSelected(node))?selectedTemplate.cloneNode(true):template.cloneNode(true);
			this.replaceUrl(cell.firstChild,node);
			div.appendChild(cell);
			
			
		}
	}
	
	this.findNode=function(id,level,node,parent){
		if(!node) node=this.node;
		for(var i in node){
			var child=node[i];
			if(parent) child.parent=parent.id;
			if(child.id==id) return child;
			var n=this.findNode(id,level,child.child,node);
			if(n!=null){
				if(level==child.level) n=child;
				return n;
			}
		}
		return null;
	}
	
	this.hasChildSelected=function(node){
		if(node.id==this.current) return true;
		for(var i in node.child){
			var child=node.child[i];
			if(this.hasChildSelected(child)) return true;
		}
		return false;
	}
	
	this.selected=function(obj,id){
		obj.mem=obj.id;
		obj.id=id;
	}
	
	this.deselected=function(obj){
		obj.id=obj.mem;
	}
}