/*
    File:   menus.js
    Author: remco
    Date:   Thu Oct 3 00:35:10 PDT 2002

    Description:
      Menu functions -- to create the main menu and sub-menus.

    Copyright (c) 2002-2003, Remco Teunen.  All Rights Reserved.
*/

var g_mms = new Array();


function MenuItem(class_prefix, text, target) {
    this.m_class_prefix = class_prefix;
    this.m_text = text;
    this.m_target = target;
    this.m_index = -1;
    this.m_id = "none";
    this.m_menu_var = "none";
}

MenuItem.prototype.generateHTML = function()
{
    var args = "(" + this.m_index + ");";

    var html_str = 
	"<TD " +
	"ID = '" + this.m_id + "' " +
	"Class = '" + this.getClassNormal() + "' " +
	"OnMouseOver = '" + this.m_menu_var + ".eventOver" + args + "' " +
	"OnMouseOut = '" + this.m_menu_var + ".eventOut" + args + "' " +
	"OnClick = '" + this.m_menu_var + ".eventClick" + args + "' " +
	">\n" +
	"<LABEL>" + this.m_text + "</LABEL></TD>\n";

    // Return the string.
    return html_str;
}

MenuItem.prototype.setContext = function(menu_var, menu_index, index)
{
    this.m_index = index;
    this.m_menu_var = menu_var;
    this.m_id = "menu" + menu_index + "_" + index;
}

MenuItem.prototype.getTarget = function()
{
    return this.m_target;
}

MenuItem.prototype.getClassNormal = function()
{
    return this.m_class_prefix + "Normal";
}

MenuItem.prototype.getClassNormalOver = function()
{
    return this.m_class_prefix + "NormalOver";
}

MenuItem.prototype.getClassSelect = function()
{
    return this.m_class_prefix + "Select";
}

MenuItem.prototype.getClassSelectOver = function()
{
    return this.m_class_prefix + "SelectOver";
}

MenuItem.prototype.getID = function()
{
    return this.m_id;
}

function MMItem(text, target) {
    MenuItem.call(this, "MMItem", text, target);
}
MMItem.prototype = new MenuItem();


function MMHome(text, target) {
    MenuItem.call(this, "MMHome", text, target);
}
MMHome.prototype = new MenuItem();

function SMItem(text, target) {
    MenuItem.call(this, "SMItem", text, target);
}
SMItem.prototype = new MenuItem();

function SMText(text, target) {
    MenuItem.call(this, "SMText", text, target);
}
SMText.prototype = new MenuItem();



/*===========================================================================*
 * class _Menu
 *
 * _Menu class.
 *===========================================================================*/
function _Menu(target_frame) {

    this.m_selected_item = -1;
    this.m_items = new Array();
    this.m_doc = document;
    this.m_target_frame = target_frame;
}

//_Menu.prototype.addItem = function(item) {}
//_Menu.prototype.finish = function() {}

_Menu.prototype.loadTarget = function(target) {}

_Menu.prototype.eventOver = function(item_index)
{
    var new_class;
    var item = this.m_items[item_index];
    var element = this.m_doc.getElementById(item.getID());

    if (this.m_selected_item == item_index) {
	new_class = item.getClassSelectOver();
    }
    else {
	new_class = item.getClassNormalOver();
    }
    element.className = new_class;
}

_Menu.prototype.eventOut = function(item_index)
{
    var new_class;
    var item = this.m_items[item_index];
    var element = this.m_doc.getElementById(item.getID());

    if (this.m_selected_item == item_index) {
	new_class = item.getClassSelect();
    }
    else {
	new_class = item.getClassNormal();
    }
    element.className = new_class;
}

_Menu.prototype.eventClick = function(item_index, do_load)
{
    // Initialize do_load, if needed.
    if (do_load == undefined) do_load = true;

    /*----
     * Change the class of the item that is currently selected.
     *---------*/
    if (this.m_selected_item >= 0 && this.m_selected_item != item_index) {
	var orig_item = this.m_items[this.m_selected_item];
	var orig_element = this.m_doc.getElementById(orig_item.getID());
	orig_element.className = orig_item.getClassNormal();
    }
    /*----
     * Set the current selection.
     *-------------*/
    var item = this.m_items[item_index];
    var element = this.m_doc.getElementById(item.getID());

    element.className = item.getClassSelectOver();
    this.m_selected_item = item_index;

    /*------
     * Load the target, if a target is specified.
     *----*/
    if (item.getTarget() != null) {
	this.loadTarget(item.getTarget(), do_load);
    }
}

