var open_items = new Array();
var open_items_time = new Array();
var tallestElement = 0;

function clearTime()
{
		//var rem = open_items_time.pop();
		//clearTimeout(rem);
	//for (var i = 0; i < open_items.length -1; i++)
	//{
		
		//var _id = open_items.pop();
		//if (_id != this.id && _id.length != 6)
		//{
		//document.getElementById(_id).lastChild.style.display="none";
		//}
	//}
}

function timeout(_id)
{
	

	
	if(_id.length == 6)
	{
		document.getElementById(_id).lastChild.style.display="none";
	}
	else{
		document.getElementById(_id).lastChild.style.display="block";
	}
	document.getElementById(_id).lastChild.style.display="none";
	
	//eval("var t = setTimeout(\"document.getElementById('"+_id+"').lastChild.style.display='none'\",1000)");
	//open_items.push(_id);
	//open_items_time.push(t);
	
				   document.getElementById(_id).className=document.getElementById(_id).className.replace("_over", "");
				   if(document.getElementById(_id).id.charAt(3) != "")
				   {
				   		if(document.getElementById(_id).id.charAt(3) == strLev1.charAt(3))
						{
							document.getElementById("imgnav"+document.getElementById(_id).id.charAt(3)).src =  navArrayTop[document.getElementById(_id).id.charAt(3)][2]
						}
						else
						{
							document.getElementById("imgnav"+document.getElementById(_id).id.charAt(3)).src =  navArrayTop[document.getElementById(_id).id.charAt(3)][0]
						}
				   	//document.getElementById("imgnav"+this.id.charAt(3)).src =  navArrayTop[this.id.charAt(3)][0]
				   	//alert("imgnav"+this.id.charAt(3));
				   }
}


var time;
var todo;



//get browser version
var bName = navigator.appName;
var bVer = parseInt(navigator.appVersion);

//get and parse the locator for the top nav images

var strLev1 = "";

if (_curPage.indexOf("nav") != -1 && _curPage.length == 4)
{
	strLev1 = _curPage;
}
else if(_curPage.indexOf("nav") != -1 && _curPage.length == 6)
{
	strLev1 = _curPage.substring(0,4);
}
else
{
	strLev1 = "nav" + _curPage.substring(3,4);
}

//adjust the height of the nav for ie
if(bName == "Microsoft Internet Explorer"){
	document.getElementById("trans").style.height="300";
	document.getElementById("trans").style.width="709";
}
else{
	document.getElementById("trans").style.height="26";
	document.getElementById("trans").style.width="709";
}
activateMenu = function(nav)  {
	
	
    /* currentStyle restricts the Javascript to IE only */
	//if (document.all && document.getElementById(nav).currentStyle) {  
       var navroot = document.getElementById(nav);
        
		/* Get all the list items within the menu */
		var lis=navroot.getElementsByTagName("LI");  
        for (i=0; i<lis.length; i++) {
        
           /* If the LI has another menu level */
            if(lis[i].lastChild.tagName=="UL"){

                /* assign the function to the LI */
             	lis[i].onmouseover=function() {	
					hideSelectBoxes();
					clearTime();
				   this.lastChild.style.display="block";
				   this.className+="_over";
				   
				   
				   if(this.id.charAt(3) != "")
				   {
				   	document.getElementById("imgnav"+this.id.charAt(3)).src =  navArrayTop[this.id.charAt(3)][1]

				   }
				   
				   
				   if(this.hasChildNodes()){
				   		var length = this.childNodes.length;
						for (x=0; x<this.childNodes.length; x++) {
						node = this.childNodes[x];
						// the tallest element so that we can calculate the maximum depth on the page
								// that we must hide any select boxes
						
								if((node.offsetHeight + node.offsetTop) > tallestElement)
								{
									
									tallestElement = node.offsetHeight + node.offsetTop;
									
								}
						//alert(tallestElement);
						
						
						if (node.nodeName=="UL") {
							for(y=0; y<node.childNodes.length; y++)
							{
								node2 = node.childNodes[y];
								var _posa = parseInt(node2.id.charAt(5));
								
								
								if(node2 != null && node2.nodeName=="UL"){
								}
								
								if(node2 != null && node2.nodeName=="LI"){
									if(node2.className.indexOf('_over')==-1){
										if(node2.id.indexOf('lev')==-1)
										{
										}
										else{
											var _pos = parseInt(node2.id.charAt(5));
											node.style.height=(_pos+1)*30+"px";
											node2.className+="_over";
										}
									}
								}
								
								
							}
						}
					else{}
					
					}
					
					}
				  
                }				
				lis[i].onmouseout=function() {   
				showSelectBoxes();
			  	timeout(this.id);
                }
            }
  }
	
	
}


