////////////////////////////////////////////////////////////////////////////
// Submenu script v 1.4
// Written by Justin Gan
// 4/10/05
////////////////////////////////////////////
//		SubMenu Object inherits Menu

// SubMenu inheritance
SubMenu.prototype = new Menu;
SubMenu.prototype.constructor = SubMenu;
SubMenu.superclass = Menu.prototype;

/* 
 * SubMenu::constructor
 *
 * Parameters
 * ID : string - unique string identifier for this submenu
 * leftOffset : integer - left offset for this submenu (pixels)
 * topOffset : integer - top offset for this submenu (pixels)
 * fullwidth : integer - width of this submenu when it is rolled out (pixels)
 * title : string - text to be displayed in the submenu header
 * description : string - text to be displayed in the status bar when this submenu is rolled over
 * hdnonrolloverimg : string - URL of the non-rollover image. May be "" for no image.
 * hdrolloverimg : string - URL of the rollover image. May be "" for no image.
 * hdheight : integer - height of the submenu header (pixels)
 * ftimg : string - URL of the submenu footer image. May be "" for no image.
 * ftheight : integer - height of the submenu footer (pixels)
 * parent : pointer - pointer to the parent menu
 * timerDelay : integer - Time for submenu to slide down (ms). Defaults to mainmenu.defaultTimerDelay if ""
 * slideOutDelay : integer - Time for submenu to slide out (ms). Defaults to mainmenu.defaultSlideoutDelay if ""
 * borderWidth : integer - Width of submenu border (pixels)
 * textClass : string - name of the class (defined in menustyles.css) for this submenu. 
 			If "", defaults to the "submenu" style.
 * rolloverScript : string - javascript to be run when this submenu is rolled over
 * rolloutScript : string - javascript to be run when the mouse rolls out of this submenu
 */
function SubMenu (ID, leftOffset, topOffset, fullwidth, title, description, hdnonrolloverimg, hdrolloverimg, hdheight, ftimg, ftheight, parent, timerDelay, slideOutDelay, borderWidth, textClass, rolloverScript, rolloutScript) {
	if (arguments.length > 0)
		this.init (ID, leftOffset, topOffset, fullwidth, title, description, hdnonrolloverimg, hdrolloverimg, hdheight, ftimg, ftheight, parent, timerDelay, slideOutDelay, borderWidth, textClass, rolloverScript, rolloutScript);
};

/*
 * SubMenu::init
 * Initialises all the storage for this menu and caches any images.
 */
 SubMenu.prototype.init = function (ID, leftOffset, topOffset, fullwidth, title, description, hdnonrolloverimg, hdrolloverimg, hdheight, ftimg, ftheight, parent, timerDelay, slideOutDelay, borderWidth, textClass, rolloverScript, rolloutScript) {
	SubMenu.superclass.init.call (this, ID, fullwidth, fullwidth, title, description, hdnonrolloverimg, hdrolloverimg, ftimg, ftheight, "", "", timerDelay, borderWidth, textClass, rolloverScript, rolloutScript, "");
	this.top = topOffset;
	this.left = leftOffset;
	this.parent = parent;
	this.parentItem = null;
	this.zindex = parent.zindex + 1;
	this.closedheight = hdheight;
	this.curwidth = 1;
	this.slideOutDelay = (slideOutDelay == "" && slideOutDelay != 0 ? mainMenu.getSlideOutDelay () : slideOutDelay);
	this.borderWidth = (borderWidth == "" ? this.parent.getBorderWidth () : borderWidth);
	if (textClass != "")
		this.textClass = textClass;
	else
		this.textClass = "submenu";
};

/*
 * SubMenu::displayMenu
 * Code called to display or hide this submenu.
 */
SubMenu.prototype.displayMenu = function () {
	if (this.showing) {
		if (window.status == mainMenu.defaultStatusBar)
			window.status = this.description;
		document.getElementById (this.ID).style.zIndex = this.zindex;
		document.getElementById (this.ID).style.visibility = "visible";
		document.getElementById (this.ID).style.top = this.getTop ();
		document.getElementById (this.ID).style.left = this.getLeft ();
		this.rolloutTimerEvent ();
	}
	else {
		window.status = mainMenu.defaultStatusBar;
		this.killTimerEvent ();
		// possible rollbackTimerEvent () ?
		if (this.hdnonrolloverimg != null)
			document.images [(this.ID + "_HD")].src = this.hdnonrolloverimg.src;
		this.curheight = 1;
		this.curwidth = 1;
		document.getElementById (this.ID).style.height = 1;
		document.getElementById (this.ID).style.zIndex = 1;
		document.getElementById (this.ID).style.visibility = "hidden";
		document.getElementById (this.ID).style.left = this.getUpperMostLeft ();
		document.getElementById (this.ID).style.top = this.getUpperMostTop ();
		this.parentItem.displayNonRollover ();
	}
};

