var dm_UniqueId = 0;
var dm_Menus = new Array();
var dm_Timeout = null;
var dm_Timeout2 = null;
var dm_exitdelay = 300;

// DynamicItem constructor
function DynamicItem(txt, url, target, imgurl)
{
  this.text = txt;
  this.url = url;
  this.imageUrl = imgurl;
  this.target = target;
}

// DynamicMenu constructor
function DynamicMenu(id, txt, url, target, imgurl)
{
  if (id == null || id == '')
    id = 'dyn_' + dm_UniqueId;

  dm_UniqueId ++;
  
  this._stack = new Array();
  this.items = new Array();
  this.text = txt;
  this.id = id;
  this.url = url;
  this.imageUrl = imgurl;
  this.target = target; 
}

// DynamicMenu member functions

function dm_pop()
{
  if (this._stack.length == 0) return;
  this._stack = this._stack.slice(0, -1); 
}

// DynamicMenu.Render
function dm_render(objInto)
{
  var s = '';
  var n;

  if (objInto != null)
  {
    s += '<table id="' + this.id + '" style="position: relative; z-index: 2; ';
    s += 'display: ' + ((this.visible == true) ? 'inline' : 'none');
 
    s += '" border=0 cellpadding=0 cellspacing=0>\r\n';
    s += '<tr>\r\n';
    s += '<td id="' + this.id + '_item" style="';

    s += 'background-color: ' + this.backgroundColor + ';';
    if (this.backgroundImage != null)
      s += 'background-image: ' + this.backgroundImage + ';';
    s += 'border: 1px solid ' + this.backgroundColor;
    s += ';cursor:pointer;cursor:hand;" onclick="dm_click(\'' + this.id + '\')" onmouseover="dm_hover(\'' + this.id + '\')" onmouseout="dm_endhover(\'' + this.id + '\')" nowrap align=center>\r\n';

    s += '<table border=0 cellpadding=0 cellspacing=0><tr><td>';
    if (this.imageUrl != null)
      s += '<img align=absmiddle src="' + this.imageUrl + '" border=0>';
    else
      s += '<span style="' + this.style + '">&nbsp;</span>';
      
    s += '</td><td nowrap>';
 
    if (this.imageUrl != null)
      s += '<span style="' + this.style + '">&nbsp;</span>';
    
    if (this.url != null && this.url!="")
    {
      s += '<a ';

      if (this.target != null)
        s += 'target="' + this.target + '" ';
        
      s += 'style="text-decoration: none;" href="' + this.url + '">';
    }

    s += '<span style="' + this.style + ';text-decoration: none; color:' + this.foreColor + '">' + this.text + '</span>';
   
    if (this.url != null)
      s += '</a>';

    s += '<span style="' + this.style + '">&nbsp;</span>';
    s += '</td></tr></table>';
  
    s += '\r\n</td>\r\n';
    s += '</tr>\r\n';
    s += '</table>\r\n';
  }
  
  s += '<div id="' + this.id + '_drop" onmouseover="dm_drophover(\'' + this.id + '\')" onmouseout="dm_enddrophover(\'' + this.id + '\')" style="left: -1000px; top: 0px; visibility: hidden; position: absolute; z-index: 1;';
  s += 'background-color: ' + this.backgroundColor + ';';
  s += ' border: 1px solid ' + this.borderColor + '">\r\n';    
  s += '<table border=0 cellpadding=0 cellspacing=0>\r\n';
  if (this.items.length == 0)
  {
//    s += '<tr>\r\n';
//    s += '<td nowrap>\r\n  ';
//    s += 'empty';
//    s += '</td>\r\n';
//    s += '</tr>\r\n';
  }
  else
  {
    for (n = 0; n < this.items.length; n++)
    {
      s += '  <tr ';
      
      if (this.items[n]._type == 'menu')
        this.items[n]._rowElement = '' + this.items[n].id + '_item';
      else
        this.items[n]._rowElement = '' + this.id + '_' + n + '_item';
        
      s += 'style="cursor:pointer; cursor: hand;" ';
      s += 'onclick="dm_subclick(\'' + this.id + '\', ' + n + ')" ';
      
      s += 'id="' + this.items[n]._rowElement + '" ';
      s +='onmouseover="dm_subhover(\'' + this.id + '\', ' + n + ')" onmouseout="dm_subendhover(\'' + this.id + '\', ' + n + ')" ';

      s += '>\r\n';
      s += '  <td ';
      s += 'style="';
      s += 'border-top: 1px solid ';
      
      if (this.gridMode == true && n > 0)
        s += this.borderColor;
      else
        s += this.backgroundColor;
        
      s += ';border-bottom: 1px solid ' + this.backgroundColor + ';';
      s += '" ';
      s += ' align=left nowrap';
      s += '>';
      if (this.items[n].imageUrl != null)
      {
        s += '<span style="' + this.style + '">&nbsp;</span>';

        if (this.items[n].url != null)
        {
          s += '<a ';
//          if (this.items[n].target != null)
//            s += 'target="' + this.items[n].target + '" ';
//          s += 'href="' + this.items[n].url + '">';
          s += '>';
        }

        s += '<img align=absmiddle vspace=2 border=0 src="' + this.items[n].imageUrl + '">';
        
        if (this.items[n].url != null)
          s += '</a>';
      }
      else
        s += '<span style="' + this.style + '">&nbsp;</span>';
        
      s += '\r\n  </td>\r\n';
      s += '  <td ';
      s += 'style="';
      s += 'border-top: 1px solid ';
      
      if (this.gridMode == true && n > 0)
        s += this.borderColor;
      else
        s += this.backgroundColor;
        
      s += ';border-bottom: 1px solid ' + this.backgroundColor + ';';

      s += '" ';
      s += 'align=left nowrap>\r\n';
      
      if (this.items[n].text.indexOf('<table') == -1)
        s += '<span style="' + this.style + '">&nbsp;</span>';
      
      if (this.items[n].url != null)
      {
        s += '<a ';
//          if (this.items[n].target != null)
//            s += 'target="' + this.items[n].target + '" ';
//        s += 'style="text-decoration: none" href="' + this.items[n].url + '">';
        s += 'style="text-decoration: none">';
      }
      
      s += '<span style="' + this.style + ';text-decoration: none; color:' + this.foreColor + '">' + this.items[n].text + '</span>';
      
      if (this.items[n].url != null)
        s += '</a>';
      
      if (this.items[n].text.indexOf('<table') == -1)
        s += '<span style="' + this.style + '">&nbsp;&nbsp;</span>';
  
      s += '\r\n  </td>\r\n';
      
      s += '  <td ';
      s += 'style="';
      s += 'border-top: 1px solid ';
      
      if (this.gridMode == true && n > 0)
        s += this.borderColor;
      else
        s += this.backgroundColor;
        
      s += ';border-bottom: 1px solid ' + this.backgroundColor + ';';
      s += '" ';
      s += 'align=left nowrap>';
      if (this.items[n]._type == 'menu')
      {
        s += '<span style="font-size: xx-small; font-family: times new roman; text-decoration: none; color:' + this.foreColor + '">&#9658;&nbsp;</span>';
      }
      else
        s += '<span style="' + this.style + '">&nbsp;</span>';
        
      s += '\r\n  </td>\r\n';
      s += '  </tr>\r\n';
    }
  }
//  s += '\r\n</td>\r\n';
//  s += '</tr>\r\n';
  s += '</table>\r\n';
  s += '</div>\r\n';
 
  dm_Menus[dm_Menus.length] = this;

  this.parentElement = objInto;
  
  for (n = 0; n < this.items.length; n++)
  {
    if (this.items[n]._type == 'menu')
    {
      s += this.items[n].render(null);
      this.items[n].parentElement = this;
    }
  }

  if (objInto == null)  
    return s;
  else
    objInto.innerHTML = s;

  this.fixup();
  
  return null;  
}

