// Copyright Serge Raty, 2002.
// This code cannot be used for commercial purposes without consent of the author.


//**************************************************************
//  Menu Class for DOM Browsers and Netscape 4
//**************************************************************
function Menu (objectName, locationId, menuFrame, defTargetFrame, closingflag) {

  // FOR NON-DOM COMPLIANT BROWSERS: THE REFRESH RE-WRITES THE ENTIRE MENU FRAME.
  //  the html file located in the menu frame should include:
  //  1. The html code that appears before the menu.
  //  2. A call to the menu's toHtml() method:
  //     <script language="javascript"> document.write(parent.chmenu.toHtml()); </script>
  //  3. The html code that appears after the menu.

  // PARAMETERS
  //  objectName:      string that contains the name of the MenuClass object you are declaring.
  //  locationId:      string that contains the ID of the <DIV> tag in which the menu is.
  //  menuFrame:       string name of the Frame object in which the menu is located.
  //  defTargetFrame:  string name of the Frame object in which the links should be loaded by default.
  //  closingflag:     (optional) '1' to have only one open branch at a time.
  this.objectName     = objectName;
  this.locationId     = locationId;
  this.menuFrame      = menuFrame;
  this.defTargetFrame = defTargetFrame;
  this.closingflag    = false;

  // ADDITIONAL PARAMETERS
  //  insertBeforeItem:  html inserted before each menu item.
  //  insertInsideItem:  html inserted between "image" and "caption".
  //  insertAfterItem:   html inserted after "caption".
  //  levelSpacing:      html inserted between "insertBeforeItem" and "image" for each level of the menu.
  //  defOpenImg:        html for the menu items default 'open'   image.
  //  defClosedImg:      html for the menu items default 'closed' image.
  //  defLeafImg:        html for the menu items default 'leaf'   image.
  //  closeMessage:      status bar message = click to close a branch. Should not include any quote.
  this.insertBeforeItem = "<table border='1' cellpadding='1' cellspacing='0'><tr><td valign='top' nowrap>";
  this.insertInsideItem = "</td><td valign='top'>";
  this.insertAfterItem  = "</td></tr></table>\n";
  this.levelSpacing     = "<font face='courier'>&nbsp;</font></td><td valign='top'>";
  this.defOpenImg       = "<font face='courier'>X</font>";
  this.defClosedImg     = "<font face='courier'>O</font>";
  this.defLeafImg       = "<font face='courier'>-</font>";
  this.closeMessage     = "- Close submenu -";

  // DATA
  this.children      = new Array();
  this.items         = new Array();
  this.levelCounter  = 0;
  this.isDOM = false;

  // METHODS
  this.addChild     = Menu_addChild;
  this.open         = Menu_open;
  this.close        = Menu_close;
  this.toHtml       = Menu_toHtml;
  this.refresh      = Menu_refresh;

  // CONSTRUCTOR
  // Set Browser version
  var appVersion = parseInt(navigator.appVersion);
  this.isDOM = (document.getElementById)? true : false;
  if (closingflag == 1 | closingflag == "1" | closingflag == "true" | closingflag == "TRUE") {
    this.closingflag = true;
  }

}
//--------------------------------------------------------------


  function Menu_addChild(item) {
    this.children[this.children.length] = item;
  }

  function Menu_open(index) {
	var indexItem = this.items[index];
    if (this.closingflag) {            // Closing all branches
      for (var idx = 0; idx < this.items.length; idx++)  this.items[idx].status = 0;
      var current = indexItem;
      current.status = 1;
      while(current.parentItem) {      // Opening all Parents of this Item
        current = current.parentItem;
        current.status = 1;
      }
    }
    else                               // Opening this Item
      indexItem.status = 1;
    this.refresh();                    // Refresh the menu
    indexItem.loadUrl();               // Load the url when branch is opened
  }

  function Menu_close(index) {
    this.items[index].status = 0;      // Closing this Item
    this.refresh();                    // Refresh the menu
  }

  function Menu_toHtml() {
    var str = "";                      // Calling toHTML() for all the Items
    for (var cnt = 0; cnt < this.children.length; cnt++) {
      var mitem = this.children[cnt];
      str += mitem.toHtml();
    }
    return str;
  }

  function Menu_refresh() {
    // For DOM Compliant Browsers : refresh the menu div.
    if (this.isDOM) {
      var menuDiv = null;
      if (frames[this.menuFrame])
        menuDiv = frames[this.menuFrame].document.getElementById(this.locationId);
      else menuDiv = document.getElementById(this.locationId);
      menuDiv.innerHTML = this.toHtml();
    }
	// For non-DOM Compliant Browsers : reload the complete menu page.
	// Target frame is loaded through html (href).
    else
	  if (frames[this.menuFrame]) frames[this.menuFrame].location.reload();
  }


//--------------------------------------------------------------
//  MenuItem Class
//--------------------------------------------------------------

