
//"Sliding Menu with Submenus" - by Tim Guhl ©2002 - All Rights Reserved
//Site: http://www.guhl.co.uk/ EMail: webmaster@guhl.co.uk

//Based on the Dynamic-FX slide in menu v6.5 (By maXimus, maximus@nsimail.com)

//For full source, and 100's more DHTML scripts, visit http://www.dynamicdrive.com


NS6 = (document.getElementById&&!document.all)
IE = (document.all)
NS = (navigator.appName=="Netscape" && navigator.appVersion.charAt(0)=="4")

//vars
tempBar='';
barBuilt=0;
ssmItems=new Array();
//timers
moving=setTimeout('null',1);
hide=setTimeout('null', 1);

function moveOut() {
	if ((NS6||NS)&&parseInt(ssm.left)<0 || IE && ssm.pixelLeft<0) {
		clearTimeout(moving);
		moving = setTimeout('moveOut()', slideSpeed);
		slideMenu(10)
	} else {
		clearTimeout(moving);
		moving=setTimeout('null',1)
	}
}

function moveBack() {
	clearTimeout(moving);
	moving = setTimeout('moveBack1()', waitTime);
}

function moveBack1() {
	if ((NS6||NS) && parseInt(ssm.left)>(-menuWidth) || IE && ssm.pixelLeft>(-menuWidth)) {
		clearTimeout(moving);
		moving = setTimeout('moveBack1()', slideSpeed);
		slideMenu(-10);
	} else {
		for (i=1;i<menuNumber;i++) {
		 	if (NS6) {
 	 	       document.getElementById("menu"+i).style.visibility="hidden";
		 	   document.refresh;
		 	} else {
		 	   eval(layerRef+'["menu'+i+'"]'+styleSwitch+'.visibility = NvisibleVar');}
		 	}
		clearTimeout(moving);
		moving=setTimeout('null',1)
	}
}

function slideMenu(num){
	if (IE) {ssm.pixelLeft += num;}
	if (NS||NS6) {ssm.left = parseInt(ssm.left)+num;}
	if (NS) {bssm.clip.right+=num;bssm2.clip.right+=num;}
}

function makeStatic() {
	if (NS||NS6) {winY = window.pageYOffset;}
	if (IE) {winY = document.body.scrollTop;}
	if (NS6||IE||NS) {
		if (winY!=lastY&&winY>YOffset-staticYOffset) {
			smooth = .2 * (winY - lastY - YOffset + staticYOffset);
		} else {
			if (YOffset-staticYOffset+lastY>YOffset-staticYOffset) {
				smooth = .2 * (winY - lastY - (YOffset-(YOffset-winY)));
			} else {smooth=0}
		}
		if(smooth > 0) smooth = Math.ceil(smooth); 
			else smooth = Math.floor(smooth);
		if (IE) bssm.pixelTop+=smooth;
		if (NS6||NS) bssm.top=parseInt(bssm.top)+smooth
		lastY = lastY+smooth;
		setTimeout('makeStatic()', 1)
	}
}

function buildBar() {
	if(barText.indexOf('<IMG')>-1) {
		tempBar=barText
	} else {
		for (b=0;b<barText.length;b++) {
			tempBar+=barText.charAt(b)+"<BR>"}
	}
	document.write('<td align="center" rowspan="'+maxMenuItems+'" width="'+barWidth+'" bgcolor="'+barBGColor+'" valign="'+barVAlign+'"><p align="center"><SPAN style="font-family:'+barFontFamily+';font-size:'+barFontSize+';color:'+barFontColor+'"><B>'+tempBar+'</B></span></p></TD>')
}