function dm_fixup()
{
  this.mainElement = document.getElementById(this.id);
  this.dropElement = document.getElementById(this.id + '_drop');
  this.itemElement = document.getElementById(this.id + '_item');

  if (this.parentElement._type == 'menu')
    this.show = dm_subshowme;
  
  var n;
  
  for (n = 0; n < this.items.length; n++)
  {
    this.items[n]._rowElement = document.getElementById(this.items[n]._rowElement);
    if (this.items[n]._type == 'menu')
    {
      this.items[n].fixup();
    }
  }  
}

function dm_addMenu(objMenu)
{
  if (objMenu._type != 'menu')
    throw 'Invalid item type';
    
  objMenu.backgroundColor = this.backgroundColor;
  objMenu.foreColor = this.foreColor;
  objMenu.hotColor = this.hotColor;
  objMenu.backgroundImage = this.backgroundImage;
  objMenu.borderColor = this.borderColor;
  objMenu.clickExpand = this.clickExpand;
  objMenu.gridMode = this.gridMode;
  objMenu.style = this.style;
  
  this.addItem(objMenu);
  this._stack[this._stack.length] = objMenu;
}

// DynamicMenu.addItem
function dm_addItem(objItem)
{
  var thisitem = this;
  
  if (this._stack.length)
    thisitem = this._stack[this._stack.length-1];
    
  try
  {
    var t = objItem._type;
    if (t == 'menu' || t == 'item')
    {
      if (t == 'menu')
      {
        objItem.parentId = thisitem.id;
        objItem.index = thisitem.items.length;
      }
      thisitem.items[thisitem.items.length] = objItem;
    }
    else
      throw 'Invalid item type';
  }
  catch(e)
  {
    throw 'Invalid item type';
  }
}