function bc_out(obj)
{
	//change the top image to the rollover state
	document.getElementById("imgnav"+obj.substring(3,4)).src = navArrayTop[obj.substring(3,4)][0]
	
	var n=document.getElementById(obj);
	if(n)
	{
		if(n.className.indexOf('_over')==-1){

		}
		else{
			n.className=n.className.replace("_over", "");
		}
	
	}
	
}


function bc(obj)
{
	//change the top image to the rollover state
	document.getElementById("imgnav"+obj.substring(3,4)).src = navArrayTop[obj.substring(3,4)][1]
	
	
	var n=document.getElementById(obj);

	var len = obj.length;
	var _pos = obj.charAt(len-1);
			
	if(n != null && n.hasChildNodes()){
	
	
	for (x=0; x<n.childNodes.length; x++) {

		node = n.childNodes[x];

		if (node.nodeName=="UL") {
			
			for(y=0; y<node.childNodes.length; y++)
			{
				node2 = node.childNodes[y];
				if(node2.nodeName=="LI"){
					if(node2.className.indexOf('_over')==-1){
						node2.className+="_over";		
					}
				}
			}
		}
	}
		
		
	}

		
	if(n)
	{
		if(n.className.indexOf('_over')==-1){
			n.className+="_over";
		}
	}
}




//create the beginning ul
var _ul = document.createElement('ul');
_ul.setAttribute("id","nav");
_ul.style.whiteSpace="nowrap";
var totalWidth = 0;
document.getElementById("trans").appendChild(_ul);

