// for IE5, IE5.5, IE6, Mozilla 1.3, Netscape 6, Opera 6, Opera 7
// uses getElementById (DOM), margin and display:block

menus.prototype.m_colorHighlight = '';
menus.prototype.m_colorBackground = '';
menus.prototype.m_colorText = '';
menus.prototype.m_colorTextHighlight = '';
menus.prototype.m_colorBorder = '';
menus.prototype.m_fontSizeMenuBar = 14;
menus.prototype.m_fontSizeMenuItem = 12;
menus.prototype.m_borderSize = 1;
menus.prototype.m_fontFamilyMenuBar = '';
menus.prototype.m_fontFamilyMenuItem = '';
menus.prototype.m_imageArrow = '';
menus.prototype.m_imageArrowHighlight = '';
menus.prototype.m_filterOpacity = 100;
menus.prototype.m_mozOpacity = 1;
menus.prototype.m_leftGraphic = null;
menus.prototype.m_rightGraphic = null;
menus.prototype.m_repeatGraphic = false;
menus.prototype.m_separatorGraphic = null;

menus.prototype.m_name = "";
menus.prototype.m_stack = new stack();
menus.prototype.m_current = null;
menus.prototype.m_start = null;
menus.prototype.m_count = 0;
menus.prototype.m_activeMenus = new activeMenuStack();
menus.prototype.m_timeout = null;
menus.prototype.m_widthOfImage = 0;
menus.prototype.m_type = 0;

function menus() {}

menus.prototype.findGraphicWidth = function() {
  if( this.m_widthOfImage>0 ) return;
  for( var n=0; n<this.m_start.m_count; n++ ) {
    if( this.m_start.m_links[n].m_graphics ) {
      this.m_widthOfImage = this.m_start.m_links[n].m_graphics.width;
      this.m_styleMenuBar = '';
      break;
    }
  }
}

menus.prototype.initialise = function( menu_type )
{
  this.m_type = menu_type;
  this.m_styleMenuBar = '';
  this.m_styleMenuBarHeading = '';
  this.m_styleMenuItem = 'z-index:100;filter:alpha( opacity='+this.m_filterOpacity+' );-moz-opacity:'+this.m_mozOpacity+';border:'+this.m_borderSize+'px solid '+this.m_colorBorder+';position:absolute;visibility:hidden;';
  this.m_styleMenuItemHeading = 'display:block;padding:3px 20px 3px 10px;position:relative;background-color:'+this.m_colorBackground+';display:block;text-decoration:none;font-family:'+this.m_fontFamilyMenuItem+';font-size:'+this.m_fontSizeMenuItem+'px;color:'+this.m_colorText+';';
  this.findGraphicWidth();
  this.createAll( this.m_start,0 );
}

menus.prototype.createEx = function( left_g,right_g,repeat )
{
  this.m_leftGraphic = new Image();
  this.m_rightGraphic = new Image();
  this.m_leftGraphic.src = left_g;
  this.m_rightGraphic.src = right_g;
  this.m_repeatGraphic = repeat;
  this.create();
}

menus.prototype.create = function()
{
  document.write( '<table border="0" cellpadding="0" cellspacing="0"><tr><td nowrap="true">' );
  if( this.m_leftGraphic && !this.m_repeatGraphic )
  {
    document.write( '<img src="'+this.m_leftGraphic.src+'" />' );
    if( this.m_type==1 ) document.write( '<br />' );
  }
  for( var i=0; i<this.m_start.m_count; i++ )
  {
    if( this.m_leftGraphic && this.m_repeatGraphic ) {
      document.write( '<img src="'+this.m_leftGraphic.src+'" />' );
      if( this.m_type==1 ) document.write( '<br />' );
    }

    document.write( '<a href="'+this.m_start.m_links[i].m_url+'" ' );
    document.write( 'onmouseover="menuItemOver( '+this.m_type+',\''+this.m_start.m_links[i].m_uid+'\',0,\''+this.m_name+i+'_d0\' );" ' );
    document.write( 'onmouseout="menuItemOut(\''+this.m_name+i+'_d0\');">' );
    document.write( '<img id="'+this.m_name+i+'_d0" style="border:0;" src="'+this.m_start.m_links[i].m_graphics.src+'" alt="'+this.m_start.m_links[i].m_name+'" />' );
    document.write( '</a>' );
    if( this.m_type==1 ) document.writeln( '<br />' );

    if( this.m_rightGraphic && this.m_repeatGraphic ) {
      document.write( '<img src="'+this.m_rightGraphic.src+'" />' );
      if( this.m_type==1 ) document.write( '<br />' );
    }

    var e = document.getElementById( this.m_name+i+'_d0' );
    e.src_normal = this.m_start.m_links[i].m_graphics;
    e.src_over = this.m_start.m_links[i].m_graphics_over;
    e.p_menus = this;
  }
  if( this.m_rightGraphic && !this.m_repeatGraphic ) document.write( '<img src="'+this.m_rightGraphic.src+'" />' );
  document.writeln( '</td></tr></table>' );
}


