//##########################################################################################################################
//method of menu_obj, makes menu visible 
//##########################################################################################################################
menu_obj.prototype.show = function()
{
	var browserWidth_int;
	
	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,this._background)
		
	//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
		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="' + this._bordercolor + '" colspan="3"><img src="/PublishingImages/spacer.gif" width="1" height="2" alt="" border="0" /></td>'+
						'</tr>';
		}				
		
		//BUTTON ROW definition
		buttonRow_str =	'<tr>'+
							'<td width="2" height="23" bgcolor="' + this._bordercolor + '"><img src="/PublishingImages/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="100%" 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="100%" cellpadding="0" border="0" onmouseover="mouseOverButtonNoChildren(this,\''+this._parent._name+'\',event);" onmouseout="mouseOutButtonNoChildren(this,event,\''+this._background+'\')" onclick="javascript:window.location=\''+this._link+'\'">'
									
			buttonRow_str +=		'<tr>'+
										'<td width="10" height="23"><img src="../PublishingImages/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="../PublishingImages/spacer.gif" width="10" height="1" alt="" border="0" /></td>'+
									'</tr>'+
								'</table></td>'+
							'<td width="2" height="23" bgcolor="' + this._bordercolor + '"><img src="../PublishingImages/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="2" bgcolor="' + this._bordercolor + '"><img src="../PublishingImages/spacer.gif" width="1" height="1" alt="" border="0"></td>'+
							'<td width="'+cellWidth_int+'" height="2" bgcolor="' + this._bordercolor + '"><img src="../PublishingImages/spacer.gif" width="1" height="1" alt="" border="0" /></td>'+
							'<td width="2" height="2" bgcolor="' + this._bordercolor + '"><img src="../PublishingImages/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="4" bgcolor="' + this._bordercolor + '" colspan="3"><img src="../PublishingImages/spacer.gif" width="1" height="2" alt="" border="0" /></td>'+
						'</tr>';
		}			
			
	
		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;
	
	
	//style
	//style options
	//main background
	this.items[position_int]._background = this._background;
	
	//main background, on state
	this.items[position_int]._background_on = this._background_on;
	
	//border color
	this.items[position_int]._bordercolor = this._bordercolor;
	
	//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
	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+'+px; left: -'+this._width+'px; visibility:hidden; z-index:'+this._level+'; background:'+ this._background +';">'+
							'<table width="'+this._width+'" cellpadding="0" cellspacing="0" border="0">'+
								strHTML+
							'</table></div>');
	
	positionLayer(gLayer(this._name),"y",this._y);					
}
//##########################################################################################################################


//##########################################################################################################################
//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;
			
			//style
			//main background
			this.items[i]._child._background = this._background;
	
			//main background, on state
			this.items[i]._child._background_on = this._background_on;
			
			//border color
			this.items[i]._child._bordercolor = this._bordercolor;
		}	
	}
	
	
	//call menu_obj method that builds the menu (creates and displays HTML code)
	this.buildMenu();			
}


//patch for MAC IE - push
if(typeof Array.prototype.push=='undefined')
  Array.prototype.push=function(){
    var i=0;
    b=this.length,a=arguments;
    for(i;i<a.length;i++)this[b+i]=a[i];
    return this.length
  };


//method to accomodate CMS300
menu_obj.prototype.addMenuItem = function(buttonStr,linkStr,position,isMenu)
{ 

  
  if(isMenu==true)
  	this._hasChildren.push(true);
  else
 	this._hasChildren.push(false);  
  
  this._links.push(linkStr);
  this._buttons.push(buttonStr);
  
}
