////////////////////////////////////////////////////////////////////////////
// Main menu script v 1.4
// Written by Justin Gan
// 4/10/05
///////////////////////////////////////////
// Main Menu class

/*
 * MainmMenu::constructor
 *
 * Parameters
 * centred : boolean - Sets whether the menu is centred or not
 * leftWidth : integer - Width of the left offset of the 1st menu (pixels)
 * leftImage : string - URL of the image to be displayed in the left offset
 * interWidth : integer - Width of the interspace's between menus (pixels) 
 * interImage : string - URL of the image to be displayed in the interspace's between menus
 * rightImage : string - URL of the image to be displayed in the right offset
 * topOffset : integer - Top offset of the menu (pixels) - measured from top of window to top of menu (pixels)
 * menuHeight : integer - Height of the menu (when closed) (pixels)
 * defaultTimerDelay : integer - Default time for menus to slide down (ms). Defaults to 500ms if ""
 * defaultSlideOutDelay : integer - Default time for submenus to slide out (ms). Defaults to 250ms if ""
 * borderWidth : integer - Width of the border (pixels)
 * defaultStatusBar : string - Default string to be displayed in the window status bar
 */
function MainMenu (centred, leftWidth, leftImage, interWidth, interImage, rightImage, topOffset, menuHeight, defaultTimerDelay, defaultSlideOutDelay, borderWidth, defaultStatusBar) {
	if (arguments.length > 0)
		this.init (centred, leftWidth, leftImage, interWidth, interImage, rightImage, topOffset, menuHeight, defaultTimerDelay, defaultSlideOutDelay, borderWidth, defaultStatusBar);
};

/*
 * MainMenu::init
 * Initialises all the storage for this menu and caches any images.
 */
MainMenu.prototype.init = function (centred, leftWidth, leftImage, interWidth, interImage, rightImage, topOffset, menuHeight, defaultTimerDelay, defaultSlideOutDelay, borderWidth, defaultStatusBar) {
	this.menuBar = new Array ();
	this.interFillers = 0;
	this.leftWidth = leftWidth;
	this.interWidth = interWidth;
	this.topOffset = topOffset;
	this.defaultStatusBar = defaultStatusBar;
	if (leftImage != "") {
		this.leftImage = new Image ();
		this.leftImage.src = leftImage;
	}
	else
		this.leftImage = null;
	if (interImage != "") {
		this.interImage = new Image ();
		this.interImage.src = interImage;
	}
	else
		this.interImage = null;
	if (rightImage != "") {
		this.rightImage = new Image ();
		this.rightImage.src = rightImage;
	}
	else
		this.rightImage = null;
	this.topOffset = topOffset;
	this.menuHeight = menuHeight;
	this.defaultTimerDelay = (defaultTimerDelay == "" ? 500 : defaultTimerDelay);
	this.defaultSlideOutDelay = (defaultSlideOutDelay == "" ? 250 : defaultSlideOutDelay);
	this.borderWidth = (borderWidth == "" ? 0 : borderWidth);
	this.centred = centred;
	window.status = this.defaultStatusBar;
};

/*
 * MainMenu::calculateMenuHeights
 * Recursively calculates the menu and submenu left offsets, top offsets and heights of all menus 
 * attached to this mainmenu
 */
MainMenu.prototype.calculateMenuHeights = function () {
	var leftOffset = this.leftWidth;
	for (j = 0; j < this.menuBar.length; j++) {
		this.menuBar [j].left = leftOffset;
		this.menuBar [j].setTop (this.topOffset);
		this.menuBar [j].setClosedHeight (this.menuHeight);
		this.menuBar [j].calculateMenuHeights ();
		leftOffset += this.menuBar [j].getWidth ();
		if (j != this.menuBar.length - 1)
			leftOffset += this.interWidth;
	}
};

/*
 * MainMenu::generateMenuHTML
 * Recursively generates the HTML for this mainmenu and all associated menus
 */
MainMenu.prototype.generateMenuHTML = function () {
	var str = "";
	str += this.generateMenuLeftFiller ();
	var leftOffset = this.leftWidth;
	for (j = 0; j < this.menuBar.length; j++) {
		str += this.menuBar [j].generateHTML ();
		leftOffset += this.menuBar [j].getWidth ();
		if (j != this.menuBar.length - 1) {
			str += this.generateInterMenuFiller (leftOffset);
			this.interFillers++;
			leftOffset += this.interWidth;
		}
	}
	str += this.generateMenuRightFiller (leftOffset);
	for (j = 0; j < this.menuBar.length; j++)
		str += this.menuBar [j].generateSubMenuHTML ();
//	alert (str);
	if (document.all) {
		// IE
		document.body.insertAdjacentHTML ("beforeEnd", str);
	}
	else if (document.getElementById) {
		// Netscape
		var range = document.createRange ();
		range.setStartBefore (document.body.lastChild);
		var docFrag = range.createContextualFragment (str);
		document.body.appendChild (docFrag);
	}
};

