<!--
/*
** Script:     menulinks.js
** Name:       Menu links
** Version:    1.2
** Author:     Scott Brisbane (Scott.Brisbane@qed.qld.gov.au)
** Date:       30.01.2002
** Compatible: Tested with IE5.5, NS 6.2, NS 4.51 on PC
** Modified:   08.03.2002
**
** Updates:		1.1 -> 1.2 : Added menuArea detection, so all menu items can be in one SSI.
*/

var itemArray = new Array();
var itemCount = 0;
	
function menuItem(itemName, itemSource, menuArea) {
/*---
Purpose: The constructer for the menuItem object
Assumptions: 
	- itemSource must be a full URL (ie. http://education.qld.gov.au/item.htm) or forward from the current location (ie. item.htm). It must not begin with a forward slash (/item.htm).
---*/
this.itemName = itemName;
this.itemSource = itemSource;
this.menuArea = menuArea;
}

function addItem(name, source, area) {
/*---
Purpose: Adds a new menu item to the itemArray
---*/
	newItem = new menuItem(name, source, area);
	itemArray[itemCount] = newItem;
	itemCount++;
}

function getURLFilename(URL){
/*---
Purpose: Returns the name of file (including the extension) at the end of a given URL
Outputs: URLFile (String)
---*/
	//store the index of the first letter of the filename, which is 1 after the last forward slash "/"
	var CutIndex = URL.lastIndexOf("/") + 1;
	//store the length of the filename
	var FileLength = URL.length - CutIndex;

	//return the file name
	return URL.substring(CutIndex, FileLength);
} //getURLFilename()

function getURLPath(URL){
/*---
Purpose: Returns the URL without the file on the end. (eg. http://education.qld.gov.au/index.html would return all but index.html).
Outputs: URLPath (String)
---*/
	//store the index of the first letter of the filename, which is 1 after the last forward slash "/"
	//store the index in the URLfrom where to cut
	var CutIndex = URL.lastIndexOf("/") + 1;

	//return the URL path
	return URL.substring(CutIndex, 0);
} //getURLPath()

function compareLocations(fullURL, givenURL){
/*---
Purpose: Returns whether or not two URLs contain the same pathname, or if the givenURL is only a filename it will return whether filename matches the one given in pageURL 
Outputs: True or False (Boolean)
---*/
	var result = false;

	//check if full URL (using http://) was given for givenURL
	if (givenURL.substring(0, 7) == "http://") {
		//if givenURL equals pageURL then result of comparison is true
		if (givenURL == fullURL) {
			result = true;
		} //end if
	} else {
		//if pathway and filename are the same as the pageURL then result is true
		if ((getURLPath(fullURL) + givenURL) == fullURL) {
			result = true;
		} //end if
	} //end if

	return result;
} //compareLocations(fullURL, givenURL)
	
function getMenuArea(URL){
/*---
Purpose: Returns the area number of the menu to be displayed
Outputs: menuArea(String)
---*/
	var menuArea;
	//Cycle through itemArray until a match is found
	for (var i = 0; i < itemArray.length; i++) {
		if (compareLocations(URL, itemArray[i].itemSource) == true) {
			menuArea = itemArray[i].menuArea;
			i = itemArray.length;
		} //if
	} //for

	//return the URL path
	return menuArea;
} //getMenuArea()

function displayMenu(classStyle, extraCode, beginCode, endCode){
/*---
Purpose: Displays the items in the array, itemArray, to the screen.
Outputs: itemArray contents to screen
Assumptions/Constraints: 
	- itemArray.itemSource is either the full URL or location from current directory. It must not begin with a forward slash (/) or contain two dots to go down a directory (../)
	- extraCode includes code that one wants within the <a> tag, except class="blah" which should be provided in classStyle.
	- classStyle should be just the name of the class (ie. menuStyle, not class="menuStyle").
---*/

	//Store current document href
	var dochref = document.location.href;
	var menuArea = getMenuArea(dochref);
	//if a class was given, set classStyle
	if (classStyle != "") {
		classStyle = 'class="' + classStyle + '" ';
	} //if

	//if itemArray is not empty
	if (itemArray.length != 0) {
		//cycle through all items in itemArray displaying appropriately
		for (var i = 0; i < itemArray.length; i++) {
			//Check if current menu item in array is a part of the menu to be displayed
			if (itemArray[i].menuArea == menuArea) {
				//if file of currently viewed webpage is equal to the item source file, then display without link; else display with link		
				if (compareLocations(dochref, itemArray[i].itemSource) == true) {
					document.write(beginCode + '<span ' + classStyle + '>' + itemArray[i].itemName + '</span>' + endCode);
				} else {
					document.write(beginCode + '<a href="' + itemArray[i].itemSource + '" ' + classStyle + extraCode + '>' + itemArray[i].itemName + '</a>' + endCode);
				} //if
			} //if
		} //for
	} //if
} // displayMenu(classStyle, extraCode, beginCode, endCode)