YAHOO.si.Tree = function(container)
{
	this.index = 0;
	this.root = {};
	this.areaid = "";
	this.selectedid = "";
	this.focus = function(divid)
	{
		var items = YAHOO.util.Dom.getElementsByClassName ( "ygtvitem");// , tag , root , apply ) 
		var setColour = function(item,colour)
		{
			var tables = item.getElementsByTagName("table");
			var firstTable = tables[0];
			YAHOO.util.Dom.setStyle(firstTable,"background-color",colour);
			var anchors = firstTable.getElementsByTagName("a");
			YAHOO.util.Dom.setStyle(anchors,"background-color",colour);				
			var rows = firstTable.getElementsByTagName("tr");
			YAHOO.util.Dom.setStyle(rows,"background-color",colour);				
			var cells = firstTable.getElementsByTagName("td");
			YAHOO.util.Dom.setStyle(cells,"background-color",colour);								
		}
		for(var i = 0; i < items.length;i++)
		{
			var item = items[i];
			if(item.id===divid)
			{
				setColour(item,"#fff");
			}
			else
			{
				setColour(item,"");				
			}
		}
	}
	this.loadPage = function(ev,itemdata)
	{
		YAHOO.util.Event.stopEvent(ev);
		var id = itemdata.id;
		var url = itemdata.url;
		var language = YAHOO.si.HistoryManager.language;
		var lookup = urllookup[url];
		var areaid = lookup.areaid;
		var pageid = lookup.pageid;
		var state = {"language":language,"areaid":areaid,"pageid":pageid};
		YAHOO.si.HistoryManager.multiNavigate(state);	
		this.focus(itemdata.divid);				
	}
	/**
	 * This creates a table, adds it to the item div. It returns it to
	 * the only row created in the table.
	 */
	this.addTableWithBodyRow = function(_parent,depth,id)
	{
		var table = document.createElement("table");
		table.setAttribute("cellspacing","0"); 
		table.setAttribute("cellpadding","0");
		table.setAttribute("border","0");		
		table.className = "ygtvdepth" +depth;		
		if(id!==null)
		{
			table.id = id;
		}
		var tbody = document.createElement("tbody");
		table.appendChild(tbody);
		var tr = document.createElement("tr");
		tr.className = "ygtvrow";
		tbody.appendChild(tr);
		_parent.appendChild(table);
		return tr;		
	};
	/**
	 * This creates a div for the children items. If there are no children nodes, this adds
	 * a style attribute and sets display to none to hide it.
	 */
	this.addChildrenDiv = function(itemdiv,itemdata,state)
	{
		var childrendiv = document.createElement("div");
		childrendiv.className = "ygtvchildren";
		childrendiv.id = "ygtvc" + this.index;
		itemdiv.appendChild(childrendiv);
		if(itemdata!=null)
		{
			if(!state)
			{
				//Can you believe this?! setAttribute doesn't work in IE
					//childrendiv.setAttribute("style","display:none");			
					//childrendiv.style = "display:none";
					YAHOO.util.Dom.setStyle(childrendiv,"display","none"); 
					//This would work in IE
					//childrendiv.style["display"] = "none";
			}
			else
			{
				var depth = parseInt(itemdata.nodetype);
				YAHOO.util.Dom.setStyle(childrendiv,"margin-left",(depth * 2) + "px"); 
				//childrendiv.setAttribute("style","margin-left:" + );							
			}
			//	childrendiv.innerHTML = "testing";
		}
		return childrendiv;
	};
	this.addContentCell = function(tr,itemdata)
	{
		var td = document.createElement("td");
		td.className = "ygtvhtml ygtvcontent";
		td.id = "ygtvcontentel" + this.index;	
		//td.innerHTML = "testing";	
		tr.appendChild(td);
		return td;	
	};
	this.addSpacerCell = function(tr,itemdata)
	{
		var td = document.createElement("td");
		td.className = "ygtvblankdepthcell";
		td.id = "blankdepthcell" + itemdata.id;
		tr.appendChild(td);
		var spacer = document.createElement("div");
		spacer.className = "ygtvspacer";
		td.appendChild(spacer);
		return td;
	};
	this.addDepthCell = function(tr)
	{
		var td = document.createElement("td");
		td.className = "ygtvdepthcell";
		tr.appendChild(td);
		var spacer = document.createElement("div");
		spacer.className = "ygtvspacer";
		td.appendChild(spacer);
		return td;		
	}
	this.addLabelCell = function(tr,itemdata)
	{
		var td = document.createElement("td");
		td.className = "ygtvlabelel";
		td.id = "TD" + itemdata.id;
		tr.appendChild(td);
		return td;
	};
	this.addLabelLink = function(td,itemdata)
	{
		var anchor = document.createElement("a");
		anchor.className ="ygtvlabel";
		anchor.id = "ygtvlabelel" + this.index;
		anchor.setAttribute("onclick","return false;");
		anchor.href= itemdata.url;
		anchor.innerHTML=itemdata.text;
		td.appendChild(anchor);
		return anchor;
	};
	this.addToggleCell = function(tr,itemdata,expand)
	{
		var td = document.createElement("td");
		if(itemdata!=null)
		{
			if(expand)
			{
				td.className = "ygtvlm";
			}	
			else
			{
				td.className = "ygtvlp";				
			}
		}
		td.id = "ygtvt" + itemdata.id;
		tr.appendChild(td);
		return td;
	}
	/**
	 * Adds a div to the parent container, creates a div for the children and returns the children div.
	 * If the data for the item is not null, this creates a table to contain the contents.
	 */
	this.addItemDiv = function(_parent,itemdata,state)
	{
		var itemdiv = document.createElement("div");
		var childrendiv = {};
		itemdiv.className = "ygtvitem";	
		itemdiv.id = "ygtv" + this.index;
		_parent.appendChild(itemdiv);
		if(itemdata!=null)
		{
			if(itemdata.id === this.areaid)
			{
				this.selectedid = itemdiv.id;
			}
			itemdata.divid = itemdiv.id;			
			var nodetype = parseInt(itemdata.nodetype);
			var itemRow = this.addTableWithBodyRow(itemdiv,nodetype-1,null);
			if(nodetype > 1)
			{
				var depthCell = this.addDepthCell(itemRow);
			}
			var contentCell = this.addContentCell(itemRow,itemdata);
			var contentRow = this.addTableWithBodyRow(contentCell,nodetype,"nodetable" + itemdata.id);
			
			if(!itemdata.submenu)
			{
				var spacerCell = this.addSpacerCell(contentRow,itemdata);
			}
			else
			{
				var expandedToggleCell = this.addToggleCell(contentRow,itemdata,state);
			}
			var labelCell = this.addLabelCell(contentRow,itemdata);
			var labelLink = this.addLabelLink(labelCell,itemdata);	
			YAHOO.util.Event.addListener(itemdiv,"click",this.loadPage,itemdata,this);
		}
		childrendiv = this.addChildrenDiv(itemdiv,itemdata,state);
		return childrendiv;	
	};
	/**
	 * Adds a div to the parent container for every item in the menu. It returns the children div
	 * because that will be the parent container for the items at a lower depth in the hierarchy.
	 */
	this.addItem = function(_parent,itemdata,state)
	{
		var url, itemdiv;
		if(itemdata!=null)
		{
			url = itemdata.url;
		    if(itemdata.nodetype !== "4")
		    {
		    	if(url.indexOf("default.json")===-1)
		    	{
			    	itemdata.url = url + "/default.json";
		    	}
		   		itemdiv = this.addItemDiv(_parent,itemdata,state);    	
		    }
			else if(itemdata.nodetype==="4" && url.indexOf("default.json")===-1)
			{
		   		itemdiv = this.addItemDiv(_parent,itemdata,state);
			}
		}
		else
		{
		   		itemdiv = this.addItemDiv(_parent,itemdata,state);  			
		}
		this.index++;
		return itemdiv;
	};
	this.init = function(areaid)
	{
		this.areaid = areaid;
		this.root = this.addItem(container,null);	
	};

	this.toString = function()
	{
		return "YAHOO.si.Tree";
	}
	return this;
}