function dm_setColors(bkColor, fgColor, hotColor, borderColor)
{
  this.backgroundColor = bkColor;
  this.foreColor = fgColor;
  this.hotColor = hotColor;
  this.borderColor = borderColor;
}

// DynamicMenu.show
function dm_showme(state)
{
  dm_show(this.id, state);
}

function dm_subshowme(state)
{
  dm_subshow(this.parentId, this.index, state);
}

// Global functions
function dm_Lookup(id)
{
  var n;
  
  try
  {
    if (typeof(id) == 'object')
    {
      if (id._type == 'menu')
        return id;
    }
  }
  catch(e)
  {
    return null;
  }
  
  for (n = 0; n < dm_Menus.length; n++)
  {
    if (dm_Menus[n].id == id) return dm_Menus[n];
  }
  return null;
}

function dm_show(id, state)
{
  var thisitem = dm_Lookup(id);
  var n;
  
  if (thisitem == null) return;
  
  if (state == 2) state = ((thisitem.dropElement.style.visibility == 'hidden') ? 1: 0);
   
  if (state == 1)
  {
    for (n = 0; n < dm_Menus.length; n++)
    {
      if (thisitem.id != dm_Menus[n].id)
      {       
        if (dm_Menus[n].parentId == null) 
          dm_Menus[n].show(false);
      }
    }

    var ww = document.body.clientWidth + document.body.scrollLeft;
    var wh = document.body.clientHeight + document.body.scrollTop;
    var x = 0;
    var y = thisitem.mainElement.offsetHeight - 1;

    if (navigator.appVersion.indexOf("MSIE") != -1)
      y --;
      
    n = thisitem.itemElement;
    while (n)
    {
      x += n.offsetLeft;
      y += n.offsetTop;
      n = n.offsetParent;
    }
    
    if (x + thisitem.dropElement.offsetWidth > ww)
    {    
      x = ww - thisitem.dropElement.offsetWidth;
      ww = 0;
    }
      
    thisitem.dropElement.style.left = x;
    thisitem.dropElement.style.top = y;
    
    if (thisitem.mainElement != null) thisitem.mainElement.style.zIndex = 10;
    thisitem.dropElement.style.zIndex  = 9;
    thisitem.itemElement.style.borderColor = thisitem.borderColor;
    thisitem.itemElement.style.backgroundColor = thisitem.hotColor;
    if (thisitem.itemElement.offsetWidth > thisitem.dropElement.offsetWidth && ww != 0)
    {
      thisitem.itemElement.style.borderBottomColor = thisitem.borderColor;
      thisitem.dropElement.style.borderTopColor = thisitem.backgroundColor;
      if (thisitem.mainElement != null) thisitem.mainElement.style.zIndex = 8;
    }
    else
    {
      thisitem.itemElement.style.borderBottomColor = thisitem.itemElement.style.backgroundColor;
    }
      
    if (thisitem.items.length > 0) 
      thisitem.dropElement.style.visibility = 'visible';
  }
  else
  {

    for (n = 0; n < thisitem.items.length; n++)
    {      
      if (thisitem.items[n]._type == 'menu')
        thisitem.items[n].show(false);
    }
    if (thisitem.mainElement != null) thisitem.mainElement.style.zIndex = 2;
    thisitem.dropElement.style.zIndex  = 1;
    thisitem.itemElement.style.backgroundColor = thisitem.backgroundColor;
    thisitem.itemElement.style.borderColor = thisitem.backgroundColor;
    thisitem.dropElement.style.visibility = 'hidden';
    thisitem.dropElement.style.borderTopColor = thisitem.borderColor;
    thisitem.dropElement.style.left = -1000;
    thisitem.dropElement.style.top = 0;
  }
}

