var rightitemstr='';
var rightitemsctr=1;
var rightparentarr;
var leftpostemlocation;
var rightdiv;
var rightid;
var firstlevelctr=0,divlevel=0,levelctr=0;
var showthis=0,firstctr;
var currentselected=null,CloseTimer=null;
var level=0;
var browser=navigator.userAgent.toLowerCase();
var nav6=(document.getElementById && browser.indexOf('netscape6')!=-1)?1:0;	
var nav4=(browser.indexOf('mozilla')!=-1&&browser.indexOf('compatible')==-1&&!document.getElementById&&document.layers)?1:0;		
init_setting();
var winwidth = (document.all)?window.document.body.clientWidth:window.innerWidth;
var winheight=(document.all)?window.document.body.clientHeight:window.innerHeight;


function init_setting()
{

winwidth = (document.all)?window.document.body.clientWidth:window.innerWidth;
winheight=(document.all)?window.document.body.clientHeight:window.innerHeight;

if (acrosswindow)
{
	if (document.all)
	{
		winwidth = winwidth - 1;
		menuwidth = (winwidth/rootitemcount)-1;	
	}
	else if (nav4)
	{
		winwidth = winwidth - 3;
		menuwidth = (winwidth/rootitemcount)-7;	
	}
	else if (nav6)
	{
		winwidth = winwidth - 2;	
		menuwidth = (winwidth/rootitemcount)-1;		
	}
}
if (nav4 && menutoppos!=0 )	menutoppos=menutoppos+8;
if (nav4)	menuleftpos=menuleftpos+8;
if (winstyle==1)
{
	bordercolor='#C0C0C0';
	lowbgcolor='#C0C0C0';
	rootlowbgcolor='#C0C0C0';
	fontlowcolor='black';
	rootfontlowcolor='black';
	
}

if (alignright==1){
	if(menulayout==0)	{
		menuleftpos = winwidth-(menuwidth+(rootitemcount));
	}
}

if (nav6){
	if (menuleftpos < 2) menuleftpos=1;
	if (menutoppos < 2)	menutoppos=1;
}
}
/***************************************************************/
/* Find the div on the right	                               */
/***************************************************************/
function rightitemsdiv(id)
{
	var allchild = parentarray[id];
	
	if (rightitemsctr > 1)
	{
		rightitemstr = rightitemstr + id + ", ";
	}

	rightitemsctr++;

	for (xyz in allchild)
	{
		if (childcount(allchild[xyz]) != 0)
		{
			rightitemsdiv(allchild[xyz]);
		}
	}
}

/***************************************************************/
/* Get the id of the div on the right                          */
/***************************************************************/
function getrightitems(id)
{
	var allchild = parentarray[0];
	var children = childcount(0);

	return allchild[children-1];
}

function isonright(id)
{
	var item;
	for (item in rightparentarr)
	{
		if(parseInt(rightparentarr[item]) == parseInt(id))
			return true;
	}
	return false;
}


/***************************************************************/
/* To deal with the items on the right                         */
/***************************************************************/
rightid = getrightitems(0);
rightitemsdiv(rightid);

if (rightitemstr != ''){rightitemstr = rightitemstr.substr(0, rightitemstr.length-2);}
if (rightitemstr != ''){rightparentarr = rightitemstr.split(",");}
/***************************************************************/
/* End                                                         */
/***************************************************************/

function showhidedropdown(mode)
{
	if (document.all)
	{
		var a = document.all.tags("SELECT");
        for (var i=0; i < a.length; i++)
        {
		  //alert(a[i].style.pixelLeft); 		
          //a[i].disabled = true; 
		 // a[i].style.zindex = -1000;
		  //document.all["dummyselect"].style.visibility=(mode=="hidden")?"visible":"hidden"; 
		  //document.all["dummyselect"].style.display = (mode=="hidden")?"":"none"; 

		  //a[i].style.display = (mode=="hidden")?"none":""; 
		  //a[i].style.visibility = mode;

        }
		var a = document.all.tags("TEXTAREA");
        for (var i=0; i < a.length; i++)
        {
		  //alert(a[i].style.pixelLeft); 		
         // a[i].style.visibility = mode;
        }
	}

}