/*
 * MainMenu::generateInterMenuFiller
 * Generates the HTML for the inter menu fillers
 */
MainMenu.prototype.generateInterMenuFiller = function (leftOffset) {
	var str = "";
	if (this.interWidth > 0) {
		str += "<DIV ID=\"interFiller_" + this.interFillers + "\" STYLE='position: absolute;";
		str += " width:" + this.interWidth + "; height:" + this.menuHeight + "; overflow: hidden;";
		str += " z-index:1; left: " + leftOffset + "; top:" + this.topOffset + ";'";
		str += ">\n<TABLE CELLSPACING=0 CELLPADDING=0 WIDTH=100%>\n<TBODY>\n";
		str += "<TR style='height:" + this.menuHeight + ";'>\n";
		str += "<TD style='height:" + this.menuHeight + ";";
		if (this.interImage != null)
			str += " background-image: url(" + this.interImage.src + ");";
		str += "' CLASS='menu_interfiller'";
		str += "></TD>\n</TR>\n";
		str += "</TBODY></TABLE></DIV>\n";
	}
	return str;
};

/*
 * MainMenu::generateMenuLeftFiller
 * Generates the HTML for the left menu filler
 */
MainMenu.prototype.generateMenuLeftFiller = function () {
	var str = "<DIV ID='LeftFiller' style='position: absolute;";
	str += " width:" + this.leftWidth + "; height:" + this.menuHeight + "; overflow: hidden;";
	str += " z-index:1; left:0;"
	str += " top:" + this.topOffset + ";'";
	str += ">\n<TABLE CELLSPACING=0 CELLPADDING=0 WIDTH=100% BORDER=0>\n<TBODY>\n";
	str += "<TR style='height:" + this.menuHeight + ";'>\n";
	str += "<TD style='height:" + this.menuHeight + ";";
	if (this.leftImage != null)
		str += " background-image: url(" + this.leftImage.src + ");";
	str += "' CLASS='menu_leftfiller'></TD>\n</TR>\n";
	str += "</TBODY></TABLE></DIV>\n";
//alert (str);
	return str;
};

/*
 * MainMenu::generateMenuRightFiller
 * Generates the HTML for the right menu filler
 */
MainMenu.prototype.generateMenuRightFiller = function (leftOffset) {
	var str = "<DIV ID='RightFiller' style='position: absolute;";
	if (document.body.clientWidth - this.calculateMenuWidth () < 0)
		str += " width:0;"
	else
		str += " width:" + (document.body.clientWidth - this.calculateMenuWidth ()) + ";"
	str += " height:" + this.menuHeight + "; overflow: hidden;";
	str += " z-index:1; left:" + leftOffset + ";"
	str += " top:" + this.topOffset + ";'";
	str += ">\n<TABLE CELLSPACING=0 CELLPADDING=0 WIDTH=100% BORDER=0>\n<TBODY>\n";
	str += "<TR style='height:" + this.menuHeight + ";'>\n";
	str += "<TD style='height:" + this.menuHeight + ";";
	if (this.rightImage != null)
		str += " background-image: url(" + this.rightImage.src + "); background-repeat: repeat;";
	str += "' CLASS='menu_rightfiller'></TD>\n</TR>\n";
	str += "</TBODY></TABLE></DIV>\n";
//alert (str);
	return str;
};

/*
 * MainMenu::calculateMenuWidth
 * Calculates the width of this menu incorporating the left menu filler and all inter menu fillers. The right 
 * menu filler is not incorporated into this calculation.
 */
MainMenu.prototype.calculateMenuWidth = function () {
	var width = this.leftWidth;
	for (j = 0; j < this.menuBar.length; j++) {
		width += this.menuBar [j].getWidth ();
		if (j != this.menuBar.length - 1)
			width += this.interWidth;
	}
	return width;
};

/*
 * MainMenu::loadMenuImages
 * Preloads all the images in this menu system into the cache
 */
MainMenu.prototype.loadMenuImages = function () {
	for (l = 0; l < this.menuBar.length; l++)
		this.menuBar [l].loadMenuImages ();
};

/*
 * MainMenu::findMenuItem
 * Recursively searches through the associated menus looking for a menuitem ID and returns that menuitem
 */
