/*------------------------------------------------------------*/ 
/*   LoadNavs_M4.js     Jason Pryde     5/9/2008             */
/*   Functions to dynamically populate navbars              */
/*------------------------------------------------------------*/ 
 


var LI_expand = "";
var LI_shrink = "";  
 
 
 
function BuildMenu(MenuArray)
/*-- Loop thru array of topnav menu items. Template file Innovations_FlatTop.tpl creates JS array and calls this function. 
------*/
{
 var TopNavContenttext="";
 var i,j=0;
 var newNodeCmds;

 TopNavContenttext ="<UL class='navlist' id='nav'>";

 for (i=0; i<MenuArray.length; i++) 
 {
	newNode = MenuArray[i];	
	i=i+1;
	newNodeURL=MenuArray[i];
  
 TopNavContenttext=AddTopNavItem(TopNavContenttext,newNode,newNodeURL);



 }  /*--end loop thru MenuArray   --*/
TopNavContenttext +="</UL>";






/*-- render navbar --*/
TopNavContent=document.getElementById('M4_TopNav');
TopNavContent.innerHTML=TopNavContenttext;


/*----- Add Event Handlers to topnav items & Dropdown divs ----*/

t3=setTimeout("AddEventHandlers()",200);

t4=setTimeout("ShrinkFirst(null)",300);
 
} /*----end function BuildMenus ----*/





/*---------- Create Dropdown menus --------*/
/*----- Creates all dropdown menus from array of options, urls as created in template file innovations_flat_top_dropdown.tpl.  Callled 
from innovations_flat_top.tpl  -----*/
function BuildDropDowns(SubMenuArray)
{
var Dropdowncontent="";
var currNodeTopNavItem;
var newNodeName, newNodeURL="";
var LI_class="";
var currNodeTopNavNbr, newNodeTopNavNbr="";
var i;


for (i=0; i<SubMenuArray.length; i++) 
{	
	newNode = SubMenuArray[i];	
	newNodeURL=newNode.url;
	newNodeName=newNode.name;
	newNodeTopNavNbr=newNode.topnavitem;
	newNodeUL="UL_"+newNodeTopNavNbr;

 /*--First item in array inits first dropdown --*/
 if (i==0) 
 { 
	Dropdowncontent= CreateNewDropDownMenu(newNodeUL);
	currNodeTopNavItem = newNodeTopNavNbr;
 }


/*-- if new topnav item, close, start new dropdown menu  --*/
 if (currNodeTopNavItem != newNodeTopNavNbr)
 {
  	CloseExistingDropDownMenu(Dropdowncontent,currNodeTopNavItem);
	Dropdowncontent= CreateNewDropDownMenu(newNodeUL);
  	currNodeTopNavItem = newNodeTopNavNbr;
 }

Dropdowncontent=AddDropDownItem(Dropdowncontent,newNodeName,newNodeURL) 

}/*-- End Loop thru SubMenuArray--*/  



CloseExistingDropDownMenu(Dropdowncontent,currNodeTopNavItem);
		
 
return;

}


/*----- Some internal utility functions for building menus ---*/

CreateNewDropDownMenu = function(newNodeURL)
{
	Dropdowncontenttext="<UL id='" + newNodeUL + "' class ='DropdownMenuUL'>";
 	return Dropdowncontenttext;
}




CloseExistingDropDownMenu= function(Dropdowncontent,currNodeTopNavItem)
{
	var DropdownUL="Dropdown_"+currNodeTopNavItem;
	Dropdowncontent+= "</UL>";	
	DropDownMenu=document.getElementById(DropdownUL);
	
	toggle(DropdownUL);	
	
	DropDownMenu.innerHTML=Dropdowncontent;
}



AddDropDownItem = function(Dropdowncontent,newNodeName,newNodeURL)
{
	var LI_Id="'LI_"+newNodeName+"'";
	Dropdowncontent +="<li id="+ LI_Id +" class=DropdownMenuLI1 >";
	Dropdowncontent +="<a href=" +  newNodeURL +  ">" + newNodeName + "</a>";
	Dropdowncontent +="</li>";
	return Dropdowncontent;
	
}