/***************************************************************/
/* User for finding objects works with IE, Netscape4, Netscape6*/
/***************************************************************/
function objectfinder(n)
{ 
	var p,i,obj, d=document; 

	if (document.all && !nav6)
	{
		if(!(obj=d[n])&&d.all) // IE
			obj=d.all[n]; 
	}

	if (nav4)
	{
		for(i=0;!obj&&d.layers&&i<d.layers.length;i++) // Netscape 4
			obj=objectfinder(n,d.layers[i].document); 
	}

	if (d.getElementById)	// Netscape 6 or DOM capable browsers
		obj= d.getElementById(n);

	return obj;
}

/***************************************************************/
/* Menu Link with three options top, self or blank              */
/***************************************************************/
function gotolink(){
	if(this.hrefsrc)
	{
		if (this.hreftarget == '_self')
		{
			window.location.href=this.hrefsrc
		}
		else if (this.hreftarget == '_top')
		{
			window.top.location.href=this.hrefsrc
		}
		else if (this.hreftarget == '_blank')
		{
			window.open(this.hrefsrc);
		}
		else
			window.location.href=this.hrefsrc
	}
}

/***************************************************************/
/* Function to handle resizing in Navigator 4                  */
/***************************************************************/
function handleResize()
{
	location.reload()
	return false
}

if (nav4)
{
	window.captureEvents(Event.RESIZE)
	
}

window.onresize = handleResize;
/***************************************************************/
/* Get the number of children of selected Menu                 */
/***************************************************************/
function childcount(id){
	var count = 0;	var y;

	if (id == 0)
		return rootitemcount;
	else
		return MenuArray[id][3];
}

/***************************************************************/
/* Loop through the Array to get the IDs of the first          */
/***************************************************************/
function getrootdiv()
{
	var count = 0;	var y;
	for (y in MenuArray)
	{
		if (MenuArray[y][4] == 1 )
			count++;
	}
	return count;
}

function hier(id)
{
	divlevel++;
	var i,rootdivitem;
	var previousdiv=null;
	var parentchild = childcount(id);
	var whichmenu='MENU' + id;
	var childarr;

	if(document.getElementById)
	{
		var rootdiv=window.document.createElement("div");
		rootdiv.style.position='absolute';
		rootdiv.style.visibility='inherit';
		if(id==0)
			rootdiv.style.align='center';
		
		window.document.body.appendChild(rootdiv)
	}
	else if(nav4) 
		var rootdiv=new Layer(menuwidth,window)
	else
	{	
		whichmenu+='c';
		window.document.body.insertAdjacentHTML("AfterBegin","<div id='"+whichmenu+"' style='visibility:visible; position:absolute;'><\/div>"); 
		var rootdiv=window.document.all[whichmenu]
	}
	
	rootdiv.SetUp=rootdivsetup;
	rootdiv.SetUp(parentchild, id);
	
	if(document.all)
	{	
		rootdiv.InnerString='';
		if (parentchild)
		{
			childarr = parentarray[id];
			for(i=0;i<childarr.length;i++)
			{
				whichmenu= "MENU"  + childarr[i];
			}
		rootdiv.innerHTML=rootdiv.InnerString
		}
	}

    if (parentchild)
    {

	  childarr = parentarray[id];
	  for(i=0;i<childarr.length;i++)
	  {
		whichmenu= "MENU"  + childarr[i];
		if(document.getElementById)
		{
			rootdivitem=window.document.createElement("div");
			rootdivitem.style.position='absolute';
			rootdivitem.style.visibility='inherit';
			
			rootdiv.appendChild(rootdivitem);
			rootdivitem.SetUp=setchilditem
		}
		else if(nav4)
		{
			rootdivitem=new Layer(menuwidth,rootdiv);
			rootdivitem.SetUp=setnavchilditem
		}
		else
		{	
			rootdivitem=window.document.all[whichmenu];
			rootdivitem.SetUp=setchilditem
		}
		
		rootdivitem.SetUp(rootdiv,previousdiv,whichmenu, id);

		if(childcount(childarr[i])) 
		{
			rootdivitem.childdiv=hier(childarr[i]);
		}
		previousdiv=rootdivitem
	  }
    }
	
	rootdiv.firstitem=rootdivitem;
	divlevel--;
	
	return(rootdiv)
}

