/*  (C) Copyright 2006, Specialty Web Design
 *  File:         Menu.js
 *  Author:       Andrew Bulmer
 *  Description:  Javascript menu
 */

var open_submenu = null;
var close_timer ;
if (!close_timeout) var close_timeout = 5000 ;

document.onclick = function ()
{
  if (open_submenu && !open_submenu.mouseover)
  {
    menu.close() ;
    open_submenu = null;
  }
}

function MenuItem (text, href, width, height, new_window)
{
  this.href = href ;
  this.text = text ;
  
  this.obj = document.createElement ('div');
  this.obj.className = 'menuItem' ;
  this.parent = null;
  if (new_window) this.openNew = true ;
  else this.openNew = false ;

  if (width)
    this.obj.style.width = width + 'px' ;
  if (height)
    this.obj.style.height = height + 'px' ;  

  var that = this;
  
  if (href)
  {
    this.link = document.createElement ('a');
    this.link.href = href;
    if (this.openNew)
      this.link.target='_blank' ;
    this.link.innerHTML = text ;
    
    
    this.link.onclick = function ()
    {
      return false; // to prevent the browser from opening links twice
    }

    this.obj.appendChild (this.link) ;
    this.obj.subMenu = null ;

    this.obj.onmouseover = function()
    {
      this.className = 'menuItemActive' ;
      if (open_submenu)
      {
        var check_menu = that ;
        var is_child = false ;
        while (check_menu)
        {
          if (check_menu === open_submenu)
          {
            is_child = true ;
            check_menu = null ;
          }
          else
          {
            check_menu = check_menu.parent;
          }
        }
        
        if(!is_child)
        {
          while (open_submenu.parent)
            open_submenu = open_submenu.parent;
            open_submenu.close();
            open_submenu = null;
        }
        
      }
    }    
    this.obj.onmouseout = function()
    {
      this.className = 'menuItem' ;
    }
    
    this.obj.onclick = function()
    {
      if (that.openNew)
        window.open(href,'_blank') ;
      else
        window.location.href = href ;
    }
  }
  else
  {
    this.obj.innerHTML = text ;
    
    if (menu_arrow) this.obj.innerHTML += menu_arrow ;
    this.obj.subMenu = document.createElement ('div');
    this.obj.subMenu.className = 'subMenu' ;
    this.obj.subMenu.items = new Array() ;
    this.obj.subMenu.open = false ;
    this.obj.subMenu.mouseover = false ;

    this.obj.subMenu.onmouseover = function()
    {
      that.mouseover = true ;
      clearTimeout(close_timer);
    }

    this.obj.subMenu.onmouseout = function()
    {
      that.mouseover = false ;
      close_timer = setTimeout('document.onclick()',close_timeout); 
    }
    
    this.obj.onmouseover = function () 
    {
      if (!that.mouseover)
      {
        that.mouseover = true ;

        var check_menu = that ;
        var is_child = false ;
        while (check_menu)
        {
          if (check_menu === open_submenu)
          {
            is_child = true ;
            check_menu = null ;
          }
          else
          {
            check_menu = check_menu.parent;
          }
        }
      

        if (open_submenu != that)
        {
        
          if (!is_child)
          { 
            while (open_submenu)
            {
              if (open_submenu == that.parent) break;
              open_submenu.close() ;
              open_submenu = open_submenu.parent ;
            }
          }
          for (var i=0 ; i < this.subMenu.items.length ; i++)
          {
            this.subMenu.appendChild (this.subMenu.items[i].obj) ;
          }
          this.subMenu.open = true ;
          this.className = 'menuItemActive' ;
          this.innerHTML = that.text + menu_arrow_active ;
          this.appendChild (this.subMenu) ;
          open_submenu = that ;
        }
      }
    }
    
    this.close = function ()
    {
        for (var i=0 ; i < this.obj.subMenu.items.length ; i++)
        {
          if (this.obj.subMenu.items[i].obj.subMenu &&
              this.obj.subMenu.items[i].obj.subMenu.open)
            this.obj.subMenu.items[i].close() ;
        }
	if(this.obj.subMenu)
	        this.obj.removeChild(this.obj.subMenu) ;
        this.obj.subMenu.open = false ;
        this.obj.className = 'menuItem' ;
        this.obj.innerHTML = this.text + menu_arrow ;
    }
    
    this.obj.onmouseout = function()
    {
      that.mouseover = false ;
    }
  }
  
  this.addItem = function (item)
  {
    that.obj.subMenu.items.push(item) ;
    item.parent = that ;
  }

}

function Menu ()
{
  this.items = new Array() ;
 
  this.addItem = function (item)
  {
    this.items.push(item) ;  
  }
  
  this.appendTo = function(id)
  {
    var node = getObj(id) ;
    var div = document.createElement ('div');
    div.className = 'menu' ;
    for (var i=0 ; i < this.items.length ; i++)
    {
      div.appendChild (this.items[i].obj) ;
    }
    node.appendChild (div) ;
  }
  
  this.close = function ()
  {
    for (var i=0 ; i < this.items.length ; i++)
    {
      if (this.items[i].obj.subMenu && this.items[i].obj.subMenu.open)
        this.items[i].close() ;
    }
  }
}