function AddTopNavItem(TopNavContenttext,TopNavContentname,TopNavContenturl) 
{ 
var LI_class;
var LI_Length=TopNavContentname.length;
var A_Length=TopNavContenturl.length; 
 
 
if (LI_Length<11)
 { LI_class="ShortItem";}
else if  (LI_Length>25)
 {LI_class="LongItem";}
else
 {LI_class="MedItem";}

var A_Id="A_"+TopNavContentname;
TopNavContenttext +="<li id='"+TopNavContentname + "' class='" + LI_class +"'>";

if(A_Length >2) 
{TopNavContenttext += "<a id='"+ A_Id +"' href=" +  TopNavContenturl + " >"  ; }
else
{ TopNavContenttext += "<div class='topnavdivitem' id='"+ A_Id +"'>"; }


TopNavContenttext +=  TopNavContentname 

if(A_Length >2) 
{ TopNavContenttext += "</a>"; }
else
{ TopNavContenttext += "</div>"; }
 
TopNavContenttext +="</li>";


return TopNavContenttext;
}

/*---- End of Internal build menu utility functions ---------*/




/*------  Operational functions for activating dropdown menus----*/
/*---- invoked thru user actions generally.  -----*/
/*-- Note node 833 has no drop down menu, just a direct link ---*/

function ShrinkAll(e,except)
{        


if(except != "UL_836") ShrinkMenu(e,-1,-1,"UL_836");  
if(except != "UL_830") ShrinkMenu(e,-1,-1,"UL_830");
if(except != "UL_832") ShrinkMenu(e,-1,-1,"UL_832");
/*-- if(except != "UL_833") ShrinkMenu(e,-1,-1,"UL_833");	 --*/
}


function ExpandAll(e)
{

ExpandMenu(e,1,1,"Dropdown_836");
ExpandMenu(e,1,1,"Dropdown_830");
ExpandMenu(e,1,1,"Dropdown_832");
/*-- ExpandMenu(e,1,1,"Dropdown_833"); --*/

}

function ShrinkFirst(e)
{
var eUL=document.getElementById("UL_836");
eUL.style.width = "0px";
eUL.style.height = "0px";
var eUL=document.getElementById("UL_830");
eUL.style.width = "0px";
eUL.style.height = "0px";
var eUL=document.getElementById("UL_832");
eUL.style.width = "0px";
eUL.style.height = "0px";
 
toggle("Dropdown_836");
toggle("Dropdown_830");
toggle("Dropdown_832");
/*--toggle("Dropdown_833");  --*/

}


 
/*-- ---------------Shrink Dropdown Menu --------- --*/
function ShrinkMenu(e,shrink_x,shrink_y,DropdownMenu)
{
var qx=20; 
var qy=20;
var e1=document.getElementById(DropdownMenu);

if (!e1) return;	
	
	if (shrink_x==-1)
	{   if (!e) var e = window.event;
		if (document.all && e.srcElement)
		  	{ var node=e.srcElement; }
		else
			{ var node=e.target; }	
		if(node)
		{
		var tag_src=node.tagName; 
		var tag_id=node.id;
		if (  tag_src == "A" ) return;
        if (  tag_src == "LI" && tag_id=="" ) return;
		}
	}
 
	var curr_x= e1.offsetWidth;
	var curr_y= e1.offsetHeight;
 
/*--alert("Shrinkmenu: "+DropdownMenu + "\n e1.nodeName:"+e1.nodeName+ "\n curr_x:" + curr_x + "\n curr_Y:" + curr_y  );  --*/


 	if(curr_x==0 || curr_y==0) return;
 
	var new_x=curr_x-qx;	
	var new_y=curr_y-qy; 
	


	if (new_x<2||new_y<2) {new_x=0; new_y=0;};
	e1.style.width = new_x + 'px'; 
   	e1.style.height = new_y + 'px';   
	
	

 

	if (new_x>0 || new_y>0)
    {
		var cmdstr= "ShrinkMenu('" + e + "'," + new_x + "," + new_y + ",'" + DropdownMenu + "')";

		var timer=35;
		var t=setTimeout(cmdstr,timer);
	}

	else
	{
/*--alert(DropdownMenu+" Ending Shrink x:"+x+"\n y:"+y); --*/
	if(t) clearTimeout(t);
	return;
	}


} /*-- end ShrinkMenu --*/




