//##########################################################################################################################
//method of menu_obj, makes menu visible
//##########################################################################################################################
menu_obj.prototype.show = function()
{
	var browserWidth_int;
	
	if(detect_obj.is_nav4)
		browserWidth_int = window.innerWidth;
	else
		browserWidth_int = document.body.offsetWidth;
	
	//position child layers depending on browser width
	if(this._parent)
	{
		if(browserWidth_int<this._parent._x+290)
			this._x = this._parent._x-130;
		else	
			this._x = this._parent._x+140;
	}		
	positionLayer(gLayer(this._name),"x",this._x)
	
	//show layer corresponding to menu
	showLayer(gLayer(this._name))
		
	//change visibility property
	this._visible = true;
}
//##########################################################################################################################

//##########################################################################################################################
//method of menu_obj, makes menu and all its open sublevels invisible
//##########################################################################################################################
menu_obj.prototype.hide = function()
{
	var i=0;
	
	//hide layer corresponding to menu
	hideLayer(gLayer(this._name))
	
	//close all the are open
	for(i=0;i<this._size;i++)
	{
		//if menu visible
		if(this.items[i]._hasChildren && this.items[i]._child._visible)
		{	
			//hide it
			this.items[i]._child.hide();
			break;
		}	
	}
	
	//turn off button background rollover 
	if(this._parent)
		off(this._parent.items[this._position]._cell)
		
	//change visibility property
	this._visible = false;
}
//##########################################################################################################################

//##########################################################################################################################
//construct html for item
//returns HTML string
//##########################################################################################################################
item_obj.prototype.generateHTML = function()
{
	
	offset_int = 0;
	
	var html_str = "";
	
	//layer extremities
	var layerTop_str;
	var layerBottom_str;
	
	//header 
	var header_str;
	
	//footer of table
	var footer_str;
	
	//rows that has button
	var buttonRow_str;
	
	//dotted line
	var dots_str;
	
	//width of table cells hosting button
	var cellWidth_int = this._width - 4;
	
	//with of single cell hosting button
	var buttonWidth_int = this._width -24;
	
	//layer definition
		if(detect_obj.is_nav4)
		{	
			
			if(this._hasChildren)
				layerTop_str = '<layer id="'+this._position+'" left="0" width="150" bgColor="#99ccff" z-index="'+this._parent._level+'" class="navtext" onmouseover="mouseOverButton(this,'+this._position+',\''+this._child._name+'\',\''+this._parent._name+'\')" onmouseout="Start()" onload="this.top = offset_int; offset_int += this.clip.height; this.parentLayer.clip.height = offset_int">';
			else
				layerTop_str = '<layer id="'+this._position+'" left="0" width="150" bgColor="#99ccff" z-index="'+this._parent._level+'" class="navtext" onmouseover="on(this); killOpenMenus(\''+this._parent._name+'\'); Stop(); Reset();" onmouseout="off(this); Start();" onload="this.top = offset_int; offset_int += this.clip.height; this.parentLayer.clip.height = offset_int">';
			
			//HEADER definition
			header_str = '<table width="'+this._width+'" height="23" cellpadding="0" cellspacing="0" border="0">';
		}	
		else
		{
			layerTop_str ='';
			header_str ='';
		}	
				
		//if it's the first item, build header
		if(this._position==0)
		{
			header_str +='<tr>' +
							'<td width="'+this._width+'" height="2" bgcolor="#336699" colspan="3"><img src="/images/spacer.gif" width="1" height="2" alt="" border="0" /></td>'+
						'</tr>';
		}				
		
		//BUTTON ROW definition
		buttonRow_str =	'<tr>'+
							'<td width="2" height="23" bgcolor="#336699"><img src="/images/spacer.gif" width="2" height="1" alt="" border="0"></td>'+
							'<td width="'+cellWidth_int+'" height="23">';
		
		if(this._hasChildren)
			buttonRow_str+=		'<table cellspacing="0" width="'+cellWidth_int+'" cellpadding="0" border="0" onmouseover="mouseOverButton(this,'+this._position+',\''+this._child._name+'\',\''+this._parent._name+'\',event)" onmouseout="mouseOutButton(this,event)" onclick="javascript:window.location=\''+this._link+'\'">'
		else
			buttonRow_str+=		'<table cellspacing="0" width="'+cellWidth_int+'" cellpadding="0" border="0" onmouseover="mouseOverButtonNoChildren(this,\''+this._parent._name+'\',event)" onmouseout="mouseOutButtonNoChildren(this,event)" onclick="javascript:window.location=\''+this._link+'\'">'
			
			buttonRow_str +=		'<tr>'+
										'<td width="10" height="23"><img src="/images/spacer.gif" width="10" height="1" alt="" border="0" /></td>'+
										'<td width="'+buttonWidth_int+'" height="23" class="navtext"><a href="'+this._link+'">'+this._button+'</a></td>'+
										'<td width="10" height="23"><img src="/images/spacer.gif" width="10" height="1" alt="" border="0" /></td>'+
									'</tr>'+
								'</table></td>'+
							'<td width="2" height="23" bgcolor="#336699"><img src="/images/spacer.gif" width="2" height="1" alt="" border="0"></td>'+
						'</tr>';
		
		//DOTTED http://www.cnn.comLINE
		
		//if it is the last item, build footer
		if(this._position!=this._parent._size-1)
		{
			dots_str = '<tr>'+
							'<td width="2" height="3" bgcolor="#336699"><img src="/images/spacer.gif" width="1" height="1" alt="" border="0"></td>'+
							'<td width="'+cellWidth_int+'" height="3" background="/images/dot.gif"><img src="/images/spacer.gif" width="1" height="1" alt="" border="0" /></td>'+
							'<td width="2" height="3" bgcolor="#336699"><img src="/images/spacer.gif" width="1" height="1" alt="" border="0"></td>'+
						'</tr>';
						
			footer_str = "";			
		}			
		else
		{
			dots_str = "";
		//FOOTER
			footer_str = '<tr>'+
							'<td width="'+this._width+'" height="2" bgcolor="#336699" colspan="3"><img src="/images/spacer.gif" width="1" height="2" alt="" border="0" /></td>'+
						'</tr>'+
						'<tr>'+
							'<td width="2" height="9" bgcolor="#336699"><img src="/images/spacer.gif" width="1" height="1" alt="" border="0"></td>'+
							'<td width="'+cellWidth_int+'" height="9" bgcolor="#336699"><img src="/images/spacer.gif" width="1" height="1" alt="" border="0"></td>'+
							'<td width="2" height="9" bgcolor="#336699"><img src="/images/spacer.gif" width="1" height="1" alt="" border="0"></td>'+
						'</tr>'+
						'<tr>'+
							'<td width="'+this._width+'" height="2" bgcolor="#336699" colspan="3"><img src="/images/spacer.gif" width="1" height="2" alt="" border="0" /></td>'+
						'</tr>';
		}			
			
	
		//LAYER BOTTOM
		if(detect_obj.is_nav4)
			layerBottom_str = '</table></layer>';
		else	
			layerBottom_str = '';
						
		html_str = layerTop_str + header_str + buttonRow_str + dots_str + footer_str + layerBottom_str;
		
		return(html_str);
}
//##########################################################################################################################


