//<!--

// Simple Menu 

// code by jab 

/* ====================================================== ABOUT

I can create simple rollovers and dropdown menus. I do this
by analyzing the HTML for some naming conventions. Just add 
me to your page.

If I find an image and a link with the same "name", then I 
add mouseover events to the link. 
(I assume there exists an "_over" state for the image.) 

If I find an image and a link with the same "name" AND a
<div> element with a matching "id", then I turn the div
into a dropdown menu. All rollovers found within the div
element become sub-navs of that menu.
*/

/* ====================================================== INDEX

SimpleMenu
  -SimpleMenu.buildMenus()
  -SimpleMenu.buildRollovers()
  -SimpleMenu.findNavObjs()
  -SimpleMenu.getNavObj()
  -SimpleMenu.getMenuByName()
  -SimpleMenu.hideMenu()
  -SimpleMenu.init() *  
  -SimpleMenu.rePositionMenus()
  -SimpleMenu.setImageDir()
  -SimpleMenu.showMenu()

Rollover
  -Rollover.addOverState()
  -Rollover.addMouseEvents()

Menu
  -Menu.buildSubRollovers()
  -Menu.positionMenu()

NavObj

PAGE CODE 

*/

// ====================================================== SimpleMenu

function SimpleMenu()
{
  this.currentMenu  = null;
  this.imgDirectory = "images/";
  this.initSwitch   = false;
  this.menuArr      = [];
  this.navObjs      = [];
  this.timerID      = false;
  this.dontPositionFlag = false;
  this.forcePositionFlag = false;
  this.forcePositionX = 0;
  this.forcePositionY = 0;

  Menu.prototype.app     = this; // create proto reference
  Rollover.prototype.app = this; // create proto reference    
  window.simpleMenuApp   = this; // create global reference
  
  // capture any existing onload event
  if (window.onload != null){ this.preExisting_onload = window.onload; };	
};
SimpleMenu.prototype.buildMenus      = SM_buildMenus;
SimpleMenu.prototype.buildRollovers  = SM_buildRollovers;
SimpleMenu.prototype.dontPosition    = SM_dontPosition;
SimpleMenu.prototype.findNavObjs     = SM_findNavObjs;
SimpleMenu.prototype.forcePosition   = SM_forcePosition;
SimpleMenu.prototype.getNavObj       = SM_getNavObj;
SimpleMenu.prototype.getMenuByName   = SM_getMenuByName;
SimpleMenu.prototype.hideMenu        = SM_hideMenu;
SimpleMenu.prototype.init            = SM_init;
SimpleMenu.prototype.rePositionMenus = SM_rePositionMenus;
SimpleMenu.prototype.showMenu        = SM_showMenu;
SimpleMenu.prototype.setImageDir     = SM_setImageDir;


// ---------------------------------- SM_buildMenus

function SM_buildMenus() {
  var allDivs = document.getElementsByTagName('div');
  
  for (var i=0; i < allDivs.length; i++){
    var navObj = this.getNavObj(allDivs[i].id);
    if (!navObj) { continue; }; 
    this.menuArr[this.menuArr.length] = new Menu(allDivs[i], navObj);
  };
  return;
};

// ---------------------------------- SM_buildRollovers

function SM_buildRollovers() {
  for (var i=0; i < this.navObjs.length; i++) {
    var navObj = this.navObjs[i];
    if (navObj.set){ continue; };
    new Rollover(navObj);
  };
  return;
};

// ---------------------------------- SM_dontPosition

function SM_dontPosition() {
  this.dontPositionFlag = true;
};

// ---------------------------------- SM_findNavObjs

function SM_findNavObjs()
{
  for (i = 0; i < document.images.length; i++)
    {
      var imgObj = document.images[i];
      if (imgObj.name == ''){ continue; };                 // skip
      if (imgObj.src.indexOf("_over") != -1){ continue; }; // skip 
      
      var link = findLinkByName(imgObj.name);
      if (link == null){ continue; };                      // skip
      
      this.navObjs[this.navObjs.length] = new NavObj(imgObj, link);
    };  
};


// ---------------------------------- SM_forcePosition

function SM_forcePosition(x,y)
{
  this.forcePositionFlag = true;
  this.forcePositionX = x;
  this.forcePositionY = y;
};


// ---------------------------------- SM_getNavObj

function SM_getNavObj(name) {
  for (var i=0; i < this.navObjs.length; i++) {
    if (this.navObjs[i].name == name){ return this.navObjs[i] };
  };
  return false;
};

// ---------------------------------- SM_getMenuByName

function SM_getMenuByName(name) {
  for (var i=0; i < this.menuArr.length; i++) {
    if (this.menuArr[i].name == name){ return this.menuArr[i] };
  };
  return false;
};

// ---------------------------------- SM_hideMenu

function SM_hideMenu(name)
{
  document.images[name].src = document.images[name].src1.src;
  var menu = this.getMenuByName(name);
  if (!menu){ return; };
  menu.obj.style.visibility = "hidden";
};

// ---------------------------------- SM_init

function SM_init() {
  if (this.preExisting_onload != null){ this.preExisting_onload(); };    

  this.findNavObjs();
  this.buildMenus();
  this.buildRollovers();  

  this.initSwitch = 1;
  window.onresize = this.rePositionMenus;
  return;
};

// ---------------------------------- SM_rePositionMenus

function SM_rePositionMenus()
{
  var app = window.simpleMenuApp;
  for (var i=0; i < app.menuArr.length; i++) {
    app.menuArr[i].positionMenu();
  };
};

// ---------------------------------- SM_setImageDir

function SM_setImageDir(imageDir) { this.imgDirectory = imageDir; };