function posdiv(id, currenttop,currentleft, level, prevleft, prevtop)
{
	
	var toppos, leftpos, dohorizontal, RootWidth, RootHeight ;
	var childtoppos,childleftpos;
	var childids = parentarray[id];

	if ((thisx=objectfinder("Parent" + id))!=null)
	{
		
		thisx.style.left=currentleft;
		thisx.style.top=currenttop;
		
		if (isonright(id) && acrosswindow==1)
		{
			thisx.style.left = prevleft-(menuwidth+1);
		}
		
		if (currentleft + menuwidth > winwidth)
		{
			thisx.style.left = prevleft-(menuwidth+1);
		}

		RootWidth = parseInt(thisx.style.width);
		RootHeight = parseInt(thisx.style.height);
	}

	if (++levelctr==1 && menulayout)
	{
		dohorizontal=1; 
		leftpos=RootWidth-menuwidth-2;
		toppos=0
	}
	else
	{
		dohorizontal=leftpos=0; 
		toppos=RootHeight-menuheight-2;
	
	}
	var prevleft = currentleft;
	var prevtop = currenttop;

	for (var x=childids.length-1; x >= 0; x--)
	{
		if ((thisx=objectfinder("MENU" + childids[x]))!=null)
		{
			thisx.style.left=leftpos+1;
			thisx.style.top=toppos+1;

			var thishavechild = childcount(childids[x]);

			if(thishavechild != 0)
			{
				if(dohorizontal)
				{	
					childtoppos=currenttop+toppos+menuheight+1; 
					childleftpos=currentleft+leftpos
				}
				else
				{	
					childleftpos=currentleft+leftpos+(1)*menuwidth+1; 
					childtoppos=currenttop+toppos + 5
				}
				var posdivid = childids[x];
				posdiv(posdivid,childtoppos,childleftpos, level+1, currentleft, currenttop)
			}
		}
		if (dohorizontal)
			leftpos-=menuwidth+1
		else
			toppos-=menuheight+1;
	}
	levelctr--;
}

function posnavlayer(rootlayer,currenttop,currentleft)
{
	var toppos,leftpos,dohorizontal;
	var root=rootlayer;
	var childlayer=root.firstitem;
	var RootWidth=root.clip.width;
	var RootHeight=root.clip.height;
	var childtoppos,childleftpos;

	root.top=currenttop;
	root.left=currentleft;


	if (++levelctr==1 && menulayout)
	{
		dohorizontal=1; 
		leftpos=RootWidth-menuwidth-2;
		toppos=0
	}
	else
	{
		dohorizontal=leftpos=0; 
		toppos=RootHeight-menuheight-2;
	}
	
	var prevleft = currentleft;
	var prevtop = currenttop;
	
	while(childlayer!=null)
	{
		childlayer.left=leftpos+1;
		childlayer.top=toppos+1;
		childlayer.newlayer.moveTo(leftpos+1,toppos+1);

		if(childlayer.childdiv)
		{
			if(dohorizontal)
			{	
				childtoppos=currenttop+toppos+menuheight+1; 
				childleftpos=currentleft+leftpos
			}
			else
			{	
				childleftpos=currentleft+leftpos+(1)*menuwidth+1; 
				childtoppos=currenttop+toppos+5
			}
			posnavlayer(childlayer.childdiv,childtoppos,childleftpos, prevleft, prevtop)
		}
		(dohorizontal)?leftpos-=menuwidth+1:toppos-=menuheight+1;
		childlayer=childlayer.previouslayer
	}
	levelctr--
}

function start()
{
	if(firstctr) 
		showrootdiv(rootdivparent0)
}

function showrootdiv(thisroot)
{
	var rootdivitem=thisroot.firstitem;

	if (nav4)
		thisroot.visibility=(++firstlevelctr==1)?"show":"hide";
	else
		thisroot.style.visibility=(++firstlevelctr==1)?"visible":"hidden";

	while(rootdivitem!=null)
	{
		if(rootdivitem.childdiv)
			showrootdiv(rootdivitem.childdiv);
		
		rootdivitem=rootdivitem.previouslayer
	}
	firstlevelctr--
}

function closeitemdiv(parent,childitem)
{
	while (parent)
	{
		if(parent.childdiv)
		{
			if(parent.childdiv!=childitem)
			{
				if (document.layers)
					parent.childdiv.visibility="hide";					
				else
					parent.childdiv.style.visibility="hidden";
			}
			closeitemdiv(parent.childdiv.firstitem,childitem)
		}
	parent=parent.previouslayer
	}
}	

