/*
* qMenus.js version 1.0, 2005/10/25
* Written by William Korb
* Copyright (c) 2005-present, QISC, Inc.
*
* QISC grants you a royalty free license to use or modify this
* software provided that this copyright notice appears on all copies.
* This software is provided "AS IS," without a warranty of any kind.
*
* This menu system is implemented using JavaScript and Cascading
* Style Sheets (CSS). In addition to this file, you will also need
* to include a css with your default menu styles.
*/
var qmImageBase = '';
var menuIndex = 0;
var qmbr = '';
qmSetImageBase('/images');  // default
var qmMaxChars = 25;
var qmCharsPerIndent = 3;
var qmMinChars = 12;
var qMenus = new Array();
function qMenu (label, showLabel) {
/*
* Menu attributes:
*/
  this.menuIndex = menuIndex;
  qMenus[menuIndex] = this;
  menuIndex++;
  this.label = label;
  this.items = new Array();
  this.listStyle = 'none';  /* none = collapsed, inline = expanded */
  this.showLabel = true;  /* false = anonymous "root" menu, true = named menu */
  if ( showLabel != null && showLabel == false ) {
    this.listStyle = 'inline';  /* none = collapsed, inline = expanded */
    this.showLabel = false; /* false = anonymous "root" menu, true = named menu */
  }
  this.isMenu = true;
  this.customAction = null;

/*
* Menu methods:
*/
  this.addItem = addItem;
  this.addMenu = addMenu;
  this.addCustomAction = addCustomAction;
  this.displayMenu = displayMenu;
  this.expand = expand;
  this.collapse = collapse;
  this.expandAll = expandAll;
  this.collapseAll = collapseAll;
  this.toggleState = toggleState;
}

/*
* Menu methods:
*/
function addItem(label, action) {
  this.items[this.items.length] = new qMenuItem(label, action);
}

function addMenu(label, showLabel) {
  if ( showLabel == null ) showLabel = true;
  var newMenuIndex = this.items.length;
  this.items[newMenuIndex] = new qMenu(label, showLabel);
  return(this.items[newMenuIndex]);
}

function addCustomAction(action) {
  this.customAction = action;
}

function displayMenu(indentLevel) {
  if ( this.isMenu == null || ! this.isMenu ) return;
  if ( indentLevel == null ) indentLevel = 0;
  if ( indentLevel > 0 ) document.writeln('<li>');
  var addedStyle = '';
  var listClass = 'class="qmul"';
  if ( indentLevel == 0 ) {
    addedStyle = ' margin:0px; padding:0px;';
    listClass = '';
  }
  if ( this.showLabel ) {
    document.write(
      '<a href="#" onClick="expandCollapse(' + this.menuIndex + ');' + this.customAction + ';return false;" class="qmlink"><img'
    );
    document.write(' src="' + qmImageBase +'/minus_circle.gif" width="19" height="16" alt="toggle menu state"');
    document.write(' border="0" id="qimg_' + this.menuIndex + '" valign="middle" />');
    //document.write('<img style="vertical-align: middle;" src="' + qmImageBase +'/folder-open-small.gif" width="16" height="16"');
    //document.write('  border="0" alt="menu state icon" id="qimg2_' + this.menuIndex + '"/>');
    document.writeln(addBreaks(this.label, indentLevel) + '</a>');
  }
  document.writeln('<ul ' + listClass + ' id="qnl_' + this.menuIndex +
    '" style="list-style-type: none;' + addedStyle + '">');
  for ( var i=0 ; i < this.items.length ; i++ ) {
    if ( this.items[i].isMenu )
      this.items[i].displayMenu(indentLevel+1);
    else this.items[i].displayItem(indentLevel+1);
  }
  document.writeln('</ul>');
  if ( indentLevel > 0 ) document.writeln('</li>');
}

