// The original ideas for the menu scripts came from the menu generator in www.braveheart.com
// The ideas have been much modified since then to enable me to build the menus
// at run time and keep maintenance easier 
// Ken Odam

var NextMain = 0 ;		// The number of the current Menu item.  Menu0 is not used

var NextSub = 0	;		// Similarly for SubMenu items - separately numberred for each Menu item

var BuildHtml = "" ;	// The string being built up for the menu which will be inserted
						//   into each web page
						
var CurrentPage = ""	// This will be filled in by loadTemplate
var CurrentPageIndex	// where it will be in the arrays

var CurrentMenu = ""	// A note of the curent menu for use by submenus						
						
var MenuItems = new Array ()
var MenuLevels = new Array ()
var MenuDisplay = new Array ()
MenuDisplay [ 0 ] = 0 ;
var CurrentMenu = 0
var CookieFound = false

function getCookie(NameOfCookie){
    if (document.cookie.length > 0) {              
    begin = document.cookie.indexOf(NameOfCookie+"=");       
    if (begin != -1) {           
      begin += NameOfCookie.length+1;       
      end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
        return unescape(document.cookie.substring(begin, end));
    } 
  }
  return null;
}

function setCookie(NameOfCookie, value, expiredays) {
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

	document.cookie = NameOfCookie + "=" + escape(value) + 
		((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function SaveCookie()
{	// Ths cookie is saved as a single string, not an array as I first thought!
	setCookie ( 'WCSCookie', MenuDisplay + ",", 1 ) ; // extra comma  on end for ease of reading
}

function LoadCookie()
{
    var tempCookie ;
    var count ;
    var CommaPosition ;

	tempCookie=getCookie('WCSCookie');
	if (tempCookie==null) {
		CookieFound = false ;
	} else {
		CookieFound = true ;
		// Now extract the various values from the cookie string
		count = 0 ;
		while ( tempCookie.search(",") != -1 ) {
			CommaPosition = tempCookie.search(",") ;
			MenuDisplay [ count ] = tempCookie.substr(0,CommaPosition) ;

			tempCookie = tempCookie.substr(CommaPosition + 1) ;
			count++ ;	
		}
	}
}

function NewMenu ( MenuTitle, MenuPopUpTitle, MenuURL, MenuLevel ) {
	
	// This builds the html for the next Menu item 
	// MenuTitle is the string displayed on the menu
	// MenuURL is the address to go to when the item is clicked
	
	var ThisClass
	var InitialClass
	
	CurrentMenu++ ;
	
    MenuItems [ CurrentMenu ] = MenuTitle ;
    MenuLevels [ CurrentMenu ] = MenuLevel ;
    if ( CookieFound == false ) {
		// if no cookie, only show top level
		if ( MenuLevel == 1 ) {
			MenuDisplay [ CurrentMenu ] = 1 ;
		} else {
			MenuDisplay [ CurrentMenu ] = 0 ;
		}
    }
    
    ThisClass = "Menu" + MenuLevel ;
    	
    if ( MenuDisplay [ CurrentMenu ] == 1 ) {
		InitialClass = ThisClass ;
	} else {
		InitialClass = ThisClass + "_hidden" ;
    }
   
    // Make sure blank pointers don't go wrong
    // I copied this from www.braveheart.com and don't understand it - but it works !
    if ( MenuURL == "" ) { MenuURL = "javascript:void(0)" }
    	
    if ( CurrentPage == MenuTitle ) { 
		TextClass = "Menu_text_current" ;
		CurrentPageIndex = CurrentMenu ;	
	} else {
		TextClass = "Menu_text" ;
	}
    
	BuildHtml = BuildHtml + "<div id=" + MenuTitle ;
	BuildHtml = BuildHtml + ' class="' + InitialClass + '"' ;
	BuildHtml = BuildHtml + ' onmouseover="roll_dropmenu(' + "'" + MenuTitle + "', '" + ThisClass + "_over')" + '"' ;
	BuildHtml = BuildHtml + ' onmouseout="roll_dropmenu(' + "'" + MenuTitle + "', '" + ThisClass + "')" + '">' ;
	BuildHtml = BuildHtml + ' <a href="' + MenuURL + '"' ;
	BuildHtml = BuildHtml + ' class="' + TextClass + '"' ;
	
	if ( MenuPopUpTitle != "" )
	{
		BuildHtml = BuildHtml + ' title="' + MenuPopUpTitle + '"' ;
	}
	
	BuildHtml = BuildHtml + ' onClick="toggle_dropmenu(' + "'" + MenuTitle + "'" + ')">' ;
	BuildHtml = BuildHtml + ' ' + MenuTitle ;
	BuildHtml = BuildHtml + ' </a></div>' ;
}	

function toggle_dropmenu(id){
	// This changes the show/hide status of the lower submenus
	// It is used to switch on and off the submenu items when the parent
	//   menu item is clicked

	var SomethingChanged = false
	var ThisLevel

    // Find the clicked menu id
    count = 1 ;
    while ( ( MenuItems [ count ] != id ) && ( MenuLevels [ count ] > 0 ) ) {
		count++ ; 
	} ;

    if ( MenuLevels [ count ] == 0 )
    {
		return ;
    }
    
	ThisLevel = MenuLevels [ count ] ;

	count++ ; // Next item
    if ( MenuLevels [ count ] > ThisLevel ) {
		// Continue if the next item is a lower level
		
		SomethingChanged = true ; // at least it will be in the next few lines !
		
		// There is a next level down, check if it is showing
		if ( MenuDisplay [ count ] == 0 ) {
			// It wasn't showing, so show the next level down
			while ( MenuLevels [ count ] == ( ThisLevel + 1 ) ) {
				document.getElementById(MenuItems [ count ]).style.display = 'block' ;
				MenuDisplay [ count ] = 1 ;
				count++ ;
			}
		} else {
			// It was showing, so hide all levels below here
			while ( MenuLevels [ count ] > ThisLevel ) {
				document.getElementById(MenuItems [ count ]).style.display = 'none' ;
				MenuDisplay [ count ] = 0 ;
				count++ ;
			}
		} ;
    }
    
    if ( SomethingChanged==true ) { 
		SaveCookie() ; 
	}
}


function roll_dropmenu(id, roll_class){
	// This sets the menu items display characteristics as the mouse rolls over or off them
	document.getElementById(id).className = roll_class;
	
} 


function loadTemplate(Where_Am_I) {

    var count
    var level

	LoadCookie () ;
	
	// This sets up each page to match the template by filling in the named blanks
	
	CurrentPage = Where_Am_I ;
	
	// Top left is the WCS logo
	
	wcs_logo.innerHTML='<img src="WCS_logo_web1.gif">' ;
	wcs_logo.bgColor="deepskyblue" ;
  
	// Top centre heading
	
	x=   '<div align=center>' ;
	x=x+   '<font size=10>' ;
	x=x+     'Woodbridge Choral Society' ;
	x=x+   '</font>' ;
	x=x+ '</div>' ;
   
	wcs_heading.innerHTML = x ;
	wcs_heading.bgColor="deepskyblue" ;
	
	
	// Top right logo for sponsor, or generic sponsor if we don't have a real one
	
	x =  '<a href="http://www.suffolkradiation.co.uk">' ;
	x=x+ '  <img src="SRTS.gif" alt="Radiation Protection Advisors" title="Radiation Protection Advisors">' ;
	x=x+ '</a>' ;

	wcs_sponsor.innerHTML =x ;
	wcs_sponsor.bgColor="deepskyblue" ;
	
	
	// Menu for the left side of the screen
 
	NewMenu ( "Home", "", "Index.html", 1 ) ;
	NewMenu ( "Introduction", "A word from our Chairman", "Introduction.html", 1 ) ;
	//NewMenu ( "Concerts", "", "", 1 ) ;
//		NewMenu ( "Future", "", "", 1 ) ;
// 			NewMenu ( "Nov2007", "", "Nov2007.html", 2 ) ;
// 			NewMenu ( "Mar2008", "", "Mar2008.html", 2 ) ;
		NewMenu ( "Past", "", "Past.html", 1 ) ;
// 			NewMenu ( "May2007", "", "May2007-0.html", 2 ) ;
// 			NewMenu ( "Nov2006", "", "Nov2006.html", 2 ) ;
// 			NewMenu ( "Mar2006", "", "Mar2006.html", 2 ) ;
// 			NewMenu ( "Nov2005", "", "Nov2005.html", 2 ) ;
// 			NewMenu ( "Mar2005", "", "Mar2005.html", 2 ) ;
// 			NewMenu ( "Tobias2004", "", "Tobias2004.html", 2 ) ;
	NewMenu ( "Gallery", "", "Gallery.html", 1 ) ;
	NewMenu ( "Friends", "", "Friends.html", 1 ) ;
	NewMenu ( "Sponsors", "", "Sponsors.html", 1 ) ;
	NewMenu ( "Members", "", "", 1 ) ;
		NewMenu ( "Information", "", "Information.html", 2 ) ; 
		NewMenu ( "Newsletters", "", "", 2 ) ;
			NewMenu ( "Newsletter2009", "", "Newsletter2009.html", 3 ) ;
			NewMenu ( "Newsletter2008", "", "Newsletter2008.html", 3 ) ;
			NewMenu ( "Newsletter2007", "", "Newsletter2007.html", 3 ) ;
			NewMenu ( "Newsletter2006", "", "Newsletter2006.html", 3 ) ;
			NewMenu ( "Newsletter2005", "", "Newsletter2005.html", 3 ) ;
			NewMenu ( "Newsletter2004", "", "Newsletter2004.html", 3 ) ;
			NewMenu ( "Newsletter2003", "", "Newsletter2003.html", 3 ) ;
	NewMenu ( "JoinUs", "", "JoinUs.html", 1 ) ;
	NewMenu ( "ContactUs", "", "ContactUs.html", 1 ) ;
	NewMenu ( "Links", "Other sites about Woodbridge and local music", "Links.html", 1 ) ;
	NewMenu ( "Disclaimer", "", "Disclaimer.html", 1 ) ;

	// End marker for menus  
	CurrentMenu++ ;
	MenuLevels [ CurrentMenu ] = 0 ;

    // Logos to show the pages have been checked	

	BuildHtml = BuildHtml + '<p align=center>' ;
	BuildHtml = BuildHtml + '  <a href="http://validator.w3.org/check?uri=referer">' ;
	BuildHtml = BuildHtml + '    <img align="middle" border="0"' ;
	BuildHtml = BuildHtml + '         src="http://www.w3.org/Icons/valid-html401"' ;
	BuildHtml = BuildHtml + '         alt="This is valid HTML 4.01 - click here to check for yourself" height="31" width="88">' ;
	BuildHtml = BuildHtml + '  </a>' ;
	BuildHtml = BuildHtml + '</p> ' ;
//	BuildHtml = BuildHtml + '<p align=center> ' ;
//	BuildHtml = BuildHtml + '  <a href="http://jigsaw.w3.org/css-validator/"> ' ;
// 	BuildHtml = BuildHtml + '    <img style="border:0;width:88px;height:31px" ' ;
//	BuildHtml = BuildHtml + '         src="http://jigsaw.w3.org/css-validator/images/vcss" ' ;
//	BuildHtml = BuildHtml + '         alt="This is valid CSS - click here to check for yourself">' ;
//	BuildHtml = BuildHtml + '  </a>' ;
//	BuildHtml = BuildHtml + '</p>' ;


	// Now put all the menu bits into the current page   
	wcs_menu.innerHTML = BuildHtml ;
	
	wcs_menu.bgColor="deepskyblue" ;


	// Finally, check if the current page title is displayed in the menu, it might not
	// be if the page has been accessed from history or 'back' etc
	
	if ( MenuDisplay [ CurrentPageIndex ] == 0 )
	{
		// not displayed, so open all the levels above it to ensure it is seen
		count = CurrentPageIndex ;
		while ( ( MenuLevels [ count ] > 1 ) && ( MenuDisplay [ count ] == 0 ) )
		{
			// Find the first item at the level above this one
			level = MenuLevels [ count ] ;
			do {
				count-- ;
			} while ( MenuLevels [ count ] == level ) ;
			toggle_dropmenu ( MenuItems [ count ] ) ;
		}
	}

}
