/*
 * This is a file of JavaScript data and functions used to draw the menus
 *
 * The first thing you need to know is that anything between a / * and a * /
 * is a comment.  Like this one, comments can span multiple lines
 * Comments can also start with / /
 */ 

thisFile = 'pkweb.js' ;		// update this variable if file gets renamed


/*
 * menus are defined as an array of text for the link and the path
 * to the desitination.  The path must be relative to the homepage
 * of the site.
 */
stdMenus = [
'Home',			'index.htm', 
'History',		'info/history_of_park_street.htm',
'Class 1',		'class_1/class_1.htm',
'Class 2',		'class_2/class_2.htm',
'Class 3',		'class_3/class_3.htm',
'Class 4',		'class_4/class_4.htm',
'Staff',		'staff_pages/staff.htm',
'Prospectus <br>& Admissions',		'info/prospectus_and_admissions.htm',
'School Clubs',		'clubs/clubs.htm',
'Children\'s Area',	'misc/children_area.htm',
'Parents\' Area',	'misc/parents_area.htm',
'OFSTED',		'info/ofsted_information.htm',
'School Governors',	'governors/governors.htm',
'Events',		'misc/events.htm',
'Websites',		'info/websites.htm',
'Contact us', 		'misc/contact_us.htm'
];


subMenus = new Object();

/*
 * subMenus are a little bit more complicated but basically you use this
 * syntax to name the main menu item that you want the subMenu to follow.
 * The submenu is activated by a "class" tag in a page"
 */
subMenus.Children = new Object();
subMenus.Children.after = 'Children\'s Area' ;
subMenus.Children.menus = [
'School Council',	'misc/school_council.htm',
'Play Leaders',		'misc/play_leaders.htm'
];

subMenus.Parents = new Object();
subMenus.Parents.after = 'Parents\' Area' ;
subMenus.Parents.menus = [
'Classes',		'misc/classes_at_park_street.htm',
'Useful Information',	'info/useful_information.htm',
'Term Dates',		'info/term_dates_2005.htm',
'PTA',			'pta/pta.htm'
];

subMenus.Governors = new Object();
subMenus.Governors.after = 'School Governors' ;
subMenus.Governors.menus = [
'SIP',	'governors/sip.htm',
'Documents',	'governors/documents.htm',
'Meetings',	'governors/meetings.htm',
'Resources',	'governors/resources.htm'
];


function writeMenu() {
    var hasSubMenu = false;
    var subMenu;

    toHome = findHome();

    /*
     * find the class of the body to see if it has any submenus
     */

    bodyClass = document.body.className ;

    if (bodyClass != '')
    {
	for (i in subMenus)
	{
	    if (bodyClass == i)
	    {
		hasSubMenu = true;
		subMenu = subMenus[i];
		break;
	    }
	}
    }
    
   	document.write('<ul class="mainMenu">');
	document.write('<img border="0" src="' + toHome
	 + 'images/icons/swan.jpg" width="44" height="39" alt="Little Swan">');

    for (var i = 0 ; i < stdMenus.length ; i += 2)
    {
	writeMenuEntry(stdMenus[i],stdMenus[i+1],toHome);
	if (hasSubMenu && stdMenus[i] == subMenu.after)
	{
	    writeSubMenu(subMenu.menus,toHome);
	}
    }

    document.write('<li><a href="http://www.ccceducation.net">'
	+ 'Cambridgeshire <br>Education Portal</a>') 
    document.write('<li><a href="http://www.starz.org.uk">'
	+ 'Starz Pupil Portal<br><img border="0" src="' + toHome
	+ 'images/icons/starz.gif" width="66" height="63" alt="Starz"></a>')  
    document.write('<li><a href="http://validator.w3.org/check?uri=referer">'
	+ '<img src="' + toHome +'images/icons/valid_html401.gif" '
	+ 'alt="Valid HTML 4.01 Transitional" height="31" width="88"></a>');

    document.write('</ul>');
}

function writeMenuEntry(string,link,toHome)
{
    document.write('<li><a href="' + toHome + link + '">' + string + '</a>');
}

function writeSubMenu(subMenu,toHome)
{
    document.write('<ul class="subMenu">');

    for (i = 0 ; i < subMenu.length ; i+= 2)
    {
	writeMenuEntry(subMenu[i],subMenu[i+1],toHome);
    }

    document.write('</ul>');
}

function findHome()
{
    /*
     * this function finds where we are in the document hierarchy
     * by examining the document for this JavaScript source file.
     * For now it relies on this file being in the root directory although it
     * wouldn't be hard to change the logic to allow it to be in a subdirectory
     *
     * N.B. The name of this JavaScript file must be accurately stored in the
     * global variable thisFile.
     */

    var result = '' ; // default to the top level


    var scriptSrc ;	// the path and filename containing a script
    var pos ;		// the position in scriptSrc of thisFile

    /*
     * first, get a list of all the script files loaded by this page
     * This is potentially browser-dependent.  It does work for Firefox and IE
     * but might fail for older or more exotic browsers.
     */
    var scripts = document.getElementsByTagName("SCRIPT");

    /*
     * now loop over all the scripts
     */
    for (var i = 0 ; i < scripts.length ; i++)
    {
	scriptSrc = scripts[i].src ;
	// Curiously, IE gives a relative path (e.g. ../pkweb.js) but
	// Mozilla based browsers give an absolute path.  Luckily for us, it
	// doesn't matter which we get.

	// look for thisFile in scriptSrc
	pos = scriptSrc.search(thisFile) ;
	if (pos != -1)
	{
	    /*
	     * if we've found pkweb.js (pos != -1) and if it isn't the first
	     * character then our answer is everything from the start of the
	     * string up to, but not including, the filename
	     */
	    if (pos > 0)
	    {
		result = scriptSrc.substring(0,pos)
	    }
		break;	// we've found what we're looking for so break out of the loop
	}
	
    }

    return result ;
}

// Add colours here in hex format for random background colours on
// pages which call the random_colour function.

var mycolours = [
{ col : '#ccffcc'},

{ col : '#99ccff'},

{ col : '#ffccff'},

{ col : '#99ffff'},

{ col : '#ccffff'},

{ col : '#cc99cc'},

{ col : '#ffcc99'},

{ col : '#ccff99'},

{ col : '#99ff99'},

{ col : '#FFFF66'}
];


function random_colour(){
    var ry=Math.floor(Math.random()*mycolours.length)
    document.bgColor = mycolours[ry].col;
    if(mycolours.length>1){
	mycolours[ry]=mycolours[mycolours.length-1];
	mycolours.length--;
    }
}
