////////////////////////////////////////////////////////////////////////////
// MenuItem script v 1.4
// Written by Justin Gan
// 4/10/05
////////////////////////////////////////////
//		MenuItem Object

/*
 * MenuItem::constructor
 *
 * Parameters
 * ID : string - unique string identifier for this menuitem
 * text : string - text to be displayed in the menuitem
 * description : string - text to be displayed in the status bar when this menuitem is rolled over
 * target : string - ID of the target window that the link will be displayed in
 * url : string - URL of the menuitem link 
 * bkgndimage : string - URL of the background image. May be "" for no image.
 * nonrolloverimage : string - URL of the non-rollover image. May be "" for no image.
 * rolloverimage : string - URL of the rollover image. May be "" for no image.
 * height : integer - height of this menuitem in pixels
 * border : integer - size of the border for this menuitem in pixels
 * textClass : string - name of the class (defined in menustyles.css) for this menuitem. 
 			If "", defaults to the "menuitem" style.
 * rolloverScript : string - javascript to be run when this menuitem is rolled over
 * rolloutScript : string - javascript to be run when the mouse rolls out of this menuitem
 * clickScript : string - javascript to be run when this menuitem is clicked
 */
function MenuItem (ID, text, description, target, url, bkgndimage, nonrolloverimage, rolloverimage, height, border, textClass, rolloverScript, rolloutScript, clickScript) {
	if (arguments.length > 0)
		this.init (ID, text, description, target, url, bkgndimage, nonrolloverimage, rolloverimage, height, border, textClass, rolloverScript, rolloutScript, clickScript);
};

/*
 * MenuItem::init
 * Initialises all the storage for this menuitem and caches any images.
 */
MenuItem.prototype.init = function (ID, text, description, target, url, bkgndimage, nonrolloverimage, rolloverimage, height, border, textClass, rolloverScript, rolloutScript, clickScript) {
	this.ID = ID;
	this.parent = null;
	this.text = text;
	this.description = description;
	this.target = target;
	this.url = url;

	// no need to cache this image as it doesn't change
	this.bkgndimage = bkgndimage;

	if (nonrolloverimage != "") {
		this.nonrolloverimage = new Image ();
		this.nonrolloverimage.src = nonrolloverimage;
	}
	else
		this.nonrolloverimage = null;
	if (rolloverimage != "") {
		this.rolloverimage = new Image ();
		this.rolloverimage.src = rolloverimage;
	}
	else
		this.rolloverimage = null;
	this.ypos = 0;
	this.border = border;
	this.height = height;
	if (textClass == "")
		this.textClass = "menuitem";
	else
		this.textClass = textClass;
	this.fullwidth = 0;
	this.submenu = null;

	this.rolloverScript = rolloverScript;
	this.rolloutScript = rolloutScript;
	this.clickScript = clickScript;
};

/*
 * MenuItem::setStyles
 * Sets the default styles for this menuitem
 */
MenuItem.prototype.setStyles = function () {
	this.border = (this.border == "" ? this.parent.getBorderWidth () : this.border);
};

/*
 * MenuItem::getID
 * Returns the unique ID for this menuitem
 */
MenuItem.prototype.getID = function () {
	return this.ID;
};

/*
 * MenuItem::getText
 * Returns the text displayed for this menuitem
 */
MenuItem.prototype.getText = function () {
	return this.text;
};

/*
 * MenuItem::getHeight
 * Returns the height of this menuitem in pixels
 */
MenuItem.prototype.getHeight = function () {
	return this.height;
};

/*
 * MenuItem::setWidth
 * Sets the width of this menuitem in pixels
 */
MenuItem.prototype.setWidth = function (width) {
	this.fullwidth = width;
};

/*
 * MenuItem::isSubmenu
 * Returns true if this menuitem contains a submenu
 */
MenuItem.prototype.isSubmenu = function () {
	return this.submenu != null;
};

/*
 * MenuItem::setSubmenu
 * Sets this menuitem to contain a submenu. Also initialises the corrsponding reciprocal parent link in the submenu 
 * to point to this menuitem.
 */
MenuItem.prototype.setSubmenu = function (submenu) {
	submenu.parentItem = this;
	this.submenu = submenu;
};

/*
 * MenuItem::getSubmenu
 * Returns the submenu contained within this menuitem
 */
MenuItem.prototype.getSubmenu = function () {
	return this.submenu;
};

/*
 * MenuItem::onMouseOver
 * Code run when the mouse is rolled over this menuitem
 */
MenuItem.prototype.onMouseOver = function () {
	// Sets the status bar to display the description text
	window.status = this.description;
	// Changes the displayed image to display the rollover image if defined
	if (this.rolloverimage != null)
		document.getElementById (this.ID).src = this.rolloverimage.src;
	else
		if (this.submenu != null)
			// Change the style of this menuitem to be sm_textClass_rollover
			document.getElementById (this.ID + "_MI").className = "sm_" + this.textClass + "_rollover";
		else
			// Change the style of this menuitem to be textClass_rollover
			document.getElementById (this.ID + "_MI").className = this.textClass + "_rollover";
	// Displays this menuitems submenu if there is one and it is not already displayed
	if (this.submenu != null) {
		this.submenu.setShowing (true);
		this.submenu.displayMenu ();
	}
};

/*
 * MenuItem::onMouseOut
 * Code run when the mouse rolls out of this menuitem
 */