/*
 * SubMenu::closeMenu
 * Closes the submenu
 */
 SubMenu.prototype.closeMenu = function () {
	mouseX = 0;
	mouseY = 0;
	this.showing = false;
	this.displayMenu ();
	this.parent.closeMenu ();
};

/*
 * SubMenu::getLeft
 * Returns the left offset of this submenu (pixels)
 */
SubMenu.prototype.getLeft = function () {
	if (this.parent.getLeft () + this.parent.fullwidth + this.left + this.fullwidth > document.body.clientWidth)
		return this.parent.getLeft () - this.left - this.fullwidth;
	else
		return this.parent.getLeft () + this.parent.fullwidth + this.left;
};

/*
 * SubMenu::getTop
 * Returns the top offset of this submenu (pixels)
 */
SubMenu.prototype.getTop = function () {
	return this.top;
};

/*
 * SubMenu::getWidth
 * Returns the full width of this submenu (pixels)
 */
SubMenu.prototype.getWidth = function () {
	return this.fullwidth;
};

/*
 * SubMenu::getUpperMostLeft
 * Returns the left most x-coordinate of all this menus parents
 */
SubMenu.prototype.getUpperMostLeft = function () {
	var left = 0;
	var menu = this.parent;
	while (menu != null) {
		left = menu.getLeft ();
		menu = menu.parent;
	}
	return left;	
};

/*
 * SubMenu::getUpperMostTop
 * Returns the top most y-coordinate of all this menus parents
 */
 SubMenu.prototype.getUpperMostTop = function () {
	var top = 0;
	var menu = this.parent;
	while (menu != null) {
		top = menu.getTop ();
		menu = menu.parent;
	}
	return top;	
};

/*
 * SubMenu::shiftX
 * Moves the left offset of this submenu and all submenus by xshift
 */
SubMenu.prototype.shiftX = function (xshift) {
	document.getElementById (this.ID).style.left += xshift;
	for (j = 0; j < this.menuitems.length; j++) {
		if (this.menuitems [j].isSubmenu ())
			this.menuitems [j].submenu.shiftX (xshift);
	}
};

/*
 * SubMenu::calculateMenuHeights
 * Recursively calculates the submenu top offsets and heights of all submenus
 */
SubMenu.prototype.calculateMenuHeights = function () {
//	this.curheight = 0;
	var topoffset = 0;
	if (this.hdrolloverimg != null || this.title != "")
		topoffset += this.closedheight;
	for (var i = 0; i < this.menuitems.length; i++) {
		if (this.menuitems [i].isSubmenu ()) {
			this.menuitems [i].submenu.setTop (topoffset + this.getTop () + this.menuitems [i].submenu.getTop ());
			this.menuitems [i].submenu.calculateMenuHeights ();
		}
		this.menuitems [i].ypos = topoffset;
		topoffset += this.menuitems [i].getHeight ();
	}
	if (this.ftimg != null)
		this.fullheight = topoffset + this.ftheight;
	else
		this.fullheight = topoffset;
};

/*
 * SubMenu::setShowing
 * Sets whether this submenu should be showing or not
 */
SubMenu.prototype.setShowing = function (show) {
	SubMenu.superclass.setShowing.call (this, show);
	if (show)
		this.parent.setShowing (show);
	else
		this.parent.setShowing (this.parent.checkShowing ());
};

/*
 * SubMenu::checkShowing
 * Returns true if this submenu should be showing
 * Browser independent
 */
SubMenu.prototype.checkShowing = function () {
	var show = false;
	// Check mouse position and this menu
	if (document.layers) {
		// NN4
		mouseX = lastEvent.pageX;
		mouseY = lastEvent.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 = lastEvent.pageX;
		mouseY = lastEvent.pageY;
	}	

	show = (mouseX <= this.getLeft () + this.fullwidth && mouseX >= this.getLeft () + 2 && 
			mouseY >= this.getTop () + 2 && mouseY <= this.getTop () + this.fullheight);

	if (!show)
		// Check to see if any submenus are showing
		show = this.checkChildrenShowing ();
	return show && this.showing;
};

/*
 * SubMenu::generateHTML
 * Generates the HTML for this submenu
 */