_Menu.prototype.loadSelectedItem = function(do_load)
{
    if (do_load == undefined) do_load = true;
    var item_index = this.m_selected_item;
    if (item_index < 0) item_index = 0;
    this.eventClick(item_index, do_load);
}

/*===========================================================================*
 * class MainMenu
 *
 * Main menu class.
 *===========================================================================*/
function MainMenu(target_frame, sm_id) {
    _Menu.call(this, target_frame);
    this.m_index = g_mms.length;
    this.m_target_frame = target_frame;
    this.m_sm_id = sm_id;
    this.m_cur_sm = null;
    g_mms[this.m_index] = this;
}
MainMenu.prototype = new _Menu();


MainMenu.prototype.addItem = function(item)
{
    // Add the item to the set.
    var menu_var = "g_mms[" + this.m_index + "]";
    item.setContext(menu_var, this.m_index, this.m_items.length);
    this.m_items.push(item);

}   /* end MainMenu.prototype.addItem */


MainMenu.prototype.finish = function()
{
    // Sanity check.
    if (this.m_items == 0) {
	throw "ERROR: No main menu items";
    }
    // Generate the start of the menu.
    document.writeln("<TABLE Class = 'MM' CellSpacing = 3><TR>");

    // Go over the items, and let them generate HTML.
    for (var i = 0; i < this.m_items.length; i++) {
	document.write(this.m_items[i].generateHTML());
    }
    // Finish the table.
    document.writeln("</TR></TABLE>");
    this.m_doc = document;

}   /* end MainMenu.prototype.finish */


MainMenu.prototype.loadTarget = function(target, do_load) 
{
    // Initialize do_load, if needed.
    if (do_load == undefined) do_load = true;

    // Finish the submenu.
    var menu_var = "g_mms[" + this.m_index + "]";
    var html_str = target.finish(
        menu_var + ".m_cur_sm", this.m_target_frame, this.m_doc);

    // Replace the submenu.
    this.m_cur_sm = target;
    var element = document.getElementById(this.m_sm_id);    
    element.innerHTML = html_str;

    // Load the selected item.
    target.loadSelectedItem(do_load);
}


MainMenu.prototype.gotoMenu = function(target, do_load) 
{
    // Initialize do_load, if needed.
    if (do_load == undefined) do_load = true;

    // If target is null, call the regular function.
    if (target == null) return this.loadSelectedItem(do_load);

    // Go through all the menus, and get the sub-menu that fits.
    for (var i = 0; i < this.m_items.length; i++) {

	// Get the submenu.
	var sm = this.m_items[i].getTarget();

	// Go through the submenu items.
	for (var j = 0; j < sm.m_items.length; j++) {

	    // Get the item.
	    var item = sm.m_items[j];
	    if (item.getTarget().indexOf(target) >= 0) {
		
		// Found match! Select this item.
		this.eventClick(i, false);
		sm.eventClick(j, do_load);
		//		this.eventClick(i, do_load);
		//		sm.eventClick(j, do_load);
		return;
	    }
	}
    }
    // Didn't find it. Must be bug.
    throw "ERROR: Didn't find target";
}
	

/*===========================================================================*
 * class SubMenu
 *
 * Sub menu class.
 *===========================================================================*/
function SubMenu() {
    _Menu.call(this, null);
}
SubMenu.prototype = new _Menu();


SubMenu.prototype.addItem = function(item)
{
    // Add the item to the set.
    this.m_items.push(item);

}   /* end MainMenu.prototype.addItem */


SubMenu.prototype.finish = function(sm_var, target_frame, doc)
{
    // Sanity check.
    if (this.m_items == 0) {
	throw "ERROR: No main menu items";
    }
    // Store the target frame and document.
    this.m_target_frame = target_frame;
    this.m_doc = doc;

    // Generate the start of the menu.
    var html_str = "<TABLE Class = 'SM' CellSpacing = 3><TR>";

    // Go over the items, and let them generate HTML.
    for (var i = 0; i < this.m_items.length; i++) {
	this.m_items[i].setContext(sm_var, sm_var, i);
	html_str += this.m_items[i].generateHTML();
    }
    // Finish the table.
    html_str += "</TR></TABLE>";

    // Done.
    return html_str;

}   /* end SubMenu.prototype.finish */


SubMenu.prototype.loadTarget = function(target, do_load) 
{
    // Initialize do_load, if needed.
    if (do_load == undefined) do_load = true;
    if (do_load == false) return;

    var target_frame = this.m_doc.getElementById(this.m_target_frame);
    target_frame.src = target;

}   /* end SubMenu::loadTarget */


/* eof menus.js */