function expand() {
  image1 = document.getElementById("qimg_" + this.menuIndex);
  //image2 = document.getElementById("qimg2_" + this.menuIndex);
  nestedList = document.getElementById("qnl_" + this.menuIndex);
  //if (!(nestedList && image1 && image2)) return false;
  if (!(nestedList && image1)) return false;
  nestedList.style.display = 'inline';
  image1.src = qmImageBase + "/minus_circle.gif";
  //image2.src = qmImageBase + "/folder-open-small.gif";
}

function collapse() {
  image1 = document.getElementById("qimg_" + this.menuIndex);
  //image2 = document.getElementById("qimg2_" + this.menuIndex);
  nestedList = document.getElementById("qnl_" + this.menuIndex);
  //if (!(nestedList && image1 && image2)) return false;
  if (!(nestedList && image1)) return false;
  nestedList.style.display = 'none';
  image1.src = qmImageBase + "/plus_circle.gif";
  //image2.src = qmImageBase + "/folder-closed-small.gif";
}

function expandAll() {
  for ( var i=0 ; i < this.items.length ; i++ ) {
    if ( this.items[i].isMenu ) {
      this.items[i].expandAll();
      this.items[i].expand();
    }
  }
}

function collapseAll() {
  for ( var i=0 ; i < this.items.length ; i++ ) {
    if ( this.items[i].isMenu ) {
      this.items[i].collapseAll();
      if ( this.items[i].showLabel ) this.items[i].collapse();
    }
  }
}

function toggleState() {
  nestedList = document.getElementById("qnl_" + this.menuIndex);
  if (! nestedList ) return false;
  if (nestedList.style.display == 'inline') {
    this.collapse();
  }
  else {
    this.expand();
  }
}

function expandCollapse(menuIndex) {
  qMenus[menuIndex].toggleState();
}


function qMenuItem (label, action) {
/*
* Menu item attributes:
*/
  this.label = label;
  this.action = action;
  this.isMenu = false;
  
/*
* Menu item methods:
*/
  this.displayItem = displayItem;
}

/*
* Display a menu item:
*/
function displayItem(indentLevel) {
  if ( this.isMenu == null || this.isMenu ) return;
  if ( indentLevel == null ) indentLevel = 0;
  document.write('<li><span class="qmpad" />');
  //document.write('<img src="' + qmImageBase + '/text-small.gif" style="vertical-align: middle;" width="16" height="16" alt="text icon"/>');
  document.writeln(
    '<a href="#" onClick="' + this.action + '" class="qmlink">' +
    addBreaks(this.label, indentLevel) + '</a></li>'
  );
}

/*
* Browsers don't do a nice job of wrapping long labels, so we force breaks when
* a label is longer than the qmMaxChars global variable.
*/
function addBreaks(label, indentLevel) {
  if ( label == null || label.length == 0 ) return('');
  if ( indentLevel == null ) indentLevel = 0;
  var maxCharsThisLevel = qmMaxChars - (qmCharsPerIndent * (indentLevel-1));
  if ( maxCharsThisLevel < qmMinChars ) maxCharsThisLevel = qmMinChars;
  if ( label.length < maxCharsThisLevel ) return(label);
  var words = label.split(" ");
  var i=1;
  var labelWithBreaks = words[0];
  var currentLineLength = words[0].length;
  for ( i=1 ; i < words.length ; i++ ) {
    if ( words[i].length < maxCharsThisLevel && (currentLineLength + words[i].length) > maxCharsThisLevel ) {
      labelWithBreaks += qmbr + words[i];
      currentLineLength = words[i].length;
    }
    else {
      labelWithBreaks += ' ' + words[i];
      currentLineLength += words[i].length + 1;
    }
  }
  return(labelWithBreaks);
}

function qmSetImageBase(newImageBase) {
  if ( newImageBase == null ) return;
  qmImageBase = newImageBase;
  qmbr = "<br><img src='" + newImageBase +
    "/qmblank.gif' width='19' height='10' border='0'>";
}
