/*******************************************
 Script writer: Ireis, Liew Git Loong
 Script name: Ireis JS Menu Creator
 Version: 1.0.0
 Date: Sunday, October 30 2005
 Files include: menu.css
 *******************************************/

var RIGHT_CLICK = 2;

/**** Menu Class ****/
function Menu(name) {
	this.status = new Array("hidden", "visible");
	
	this.name = name;
	this.objName = null;
	
	this.itemCount = 0;
	this.menuItem = new Array();
	
	this.state = 0;
	this.posX = 0;
	this.posY = 0;
	
	this.addMenuItem = addMenuItem;
	this.addSeparator = addSeparator;
	this.buildMenu = buildMenu;
	this.setVisible = setVisible;
	this.setPosition = setPosition;
	
	this.popup = popup;
	this.popdown = popdown;
	this.actionPerformed = actionPerformed;
}

function actionPerformed(e) {
	var mouse_btn;
	var tempX = 0, tempY = 0;

	if(!e){
		tempX = event.clientX + parseInt(document.body.scrollLeft);
  	tempY = event.clientY + parseInt(document.body.scrollTop);
		mouse_btn = event.button;
	} else {
		tempX = e.pageX
		tempY = e.pageY
		mouse_btn = e.button
	}
	
	if(mouse_btn == RIGHT_CLICK)
		this.popup(tempX, tempY);
	else
		this.popdown();
	return false;
}

function popup(x, y) {
	this.setPosition(x, y);
	this.setVisible(1);
}

function popdown() {
	if(this.state == 1) {
		this.setPosition(0, 0);
		this.setVisible(0);
	}
}

function setPosition(x, y) {
	this.posX = x;
	this.posY = y;
	this.objName.style.left = this.posX + "px";
	this.objName.style.top = this.posY + "px";
}

function setVisible(index) {
	this.state = (index > 0) ? 1 : 0;
	this.objName.style.visibility = this.status[this.state];
}

function addSeparator() {
	var mItem = new menuItem(this, "separator", "-", "", "", "", 1);
	this.menuItem[this.itemCount] = mItem;
	this.itemCount++;
}

function addMenuItem(caption, itemname, linkto, tooltips, icon, enabled) {
	if(!linkto)
		var linkto = "";
	if(!tooltips)
		var tooltips = "";
	if(!icon)
		var icon = "";
	if(!enabled)
		var enabled = 1;
	if(caption == "-")
		var itemname = "separator";
		
	var mItem = new menuItem(this, itemname, caption, linkto, tooltips, icon, enabled);
	this.menuItem[this.itemCount] = mItem
	this.itemCount++;
}

function buildMenu() {
	var code = "";
	code += "<table border=\"0\" id=\""+this.name+"\" cellspacing=\"0\" cellpadding=\"0\"\n";
	code += " style=\"position:absolute;visibility:"+this.status[this.state]+";left:"+this.posx+"px;top:"+this.posy+"px;z-index:99\">\n";
	code += "	<tr>\n";
	code += "		<td colspan=\"2\" class=\"menu_topleft\"></td>\n";
	code += "		<td colspan=\"2\" class=\"menu_top\"></td>\n";
	code += "		<td class=\"menu_topright\"></td>\n";
	code += "  </tr>\n";
	code += "  <tr>\n";
	code += "    <td class=\"menu_left\"></td>\n";
	code += "    <td colspan=\"3\" class=\"menu_item\"></td>\n";
  code += "    <td class=\"menu_ttright\"></td>\n";
  code += "  </tr>\n";
  code += "	<tr>\n";
	code += "    <td class=\"menu_left\" rowspan=\""+(this.itemCount+1)+"\"></td>\n";
	code += "		<td style=\"height:1px;background-color:#FFFFFF\" colspan=\"3\"></td>\n";
	code += "    <td class=\"menu_right\" rowspan=\""+(this.itemCount+1)+"\"></td>\n";
	code += "	</tr>\n";
	
	for(var i=0; i<this.itemCount; i++) {
		code += this.menuItem[i].HTMLCode();
	}
	
	code += "	<tr>\n";
	code += "    <td colspan=\"2\" class=\"menu_bottomleft\"></td>\n";
	code += "    <td colspan=\"2\" class=\"menu_bottom\"></td>\n";
	code += "    <td class=\"menu_bottomright\"></td>\n";
	code += "  </tr>\n";
	code += "</table>\n";
	
	document.write(code);
	this.objName = document.getElementById(this.name);
	return code;
}


/* Menu Item Class and methods */
function menuItem(parent, itemname, caption, linkto, tooltips, imgicon, enabled) {
	this.parent = parent;
	this.name = itemname;
	this.caption = caption;
	this.linkpage = linkto;
	this.tooltips = tooltips;
	this.icon = imgicon;
	this.enabled = enabled;
	
	this.isSeparator = is_separator;
	this.HTMLCode = buildItem;
}

function is_separator() {
	return (this.caption == "-");
}

function buildItem() {
	var menu_class = new Array('menu_item', 'menu_highlight', 'menu_disabled', 'menu_disabled_highlight');
	var default_class = menu_class[0];
	var highlight_class = menu_class[1];
	if(!this.enabled) {
		default_class = menu_class[2];
		highlight_class = menu_class[3];
	}
	
	var htmlcode = "";
	if(this.isSeparator()) {
		htmlcode = "<tr>\n";
		htmlcode += "	<td class=\"menu_separator\" colspan=\"3\"></td>\n";
		htmlcode += "</tr>\n";
	}
	else {
		htmlcode = "<tr id=\""+this.name+"\" name=\""+this.parent.name+"_item\" class=\""+default_class+"\" title=\""+this.tooltips+"\"";
		if(this.linkpage != "" || this.enabled) {
			htmlcode += "\n onclick=\""+this.linkpage+"\"\n";
			htmlcode += "\n onmouseover=\"this.className='"+highlight_class+"'\" onmouseout=\"this.className='"+default_class+"'\"\n";
		} 
		htmlcode += ">\n";
		htmlcode += "<td class=\"menu_left_panel\"></td>\n";
	  htmlcode += "<td valign=\"middle\">";
		if(this.icon != "") {
			htmlcode += "<img src=\""+this.icon+"\" border=\"0\" width=\"12\" height=\"12\" vspace=\"2\" hspace=\"2\" align=\"absbottom\" />";
		}
		htmlcode += this.caption;
		htmlcode += "</td>\n";
	  htmlcode += "<td id=\""+this.name+"_sub\" class=\"menu_right_panel\" align=\"right\"></td>\n";
  	htmlcode += "</tr>";
	}
	return htmlcode;
}
/* End of Menu Item Class and methods */