MainMenu.prototype.findMenuItem = function (ID) {
	var done = false;
	var menuitem = null;
	var i = 0;
	while (!done && i < this.menuBar.length) {
		menuitem = this.menuBar [i].findMenuItem (ID);
		done = menuitem != null;
		i++;
	}
	if (menuitem == null)
		alert ("MainMenu.prototype.findMenuItem " + ID + " = null");
	return menuitem;
};

/*
 * MainMenu::findMenu
 * Recursively searches through the associated menus looking for a menu or submenu ID and returns that menu
 */
MainMenu.prototype.findMenu = function (ID) {
	var done = false;
	var menu = null;
	var i = 0;
	while (!done && i < this.menuBar.length) {
		menu = this.menuBar [i].findMenu (ID);
		done = menu != null;
		i++;
	}
	if (menu == null)
		alert ("MainMenu.prototype.findMenu " + ID + " = null");
	return menu;
};

/*
 * MainMenu::addMenu
 * Adds the menu to this mainmenu
 */
MainMenu.prototype.addMenu = function (menu) {
	this.menuBar [this.menuBar.length] = menu;
};

/*
 * MainMenu::resize
 * If the mainmenu is centred, resizes the left and right menu fillers to take up the remaining window space
 * If the mainmenu is not centred, resizes the right menu filler to take up the remaining window space
 */
MainMenu.prototype.resize = function () {
	if (this.centred) {
		var w = this.calculateMenuWidth ();
		var leftspace = (document.body.clientWidth - w) / 2 - this.leftWidth;
		if (leftspace < 0)
			leftspace = this.leftWidth;
		var leftOffset = leftspace;
		document.getElementById ("LeftFiller").style.width = leftspace;
		for (i = 0; i < this.menuBar.length; i++) {
			this.menuBar [i].setLeft (leftOffset);
			leftOffset += this.menuBar [i].getWidth ();
			if (i != this.menuBar.length - 1) {
				document.getElementById ("interFiller_" + i).style.left = leftOffset;
				leftOffset += this.interWidth;
			}
		}
		document.getElementById ("RightFiller").style.left = leftOffset;
		if (this.leftWidth + (document.body.clientWidth - w) / 2 < 0)
			document.getElementById ("RightFiller").style.width = 0;
		else
			document.getElementById ("RightFiller").style.width = this.leftWidth + (document.body.clientWidth - w) / 2;
	}
	else {
		if (document.body.clientWidth - this.calculateMenuWidth () < 0)
			document.getElementById ("RightFiller").style.width = 0;
		else
			document.getElementById ("RightFiller").style.width = (document.body.clientWidth - this.calculateMenuWidth ());
	}
};

/*
 * MainMenu::getTimerDelay
 * Returns the default timer delay for menu slide downs (ms)
 */
MainMenu.prototype.getTimerDelay = function () {
	return this.defaultTimerDelay;
};

/*
 * MainMenu::getSlideOutDelay
 * Returns the default timer delay for submenu slide outs (ms)
 */
MainMenu.prototype.getSlideOutDelay = function () {
	return this.defaultSlideOutDelay;
};

/*
 * MainMenu::getBorderWidth
 * Returns the main menu border width (pixels)
 */
MainMenu.prototype.getBorderWidth = function () {
	return this.borderWidth;
};

/*
 * MainMenu::toString
 * Returns a string containing the list of parameters for this menu
 */
MainMenu.prototype.toString = function () {
	var str = "";
	var i = 0;
	for (i = 0; i < this.menuBar.length; i++)
		str += this.menuBar [i].toString (0) + "\n";
	return str;
};

///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////
// Globals

///////////////////////////////////////////
//	Mouse Event handler
var mouseX = 0;
var mouseY = 0;
// last event for Netscape
var lastEvent = null;

// Set up event capturing.
if (document.layers) // NN4 specific
	document.captureEvents (Event.MOUSEMOVE);
document.onmousemove = mouseMoveEventHandler;

/*
 * mouseMoveEventHandler
 * Handles the mouse movement events and ensures that the correct coordinates are calculated.
 * Browser independent
 */
function mouseMoveEventHandler (e) {
	lastEvent = e;
	if (document.layers) {
		// NN4
		mouseX = e.pageX;
		mouseY = e.pageY;
	}
	else if (document.all) {
		// IE
  		mouseX = window.event.clientX + document.body.scrollLeft;
	  	mouseY = window.event.clientY + document.body.scrollTop;
	}
	else if (document.getElementById) {
		// NN6
		mouseX = e.pageX;
		mouseY = e.pageY;
	}
};