function dm_hover(id)
{
  var thisitem = dm_Lookup(id);
  var n;
  var itm;
  var bOneDrop;
  
  if (thisitem == null) return;

  dm_cancelTimer();

  for (n = 0; n < dm_Menus.length; n++)
  {
    if (thisitem.id != dm_Menus[n].id)
    {
      if (dm_Menus[n].dropElement.style.visibility != 'hidden')
        bOneDrop = true;
        
      if (dm_Menus[n].parentElement != null)
        dm_Menus[n].show(false);
    }
  }
  
  if (thisitem.dropElement.style.visibility == 'hidden')
  {
    thisitem.itemElement.style.borderColor = thisitem.borderColor;
  }
  thisitem.itemElement.style.backgroundColor = thisitem.hotColor;
  
  if (thisitem.clickExpand == false)
  {
    if (bOneDrop == true)
    {
      thisitem.show(true);
    }
    else
    {
      if (dm_Timeout2 != null) window.clearTimeout(dm_Timeout2);
      dm_Timeout2 = window.setTimeout('dm_firstDrop(\'' + id + '\')', dm_exitdelay);
    }
  }
}

function dm_endhover(id)
{
  var thisitem = dm_Lookup(id);
  if (thisitem == null) return;

  while (thisitem.parentId != null)
    thisitem = thisitem.parentElement;
    
  if (dm_Timeout2 != null) window.clearTimeout(dm_Timeout2);
  dm_Timeout2 = null;
  
  dm_Timeout = window.setTimeout('dm_timer(\'' + thisitem.id + '\')', dm_exitdelay);
}

function dm_click(id)
{
  var thisitem = dm_Lookup(id);
  if (thisitem == null) return;

  if (thisitem.url == null)
    thisitem.show(2);
  else
    window.open(thisitem.url, (thisitem.target == null || thisitem.target == '') ? '_self':thisitem.target);
}


function dm_timer(id)
{
  var thisitem = dm_Lookup(id);
  if (thisitem == null) return;

  if (dm_Timeout == null) return;
  dm_Timeout = null;

  thisitem.show(false);
}

function dm_drophover(id)
{
  var thisitem = dm_Lookup(id);
  if (thisitem == null) return;

  dm_cancelTimer();
}

function dm_enddrophover(id)
{
  var thisitem = dm_Lookup(id);
  if (thisitem == null) return;

  dm_endhover(id);
}

function dm_cancelTimer()
{
  if (dm_Timeout != null)
  {
    window.clearTimeout(dm_Timeout);
    dm_Timeout = null;
  }
}

function dm_firstDrop(id)
{
  var thisitem = dm_Lookup(id);
  if (thisitem == null) return;
  
  dm_Timeout2 = null;
  
  thisitem.show(1);
}

function dm_subhover(id, idx)
{
  var thisitem = dm_Lookup(id);
  if (thisitem == null) return;
  
  var subitem = thisitem.items[idx];
  var cells;
  var n;
 
  cells = subitem._rowElement.cells;

  dm_cancelTimer();
    
  subitem._rowElement.style.backgroundColor = thisitem.hotColor;
  
  if (thisitem.gridMode == false)
  {
    for (n = 0; n < cells.length; n++)
    {
      cells[n].style.borderTopColor = thisitem.borderColor;      
      cells[n].style.borderBottomColor = thisitem.borderColor;
    }
  }
  
  if (subitem._type == 'menu') 
  {
    subitem.show(true);
  }
  else
  {
    for (n = 0; n < thisitem.items.length; n++)
    {
      if (thisitem.items[n]._type == 'menu' && n != idx)
        thisitem.items[n].show(false);
    }
  }
}

function dm_subendhover(id, idx)
{
  var thisitem = dm_Lookup(id);
  if (thisitem == null) return;
  
  var subitem = thisitem.items[idx];
  var n;
  
  if (subitem._type != 'menu')
  {
    var cells = subitem._rowElement.cells;
    
    subitem._rowElement.style.backgroundColor = thisitem.backgroundColor;
    if (thisitem.gridMode == false)
    {
      for (n = 0; n < cells.length; n++)
      {
        cells[n].style.borderTopColor = thisitem.backgroundColor;        
        cells[n].style.borderBottomColor = thisitem.backgroundColor;
      }
    }
  }
  else
    dm_Timeout = window.setTimeout('dm_subtimer(\'' + id + '\', ' + idx + ')', dm_exitdelay);  
}

function dm_subtimer(id, idx)
{
  var thisitem = dm_Lookup(id);
  if (thisitem == null) return;
  
  if (dm_Timeout == null) return;
  
  var subitem = thisitem.items[idx];

  dm_Timeout = null;

  while (subitem.mainElement == null)
    subitem = subitem.parentElement;
    
  subitem.show(false);  
}

function dm_subclick(id, idx)
{
  var thisitem = dm_Lookup(id);
  if (thisitem == null) return;
  
  var subitem = thisitem.items[idx];
  
  if (subitem.url != null)
    window.open(subitem.url, (subitem.target == null || subitem.target == '') ? '_self':subitem.target);
}