function initSlide() {
	if (NS6){
		ssm=document.getElementById("menu0").style;
		bssm=document.getElementById("basessm").style;
		//bssm.clip="rect(0 "+maxDepth*document.getElementById("menu0").offsetWidth+" "+document.getElementById("menu0").offsetHeight+" 0)";
		ssm.visibility="visible";
	} else if (IE) {
		ssm=document.all("menu0").style;
		bssm=document.all("basessm").style
		//bssm.clip="rect(0 "+maxDepth*menu0.offsetWidth+" "+menu0.offsetHeight+" 0)";
		bssm.visibility = "visible";
	} else if (NS) {
		bssm=document.layers["basessm1"];
		bssm2=bssm.document.layers["basemenu0"];
		ssm=bssm2.document.layers["menu0"];
		//bssm2.clip.left=0;
		ssm.visibility = "show";
	}
	
	if (NS6){
		layerStyleRef ="";
		layerRef="getElementById('";
		styleSwitch="').style";
		visibleVar="visible";
		NvisibleVar="hidden";
	}
	if (NS) {
		layerStyleRef="layer.";
		layerRef="document.layers";
		styleSwitch="";
		visibleVar="show";
		NvisibleVar="hide";
    	} 
	if (IE){
		layerStyleRef="layer.style.";
		layerRef="document.all";
		styleSwitch=".style";
		visibleVar="visible";
		NvisibleVar="hidden";
	}

	//initialise visible layers
	for (layerNo=0;layerNo<menuNumber;layerNo++) {
		eval(layerRef+'["menu'+layerNo+'"]'+styleSwitch+'.visibility = NvisibleVar');}
	eval(layerRef+'["menu0"]'+styleSwitch+'.visibility = visibleVar');

	if (menuIsStatic=="yes") makeStatic();
}

function showsub(layerNo){
	//this function brings a defined layer to the front
	origins = new Array();
	c = 1;	
	//find out which layers it is coming from 
	if (ssmItems[layerNo][2] != "no") {	
		for (l=0;l<ssmItems.length-1;l++) {if (ssmItems[l][0] == ssmItems[layerNo][2]) {origin=l;break;};};
	} else { 
		origin = layerNo;
	};

	origins[0]=ssmItems[origin][0];
	while (ssmItems[origin][0] != 0) {
		for (z=0;z<ssmItems.length-1;z++) {
			if (ssmItems[z][2] == ssmItems[origin][0]) {
				origins[c] = ssmItems[z][0];
				origin = z;
				c++;
			};
		};
	};
	//hide all layers
	for (i=1;i<=menuNumber-1;i++) {
	   if (NS6) {
	      document.getElementById("menu"+i).style.visibility="hidden";
	   } else {
	      eval(layerRef+'["menu'+i+'"]'+styleSwitch+'.visibility = NvisibleVar');
	   }
	}
	document.refresh;
	// make visible wanted layers except the last one as that is the main menu and always visible
	for (t=0;t<c-1;t++) {
	   if (NS6) {
	      document.getElementById("menu"+origins[t]).style.visibility="visible";
	   } else {   
	      eval(layerRef+'["menu'+origins[t]+'"]'+styleSwitch+'.visibility = visibleVar');
	   }   
	};
    document.refresh;
}