//##########################################################################################################################
//add item to menu
//position_int - position of item object inside menu - int
//##########################################################################################################################
menu_obj.prototype.addItem = function(position_int)
{
		 
	//position in menu
	this.items[position_int]._position = position_int;
	
	//width
	this.items[position_int]._width = this._width;
	
	//link corresponding to item
	this.items[position_int]._link = this._links[position_int];
	
	//button corresponding to item
	this.items[position_int]._button = this._buttons[position_int];
	
	//parent object
	this.items[position_int]._parent = this;
	
	//create html
	this.items[position_int]._html = this.items[position_int].generateHTML();
	
	//return html
	return(this.items[position_int]._html);
}
//##########################################################################################################################

//##########################################################################################################################
//constructor method for popup menu object
//##########################################################################################################################
menu_obj.prototype.buildMenu = function()
{
		
	//size
	this._size = this._links.length;
	
	//counter
	var i=0;
	
	//final html string
	var strHTML = "";
	
	for(i=0;i<this._size;i++)
		strHTML += this.addItem(i);		
		
	//write HTML code to the window object for display
	//Netscape 4 uses <layer> tags, the rest uses <div> tags
	if(detect_obj.is_nav4)
		document.write('<layer name="'+this._name+'" top="'+this._y+'" left="0" z-index="'+this._level+'" width="'+this._widh+'" visibility="hidden">'
						+strHTML
					  +'</layer>');
	else
		document.write('<div id="'+this._name+'" width="'+this._widh+'" onmouseover="//overLayerIE(this,event.relatedTarget)" onmouseout="//outLayerIE(this,event.relatedTarget)" style="position:absolute; top:'+this._y+'; left: -'+this._width+'; visibility:hidden; z-index:'+this._level+'; background:#99ccff;">'+
							'<table width="'+this._width+'" cellpadding="0" cellspacing="0" border="0">'+
								strHTML+
							'</table></div>');
}
//##########################################################################################################################


//##########################################################################################################################
//main function, generates one menu object structured after the XML model
//##########################################################################################################################
menu_obj.prototype.generateMenu =  function()
{
	
	//create instances of the item_obj object, one for each button
	var i;
					
	for(i=0;i<this._links.length;i++)
	{
		
		//create instance item object
		this.items[i] = new item_obj();
		//position it
		this.items[i]._position = i;
					
		//if current item has submenus
		if(this._hasChildren[i])
		{
			this.items[i]._hasChildren = true;
			
			//create a new instance of menu object, using the name of the current menu as the starting base
			eval("m"+this._name + "L" +i+" = new menu_obj()");
			
			//assign new instance to child branch of current item
			this.items[i]._child = eval("m"+this._name + "L" +i);
			
			//set position
			this.items[i]._child._position = i;
			
			//assign current instance to the parent branch of child
			this.items[i]._child._parent = this;
			
			//set name property of child object
			this.items[i]._child._name = this._name+ "L" +i;
		}	
	}
	
	
	//call menu_obj method that builds the menu (creates and displays HTML code)
	this.buildMenu();			
}