function opendiv()
{	
	window.status = '';
	var childdiv=(nav4)?this.childlayer.childdiv:this.childdiv;
	var thismenu = this.id, parentdiv
	var thisid = thismenu.substr(4, thismenu.length);	

	currentselected=this; firstctr=0;
	
	if(showthis)
	{
		showhidedropdown("hidden");
		closeitemdiv(this.rootnavlayer.firstitem,childdiv); 
		if(this.level==1) 
			showthis=0
	}
	
	if(nav4)
	{	
		if (!childcount(this.layerid))
		{
			this.childlayer.bgColor=this.childlayer.highback;
			this.childlayer.document.write(this.childlayer.hoverval);
			this.childlayer.document.close()
		}
	}
	else
	{	
		if (!childcount(thisid))
		{
			this.style.backgroundColor=this.highback;
			this.style.color=this.highfontcolor
		}
	}

	if(childdiv!=null)
	{
		if (document.layers)
			this.childlayer.childdiv.visibility = "show";
		else
			this.childdiv.style.visibility = "visible";

		showthis=1
	}
	
	if (!nav4)
	{
		if (childcount(thisid))
		{
			if ((parentdiv=objectfinder("Parent" + thisid))!=null)
			{
				if (parseInt(parentdiv.style.left) + parseInt(menuwidth) > winwidth)
				{
					var templeft = parseInt(parentdiv.style.left) - parseInt(((menuwidth+2)*2));
					parentdiv.style.left = templeft;
				}
			}
		}

	}
	
	if (childdiv!=null)
	{
		if (childdiv.below)
		{
			if (this.childlayer.childdiv.left + menuwidth > winwidth)
			{
				this.childlayer.childdiv.left = this.childlayer.childdiv.left  - ((menuwidth+1)*2);
			}
		}
	}
	
	dash = (this.hrefsrc=='')?" " : " - "
	window.status = this.hrefname + dash + this.hrefsrc;
	
	currid = this.id;
}

function closediv()
{
	window.status = '';
	if(nav4)
	{
		this.childlayer.bgColor=this.childlayer.lowback;
		this.childlayer.document.write(this.childlayer.value);
		this.childlayer.document.close()
	}
	else
	{	
		this.style.backgroundColor=this.lowback;
		this.style.color=this.lowfontcolor
	}
	if(this==currentselected)
	{
		showhidedropdown("visible");	
		firstctr=1;
		if (CloseTimer) clearTimeout(CloseTimer);
		CloseTimer=setTimeout('start(currentselected)',500)
	}
}

function rootdivsetup(itemcount, id)
{

	var rootwidth = menuwidth;
	var rootheight = menuheight;
	this.firstitem=null;
	this.rightid = id
	this.level=divlevel;

	if (divlevel==1&&menulayout)
	{
		rootwidth=itemcount*(menuwidth+1)+borderwidth;
		rootheight+=2*borderwidth
	}
	else
	{ 	rootheight=itemcount*(menuheight+borderwidth)+borderwidth;
		rootwidth+=2*borderwidth
	}

	if(nav4)
	{	
		this.visibility='hide';
		this.bgColor=bordercolor;
		this.resizeTo(rootwidth,rootheight)
	}
	else
	{	
		this.style.backgroundColor=bordercolor;
		this.style.width=rootwidth;
		this.style.height=rootheight;
		this.style.fontFamily=fontfamily;
		this.style.fontWeight=(isfontbold)?'bold':'normal';
		this.style.fontStyle=(isfontitalic)?'italic':'normal';
		this.style.fontSize=fontsize+'pt';
		this.style.zIndex=divlevel;
		this.style.paddingTop = 1
	}
	this.id = "Parent" + id
}

