/*------------------------------------------------------------------------------------------------- 
Author's Statement:
This script is based on ideas of the author.
You may copy, modify and use it for any purpose. The only condition is that if you publish web pages that use 
this script you point to its author at a suitable place and don't remove this Statement from it.
It's your responsibility to handle possible bugs even if you didn't modify anything. I cannot promise any support.
Dieter Bungers
GMD (www.gmd.de) and infovation (www.infovation.de)

++

Script is mainly based on the idears from original script but with a few extensions.
They are :
	- Is now usable in Netscape on Macintosh.
	- Deals with Netscape resize problems and remembers chosen menu item and level
	- Rollover on grafix
Alterations made by Søren Tramm, str@subsilo.dk, http://www.subsilo.dk 
-------------------------------------------------------------------------------------------------*/

// -----------------------------------------------------------------------------------------------
// Function definision - Dynamic menu
// -----------------------------------------------------------------------------------------------
function menuGenerate(currentNumber,currentIsExpanded) {
	rememberCurrentNumber = currentNumber;
	rememberCurrentIsExpanded = currentIsExpanded;
	tmp = menuList[menuList.length-1][0];
	tmp = tmp.split(".");
	var numberOfMainTopics = tmp[0];

	// Frame window's name for menu
	win_menu = eval(window_menu);

	// Frame window's name for content
	winContent = eval(window_content);

	win_menu.document.open();


	// Draw HTML - start
	string_HTML =		"<html>\n<head>\n"+
								"<title></title>\n"+
								"<link rel='stylesheet' href='"+styleSheetPath+"'>"+
								"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"></meta></head>\n"+
								"<body MARGINWIDTH="+body_tag.marginWidth+" MARGINHEIGHT="+body_tag.marginHeight+" LEFTMARGIN="+body_tag.leftMargin+" TOPMARGIN="+body_tag.topMargin+">\n";


   // --------------------------------------------------------------------------- Menu table ------------------------------- //
	//string_HTML +=		"<table border="+table_menu.border+" cellspacing="+table_menu.cellSpacing+" cellpadding="+table_menu.cellPadding+">\n";
	string_HTML +=		"<!-- Table 1 -->";
	string_HTML +=		"<table border=0 cellspacing="+table_menu.cellSpacing+" cellpadding="+table_menu.cellPadding+">\n";
	string_HTML +=		"<tr><td colspan=2><img src=" +img_root+img_trans+" width=120 height=10></td></tr>";
	string_HTML +=		"<tr><td><img src=" +img_root+img_trans+" width=10 height=1></td>";
	string_HTML +=		"<td>";
	string_HTML +=		"<!-- Table 2 -->";
	string_HTML +=		"<table border=0 cellspacing="+table_menu.cellSpacing+" cellpadding="+table_menu.cellPadding+">\n";

   // --------------------------------------------------------------------------- Logo ------------------------------------- //
	string_HTML +=		"<tr><td align=left valign=top colspan=6><img src="+img_root+img_head.logo+"></td></tr>\n";


   // --------------------------------------------------------------------------- Start row with full menu ----------------- //
	string_HTML +=		"<tr><td align=left valign=top colspan=6>\n";


	// If Netscape we have to write step by step...(sigh)
	if(is.ns){
	 win_menu.document.writeln(string_HTML);
	}else{
	 all_HTML = string_HTML;
	}

 	var currentNumArray = currentNumber.split(".");
	var currentLevel = currentNumArray.length-1;
	var scrollY=0, addScroll=true, theHref="";
	for (i=0; i<menuList.length; i++) {					// Number of menu topics - level top and sub
		if(i>0) previousNumber = menuList[i-1][0];			// Previous item number
		if(i<menuList.length-1) nextNumber = menuList[i+1][0];		// Next item number
		thisNumber = menuList[i][0];					// This item number
		var isCurrentNumber = (thisNumber == currentNumber);		// True if this is chosen number
		if (isCurrentNumber) theHref=menuList[i][2];			// Set href link
		var thisNumArray = thisNumber.split(".");			// Current number(string) split into array
		if(i>0) previousNumArray = previousNumber.split(".");		// Previousnumber(string) split into array
		if(i<menuList.length-1) nextNumArray = nextNumber.split(".");	// Nextnumber(string) split into array
		var thisLevel = thisNumArray.length-1;				// Level number -1
		var toDisplay = true;						// 
		var thisMainTopics = thisNumArray[0];

		// If item has no sublevels. But we take it for granted that it has NOT
		hasSublevels = false;
		if( (i>0 && thisNumArray[0] == previousNumArray[0]) || (i<menuList.length-1 && thisNumArray[0] == nextNumArray[0]) )
		 hasSublevels = true;

		if (thisLevel > 0) {
			for (j=0; j<thisLevel; j++) {
				toDisplay = (j>currentLevel)?false:toDisplay && (thisNumArray[j] == currentNumArray[j]);
			}
		}

		thisIsExpanded = toDisplay && (thisNumArray[thisLevel] == currentNumArray[thisLevel])
		if (currentIsExpanded) {
			toDisplay = toDisplay && (thisLevel<=currentLevel);
			if (isCurrentNumber) thisIsExpanded = false;
		}

		// ---------------------------------------------- Show menu item --------------------------//
		if (toDisplay) {

			// ---------------------------------------------- Stylesheet ------------------------------//
			if(thisLevel == 0){
				fontClass = "menu";	
			}else
			if(thisLevel == 1){
				fontClass = "subMenu";		
			}else
			if(thisLevel > 1){
				fontClass = "subMenu"+thisLevel;			
			}

			// Colspan
			colsNum = 10;

			//------------------------------------------------- Draw Menu - Start -------------------//
			if (isCurrentNumber) addScroll=false;

			var isLeaf = (i==menuList.length-1) || (thisLevel >= menuList[i+1][0].split(".").length-1); // Has no sub topics

			// ---------------------------------------------- Top line and space between topics -------//
			string_HTML = "";
			// First space is used for messure. Seccond item , top level , only
			if(thisLevel == 0 && i==1){
				string_HTML = "<tr>";
			 	for (j=0; j<nCols; j++) {
					string_HTML += "<td><img src="+img_root+img_trans+" height="+menu_verticalSpace_top+" width="+colsTemplate[j]+"></td>";
				}
				string_HTML += "</tr>";
			}else

				// If Netscape we have to write stpe by step...(sigh)
				if(is.ns){
				 win_menu.document.writeln(string_HTML);
				}else{
				 all_HTML += string_HTML;
				}
			// ----------------------------------------------------------------------------------------//

			// ---------------------------------------------- Menu item -------------------------------//
			string_HTML = "";			

			// The horizontal space inserted before item text
			horizontal_space = (item_space_horizontal*thisLevel)

			// Text item or thumbnail
			if(thisLevel < thumb_level){ // Text
				if(thisLevel>0){
					pre_space = "<td><img src=" +img_root+img_trans+" width=14 height=1></td>";
				}else{
					pre_space = "";
				}

				col = 1;
				// Menu topic
				string_HTML +=		"<!-- Table 3 -->";
				string_HTML +=	"<table border=0 cellspacing="+table_menu.cellSpacing+" cellpadding="+table_menu.cellPadding+"><tr>"+ pre_space +"<td  width="+(menuWidth-horizontal_space)+" class="+fontClass+" colspan="+col+"><a href=\"javascript: parent.menuGenerate('" + thisNumber + "'," + thisIsExpanded + ");\" class=\""+fontClass+"\">"+menuList[i][1]+"</a></td></tr></table>";		// Text 	
				
				// ----------------------------------------------------- horizontal space lines --------------------------------------------
				// Level 0
				if(thisLevel == 0){	
					string_HTML +=		"<!-- Table level 0 -->";
					string_HTML +=	"<table border=0 cellspacing="+table_menu.cellSpacing+" cellpadding="+table_menu.cellPadding+">";
					string_HTML +=	"<tr><td class="+fontClass+" colspan=1><img src="+img_root+img_gray+" height="+menu_verticalSpace_top+" width=100></td></tr>";
					string_HTML +=	"</table>";

				// Level 1
				}else if(thisLevel == 1){
					string_HTML +=		"<!-- Table level 1 -->";
					string_HTML +=	"<table border=0 cellspacing="+table_menu.cellSpacing+" cellpadding="+table_menu.cellPadding+">";
					string_HTML +=	"<tr><td><img src=" +img_root+img_trans+" width=14 height=1></td>";
					string_HTML +=  "<td class="+fontClass+" colspan=1><img src="+img_root+img_gray+" height="+menu_verticalSpace_top+" width=86></td></tr>";
					string_HTML +=	"</table>";
				}

			}else{	// Thumbnails

				// Thumbnail list item
				thumbs = thumbList[menuList[i][3]]
				t=0;
				string_HTML +=		"<!-- Table level 1+ -->";
				string_HTML = "<table border=0 cellspacing="+table_menu.cellSpacing+" cellpadding="+table_menu.cellPadding+">\n";
				
				// Row 1
				if(t<thumbs.length)	{
					string_HTML += submenu_space_logo;
					for(t=0;t<4;t++){
						if(t<thumbs.length){
							thumbPath = thumb_root + thumbs[t][0];
							thumbLink =  thumbs[t][1];
							string_HTML +=	"<td class=subMenuThumb colspan=1><a href=\""+thumbLink+"\" target=content><img src="+thumbPath+" border=0></a></td>";		// Thumbnail 	
						}
					}
					string_HTML += "</tr>";	 
				}
				
				// Row 2
				if(t<thumbs.length)	{
					string_HTML +=	submenu_space_logo;
					for(t=4;t<8;t++){
						if(t<thumbs.length){
							thumbPath = thumb_root + thumbs[t][0];
							thumbLink =  thumbs[t][1];
							string_HTML +=	"<td class=subMenuThumb colspan=1><a href=\""+thumbLink+"\" target=content><img src="+thumbPath+" border=0></a></td>";		// Thumbnail 	
						}
					}
					string_HTML += "</tr>";	 
				}

				// Row 3
				if(t<thumbs.length)	{
					string_HTML +=	submenu_space_logo;
					for(t=8;t<12;t++){
						if(t<thumbs.length){
							thumbPath = thumb_root + thumbs[t][0];
							thumbLink =  thumbs[t][1];
							string_HTML +=	"<td class=subMenuThumb colspan=1><a href=\""+thumbLink+"\" target=content><img src="+thumbPath+" border=0></a></td>";		// Thumbnail 	
						}
					}
					string_HTML += "</tr>";	 
				}

				// Row 4
				if(t<thumbs.length)	{
					string_HTML +=	submenu_space_logo;
					for(t=12;t<16;t++){
						if(t<thumbs.length){
							thumbPath = thumb_root + thumbs[t][0];
							thumbLink =  thumbs[t][1];
							string_HTML +=	"<td class=subMenuThumb colspan=1><a href=\""+thumbLink+"\" target=content><img src="+thumbPath+" border=0></a></td>";		// Thumbnail 	
						}
					}
					string_HTML += "</tr>";	 
				}

				// Row 5
				if(t<thumbs.length)	{
					string_HTML +=	submenu_space_logo;
					for(t=16;t<20;t++){
						if(t<thumbs.length){
							thumbPath = thumb_root + thumbs[t][0];
							thumbLink =  thumbs[t][1];
							string_HTML +=	"<td class=subMenuThumb colspan=1><a href=\""+thumbLink+"\" target=content><img src="+thumbPath+" border=0></a></td>";		// Thumbnail 	
						}
					}
					string_HTML += "</tr>";	 
				}	

				// Row 6
				if(t<thumbs.length)	{
					string_HTML +=	submenu_space_logo;
					for(t=18;t<22;t++){
						if(t<thumbs.length){
							thumbPath = thumb_root + thumbs[t][0];
							thumbLink =  thumbs[t][1];
							string_HTML +=	"<td class=subMenuThumb colspan=1><a href=\""+thumbLink+"\" target=content><img src="+thumbPath+" border=0></a></td>";		// Thumbnail 	
						}
					}
				string_HTML += "</tr>";	 
				}

				// Row 7
				if(t<thumbs.length)	{
					string_HTML +=	submenu_space_logo;
					for(t=22;t<26;t++){
						if(t<thumbs.length){
							thumbPath = thumb_root + thumbs[t][0];
							thumbLink =  thumbs[t][1];
							string_HTML +=	"<td class=subMenuThumb colspan=1><a href=\""+thumbLink+"\" target=content><img src="+thumbPath+" border=0></a></td>";		// Thumbnail 	
						}
					}
				string_HTML += "</tr>";	 
				}

				// Row 8
				if(t<thumbs.length)	{
					string_HTML +=	submenu_space_logo;
					for(t=26;t<30;t++){
						if(t<thumbs.length){
							thumbPath = thumb_root + thumbs[t][0];
							thumbLink =  thumbs[t][1];
							string_HTML +=	"<td class=subMenuThumb colspan=1><a href=\""+thumbLink+"\" target=content><img src="+thumbPath+" border=0></a></td>";		// Thumbnail 	
						}
					}
				string_HTML += "</tr>";	 
				}

				string_HTML += "</table>";	 
				

				// ----------------------------------------------------- horizontal space lines --------------------------------------------
				string_HTML +=		"<!-- Table horizontal spacer -->";
				string_HTML +=	"<table border=0 cellspacing="+table_menu.cellSpacing+" cellpadding="+table_menu.cellPadding+">";
				string_HTML +=	"<tr><td><img src=" +img_root+img_trans+" width=14 height=1></td>";
				string_HTML +=  "<td class="+fontClass+" colspan=1><img src="+img_root+img_gray+" height="+menu_verticalSpace_top+" width=86></td></tr>";
				string_HTML +=	"</table>";

				//string_HTML += "<!-- End table 1 -->";
				//string_HTML += "</td></tr></TABLE>";
			}


			// If Netscape we have to write step by step...(sigh)
			if(is.ns){
			 win_menu.document.writeln(string_HTML);
			}else{
			 all_HTML += string_HTML;
			}	
			// ----------------------------------------------------------------------------------------//

			// rememeber this level
			previousLevel = thisLevel;
		}
	} // For-loop

	// ---------------------------------------------- Close Tables and HTML -------------------//
	string_HTML += "<!-- End table 2 -->";
	string_HTML =	"</td></tr></table>";
	string_HTML += "<!-- End table 1 -->";
	string_HTML += "</td></tr></TABLE>";
	string_HTML +=	"</body></html>";

       	// If Netscape we have to write stpe by step...(sigh)
    	if(is.ns){
      	 win_menu.document.writeln(string_HTML);
      	}else{
       	 all_HTML += string_HTML;
       	}
	// ----------------------------------------------------------------------------------------//


	// ---------------------------------------------- Write and close HTML --------------------//
	// OBS !!!
	// We have written for NS during all the document
	// Now we'll write for IE
	// IE4 needs a small brake before writing...doughhh
	if(is.ie4){
	 setTimeout("win_menu.document.write(all_HTML)", 100);
	}else
	if(is.ie && !is.ie4){
	 win_menu.document.writeln(all_HTML);
	}

	win_menu.document.close();
	// ----------------------------------------------------------------------------------------//

	//------------------------------------------------- Draw Menu - End ---------------------//

	// Scroll document to top left corner
	win_menu.scroll(0,0);
	if(theHref != "") setTimeout("winContent.location.href = '"+theHref+"'", 250);
}

// -----------------------------------------------------------------------------------------------
// // Function definision - resize Netscape problem fixer from webmonkey.com
// -----------------------------------------------------------------------------------------------
function WM_netscapeCssFix() {
	// Redraw menu with correct menu index
	menuGenerate(rememberCurrentNumber,rememberCurrentIsExpanded);
}


function WM_netscapeCssFixCheckIn() {
	if ((navigator.appName == 'Netscape') && (parseInt(navigator.appVersion) >= 4)) {
   	 if (typeof document.WM == 'undefined'){
  		document.WM = new Object;
  	 }
  	 if (typeof document.WM.WM_scaleFont == 'undefined') {
  		document.WM.WM_netscapeCssFix = new Object;
  		document.WM.WM_netscapeCssFix.initWindowWidth = window.innerWidth;
  		document.WM.WM_netscapeCssFix.initWindowHeight = window.innerHeight;
  	 }
    	window.onresize = WM_netscapeCssFix;
  	}

}

WM_netscapeCssFixCheckIn();


// -----------------------------------------------------------------------------------------------
// Function definision - Rollover
// -----------------------------------------------------------------------------------------------
function changeImage(imgName,img,expanded) {
	if (win_menu.document.images)
		win_menu.document.images[imgName].src = img;
}
