﻿//CG, 18032008

function GMenu(id, node) {
  var menu = new gMenu(id, node, "");
  var str = menu.build();
  document.write(str);
  
  return menu;
}

function gMenu(id, node, parent) {
  this.id = id;
  this.node = node;
  this.parent = parent;
  this.items = node.sub;
  
  if (node.direction) this.direction = node.direction;
  else this.direction = 'h';
  
  if (node.root) this.root = node.root;
  else this.root = false;
  
  if (this.root) this.zIndex = 600;
  else this.zIndex = parent.zIndex - 1;

  if (node.top) this.top = node.top;
  else this.top = 0;
  
  if (node.left) this.left = node.left;
  else this.left = 0;
  
  if (node.width) this.width = node.width;
  else if (parent.width) this.width = parent.width;
  else this.width = 100;
  
  if (node.height) this.height = node.height;
  else if (parent.height) this.height = parent.height;
  else this.height = 20;
  
  if (node.itemWidth) this.itemWidth = node.itemWidth;
  else if (parent.itemWidth) this.itemWidth = parent.itemWidth;
  else this.itemWidth = 100;
  
  if (node.itemHeight) this.itemHeight = node.itemHeight;
  else if (parent.itemHeight) this.itemHeight = parent.itemHeight;
  else this.itemHeight = 20;
  
  if (node.borderStyle) this.borderStyle = node.borderStyle;
  else if (parent.borderStyle) this.borderStyle = parent.borderStyle;
  else this.borderStyle = "none";
  
  if (node.borderWidth) this.borderWidth = node.borderWidth;
  else if (parent.borderWidth) this.borderWidth = parent.borderWidth;
  else this.borderWidth = "0";
  
  if (node.backgroundColor) this.backgroundColor = node.backgroundColor;
  else if (parent.itemBackgroundColor) this.backgroundColor = parent.itemBackgroundColor;
  else this.backgroundColor = "";
  
  if (node.backgroundColorOver) this.backgroundColorOver = node.backgroundColorOver;
  else if (parent.itemBackgroundColorOver) this.backgroundColorOver = parent.itemBackgroundColorOver;
  else this.backgroundColorOver = "";
  
  if (node.background) this.background = node.background;
  else if (parent.itemBackground) this.background = parent.itemBackground;
  else this.background = "";
  
  if (node.backgroundOver) this.backgroundOver = node.backgroundOver;
  else if (parent.itemBackgroundOver) this.backgroundOver = parent.itemBackgroundOver;
  else this.backgroundOver = "";
  
  if (node.itemBackgroundColor) this.itemBackgroundColor = node.itemBackgroundColor;
  else if (parent.itemBackgroundColor) this.itemBackgroundColor = parent.itemBackgroundColor;
  else this.itemBackgroundColor = "";
  
  if (node.itemBackgroundColorOver) this.itemBackgroundColorOver = node.itemBackgroundColorOver;
  else if (parent.itemBackgroundColorOver) this.itemBackgroundColorOver = parent.itemBackgroundColorOver;
  else this.itemBackgroundColorOver = "";
  
  if (node.itemBackground) this.itemBackground = node.itemBackground;
  else if (parent.itemBackground) this.itemBackground = parent.itemBackground;
  else this.itemBackground = "";
  
  if (node.itemBackgroundOver) this.itemBackgroundOver = node.itemBackgroundOver;
  else if (parent.itemBackgroundOver) this.itemBackgroundOver = parent.itemBackgroundOver;
  else this.itemBackgroundOver = "";

  if (node.styleClass) this.styleClass = node.styleClass;
  else if (parent.styleClass) this.styleClass = parent.styleClass;
  else this.styleClass = "";
  
  this.build = function() { return gBuildMenu(this);}
}

function gMenuItem(id, index, node, parent) {
  this.id = id;
  this.index = index;
  this.parent = parent;
  this.zIndex = parent.zIndex;
  
  if (node.sub) this.sub = node.sub;
  
  if (node.caption) this.caption = node.caption;
  else this.caption = "";
  
  if ((node.href) && (node.href != "")) this.href = node.href;
  else this.href = "javascript:void(0);";
  
  if (node.top) this.top = node.top;
  else this.top = 0;
  
  if (node.left) this.left = node.left;
  else this.left = 0;
  
  if (node.width) this.width = node.width;
  else if (parent.itemWidth) this.width = parent.itemWidth;
  else this.width = 100;
  
  if (node.height) this.height = node.height;
  else if (parent.itemHeight) this.height = parent.itemHeight;
  else this.height = 20;
  
  if (node.borderStyle) this.borderStyle = node.borderStyle;
  else if (parent.borderStyle) this.borderStyle = parent.borderStyle;
  else this.borderStyle = "none";
  
  if (node.borderWidth) this.borderWidth = node.borderWidth;
  else if (parent.borderWidth) this.borderWidth = parent.borderWidth;
  else this.borderWidth = "0";
  
  if (node.backgroundColor) this.backgroundColor = node.backgroundColor;
  else if (parent.itemBackgroundColor) this.backgroundColor = parent.itemBackgroundColor;
  else this.backgroundColor = "";
  
  if (node.backgroundColorOver) this.backgroundColorOver = node.backgroundColorOver;
  else if (parent.itemBackgroundColorOver) this.backgroundColorOver = parent.itemBackgroundColorOver;
  else this.backgroundColorOver = "";
  
  if (node.background) this.background = node.background;
  else if (parent.itemBackground) this.background = parent.itemBackground;
  else this.background = "";
  
  if (node.backgroundOver) this.backgroundOver = node.backgroundOver;
  else if (parent.itemBackgroundOver) this.backgroundOver = parent.itemBackgroundOver;
  else this.backgroundOver = "";

  if (node.styleClass) this.styleClass = node.styleClass;
  else if (parent.styleClass) this.styleClass = parent.styleClass;
  else this.styleClass = "";
  
  this.build = function() { return gBuildMenuItem(this);}
  this.showSub = gShowMenu;
  this.hideSub = gHideMenu;
}

