// ___________________________________________________________________________
//
// Name:        menu.js 
//
// Author:      Tim Schramer (with others) - bifftoe@yahoo.com
//
// Description: Pulldown menu builder.
//
// Usage:       Include at beginning of web pages with <SCRIPT> element.
//              Change items and format in companion "menu_items.js" and 
//              "menu_fmt.js" files before including them also.
//
// ___________________________________________________________________________

var menus=[];

// Menu class
// ___________________________________________________________________________
function menu(item_struct, pos, styles)
{
// background color
// browser check
  this.item_struct=item_struct;
  this.pos=pos;
  this.styles=styles;
  this.id=menus.length;
  this.items=[];
  this.children=[];
  this.add_item=menu_add_item;
  this.hide=menu_hide;

  this.onclick=menu_onclick;
  this.onmouseout=menu_onmouseout;
  this.onmouseover=menu_onmouseover;
  this.onmousedown=menu_onmousedown;

  var i;
  for(i=0; i < this.item_struct.length; i++)
  {
    var location='middle';
    if(i == 0)
      location='mfirst';
    if(i == this.item_struct.length - 1)
      location='mlast';
    new menu_item(i, this, this, location);
  }

  for(i=0; i < this.children.length; i++)
    this.children[i].visibility(true);
  menus[this.id]=this;
}

// ___________________________________________________________________________
function menu_add_item(item)
{
  var id=this.items.length;
  this.items[id]=item;
  return(id);
}

// ___________________________________________________________________________
function menu_hide()
{
  for(var i=0; i < this.items.length; i++)
  {
    this.items[i].visibility(false);
    this.items[i].switch_style('onmouseout');
  }
}
// Resize 1st level menu items and re-align their children.
// ___________________________________________________________________________
function menu_resize(size)
{
  var menu;
  var offset=0;
  var maincnt=0;

  if(size < 100 || size > 4000)
    return;

  for(var i=0; i < menus.length; i++)
  {
    menu=menus[i];
    for(var j=0; j < menu.items.length; j++)
      if(menu.items[j].depth == 0)
        maincnt++;
    width=size / maincnt;

// Cap on shrinking.
    var width1=menu.items[0].container.pos['width'][0];
    if((width / width1) < .67) width=.67 * width1;

    width=parseInt(width);
    if(width == width1)
      return;

    maincnt=0;
    for(var j=0; j < menu.items.length; j++)
    {
      if(menu.items[j].depth == 0)
      {
        menu.items[j].width(width);
        maincnt++;
      }

      offset=(maincnt - 1) * width;

      if(menu.items[j].depth > 0)
      {
        for(var k=0; k <= menu.items[j].depth; k++)
        {
          offset+=menu.items[j].container.pos['block_left'][k];
        }
      }

      menu.items[j].left(offset);
    }
  }
}

// ___________________________________________________________________________
function menu_onclick(id)
{
  var item=this.items[id];
  return(item.fields[1] ? true : false);
}

// ___________________________________________________________________________
function menu_onmouseout(id)
{
//parent.document.body.rows = "100,*";
  this.hide_timer=setTimeout('menus[' + this.id + '].hide();',
                             this.pos['hide_delay'][this.active_item.depth]);
  if(this.active_item.id == id)
    this.active_item=null;
}

// ___________________________________________________________________________
function menu_onmouseover(id)
{
//parent.document.body.rows = "60%,40%";
  this.active_item=this.items[id];
  clearTimeout(this.hide_timer);

  var curr_item, visib;
  for(var i=0; i < this.items.length; i++)
  {
    curr_item=this.items[i];
    visib=(curr_item.arrpath.slice(0, curr_item.depth).join('_') == this.active_item.arrpath.slice(0, curr_item.depth).join('_'));
    if(visib)
    {
      curr_item.switch_style(curr_item == this.active_item ? 'onmouseover' : 'onmouseout');
    }

    curr_item.visibility(visib);
  }
}

// ___________________________________________________________________________
function menu_onmousedown(id)
{
  this.items[id].switch_style('onmousedown');
}