for (var i=1; i < navArray.length; i++)
{
	var _id = navArray[i][0];
	if(_id.length == 4)
	{
		var _li = document.createElement('li');
		_li.setAttribute("id",_id);
		_li.className="sub";
		
		if(bName == "Microsoft Internet Explorer"){
		//_ul.style.top="26px";
		//_ul.style.left="0px";
		}
		else{
			_li.style.top="0px";
		}
		
		var _img = document.createElement('img');
		var text = navArray[i][2];
		//alert(_id + "  " + strLev1);
		if(_id == strLev1)
		{
			_img.src=navArrayTop[_id.substring(3,4)][2];
		}
		else
		{
			_img.src=navArrayTop[_id.substring(3,4)][0];
		}
		_img.alt=text;

		_img.setAttribute("id","img"+_id);
		_img.onmouseover = function() {
			hideSelectBoxes();
			change_img_src(this,"over");
			clearTime();
			}
		var w = navArrayTop[i][3];

		totalWidth = totalWidth + w;
		//alert(totalWidth);
		_img.onmouseout = function() {change_img_src(this,"out")}
		_img.style.border="0";
		_img.style.padding="0";
		_img.style.margin="0";
		_a = document.createElement("a");
		_a.href = navArray[i][1];
		_a.style.padding="0";
		_a.style.margin="0";
		_li.style.margin="0";
		_li.style.width="100px"
		_li.style.padding="0";
		_li.style.display="block";
		//_li.style.width=w+"px";
		_li.style.position="absolute";
		_li.style.left=totalWidth - w + "px";
		_a.appendChild(_img);
		_li.appendChild(_a);
		document.getElementById("nav").appendChild(_li);
		

		var _ul = document.createElement('ul');
		if(bName == "Microsoft Internet Explorer"){
		_ul.style.top="22px";
		_ul.style.left="0px";
		}
		else{
		_ul.style.top="22px";
		}
		
		_ul.setAttribute("id","ul"+_id);
		_ul.id="ul"+_id;
		//_ul.style.width="25px";
		document.getElementById(_id).appendChild(_ul);
		//change_img_src(_ul.id,"over");
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
		if(navArray[i][3].indexOf("dropDiv")!= -1)
		{
		if(document.location.href.indexOf("corporate") == -1 && document.location.href.indexOf("phoenix") == -1){
		var initial = true;
		var _li = document.createElement('li');
		_li.setAttribute("id",_id2);
		_li.style.position="absolute";
		_li.style.top="0px";
		_li.style.left= "0px";
		_li.style.padding="0";
		_li.style.margin="0";
		_li.style.display="block";
		_li.style.zIndex="10";
		

				
		var _id4Div = navArray[i][0];
		//alert(_id4Div);
		var div_id = navArray[i][3].substring(8,12);
		var numPerRow = eval(div_id+"ItemPerRow");
		var nameOfArray = eval("navArray_"+div_id);
		var numOfItems = nameOfArray.length;
		var numRows = numOfItems / numPerRow;
		var numRowsMod = numOfItems%numPerRow;
		
		if(numRowsMod != 0)
		{
			numRows = numRows + 1;
		}
		//alert(numOfItems);
		var _div = document.createElement('div');
		_div.style.display="block";
		//_div.style.position="absolute";
		_div.style.top="0px";
		_div.style.left="0px";
		_div.style.borderLeft="solid 1px #BD3431";
		_div.style.zIndex="11";
		_div.style.lineHeight="10px";
		//_div.style.backgroundColor="#4D4D4D";
		//_div.style.paddingBottom="15px";
		//_div.style.paddingTop="15px";
		
		var _table = document.createElement('table');
		_table.style.border="0px solid #000000";
		_table.style.margin="0";
		_table.style.padding="0";
		_table.style.backgroundColor="#4D4D4D";
		_table.style.position="relative";
		_table.style.width="870px";
		
		var _tbody = document.createElement('tbody');
		var _tr = document.createElement('tr');
		var _td = document.createElement('td');
			_td.style.width="90px";
			_td.rowSpan=numRows;
			_td.style.verticalAlign="top";
			_td.style.lineHeight="15px";
			_td.style.height="15px";
			
		var _img = document.createElement('img');
		_img.src = baseURLimages + "/images/consumer/nav/brands/cag_crop_logo.gif";
		_img.style.height="84px";
		_img.style.width="84px";
		_img.id="prod_img";
		_img.name="prod_img";
		_td.valign="top";
		_td.appendChild(_img);
		_tr.appendChild(_td);
		_tbody.appendChild(_tr);
			
		var _current = 0;
		for(var z = 0; z < numOfItems; z++)
		{
			if (_current == 0 && initial == false)
			{
				var _tr = document.createElement('tr');
				_tr.style.lineHeight="12px";
			}
			var _td = document.createElement('td');
			_td.className="prodDrop";
			var _a = document.createElement('a');
			_a.href = nameOfArray[z][1];
			var text = document.createTextNode( nameOfArray[z][0]);
			//_a.style.fontWeight="bold";\
			_a.style.fontSize="10px";
			_a.style.fontWeight="bold";
			_a.style.padding="0px";
			_a.style.margin="0px";
			_a.appendChild(text);
			_td.appendChild(_a);
			_td.id = "prod_"+z;
			_td.setAttribute("id","prod_"+z);
			_td.style.padding="0px";
			_td.style.margin="0px";
			_td.style.whiteSpace="nowrap";
			_td.style.width="240px";
			_td.style.height="15px";
			_td.style.lineHeight="15px";
			_td.onmouseover = function() {
				clearTime();
				this.className="prodDrop_over";
				var img_loc = this.id.substring(5,7);
				document.getElementById("prod_img").src = nameOfArray[img_loc][2];
				
			}
			_td.onmouseout = function() {
				
				this.className="prodDrop";
				document.getElementById("prod_img").src = baseURLimages+"/images/consumer/nav/brands/cag_crop_logo.gif";
				
			}
			_tr.appendChild(_td);
			if(_current == numPerRow - 1 || z == numOfItems - 1)
			{
				_tbody.appendChild(_tr);
				_current = 0;
			}
			else{
				_current = _current + 1;
			}
			initial = false;
		}
		
		_table.appendChild(_tbody);
		_div.appendChild(_table);
		_li.appendChild(_div);
		document.getElementById("ul"+_id).appendChild(_li);
		}
	}
		else
		{		
			var count = 0;
			for (var x = 1; x < navArray.length; x++)
			{
			
			var _id2 = navArray[x][0];
			if(_id2.length == 6 && _id2.indexOf(_id)!= -1)
			{
				count = count + 1;
				var _li = document.createElement('li');
				_li.setAttribute("id",_id2);
				var text = document.createTextNode(navArray[x][2]);
				var a = document.createElement("a");
				a.href = navArray[x][1];
				a.style.fontWeight="bold";
				a.style.textTransform="uppercase";
				a.appendChild(text);
				_li.onmouseover = function() {
					hideSelectBoxes();
					clearTime();
					bc(this.id);
					this.className='sub1_over';
					}
				_li.onmouseout = function() {
					showSelectBoxes();
					bc_out(this.id);
					this.className='sub1';
					}
				_li.className="sub1";
				_li.appendChild(a);
				document.getElementById("ul"+_id).appendChild(_li);
				if(navArray[x][3] == "parent")
				{
						var _ul = document.createElement('ul');
						_ul.className="sub3";
						_ul.style.position = "absolute";
						_ul.style.whiteSpace="normal";
						_ul.style.width="275px";
						
						if(_id2.substring(5,6) > 0)
						{
							_ul.style.top = "-" + (count-1) * 16 + "px";
						}
						else{
							_ul.style.top = "0px";
						}
						if(bName == "Microsoft Internet Explorer"){
						_ul.style.height = ((count-1) * 23) - ((count-1) * 1) + "px";
						}
						else
						{
						_ul.style.height = ((count-1) * 23) - ((count-1) * 1) + "px";
						}
						_ul.setAttribute("id","_ul"+_id2);
						document.getElementById(_id2).appendChild(_ul);
							
					for (var y = 1; y < navArray.length; y++)
					{
						var _id3 = navArray[y][0];
						if(_id3.length == 8 && _id3.indexOf("lev"+_id2.substring(3,8))!= -1)
						{
							var _li = document.createElement('li');
							_li.className = "sub3";
							_li.style.width="275px";
							_li.setAttribute("id","_id3");
							_li.onmouseover = function() {
								hideSelectBoxes();
								this.className='sub3_over2'
								//alert(this.offsetTop);
								clearTime();
								}
							_li.onmouseout = function() {this.className='sub3'}
							var _text = document.createTextNode(navArray[y][2]);
							var a = document.createElement("a");
							a.href = navArray[y][1];
							a.style.fontWeight="bold";
							a.style.width="250px";
							a.style.textTransform="uppercase";
							a.appendChild(_text);
							_li.appendChild(a);
							if(navArray[y][4]=="show")
							{
							document.getElementById("_ul"+_id2).appendChild(_li);
							}
						}
					}
				}
			}
		}
	}
}
}
	//change the top nav image 
	//what it gets changed to depends on the action that is passed to it