function gBuildMenu(menu) {
  var str = "";
  var m = menu;
  var visible

  if (m.root) visible = "visible";
  else visible = "hidden";
  
  str += '<div id="' + m.id + '" ';
  str += 'style="position:absolute;left:' + m.left + ';top:' + m.top + ';visibility:' + visible + ';';
  if (m.backgroundColor != "")
    str += 'background-color:' + m.backgroundColor + ';';
  if (m.background != "")
    str += 'background-image:url(' + m.background + ');';
  str += '"';
  
  if (!m.root) {
    str += 'onMouseOver="';
    str += 'gShowMenu(' + "'" + m.id + "'" + ');" ';
    str += 'onMouseOut="';
    str += 'gHideMenu(' + "'" + m.id + "'" + ');"';
  }
  str += '>';
  
  var i;
  var mi;
  var left = 0;
  var top = 0;
  
  for (i=0; i<m.items.length; i++) {
    mi = new gMenuItem(m.id+"i_"+i, i, m.items[i], m);
    
    if (m.direction == 'h') {
      mi.left = left;
      if (m.parent.height) mi.top = m.parent.height;
      else mi.top = 0;
    } else if (m.direction == 'v') {
      mi.left = 0;
      if (m.parent.height) mi.top = m.parent.height + top;
      else mi.top = top;
    }
    top += mi.height +1;
    left += mi.width;
    str += mi.build();
  }

  str += '</div>';
  
  return str;
}

function gBuildMenuItem(menuItem) {
  var str = "";
  var mi = menuItem;
  //border-top-style:solid;border-top-width:1px;border-top-color:#FFFFFF;
  str += '<div id="' + mi.id + '" ';
  str += 'style="position:absolute;border-bottom-style:solid;border-bottom-width:1px;border-bottom-color:#FFFFFF;left:' + mi.left + ';top:' + mi.top + ';height:' + mi.height + ';width:' + mi.width + ';';
  if (mi.backgroundColor != "")
    str += 'background-color:' + mi.backgroundColor + ';';
  if (mi.background != "")
    str += 'background-image:url(' + mi.background + ');';
  str += '" ';
  
  str += 'onMouseOver="';
  if (mi.backgroundColorOver != "")
    str += 'gSetMenuItemColor(' + "'" + mi.id + "'" + ', ' + "'" + mi.backgroundColorOver + "'" + ');';
  if (mi.backgroundOver != "")
    str += 'gSetBackground(' + "'" + mi.id + "'" + ', ' + "'" + mi.backgroundOver + "'" + ');';
  if (mi.sub != null) {
    str += 'gShowMenu(' + "'" + mi.id + '_m' + "'" + ');';
  }
  if ((mi.backgroundColorOver != "") && (mi.sub != null))
    str += 'gSetBorderBottomColor(' + "'" + mi.id + "'" + ', ' + "'" + mi.backgroundColorOver + "'" + ');';
  str += '" ';
  
  str += 'onMouseOut="';
  if (mi.backgroundColorOver != "")
    str += 'gSetMenuItemColor(' + "'" + mi.id + "'" + ', ' + "'" + mi.backgroundColor + "'" + ');';
  if (mi.backgroundOver != "")
    str += 'gSetBackground(' + "'" + mi.id + "'" + ', ' + "'" + mi.background + "'" + ');';
  if (mi.sub != null)
    str += 'gHideMenu(' + "'" + mi.id + '_m' + "'" + ');';
  if ((mi.backgroundColorOver != "") && (mi.sub != null))
    str += 'gSetBorderBottomColor(' + "'" + mi.id + "'" + ', ' + "'" + "#FFFFFF" + "'" + ');';
  str += '">';
  
  str += '<table border="0" cellspacing="0" cellpadding="0" style="height:' + mi.height + ';width:' + mi.width + '">';
  str += '<tr><td align="center" valign="middle">';
  str += '<a href="' + mi.href + '" ';
  if (mi.styleClass != "")
    str += 'class="' + mi.styleClass + '"';
  str += '>';
  str += mi.caption;
  str += '</a></td></tr></table>';
  
  var m;
  if (mi.sub) {
    m = new gMenu(mi.id + '_m', mi.sub, mi.parent);
    if (mi.parent.direction == 'h') m.top = 1;
    else if (mi.parent.direction == 'v') {
      m.left = mi.width;
    }
    str += m.build();
  }
  
  str += '</div>';
  
  return str;
}

function gShowMenu(id) {
  _showDiv(id);
}

function gHideMenu(id) {
  _hideDiv(id);
}

function gSetMenuItemColor(id, color) {
  _setColor(id, color);
}

function gSetBackground(id, background) {
  _setBackground(id, background);
}

function gSetBorderBottomColor(id, color) {
  _setBorderBottomColor(id, color);
}

function _getObj(id) {
  return document.getElementById(id);
}

function _showDiv(id) {
  _getObj(id).style.visibility = "visible";
}

function _hideDiv(id) {
  _getObj(id).style.visibility = "hidden";
}

function _setColor(id, color) {
  _getObj(id).style.backgroundColor = color;
}

function _setBackground(id, background) {
  _getObj(id).style.backgroundImage = "url(" + background + ")";
}

function _setBorderBottomColor(id, color) {
  _getObj(id).style.borderColor = color;
}