// Menu Item Class
// ___________________________________________________________________________
function menu_item(path, parent, container, location)
{
  this.path=new String(path);
  this.parent=parent;
  this.container=container;
  this.arrpath=this.path.split('_');
  this.depth=this.arrpath.length - 1;
  this.location=location;

// Get pointer to item's data in the structure
  var struct_path='', i;
  for(i=0; i <= this.depth; i++)
  {
    struct_path+='[' + (Number(this.arrpath[i]) + (i ? 3 : 0)) + ']';
  }

  eval('this.fields = this.container.item_struct' + struct_path);
  if(!this.fields)
    return;

// Assign methods	
  this.get_x=mitem_get_x;
  this.get_y=mitem_get_y;

// These methods may be different for different browsers (i.e. non DOM compatible)
  this.init=mitem_init;
  this.visibility=mitem_visibility;
  this.switch_style=mitem_switch_style;
  this.width=mitem_width;
  this.left=mitem_left;

// Register in the collections
  this.id=this.container.add_item(this);
  parent.children[parent.children.length]=this;

// Init recursively
  this.init();
  this.children=[];

  var child_count=this.fields.length - 3;
  for(i=0; i < child_count; i++)
  {
    new menu_item(this.path + '_' + i, this, this.container, 'child');
  }

  this.switch_style('onmouseout');
}

// ___________________________________________________________________________
function mitem_init()
{
  var cssclass='m' + this.container.id + 'l' + this.depth + 'o';
  if(this.location == 'mfirst')
    cssclass='mfirst';
  if(this.location == 'mlast')
    cssclass='mlast';
  document.write('<a id="mi_' + this.container.id + '_' + this.id +
                 '" class="' + cssclass + '" href="' + this.fields[1] + '"' +
                 (this.fields[2] ? ' target="' + this.fields[2] + '"' : '') +
                 ' style="position: absolute; top: ' + this.get_y() +
                 'px; left: ' + this.get_x() + 'px; width: ' +
                 this.container.pos['width'][this.depth] + 'px; height: ' +
                 this.container.pos['height'][this.depth] +
                 'px; visibility: hidden;' +
                 ' background: black; color: white; z-index: ' + this.depth +
                 ';" ' + 'onclick="return menus[' + this.container.id +
                 '].onclick(' + this.id + ');" onmouseout="menus[' +
                 this.container.id + '].onmouseout(' + this.id +
                 ');" onmouseover="menus[' + this.container.id +
                 '].onmouseover(' + this.id + ');" onmousedown="menus[' +
                 this.container.id + '].onmousedown(' + this.id +
                 ');"><div class="m' + this.container.id + 'l' + this.depth +
                 'i">' + this.fields[0] + "</div></a>\n");

  this.element=document.getElementById('mi_' + this.container.id + '_' + this.id);
}

// ___________________________________________________________________________
function mitem_visibility(make_visible)
{
  if(make_visible != null)
  {
    if(this.visible == make_visible)
      return;
    this.visible=make_visible;
    if(make_visible)
      this.element.style.visibility='visible';
    else if(this.depth)
      this.element.style.visibility='hidden';
  }

  return(this.visible);
}

// ___________________________________________________________________________
function mitem_get_x()
{
  var value=0;
  for(var i=0; i <= this.depth; i++)
  {
    value+=this.container.pos['block_left'][i] +
      this.arrpath[i] *
      this.container.pos['left'][i];
  }

  return(value);
}

// ___________________________________________________________________________
function mitem_get_y()
{
  var value=0;
  for(var i=0; i <= this.depth; i++)
  {
    value+=this.container.pos['block_top'][i] +
      this.arrpath[i] *
      this.container.pos['top'][i];
  }

  return(value);
}

// ___________________________________________________________________________
function mitem_switch_style(state)
{
  if(this.state == state)
    return;
  this.state=state;

  var style=this.container.styles[state];
  for(var i=0; i < style.length; i+=2)
  {
    if(style[i] && style[i + 1])
    {
      eval('this.element.style.' + style[i] + "='" + style[i + 1][this.depth] +
           "';");
    }
  }
}

// ___________________________________________________________________________
function mitem_width(newwidth)
{
  var width=this.element.style.width;
  if(width == newwidth + 'px')
    return;

  this.element.style.width=newwidth + 'px';
}

// ___________________________________________________________________________
function mitem_left(newoffset)
{
  var offset=this.element.style.left;
  if(offset == newoffset + 'px')
    return;

  this.element.style.left=newoffset + 'px';
}