function buildMenu() {
	//outside layer
	if (IE||NS6) {							//visibility:hidden;
		document.write('<DIV ID="basessm" style="visibility:hidden;Position : Absolute ;Left : '+XOffset+' ;Top : '+YOffset+' ;Z-Index : 20;width:'+(menuWidth+barWidth+10)+'">')
	}
	if (NS) {
		document.write('<LAYER name="basessm1" top="'+YOffset+'" LEFT='+XOffset+' visibility="show">')
	}
  for (currentmenu=0;currentmenu<menuNumber;currentmenu++) {
		// find out how far to move the clip to make it fit...
		count=0;
		while ((count<ssmItems.length-1)&&ssmItems[count][0] != currentmenu) {count++};
		leveltimes = ssmItems[count][1];
		if (currentmenu == 0) {
			posLeft = -menuWidth;
		} else {
			posLeft = leveltimes*(menuWidth - barWidth + SubXOffset);
			posLeft += (barWidth+1)*(ssmItems[count][1]-1);
			posLeft += barWidth;
		};
		// find vertical position 
		if (currentmenu == 0) {
			posTop = 0;
		} else {
			posTop = 0;
			for (l=0;l<ssmItems.length-1;l++) {if (ssmItems[l][2] == currentmenu) {origin=l;break;};}
			for (m=0;m<ssmItems.length-1;m++) {if (ssmItems[m][0] == ssmItems[origin][0]) {firstoforigin = m;break;};}
			posTop += (((origin-firstoforigin)*hdrHeight)+origin-firstoforigin) + SubYOffset;
			//if not at level 1
			while (ssmItems[origin][0] != 0) {
				for (l=0;l<ssmItems.length-1;l++) {if (ssmItems[l][2] == ssmItems[origin][0]) {origin=l;break;};}
				for (m=0;m<ssmItems.length-1;m++) {if (ssmItems[m][0] == ssmItems[origin][0]) {firstoforigin = m;break;};}
				posTop += (((origin-firstoforigin)*hdrHeight)+origin-firstoforigin);
			posTop += SubYOffset;
			}
		}
	if (IE||NS6) {							
		document.write('<DIV ID="menu'+currentmenu+'" style="Position : Absolute ;Left : '+(posLeft)+' ;Top: '+posTop+'; Z-Index : 20;" onmouseover="moveOut()" onmouseout="moveBack()">')
	}
	if (NS) {
		document.write('<ILAYER name="basemenu'+currentmenu+'"><LAYER visibility="hide" name="menu'+currentmenu+'" bgcolor="'+menuBGColor+'" left="'+(posLeft)+'" onmouseover="moveOut()" onmouseout="moveBack()">')
	}
	var hereWidth = 0;
	if (currentmenu == 0) {hereWidth = menuWidth+barWidth+2;} else {hereWidth = menuWidth + 2;}
	if (NS6) {
		document.write('<table border="0" cellpadding="0" cellspacing="0" width="'+(hereWidth)+'" bgcolor="'+menuBGColor+'"><TR><TD>')
	}
	
	document.write('<table border="0" cellpadding="0" cellspacing="1" width="'+(hereWidth)+'" bgcolor="'+menuBGColor+'">');
	for(i=0;i<ssmItems.length;i++) {  
	  if(ssmItems[i][0]==currentmenu) {
		if(!ssmItems[i][6]){
			ssmItems[i][6]=menuCols;		// header sub: no colorchange only change number 
			ssmItems[i][8]=menuWidth-1		// link sub: change color and change number
		} else if (ssmItems[i][6]!=menuCols)
			ssmItems[i][8]=Math.round(menuWidth*(ssmItems[i][6]/menuCols)-1);
		if(ssmItems[i-1]&&ssmItems[i-1][7]!="no"){document.write('<TR>')}
		if(!ssmItems[i][4]){ //<!-- no link -->
			if (ssmItems[i][2]!="no") {				
				// header with submenu  
				document.write('<td  bgcolor="'+hdrSubBGColor+'" onmouseover="showsub('+i+'),bgColor=\''+hdrSubOverBGColor+'\'" onmouseout="bgColor=\''+hdrSubBGColor+'\'"HEIGHT="'+hdrHeight+'" WIDTH="'+ssmItems[i][8]+'" COLSPAN="'+ssmItems[i][6]+'"><ilayer><LAYER onmouseover="bgColor=\''+hdrSubOverBGColor+'\'" onmouseout="bgColor=\''+hdrSubBGColor+'\'" WIDTH="'+(menuWidth-1)+'" ALIGN="'+hdrAlign+'"><DIV  ALIGN="'+hdrAlign+'" height="100%" width="'+(menuWidth-1)+'"><table width="'+(menuWidth-1)+'" height="'+hdrHeight+'" cellpadding="0" border="0" cellspacing="0"><tr><td style="font-family:'+hdrFontFamily+';font-size:'+hdrFontSize+';color:'+hdrFontColor+'"> <b>'+ssmItems[i][3]+'</b></td><td><img src="'+namePic+'" align="right" width="5" height="10"></td></tr></table></DIV></LAYER></ILAYER></TD>')
			} else {
				// header no submenu
				document.write('<td bgcolor="'+hdrBGColor+'" onmouseover="showsub('+i+')" HEIGHT="'+hdrHeight+'" ALIGN="'+hdrAlign+'" VALIGN="'+hdrVAlign+'" WIDTH="'+ssmItems[i][8]+'" COLSPAN="'+ssmItems[i][6]+'"> <SPAN style="font-family:'+hdrFontFamily+';font-size:'+hdrFontSize+';color:'+hdrFontColor+'"><b>'+ssmItems[i][3]+'</b></span></td>')
			}
		} else { // with link
			if(!ssmItems[i][5]) ssmItems[i][5]=linkTarget;
			if (ssmItems[i][2]!="no") {				
				// link with submenu
				document.write('<TD BGCOLOR="'+linkSubBGColor+'" onmouseover="showsub('+i+'),bgColor=\''+linkSubOverBGColor+'\'" onmouseout="bgColor=\''+linkSubBGColor+'\'" HEIGHT="'+hdrHeight+'" WIDTH="'+ssmItems[i][8]+'" COLSPAN="'+ssmItems[i][6]+'"><ilayer><LAYER onmouseover="bgColor=\''+linkSubOverBGColor+'\'" onmouseout="bgColor=\''+linkSubBGColor+'\'" WIDTH="'+(menuWidth-1)+'" ALIGN="'+linkAlign+'"><DIV  ALIGN="'+linkAlign+'" height="100%" width="'+(menuWidth-1)+'"><table width="'+(menuWidth-1)+'" height="'+hdrHeight+'" cellpadding="0" border="0" cellspacing="0"><tr><td style="font-family:'+linkFontFamily+';font-size:'+linkFontSize+';color:'+linkFontColor+'"> <A HREF="'+ssmItems[i][4]+'" target="'+ssmItems[i][5]+'" CLASS="ssmItems" >'+ssmItems[i][3]+'</a></td><td><img src="'+namePic+'" align="right" width="5" height="10"></td></tr></table></DIV></LAYER></ILAYER></TD>')
			} else {
				// link no submenu
				document.write('<TD BGCOLOR="'+linkBGColor+'" onmouseover="showsub('+i+'),bgColor=\''+linkOverBGColor+'\'" onmouseout="bgColor=\''+linkBGColor+'\'" HEIGHT="'+hdrHeight+'" WIDTH="'+ssmItems[i][8]+'" COLSPAN="'+ssmItems[i][6]+'"><ILAYER><LAYER onmouseover="bgColor=\''+linkOverBGColor+'\'" onmouseout="bgColor=\''+linkBGColor+'\'" WIDTH="100%" ALIGN="'+linkAlign+'"><DIV  ALIGN="'+linkAlign+'"><SPAN style="font-family:'+linkFontFamily+';font-size:'+linkFontSize+';color:'+linkFontColor+'"> <A HREF="'+ssmItems[i][4]+'" target="'+ssmItems[i][5]+'" CLASS="ssmItems">'+ssmItems[i][3]+'</a></span></DIV></LAYER></ILAYER></TD>')
			}
		}
		if(ssmItems[i][7]!="no"&&barBuilt==0) {buildBar();barBuilt=1}
		if(ssmItems[i][7]!="no") {document.write('</TR>')}
	  }
	}
	document.write('</table>')
	if (NS6) {document.write('</TD></TR></TABLE>')}

    // close first layer to do submenu if necessary
	if (IE||NS6) {document.write('</DIV>')}		
	if (NS) {document.write('</LAYER>')}
  }
	// close the whole table
	if (IE||NS6) {document.write('</DIV>')}
	if (NS) {document.write('</ILAYER></LAYER>')}
	theleft=-menuWidth;
	lastY=0;
	setTimeout('initSlide();', 1)
}