YAHOO.si.LeftMenu = function()
{
	var leftmenuboxid = "leftmenubox";
	var expanded = true;
	var collapsed = false;
	this.menuBox = {};
	this.tree = {};
	this.progress = YAHOO.si.Progress;
	this.init = function()
	{
		this.language = YAHOO.si.HistoryManager.language;
		this.areaid = YAHOO.si.HistoryManager.areaid;
		
		if(!YAHOO.util.Dom.inDocument(leftmenuboxid))
		{
			throw new Error("The element '" + leftmenuboxid + "' seems to missing from the document");
		}
		this.progress.show("Loading left menu...",true);
		var leftmenubox = document.getElementById(leftmenuboxid);
		this.menuBox = leftmenubox;////document.createElement("div");
		this.menuBox.innerHTML = null;
		this.menuBox.innerHTML = "";
		//this.menuBox.id = leftmenuboxid;
		//leftmenubox.appendChild(this.menuBox);
		this.tree = YAHOO.si.Tree(this.menuBox);
		this.tree.init(this.areaid);
	    var menudata = leftmenudata[this.language].menudata;
	    try
	    {
		    if(menudata.length > 0)
		    {
				this.walkTree(this.tree.root,menudata,expanded);
		    }
		    this.tree.focus(this.tree.selectedid);
		    this.progress.show("Left menu loaded.",true);
	    }
	    catch(e)
	    {
	    	e.contextMessage = "Check that the data file leftmenu.js has been included in the page.";
	    	YAHOO.si.handleErrors(e);
	    }
	    //for debugging:
	   // var viewer = document.getElementById("htmlviewer");
	    //viewer.value = this.menuBox.innerHTML;
	};
	this.walkTree = function (_parent,itemdata,expand,expandChildren)
	{
		//var item;		
	    for(var i = 0 ; i < itemdata.length ; i++)
	    {	
			var item = itemdata[i];
			var div;
			if(item.id!=this.areaid)
			{
				if(!expandChildren)
				{
					div = this.tree.addItem(_parent,item,collapsed);								
				}
				else
				{
			        if(item.submenu)
			        {      	
						div = this.tree.addItem(_parent,item,expanded);
			        	//pass true as the 4th argument to signal that all child nodes should be expanded
			            this.walkTree(div,item.submenu.itemdata,expanded,true);
			        }
			        else
			        {
						div = this.tree.addItem(_parent,item,collapsed);		        	
			        }	
				}				
			}
			else if(item.id===this.areaid )
			{
		        if(item.submenu)
		        {      	
					div = this.tree.addItem(_parent,item,expanded);
		        	//pass true as the 4th argument to signal that all child nodes should be expanded
		            this.walkTree(div,item.submenu.itemdata,expanded,true);
		        }
		        else
		        {
					div = this.tree.addItem(_parent,item,collapsed);		        	
		        }				
			}
	    }
	};
};