function change_img_src(obj,what){

	if(what == "over"){
		hideSelectBoxes();
		obj.src = navArrayTop[obj.id.substring(6,7)][1];
	}
	if(what == "out"){
		obj.src = navArrayTop[obj.id.substring(6,7)][0];
	}

}

//hide the select boxes that would show under the dropdowns for ie
function hideSelectBoxes()
{
	if(bName == "Microsoft Internet Explorer"){

			var ele = document.getElementById("Brands");
			//alert(ele);
			if(ele != null)
			{
			if(document.all.Brands!=null)
			{
			document.all.Brands.style.visibility="hidden";
			}
			}
			
			var ele2 = document.getElementById("fms_8");
			if(ele2 != null)
			{
			document.all.fms_8.style.visibility="hidden";
			}
	}
	document.getElementById("trans").style.width="1000";
	
}

function showSelectBoxes()
{
	if(bName == "Microsoft Internet Explorer"){
			var ele = document.getElementById("Brands")
			if(ele != null)
			{
				if(document.all.Brands!=null)
			{
			document.all.Brands.style.visibility="";
			}
			}
			
			var ele2 = document.getElementById("fms_8");
			if(ele2 != null)
			{
			document.all.fms_8.style.visibility="";
			}

	}
	document.getElementById("trans").style.width="709";

}