MenuItem.prototype.onMouseOut = function () {
	// Sets the status bar to display nothing
	window.status = "";
	// Hides this menuitems submenu if there is one and it is not already hidden
	if (this.submenu != null) {
		this.submenu.setShowing (this.submenu.checkShowing ());
		this.submenu.displayMenu ();
		if (!this.submenu.isShowing ())
			if (this.nonrolloverimage != null)
				// Change the displayed image to display the non-rollover image if defined
				document.images [this.ID].src = this.nonrolloverimage.src;
			else
				// Change the style of this menuitem to be sm_textClass_nonrollover
				document.getElementById (this.ID + "_MI").className = "sm_" + this.textClass + "_nonrollover";
	}
	else {
		if (this.nonrolloverimage != null)
			// Change the displayed image to display the non-rollover image if defined
			document.images [this.ID].src = this.nonrolloverimage.src;
		else
			// Change the style of this menuitem to be textClass_nonrollover
			document.getElementById (this.ID + "_MI").className = this.textClass + "_nonrollover";
	}
};

/*
 * MenuItem::onClick
 * Code run when the mouse is left-clicked on this menuitem
 */
MenuItem.prototype.onClick = function () {
	if (this.url != "") {
		// if a url is defined, set the source of the target window to url
		document.getElementById (this.target).src = this.url;
		if (this.submenu != null) {
			// if this menuitem contains a submenu, close it
			this.submenu.closeMenu ();
			if (this.nonrolloverimage != null)
				// Change the displayed image to display the non-rollover image if defined
				document.images [this.ID].src = this.nonrolloverimage.src;
			else
				// Change the style of this menuitem to be sm_textClass_nonrollover
				document.getElementById (this.ID + "_MI").className = "sm_" + this.textClass + "_nonrollover";
		}
		else {
			if (this.nonrolloverimage != null)
				// Change the displayed image to display the non-rollover image if defined
				document.images [this.ID].src = this.nonrolloverimage.src;
			else
				// Change the style of this menuitem to be textClass_nonrollover
				document.getElementById (this.ID + "_MI").className = this.textClass + "_nonrollover";
		}
		// Close the parent menu as well
		this.parent.closeMenu ();
	}
};

/*
 * MenuItem::displayNonRollover
 * Changes this menuitem to display the non-rollover style
 */
MenuItem.prototype.displayNonRollover = function () {
	if (this.nonrolloverimage != null)
		// Set the menuitem image to display the non-rolloverimage if defined
		document.images [this.ID].src = this.nonrolloverimage.src;
	else
		if (this.submenu != null)
			// Set the style of this menuitem to be sm_textClass_nonrollover if a submenu is attached to this menuitem
			document.getElementById (this.ID + "_MI").className = "sm_" + this.textClass + "_nonrollover";
		else
			// Set the style of this menuitem to be textClass_nonrollover if no submenu is attached to this menuitem
			document.getElementById (this.ID + "_MI").className = this.textClass + "_nonrollover";
};

/*
 * MenuItem::generateHTML
 * Returns a string containing the HTML for this menuitem
 */
MenuItem.prototype.generateHTML = function () {
	var str = "<TR style='height: " + this.height + ";'>\n";
	str += "<TD style='height: " + this.height + "; width: " + this.fullwidth + "; ";
	if (this.bkgndimage != "")
		str += " background-image: url(" + this.bkgndimage + ");";
	str += "' ID=\"" + this.ID + "_MI\"";
	str += " CLASS=\"" 
	if (this.submenu != null)
		str += "sm_";
	str += this.textClass + "_nonrollover\"";
	str += " onMouseOver=\"mainMenu.findMenuItem ('" + this.ID + "').onMouseOver (); " + this.rolloverScript + "\"";
	str += " onMouseOut=\"mainMenu.findMenuItem ('" + this.ID + "').onMouseOut (); " + this.rolloutScript + "\"";
	str += " onClick=\"mainMenu.findMenuItem ('" + this.ID + "').onClick (); " + this.clickScript + "\"";
	str += ">";
	if (this.nonrolloverimage != null) { // Non rollover image present
		str += "<IMG STYLE='height: " +  (this.height - 2*this.border) + "; width: ";
		str += (this.fullwidth - 2*this.border) + ";' BORDER=0 ID=\"" + this.ID + "\""
		str += " ALT=\"" + this.description + "\""; 
		//str += "SRC=\"" + this.nonrolloverimage.src + "\"";
		str += ">";
	}
	else {	// No rollover image present
		str += this.text;
	}
	str += "</TD>\n</TR>\n";
//alert (str);
	return str;
};

/*
 * MenuItem::generateSubMenuHTML
 * Recursively generates the submenu html if a submenu exists for this menuitem
 */
MenuItem.prototype.generateSubMenuHTML = function () {
	var str = "";
	if (this.submenu != null)
		str = this.submenu.generateHTML ();
	return str;
};

/*
 * MenuItem::loadMenuImages
 * Preloads all the images in this menuitem and associated submenus into the cache
 */
MenuItem.prototype.loadMenuImages = function () {
	if (this.nonrolloverimage != null)
		document.getElementById (this.ID).src = this.nonrolloverimage.src;
	if (this.submenu != null)
		this.submenu.loadMenuImages ();
};

/*
 * MenuItem::toString
 * Returns a string containing the list of parameters for this menuitem
 */
MenuItem.prototype.toString = function (index) {
	var str = "";
	for (j = 0; j < index; j++)
		str += "\t";
	str += this.ID;
	if (this.submenu != null) {
		str += "\n";
		str += this.submenu.toString (index + 1);
	}
	return str;
};
