/**
  menu.js

  function mnAddTitle(title, link, id)
  function mnAddItem(title, link, id)
  function mnAddDivider(link)
  function mnPrintMenu()
  function mnClear()
 
  Copyright (c) 2007 New Fire
 */

mnImgDivider = "";

// select item menu's background color
mnBackgroundColor = "#BBBBEE";

var mnSelected = null;

/**
  4.3: set cookie for the selected menu title.
 */
function mnSelect(element)
{
  mnSetId(element.id);
  // highlight this element
  mnHiLi(element);

  // unhighlight previous element if any
  if (mnSelected)
  {
    mnLoLi(mnSelected);
  }

  mnSelected = element;
}
	
/**
  4.6  Preselect an menu item.
 */
function mnSetId(id)
{
  cmSetCookie("mnId", id);
}

/**
  4.3: Get the previously selected menu title.
  4.6: 1. Use cookie "mnTitle" to remember the menu selected
       2. Get cookie of the menu selected
       3. Clear this cookie so that a new menu will not use it.
 */
function mnGetId()
{
  return cmGetCookie("mnId");
}

function mnClearId()
{
  return cmClearCookie("mnId");
}

// deprecated
function mnClicked(name)
{
}

/**
  highlight
 */
function mnHiLi(element)
{
  if (element.style)
  {
/*
    //if (element.id != mnGetId() && element.style)
    if (element.id == mnGetId())
    {
      element.style.backgroundColor = mnBackgroundColor; 
      element.style.cursor = "pointer";
    }
    else
    {
      element.style.cursor = "default";
    }
*/
    element.style.backgroundColor = mnBackgroundColor;
    element.style.cursor = "pointer";
  }
}

/**
  unhighlight
 */
function mnLoLi(element)
{
  //if (element.id != mnGetId() && element.style)
  // 4.6 Always unhighlight
  if (element.style)
  {
    //element.style.color = "#FFFFFF";
    element.style.backgroundColor = "";
    element.style.cursor = "default";
  }
}

/**
  Menu 
 */

var mnTitles = new Array;
var mnTitlesSize = 0;
var mnItems = new Array;
var mnItemsSize = 0;

/**
  MenuItem Class
  divider: e.g. ---, ***, <img ...>
 */
function MenuItem(title, link, id, divider)
{
  this.title = title;  // diplay title
  this.link = link;
  this.id = id;        
  this.divider = divider;

  // 4.6: a unique id (optional)
  if (id == null)
  {
    this.id = title;        
  }
}

function mnAddTitle(title, link, id, divider)
{
  mnTitles[mnTitlesSize] = new MenuItem(title, link, id, divider);
  mnTitlesSize++;
}

function mnAddItem(title, link, id, divider)
{
  mnItems[mnItemsSize] = new MenuItem(title, link, id, divider);
  mnItemsSize++;
}

/**
  Vertical Menu
 */
function mnPrintMenu()
{
  // Print menu titles 
  document.writeln("<table border=0 cellpadding=0 cellspacing=0 width=100%>"); 

  if (mnTitlesSize > 0)
  {
    for (var i=0; i<=mnTitlesSize-1; i++)
    {
      mnPrintRow('title', mnTitles[i]);
    }

    //mnPrintRow('menutitle', mnTitles[mnTitlesSize-1]);
  }


  document.writeln("</table>");
  document.writeln("<br>");

  // Print menu items
  document.writeln("<table border=0 cellpadding=0 cellspacing=0 width=100%>"); 

  for (var i=0; i<mnItemsSize; i++)
  {
    mnPrintRow('item', mnItems[i]);
    if (mnImgDivider)
    {
      document.writeln("<tr><td><img src='"+mnImgDivider+"'></td></tr>");
    }
  }

  document.writeln("</table>");
  document.writeln("<br>");

  mnClear();
}

/*
  type is "title" or "item"
 */
function mnPrintRow(type, menuitem)
{
  document.writeln("  <tr>");
  if (menuitem.link)
  {
    if (type == "title")
    {
      if (mnGetId() == menuitem.id)
      {
        type= "menu"+type;
      }
      else
      {
        type= "menu"+type+"i";
      }
    }
    else
    {
      type= "menuitem";
    }

    document.writeln("<td"
      +" id='"+menuitem.id+"'"
      +" class="+type
      +" onmouseover='mnHiLi(this)' onmouseout='mnLoLi(this)'"
      +" onclick=\"mnSelect(this);"+menuitem.link+"\">"
      +menuitem.title
      +"</td>"
      ); 
  }
  else
  {
    document.writeln("<td class=menu"+type+">"+menuitem.title+"</td>"); 
  }
  document.writeln("</tr>");

  if (menuitem.divider)
  {
    document.writeln("<tr><td align=center>"+menuitem.divider+"</td></tr>");
  }
}

/**
  Horizontal Menu
 */
function mnPrintMenu2()
{
  document.writeln("<table border=0 cellpadding=0 cellspacing=0 width=100%>"); 
  document.writeln("  <tr>");

  // Print menu titles 
  for (var i=0; i<mnTitlesSize; i++)
  {
    if (mnTitles[i].link)
    {
      document.writeln("    <td class=menutitle2"
        +" onmouseover=\"mnHiLi(this)\" onmouseout=\"mnLoLi(this)\""
        +" onclick=\""+mnTitles[i].link+"\">"
        +mnTitles[i].title
        +"</td>"
        ); 
    }
    else
    {
      document.writeln("    <td class=menutitle2>"
        +mnTitles[i].title
        +"</td>"
        ); 
    }
  }

  // separate zone
  document.writeln("    <td width=10000>&nbsp;</td>");

  // Print menu items
  for (var i=0; i<mnItemsSize; i++)
  {
    document.writeln("    <td class=menuitem2"
      +" onmouseover=\"mnHiLi(this)\" onmouseout=\"mnLoLi(this)\""
      +" onclick=\""+mnItems[i].link+"\">"
      +mnItems[i].title
      +"</td>"
      ); 
  }

  document.writeln("  </tr>");
  document.writeln("</table>");

  mnClear();
}

/**
  Since 4.5: Print menu, clear it, add more, print more.
 */
function mnClear()
{
  mnTitles = new Array;
  mnTitlesSize = 0;
  mnItems = new Array;
  mnItemsSize = 0;

  mnClearId();
}