function dm_subshow(id, idx, state)
{
  var thisitem = dm_Lookup(id);
  var subitem = thisitem.items[idx];
  var n;

  var cells = subitem.itemElement.cells;

  if (state == 2)
    state = (subitem.dropElement.style.visibility == 'hidden') ? 1: 0;
  
  if (state == 1)
  {
    n = subitem;
    do
    {
      n = n.parentElement;
      if (n.parentId != null)
      {
        if (n.dropElement.style.visibility == 'hidden')
          n.show(true);
      }
    } while (n.parentId != null);
    
    for (n = 0; n < subitem.parentElement.items.length; n++)
    {
      if (subitem.parentElement.items[n]._type == 'menu' && n != subitem.index)
        subitem.parentElement.items[n].show(false);
    }
    
    if (thisitem.gridMode == false)
    {
      for (n = 0; n < cells.length; n++)
      {
        cells[n].style.borderTopColor = subitem.borderColor;
        cells[n].style.borderBottomColor = subitem.borderColor;
      }
    }
     
    var ww = document.body.clientWidth + document.body.scrollLeft;
    var wh = document.body.clientHeight + document.body.scrollTop;
    var x = 0;
    var y = 0;
    
    n = subitem.itemElement;
    while (n)
    {
      x = x + n.offsetLeft;
      y = y + n.offsetTop;
      n = n.offsetParent;     
    }
    
    x += subitem.itemElement.offsetWidth;

    if (x + subitem.dropElement.offsetWidth > ww)
    {
      x -= subitem.itemElement.offsetWidth;
      x -= subitem.dropElement.offsetWidth;
    }
      
    subitem.dropElement.style.left = x;
    subitem.dropElement.style.top = y;    

    subitem.dropElement.style.visibility = 'visible';
    subitem.dropElement.style.zIndex = 11;

  }
  else
  {
    subitem.itemElement.style.backgroundColor = subitem.backgroundColor;
  
    if (thisitem.gridMode == false)
    {
      for (n = 0; n < cells.length; n++)
      {
        cells[n].style.borderTopColor = subitem.backgroundColor;
        cells[n].style.borderBottomColor = subitem.backgroundColor;
      }
    }
    
    subitem.dropElement.style.visibility = 'hidden';
    subitem.dropElement.style.left = -1000;
    subitem.dropElement.style.top = 0;

    for (n = 0; n < subitem.items.length; n++)
    {
      if (subitem.items[n]._type == 'menu')
        subitem.items[n].show(false);
    }
  }
}

// DynamicMenu prototypes
DynamicMenu.prototype.backgroundColor = '#033D6F';
DynamicMenu.prototype.foreColor       = '#FFFFFF';
DynamicMenu.prototype.hotColor        = '#0663B2';
DynamicMenu.prototype.backgroundImage = null;
DynamicMenu.prototype.borderColor     = '#B19D58';
DynamicMenu.prototype.text            = '';
DynamicMenu.prototype.visible         = true;
DynamicMenu.prototype.items           = null;
DynamicMenu.prototype.url             = null;
DynamicMenu.prototype.imageUrl        = null;
DynamicMenu.prototype.id              = null;
DynamicMenu.prototype.index           = null;
DynamicMenu.prototype.parentId        = null;
DynamicMenu.prototype.dropElement     = null;
DynamicMenu.prototype.itemElement     = null;
DynamicMenu.prototype.mainElement     = null;
DynamicMenu.prototype.parentElement   = null;
DynamicMenu.prototype.clickExpand     = false;
DynamicMenu.prototype.gridMode        = true;
DynamicMenu.prototype.target          = null;
DynamicMenu.prototype.style           = '';

// Private stuff
DynamicMenu.prototype._type           = 'menu';
DynamicMenu.prototype._stack          = null;
DynamicMenu.prototype._rowElement     = null;

// Member functions
DynamicMenu.prototype.render    = dm_render;
DynamicMenu.prototype.addItem   = dm_addItem;
DynamicMenu.prototype.show      = dm_showme;
DynamicMenu.prototype.pop       = dm_pop;
DynamicMenu.prototype.addMenu   = dm_addMenu;
DynamicMenu.prototype.setColors = dm_setColors;
DynamicMenu.prototype.fixup     = dm_fixup;


// DynamicItem prototypes
DynamicItem.prototype.foreColor   = 'black';
DynamicItem.prototype.text        = '';
DynamicItem.prototype.imageUrl    = null;
DynamicItem.prototype.url         = null;
DynamicItem.prototype.target      = null;
DynamicItem.prototype._type       = 'item';

// Private stuff
DynamicItem.prototype._rowElement = null;