/*--------------Expand Dropdown-----------*/
function ExpandMenu(e,x,y,DropdownMenu)
{
var qx=12; 
var qy=15; 
var maxx;
var maxy;
var tag_node;
var tag_src;
var e1=document.getElementById(DropdownMenu);



var DDM_Word= DropdownMenu.substr(DropdownMenu.indexOf("_")+1,3);
 

switch(DDM_Word)
{
case "836":
  maxy = 75;
  maxx = 180;
  break;    
case "830":
  maxy = 75;
  maxx = 235;
  break;
case "832":
  maxy = 55;
  maxx = 200;
  break;
case "833":
  maxy = 75;
  maxx = 200;
  break;
default:
  maxy = 115;
  maxx = 200;
}


/*--alert(e+"-----Expandmenu: "+DropdownMenu + "\n e1.nodeName:"+e1.nodeName + "y:" + y + "maxy:" + maxy  );  --*/

 

if(x>maxx) return;

if (x==-1)
	{	ShrinkAll(e,DropdownMenu);
		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();

		if (document.all && e.srcElement)
		  	{ var node=e.srcElement;  	}
		else
			{ var node=e.target; }
		
		tag_src=node.tagName;
		tag_id=node.id;
 		
/*--alert(DropdownMenu+"--Expandmenu tag_src:"+tag_src + "\n tag_id:"+tag_id);   --*/
 
 
 
  
	if (  tag_src == "LI" && tag_id=="" ) return;
		if (  tag_src == "A" ) return; 

	
		x=e1.offsetWidth;
		y=e1.offsetHeight;	
	}
  



var new_x=x+qx; 
var new_y=y+qy;



if (x<maxx || y<maxy)
    {
		if (x<maxx) e1.style.width = new_x + 'px';
		
		
		if (y<maxy) e1.style.height = new_y + 'px';	

		var Timer2=15 ;		
		var cmdstr= "ExpandMenu('" + e + "'," + new_x + "," + new_y + ",'" + DropdownMenu + "')";
		var t2=setTimeout(cmdstr,Timer2);
	}
else
{
	if(t2) clearTimeout(t2);
}

/*---- var cmdstr5= "ShrinkMenu('" + e + "'," + -1 + "," + -1 + ",'" + DropdownMenu + "')";
if(t5) clearTimeout(t5);	
var t5=setTimeout(cmdstr5,20000);
---*/


 

} /*-- end ExpandMenu --*/







function TopNavCmds(topnavitem_ID,LI_shrink ,LI_expand ) 
{
	this.topnavitem_ID = topnavitem_ID;
	this.LI_shrink = LI_shrink;
	this.LI_expand = LI_expand;
}


 function URL( url, name, topnavitem_ID ) 
{
	this.url = url;
	this.name = name;
	this.topnavitem=topnavitem_ID;
}



function AddEventHandlers() 
{

/*----  mouseover for topnav LI elements ----*/
var el_nav836=document.getElementById("About Steve Goldsmith");
el_nav836.onmouseover= function(event){ExpandMenu(event,-1,-1,"UL_836")};


var el_nav830=document.getElementById("Resources for Mayors & Local Officials");
el_nav830.onmouseover =function(event){ExpandMenu(event,-1,-1,"UL_830")};



var el_nav832=document.getElementById("Tools");
el_nav832.onmouseover =function(event){ExpandMenu(event,-1,-1,"UL_832")};

 
var BannerDiv=document.getElementById("Mgmt_V4_header");
BannerDiv.onmouseover= function(event){ShrinkAll(event,"")};
 

var BodyDiv=document.getElementById("Dropdown_Bottom");
BodyDiv.onmouseover= function(event){ShrinkAll(event,"")};
 
 
var BodyDiv=document.getElementById("Home");
BodyDiv.onmouseover= function(event){ShrinkAll(event,"")};

var BodyDiv=document.getElementById("Picks for Practitioners");
BodyDiv.onmouseover= function(event){ShrinkAll(event,"")};
 
}




function toggle(elem){
	var div1 = document.getElementById(elem) 
	if (div1.style.display == 'none') {
		div1.style.display = 'block'
	} else {
		div1.style.display = 'none'
	}
}


