function setchilditem(navchildlayer,prevlayer,thismenu,parentid)
{
	var thisid = thismenu.substr(4, thismenu.length);
    var childtext=MenuArray[thisid][0], obj;
	this.id = thismenu;
	this.parent = "Parent" + parentid;
	this.name = thismenu;
	this.value= childtext;
	this.previouslayer=prevlayer;
	this.level=divlevel;
	this.hrefname=MenuArray[thisid][0];
	this.hrefsrc=MenuArray[thisid][1];
	this.hreftarget=MenuArray[thisid][5];
	this.rootnavlayer=navchildlayer;
	this.style.cursor='default';
	this.style.width=menuwidth; 
	this.style.height=menuheight
	this.style.overflow='hidden';
	this.lowback=(divlevel != 1)?lowbgcolor:rootlowbgcolor;
	this.lowfontcolor=(divlevel != 1)?fontlowcolor:rootfontlowcolor;
	this.highback=(divlevel != 1)?highbgcolor:roothighbgcolor;
	this.highfontcolor=(divlevel != 1)?fonthighcolor:rootfonthighcolor; 
	this.style.color=this.lowfontcolor;
	this.style.backgroundColor=this.lowback;
	this.style.zIndex=divlevel
	//this.align = "center";
	//this.style.textAlign = "center"
	this.style.paddingTop = 3
	if (document.getElementById)
	{	
		if (winstyle==0)
		{
			obj=window.document.createElement('img'); this.appendChild(obj); obj.style.position='absolute'; obj.src=sideimage.src; obj.style.top=0;
		}
		else
		{
			if (childcount(thisid) != 0)
			{
				obj=window.document.createElement('img'); this.appendChild(obj); obj.style.position='absolute'; obj.src=arrowimage.src; obj.style.top=5; obj.style.left=menuwidth-10;
			}
			
		}	

		obj=window.document.createTextNode("\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0"  + childtext );
		
		this.appendChild(obj);
	}
	else
	{
		var imstr;
		if (winstyle==0)
			imstr = "<img style='position:absolute; top:0px' src='"+ sideimage.src +"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
		this.innerHTML= imstr + childtext;
	}
	if(document.all)
	{
		this.onmouseover=opendiv; 
		this.onmouseout=closediv; 
		this.onclick=gotolink
	}
	else if (document.getElementById)
	{
		this.addEventListener('mouseover',opendiv,false); 
		this.addEventListener('mouseout',closediv,false); 
		this.addEventListener('click',gotolink,false)
	}
}

function setnavchilditem(navchildlayer,prevlayer,thismenu, parentid)
{
	var thisid = thismenu.substr(4, thismenu.length);
	this.value='&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp\;' + MenuArray[thisid][0];
	this.layerid = thisid;

	if(isfontbold)this.value=this.value.bold();
	if(isfontitalic)this.value=this.value.italics();
	
	this.hoverval=this.value;
	this.value=this.value.fontcolor(fontlowcolor);
	this.hoverval=this.hoverval.fontcolor(fonthighcolor);
	this.value="<div align='left'><font face='"+fontfamily+"' point-size='"+fontsize+"' color='"+fontlowcolor+"'>"+this.value+"</font></div>";
	this.hoverval="<div align='left'><font face='"+fontfamily+"' point-size='"+fontsize+"' color='"+fonthighcolor+"'>"+this.hoverval+"</font></div>";
	this.lowback=lowbgcolor;
	this.highback=highbgcolor;
	this.parent = "Parent" + parentid;
	
	if (divlevel == 1){
		this.value=this.value.fontcolor(rootfontlowcolor);
		this.hoverval=this.hoverval.fontcolor(rootfonthighcolor);
		this.lowback=rootlowbgcolor;
		this.highback=roothighbgcolor;
	}

	this.visibility='inherit';
	this.childdiv=null;
	this.previouslayer=prevlayer;
	this.bgColor=lowbgcolor;
	this.resizeTo(menuwidth,menuheight);
	this.document.write(this.value);
	this.document.close();

	this.newlayer=new Layer(menuwidth,navchildlayer);
	this.newlayer.layerid = thisid;
	this.newlayer.visibility='inherit';
	this.newlayer.level=divlevel;
	this.newlayer.hrefsrc=MenuArray[thisid][1];
	this.newlayer.hrefname=MenuArray[thisid][0];
	this.newlayer.hreftarget=MenuArray[thisid][5];
	
	this.newlayer.childlayer=this;
	this.newlayer.rootnavlayer=navchildlayer;
	this.newlayer.resizeTo(menuwidth,menuheight);
	
	this.newlayer.sideimagelayer=new Layer(10,this.newlayer);
	this.newlayer.sideimagelayer.visibility='inherit';
	imagestr="<img src='"+ sideimage.src +"'>";
	this.newlayer.sideimagelayer.document.write(imagestr);
	this.newlayer.sideimagelayer.document.close()

	this.newlayer.onmouseover=opendiv;
	this.newlayer.onmouseout=closediv;
	this.newlayer.captureEvents(Event.MOUSEUP);
	this.newlayer.onmouseup=gotolink;
}

/****************************************************************/
/* Start the whole thing create the hier and postion the menu  */
/****************************************************************/
function drawmenu()
{
	rootdivparent0=hier(0);
	
	if (nav4)
		posnavlayer(rootdivparent0,menutoppos,menuleftpos, 0, 0);
	else
		posdiv(0, menutoppos,menuleftpos, level, 0, 0);

	firstctr=1;	start(); firstctr=0; 
}

(nav6)?window.addEventListener('load',drawmenu,false):window.onload=drawmenu;