SubMenu.prototype.generateHTML = function () {
	var str = "<DIV ID=\"" + this.ID + "\" style='position: absolute;";
	str += " width:" + this.fullwidth + "; height:1; overflow: hidden; visibility: hidden;";
	str += " z-index:1; left:" + this.getUpperMostLeft () + ";"
	str += " top:" + (this.getUpperMostTop ()) + "; border-width: 0;'";
	str += " onMouseOver=\"mainMenu.findMenu ('" + this.ID + "').onMouseOver (); " + this.rolloverScript + "\"";
	str += " onMouseOut=\"mainMenu.findMenu ('" + this.ID + "').onMouseOut (); " + this.rolloutScript + "\"";
	str += ">\n<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 WIDTH=100%>\n<TBODY>\n";
	if (this.hdrolloverimg != null || this.title != "") {
		str += "<TR style='height:" + this.closedheight + ";'>\n";
		str += "<TD ID=\"" + this.ID + "_M\" style='height:" + this.closedheight + ";";
		str += " width: " + this.fullwidth + ";' CLASS=\"" + this.textClass + "\">";
		if (this.hdrolloverimg != null) {
			str += "<IMG STYLE='height: " +  (this.closedheight - 2*this.borderWidth);
			str += "; width: " + (this.fullwidth - 2*this.borderWidth) + ";' BORDER=0 ";
			str += "ALT=\"" + this.description + "\" ";
//str += "SRC=\"" + this.hdrolloverimg.src + "\" ";
			str += "ID=\"" + this.ID + "_HD\">";
		}
		else {
			str += this.title;
		}
		str += "</TD>\n</TR>\n";
	}	
	var furthersubmenus = false;
	for (i = 0; i < this.menuitems.length; i++) {
		if (this.menuitems [i].isSubmenu ())
			furthersubmenus = true;
		str += this.menuitems [i].generateHTML ();
	}
	if (this.ftimg != null) {
		str += "<TR style='height:" + this.ftheight + ";'>\n";
		str += "<TD style='height:" + this.ftheight + "; width: " + this.fullwidth + ";'>";
		str += "<IMG STYLE='height: " + this.ftheight + "; width: " + this.fullwidth;
		str += ";' BORDER=0 ALT=\"" + this.description + "\" ";
//str += "SRC=\"" + this.ftimg.src + "\" ";
		str += " ID=\"" + this.ID + "_FT\"></TD>\n</TR>\n";
	}
	str += "</TBODY></TABLE></DIV>\n";
	if (furthersubmenus) {
		for (i = 0; i < this.menuitems.length; i++)
			if (this.menuitems [i].isSubmenu ())
				str += this.menuitems [i].submenu.generateHTML ();
	}
	return str;
};

/*
 * SubMenu::loadMenuImages
 * Preloads all the images in this associated submenus into the cache
 */
SubMenu.prototype.loadMenuImages = function () {
	if (this.hdrolloverimg != null)
		document.getElementById (this.ID + "_HD").src = this.hdnonrolloverimg.src;
	if (this.ftimg != null)
		document.getElementById (this.ID + "_FT").src = this.ftimg.src;
	for (u = 0; u < this.menuitems.length; u++)
		this.menuitems [u].loadMenuImages ();
};

/*
 * SubMenu::rolloutTimerEvent
 * Callback function - called on every timer event tick
 * Rolls out the submenu to its full width
 * Rolls down the submenu to its full height
 */
 SubMenu.prototype.rolloutTimerEvent = function () {
	if (this.slideOutDelay != 0) {
		if (this.curwidth < this.fullwidth) {
			document.getElementById (this.ID).style.height = this.closedheight;
			this.curheight = this.closedheight;
			this.curwidth += this.fullwidth * (this.timerDelayStep / this.slideOutDelay);
			if (this.curwidth >= this.fullwidth) {
				this.curwidth = this.fullwidth;
				document.getElementById (this.ID).style.width = this.curwidth;
				this.rolloutTimerEvent ();
				return;
			}
			document.getElementById (this.ID).style.width = this.curwidth;
			if (this.curwidth < this.fullwidth)
				this.timerID = window.setTimeout ("mainMenu.findMenu ('" + this.ID + "').rolloutTimerEvent ()", this.timerDelayStep);
		}
		else {
			if (this.hdrolloverimg != null)
				document.images [(this.ID + "_HD")].src = this.hdrolloverimg.src;
			if (this.timerDelay != 0) {
				this.curheight += (this.fullheight - this.closedheight) * (this.timerDelayStep / this.timerDelay);
				if (this.curheight >= this.fullheight) {
					this.curheight = this.fullheight;
					this.timerID = null;
				}
				document.getElementById (this.ID).style.height = this.curheight;
				if (this.curheight < this.fullheight)
					this.timerID = window.setTimeout ("mainMenu.findMenu ('" + this.ID + "').rolloutTimerEvent ()", this.timerDelayStep);
			}
			else {
				this.curheight = this.fullheight;
				document.getElementById (this.ID).style.height = this.curheight;
			}
		}
	}
	else {
		this.curwidth = this.fullwidth;
		document.getElementById (this.ID).style.width = this.curwidth;
		if (this.hdrolloverimg != null)
			document.images [(this.ID + "_HD")].src = this.hdrolloverimg.src;
		this.curheight += (this.fullheight - this.closedheight) * (this.timerDelayStep / this.timerDelay);
		if (this.curheight >= this.fullheight) {
			this.curheight = this.fullheight;
			this.timerID = null;
		}
		document.getElementById (this.ID).style.height = this.curheight;
		if (this.curheight < this.fullheight)
			this.timerID = window.setTimeout ("mainMenu.findMenu ('" + this.ID + "').rolloutTimerEvent ()", this.timerDelayStep);
	}
};