function MenuItem(parentItem, style, url, caption) {

  // PARAMETERS
  //  parentItem:  the Item's parent Item.
  //  style:       style specifications to be applied to caption text.
  //  url:         url to load in the content frame.
  //  caption:     caption *title text* only.
  this.parentItem   = parentItem;
  this.style        = style;
  this.url          = url;
  this.caption      = caption;

  // ADDITIONAL PARAMETERS
  //  targetFrame:  string name of the Frame object in which the url should be loaded.
  //  openImage:    html for the menu item 'open'   image.
  //  closedImage:  html for the menu item 'closed' image.
  //  leafImage:    html for the menu item 'leaf'   image.
  this.targetFrame  = null;
  this.openImage    = null;
  this.closedImage  = null;
  this.leafImage    = null;

  // DATA
  this.menu         = null;          // the Item's menu.
  this.children     = new Array();   // the Item's children Items.
  this.index        = 0;
  this.status       = 0;             // 0:closed  1:open

  // METHODS
  this.addChild  = MenuItem_addChild;
  this.toHtml    = MenuItem_toHtml;
  this.loadUrl   = MenuItem_loadUrl;

  // CONSTRUCTOR
  this.constructor = MenuItem_constructor;

  this.constructor();
}
//--------------------------------------------------------------


// Constructor for the Menu Item
  function MenuItem_constructor() {
	// Registering this Item with the menu and the parent.
    this.menu = (this.parentItem.menu)? this.parentItem.menu : this.parentItem;
    this.index = this.menu.items.length;
    this.menu.items[this.menu.items.length] = this;
    this.parentItem.addChild(this);
  }

// Set the parent/child relationship
  function MenuItem_addChild(subItem) {
    this.children[this.children.length] = subItem;
    subItem.parentItem = this;
  }

// Load the url in the target frame.
  function MenuItem_loadUrl() {
	my_targetFrame = (this.targetFrame)? this.targetFrame : this.menu.defTargetFrame;
    if (this.url) {
	  if (frames.length > 0) frames[my_targetFrame].location.href = this.url;
	  else {
	    contentWindow = window.open(this.url, my_targetFrame);
      }
    }
  }

//***************************************************************************************
// Generate the Html code
//***************************************************************************************
  function MenuItem_toHtml() {
    var str = this.menu.insertBeforeItem;

    // LEVEL SPACING
    for (var i = 0; i < this.menu.levelCounter; i++)   str += this.menu.levelSpacing;

    // Using the Menu's values as defaults.
    var my_targetFrame = (this.targetFrame)? this.targetFrame : this.menu.defTargetFrame;
    var my_openImage   = (this.openImage  )? this.openImage   : this.menu.defOpenImg  ;
    var my_closedImage = (this.closedImage)? this.closedImage : this.menu.defClosedImg;
    var my_leafImage   = (this.leafImage  )? this.leafImage   : this.menu.defLeafImg  ;

    // Simple Anchor with style url and target
    var simpleAnchor = "<a " + this.style;
    if (this.url)       simpleAnchor += " href='"   + this.url       + "'";
    if (my_targetFrame) simpleAnchor += " target='" + my_targetFrame + "'";

    // LEAF ITEM
    //-----------------------------------------------------------------------------------
    if (this.children.length == 0) {
	  str += my_leafImage + this.menu.insertInsideItem;
      // Simple Anchor on Text for Leaf Items.
      str += simpleAnchor + ">" + this.caption + "</a>";
      str += this.menu.insertAfterItem;
    }

    // BRANCH ITEM
    //-----------------------------------------------------------------------------------
    else {
      // Complex Anchor with onClick attribute
      var complexAnchor = simpleAnchor + " onClick='parent." + this.menu.objectName + ".";
      complexAnchor += (this.status == 0)? "open" : "close";
      complexAnchor += "(" + this.index + "); return false;'";

      // Complex Anchor on Image for Branch Items.
      str += complexAnchor;
      // ...plus onMouseOver/Out for open Branch Items (added a setTimeout() to turn a NN6 bug)
      if (this.status != 0) {
		str += " onMouseOver=\"setTimeout('window.status=\\'" + this.menu.closeMessage + "\\'',0);";
		str += " return true;\" onMouseOut=\"window.status=''; return true;\"";
      }
      str += ">";
      str += (this.status == 0)? my_closedImage : my_openImage;
      str += "</a>" + this.menu.insertInsideItem;
      // Complex Anchor on Text for closed Branch Items; Simple Anchor for open BI.
      str += (this.status == 0)? complexAnchor : simpleAnchor;
      str += ">" + this.caption + "</a>"
      str += this.menu.insertAfterItem;

	  my_targetFrame = (this.targetFrame)? this.targetFrame : this.menu.defTargetFrame;
      if (this.status == 1 || (frames.length == 0 && !this.menu.isDOM)) {
      // When open, call the method for all the children.
        for (var subcnt = 0; subcnt < this.children.length; subcnt++) {
          var subitem = this.children[subcnt];
          this.menu.levelCounter++;
          str += subitem.toHtml();
          this.menu.levelCounter--;
        }
      }
    }
    return str;
  }



