///////
//
//	This file defines a few classes - anything that is in a menu (ie, a menu item, divider, submenu, etc) goes in here
//
///////////
function CMItem(txt, myURL, icon, help) {
	this.text = txt;
	this.URL = myURL;
	this.icon = icon;		// path to an image
	this.help = help;
	this.enabled = true;	// grey it out or not
	this.toggler = false;	// if this item is a toggler
	this.pressed = false;	// if it's been pushed in

	this.subNeedle = "%s";	// this is the string searched for in the url text replacement
	this.subIgnore = "%i";	// this string will be deleted and kill off a parameter, allowing optionals to be sent

	this.draw = function (subText) {
		if (this.URL == undefined || this.URL == "") this.URL = "#";
		var out = "";

		var classes = "";
		if (!this.enabled) classes += "disabled ";
		if (this.pressed) classes += "pressed ";
		
		var outputURL = this.URL;
		
		if (this.enabled) {
			if (subText != undefined && subText.constructor == Array) {		//subtext is an array object
				for (var i=0; i < subText.length; i++) {
					var theRegex = new RegExp(this.subNeedle+"|"+this.subIgnore, '');
					var myMatch = theRegex.exec(outputURL);
					if (myMatch[0] == this.subIgnore) {
						outputURL = outputURL.replace(theRegex, '');
						continue;
					} else if (myMatch[0] == this.subNeedle) {
						outputURL = outputURL.replace(theRegex, subText[i]);
						continue;
					}
				}
			} else {
				outputURL = this.URL.replace(new RegExp(this.subNeedle, "g"), subText);
			}
		}
		else
			outputURL = "#";
		
		out += "<a href=\""
			+ outputURL + "\""
			+ (classes == "" ? "" : " class=\"" + classes + "\"")
			+ (this.toggler ? " onclick=\"menuHandler.innerClick(); return false;\"" : "")
			+ ">";
		
		if (this.help != undefined && this.help != "") {
			out += "<img src=\"images/help.png\" onmouseover=\"setActiveHelp(this, '"+this.help+"');\" "
			out	+= "style=\"float: right; margin: -2px 0 -2px 0\">";
		}
		
		if (this.icon != undefined && this.icon != "") {
			out += "<img src=\"" + this.icon + "\">";
		}

		out += this.text;
		out += "</a>";
		return out;
	}
}

function CMDivider() {
	this.draw = function () {
		return "<hr noshade size=\"1\">";
	}
}

function CMLabel(txt) {
	this.text = txt;
	this.draw = function () {
		return "<div class=\"CMLabel\" onclick=\"menuHandler.innerClick()\">" + this.text + "</div>";
	}
}

function CMSubmenu(txt, items, icon) {
	this.text = txt;
	this.items = items;
	this.icon = icon;
	this.subId = 0;	// this gets changed when the item (this submenu) is added into the menu.
	this.linkId = "";

	this.draw = function () {
		return "<a href=\"#\" id='" + this.linkId + "' onmouseover=\"showSubmenu(" + this.subId + ", this);\" onclick=\"menuHandler.innerClick(); return false;\" onmouseout=\"closeSubmenu();\" class=\"submenu\">"
			 + (this.icon ? "<img src=\"" + this.icon + "\">" : "")
			 + this.text
			 + "</a>";
	}

	this.drawItems = function (subText) {
		var out = "";
		for (var i = 0; i < this.items.length; i++) {
			out += this.items[i].draw(subText);
		}
		return out;
	}
}