menus.prototype.createAll = function( current,depth ) {
  if( !current ) return;
  if( depth ) current.create( depth );
  for( var i=0;i<current.m_count;i++ ) {
    this.createAll( current.m_links[i],depth+1 );
  }
}

menu.prototype.create = function( depth )
{
  if( this.m_count )
  {
    document.write( '<div id="'+this.m_uid+'" style="'+this.m_root.m_styleMenuItem+'">' );
    document.write( '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="173" style="' );
    if( depth==1 && this.m_root.m_type==0 && this.m_m_widthOfImage > 0 ) document.write( 'width:'+this.m_root.m_widthOfImage+'px;' );
    if( browser.isOpera5 ) document.write( 'background-color:'+this.m_colorBackground+';' );
    document.write( '">' );

    for( var i=0; i<this.m_count;i++ )
    {
      document.write( '<a id="'+this.m_uid+'_'+i+'d'+depth+'" href="'+this.m_links[i].m_url+'" ' );
      document.write( 'style="'+this.m_root.m_styleMenuItemHeading+';' );
			if( this.m_links[i].m_count )
				document.write( 'background-image:url('+this.m_root.m_imageArrow+');background-repeat:no-repeat;background-position:right;" ' );
			else
				document.write( '" ' );
      document.write( 'onmouseover="menuItemOver( 1,\''+this.m_links[i].m_uid+'\','+depth+',\''+this.m_uid+'_'+i+'d'+depth+'\' );" ' );
      document.write( 'onmouseout="menuItemOut(\''+this.m_uid+'_'+i+'d'+depth+'\');">' );
      document.writeln( this.m_links[i].m_name+'</a>' );
      
      var e = document.getElementById( this.m_uid+'_'+i+'d'+depth );
      e.src_normal = this.m_links[i].m_graphics;
      e.src_over = this.m_links[i].m_graphics_over;
      e.p_menus = this.m_root;
    }

    document.writeln( '</td></tr></table></div>' );
  }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//*** GLOBAL FUNCTIONS ***
function menuTimeout( id_name ) {
  var m = null;
  m = document.getElementById( id_name );
  m.p_menus.m_activeMenus.clear();
  clearInterval( m.p_menus.m_timeout );
}
function menuItemOver( type,id_next,depth,id_name ) {
  var m = null;
  m = document.getElementById( id_name );
  clearInterval( m.p_menus.m_timeout );
  m.p_menus.m_activeMenus.push( type,id_next,depth,id_name );
}

function menuItemOut( id_name ) {
  var m = null;
  m = document.getElementById( id_name );
  m.p_menus.m_timeout = setInterval( "menuTimeout('"+id_name+"')",500 );
}

activeMenuStack.prototype.showMenu = function( type,id_next,depth,id_name ) {
  var next = null;
  var current = null;
  next = document.getElementById( id_next );
  current = document.getElementById( id_name );
  
  if( current.src_normal ) current.src = current.src_over.src;
  else {
    current.style.backgroundColor = current.p_menus.m_colorHighlight;
    current.style.color = current.p_menus.m_colorTextHighlight;
    if( current.style.backgroundImage ) current.style.backgroundImage = 'url('+current.p_menus.m_imageArrowHighlight+')'
  }

  // next menu actions
  if( next )
  {
    var tp = current;
    var top = 0; var left = 0;
    while( tp ) {
      top  += tp.offsetTop;
      left += tp.offsetLeft;
      tp = tp.offsetParent;
    }
    if( type==0 ) { // bottom
      top += current.offsetHeight;
    } else { // right
      left += current.offsetWidth;
    }
    if( browser.isOpera6 ) top += 7;
    next.style.top = top+"px";
    next.style.left = left+"px";
    next.style.visibility = "visible";
  }
}

activeMenuStack.prototype.removeMenu = function( id_next,depth,id_name ) {
  var next = null;
  var current = null;
  next = document.getElementById( id_next );
  current = document.getElementById( id_name );

  if( current.src_normal ) current.src = current.src_normal.src;
  else {
    current.style.backgroundColor = current.p_menus.m_colorBackground;
    current.style.color = current.p_menus.m_colorText;
    if( current.style.backgroundImage ) current.style.backgroundImage = 'url('+current.p_menus.m_imageArrow+')'
  }

  if( next ) next.style.visibility = "hidden";
}

// tells the browser to begin a new menu under the 'url' link 'name'
menus.prototype.beginMenu = function( name,url,g,g_over )
{
  with( this ) {
    if( !m_current ) { // tree is empty
      m_count++;
      m_name = name;
      m_start = m_current = new menu( this,name,url,m_name+m_count,g,g_over );
    } else { // tree is not empty
      m_count++;
      m_current.addLink( this,name,url,m_name+m_count,g,g_over );
      m_stack.push( m_current );
      m_current = m_current.m_links[m_current.m_count-1];
    }
  }
}

// just simply adds a link with no sub menus available
menus.prototype.addLink = function( name,url,g,g_over ) { with( this ) { m_count++; m_current.addLink( this,name,url,m_name+m_count,g,g_over ); }}

// ends the current menu and moves 'up' a level
menus.prototype.endMenu = function() { with( this ) { m_current = m_stack.pop(); }}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// menu class
// the sub-menu class constructor
// preloads the graphics if a filename is specified
function menu( root,name,url,id,g,g_over )
{
  this.m_root  = root;
  this.m_count = 0;
  this.m_name  = name;
  this.m_url   = url;
  this.m_links = null;
  this.m_uid   = id;
  this.m_graphics = null;
  this.m_graphics_over = null;
  if( g!=null ) { this.m_graphics = new Image(); this.m_graphics.src = g; }
  if( g_over!=null ) { this.m_graphics_over = new Image(); this.m_graphics_over.src = g_over; }
}

// adds a link to the sub menu
menu.prototype.addLink = function( root,name,url,id,g,g_over ) {
  with( this ) {
    if( !m_links ) m_links = new Array();
    m_links[m_count] = new menu( root,name,url,id,g,g_over );
    m_count++;
  }
}

// adds a line seperator
menus.prototype.addSeparator = function() { with( this ) {
  m_count++;
  m_current.addLink( "","","id_hr",null,null );
}}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// The Stack Class
stack.prototype.stack_array = new Array();
stack.prototype.stack_length = 0;

// constructor
function stack( name ) { this.stack_name = name; }
// returns the state of the stack
stack.prototype.isEmpty = function() { return( this.stack_length==0 ); }
// push an item onto the stack
stack.prototype.push = function( x ) { this.stack_array[this.stack_length++] = x; }

// pop an item off the stack
stack.prototype.pop = function() {
  with( this ) {
    if( stack_length==0 ) return null;
    var x = stack_array[--stack_length];
    stack_array[stack_length] = null;
    return x;
  }
}

// take a look at the item on the stack
stack.prototype.peek = function() {
  with( this ) {
    if( stack_length==0 ) return null;
    return stack_array[stack_length-1];
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// The ActiveMenuStack Class
activeMenuStack.prototype.count  = 0;            // the number of items in the stack
activeMenuStack.prototype.id     = new Array();  // array to store the id names
activeMenuStack.prototype.depth  = new Array();  // array to store the depth of the equivanlent id names
activeMenuStack.prototype.parent = new Array();  // the parent id of the current id
function activeMenuStack() {}                    // constructor, class definition

// pushes an item onto the stack
// id is stored for easy referencing
// depth stores the depth of the menu
activeMenuStack.prototype.push = function( align,id,depth,parent ) {
  // removes all higher depth stack
  while(( this.count>0 )&&( depth<=this.depth[this.count-1] )) { this.pop(); }
  // update the stack
  this.id[this.count]     = id;
  this.depth[this.count]  = depth;
  this.parent[this.count] = parent;
  this.count++;
  this.showMenu( align,id,depth,parent );
}

// removes the first item on the stack if there are any
activeMenuStack.prototype.pop = function() {
  with( this ) {
    if( count>0 ) {
      count--;
      removeMenu( id[count],depth[count],parent[count] );
      id[count] = depth[count] = parent[count] = null;
    }
  }
}

// clears the stack and hides all memory
activeMenuStack.prototype.clear = function() {
  with( this ) {
    while( count>0 ) {
      count--;
      removeMenu( id[count],depth[count],parent[count] );
      id[count] = depth[count] = parent[count] = null;
    }
  }
}