// ---------------------------------- SM_showMenu
// sho nuff 
function SM_showMenu(name)
{
  document.images[name].src = document.images[name].src2.src; 
  var menu = this.getMenuByName(name);
  if (!menu){ return; };
  menu.obj.style.visibility = "visible";
};



// ============================================= Rollover

function Rollover(navObj) {
  this.image    = navObj.img;
  this.link     = navObj.link;
  this.link.app = this.app
  this.name     = this.image.name;
  this.addOverState(this.image); // append src 2
  
  if (arguments.length == 2){ 
    this.menu = arguments[1]; 
    
    if (this.menu != this.name){
      this.addMouseEvents('sub', this.menu);
    } else {
      this.addMouseEvents('menu');
    };
    
  } else {
    this.addMouseEvents('roll');
  };
  
  navObj.set = 1;
};
Rollover.prototype.addOverState   = Rollover_addOverState;
Rollover.prototype.addMouseEvents = Rollover_addMouseEvents;

// ---------------------------------- Rollover_addOverState

function Rollover_addOverState(imgObj) {
  // create rollover image objects and append them as 
  // properties (src1,src2) to the original image
  srcExt = imgObj.src.substring(imgObj.src.length - 3);
  var tempImgSrcStart = imgObj.src.lastIndexOf("/") + 1;
  if (tempImgSrcStart == -1){ tempImgSrcStart = 0; };
  tempImgSrcEnd = imgObj.src.length - 4;
  
  var overSrc = this.app.imgDirectory;
  overSrc += imgObj.src.substring(tempImgSrcStart, tempImgSrcEnd);
  overSrc += "_over.";
  overSrc += srcExt;
  
  imgObj.src1     = new Image(); 
  imgObj.src1.src = imgObj.src;
  imgObj.src2     = new Image();
  imgObj.src2.src = overSrc;
  return;
};

// ---------------------------------- Rollover_addMouseEvents

function Rollover_addMouseEvents(type) {

  if (type == "roll") {
    this.link.onmouseover = function() {
      if (!this.app.initSwitch){ return; };
      document.images[this.name].src = document.images[this.name].src2.src; 
    };
    
    this.link.onmouseout = function() {
      if (!this.app.initSwitch){ return; };
      document.images[this.name].src = document.images[this.name].src1.src; 
    };
    return true;
  };  

  if (type == "menu") {
    this.link.onmouseover = function() {
      if (!this.app.initSwitch){ return; };
      if (this.app.currentMenu){ this.app.hideMenu(this.app.currentMenu); };
      window.clearTimeout(this.app.timerID);
      this.app.showMenu(this.name);
      this.app.currentMenu = this.name;
    };
    
    this.link.onmouseout = function() {
      if (!this.app.initSwitch){ return; };
      window.clearTimeout(this.app.timerID);
      this.app.timerID = window.setTimeout("window.simpleMenuApp.hideMenu('" + this.name + "')", 800);
    };
    return true;
  };  

  if (type == "sub") {
    var menuName = arguments[1];

    this.link.onmouseover = function() {
      if (!this.app.initSwitch){ return; };
      document.images[this.name].src = document.images[this.name].src2.src; 
      window.clearTimeout(this.app.timerID);
      this.app.showMenu(menuName);
    };
    
    this.link.onmouseout = function() {
      if (!this.app.initSwitch){ return; };
      document.images[this.name].src = document.images[this.name].src1.src; 
      window.clearTimeout(this.app.timerID);
      this.app.timerID = window.setTimeout("window.simpleMenuApp.hideMenu('" + menuName + "')", 800);
    };
    return true;
  };  
  
  return false;
};



// ============================================= Menu

function Menu(divObj, navObj) {  
  this.obj    = divObj;
  this.name   = this.obj.id;
  this.navObj = navObj;
  
  new Rollover(navObj, this.name);
  
  this.buildSubRollovers();

  if(this.app.dontPositionFlag == false){
    this.positionMenu();
  };

  if(this.app.forcePositionFlag == true){
    this.obj.style.left = this.app.forcePositionX + "px";
    this.obj.style.top  = this.app.forcePositionY + "px";
  };

};
Menu.prototype.buildSubRollovers = Menu_buildSubRollovers;
Menu.prototype.positionMenu      = Menu_positionMenu;

// ---------------------------------- Menu_buildSubRollovers

function Menu_buildSubRollovers() {
  var menuLinks = this.obj.getElementsByTagName('a');

  for (var i = 0; i < menuLinks.length; i++) {
    if (menuLinks[i].name == ""){ continue; };

    var navObj = this.app.getNavObj(menuLinks[i].name);
    if (!navObj){ continue; };

    new Rollover(navObj, this.name);
  };
};

// ---------------------------------- Menu_positionMenu

function Menu_positionMenu() {
  var pos = findPos(this.navObj.img);
  this.obj.style.left = pos[0] + "px";
  this.obj.style.top  = pos[1] + this.navObj.img.height + "px";  
};



// ============================================= NavObj

function NavObj(image, link) {
  this.img  = image;
  this.link = link;
  this.name = image.name;
  this.set  = 0;
};



// findLinkByName

function findLinkByName(name) {
  for (j=0; j < document.links.length; j++)
    {  
      if (document.links[j].name == name) { return document.links[j]; };
    };
  return null;
};


// findPos
function findPos(obj) {
  // thanks ppk
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft;
    curtop  = obj.offsetTop;
    // if an obj has no offsetParent, this condition 
    // evaluates to null, ending the loop 
    while (obj = obj.offsetParent) 
      {
        curleft += obj.offsetLeft;
        curtop += obj.offsetTop;
      };
  };
  return [curleft,curtop];
};


// ============================================= PAGE CODE


var SM = new SimpleMenu();
SM.setImageDir('images/');
SM.dontPosition();  

window.onload = function(){ 
  SM.init(); 
};


function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

