
//====================================
// DDL Menu
// From Dawson's DHTML Library
//
// Version: 4.0
//
// Author:   Dawson Cowals
// Created:  06-07-2002
// Modified: 02-07-2005
//
// Now supports IE 5 properly
//====================================



//============================================================================
// DO NOT CHANGE ANYTHING BELOW THIS LINE OR THE MENUS WILL BREAK
//============================================================================

//DDLMenuObj type constructor
function DDLMenuObj() {

  //number of main menu entries
  this.numberOfMenus          = 0;
  this.imagePath              = "";
  this.imageIDPrefix          = "img_";
  this.imageOffSuffix         = "_off";
  this.imageOnSuffix          = "_on";

  //main menu properties
  this.mainMenuLayer          = "divMainMenu";
  this.mainImgPrefix          = "main";
  this.mainImgExt             = ".gif";
  this.mainImgOffSrc          = new Array();
  this.mainImgOnSrc           = new Array();

  //sub menu properties
  this.subMenuActive          = new Array();
  this.subMenuLayerPrefix     = "divMenu";
  this.subMenuItems           = new Array();
  this.menuImgPrefix          = "menu";
  this.menuImgExt             = ".gif";
  this.menuImgOffSrc          = new Array();
  this.menuImgOnSrc           = new Array();

  //member functions
  this.initMenu               = initMenu;
  this.initSubMenu            = initSubMenu;
  this.showMenu               = showMenu;
  this.closeMenu              = closeMenu;
  this.hideAllMenusExcept     = hideAllMenusExcept;
  this.positionSubMenu        = positionSubMenu;
  this.getSubMenuLayerName    = getSubMenuLayerName;
  this.mainImageSwap          = mainImageSwap;
  this.menuImageSwap          = menuImageSwap;

}

function initMenu(numberOfMenus, imagePath, imageIDPrefix, imageOffSuffix, imageOnSuffix, mainMenuLayer, mainImgPrefix, mainImgExt, subMenuLayerPrefix, menuImgPrefix, menuImgExt) {
  var i = 0;

  this.numberOfMenus      = numberOfMenus;
  this.imagePath          = imagePath;
  this.mainMenuLayer      = mainMenuLayer;
  this.mainImgPrefix      = mainImgPrefix;
  this.mainImgExt         = mainImgExt;
  this.subMenuLayerPrefix = subMenuLayerPrefix;
  this.menuImgPrefix      = menuImgPrefix;
  this.menuImgExt         = menuImgExt;
  this.imageIDPrefix      = imageIDPrefix;
  this.imageOffSuffix     = imageOffSuffix;
  this.imageOnSuffix      = imageOnSuffix;

  //inner loop through each main menu item
  for (i=0; i < numberOfMenus; i++) {
    this.mainImgOffSrc[i]     = new Image();
    this.mainImgOffSrc[i].src = imagePath + mainImgPrefix + i + imageOffSuffix + mainImgExt;
    this.mainImgOnSrc[i]      = new Image();
    this.mainImgOnSrc[i].src  = imagePath + mainImgPrefix + i + imageOnSuffix  + mainImgExt;
  }
}

function initSubMenu(subMenuIndex, numberOfItems) {
  var i        = 0;
  var tmpIndex = "";

  this.subMenuItems[subMenuIndex]  = numberOfItems;

  //loop through each sub menu item
  for (i=0; i < numberOfItems; i++) {
  tmpIndex = "" + subMenuIndex + nameDelimiter + i + "";
    this.menuImgOffSrc[tmpIndex]     = new Image();
    this.menuImgOffSrc[tmpIndex].src = this.imagePath + this.menuImgPrefix + tmpIndex + this.imageOffSuffix + this.menuImgExt;
    this.menuImgOnSrc[tmpIndex]      = new Image();
    this.menuImgOnSrc[tmpIndex].src  = this.imagePath + this.menuImgPrefix + tmpIndex + this.imageOnSuffix  + this.menuImgExt;
  }
}

function mainImageSwap(imgIndex,imgState) {
  if (document.images) {
  var imgName  = this.imageIDPrefix + imgIndex + "";
  var imgLayer = this.mainMenuLayer;

  switch (imgState) {
    case 'off':
      //if we are using a newer browser
      if (is_nav6up || is_ie6up || is_gecko) {
         changeSrc = eval('document.getElementById("'+imgName+'").setAttribute("src","'+this.imagePath + this.mainImgPrefix + imgIndex + this.imageOffSuffix + this.mainImgExt + '")');
      //if we are dealing with NS4 and we are inside a layer
      //we have to call the div name first
      } else if (is_nav4up && imgLayer!=null) {
         changeSrc = eval('document.layers["'+imgLayer+'"].document.images["'+imgName+'"].src=this.mainImgOffSrc['+imgIndex+'].src');
      //otherwise if we are using IE5 or not accessing an image inside a layer
      } else {
         document.images[imgName].src = this.mainImgOffSrc[imgIndex].src;
      }
      break;
    case 'on':
      //if we are using a newer browser
      if (is_nav6up || is_ie6up || is_gecko) {
         changeSrc = eval('document.getElementById("'+imgName+'").setAttribute("src","'+this.imagePath + this.mainImgPrefix + imgIndex + this.imageOnSuffix + this.mainImgExt+'")');
      //if we are dealing with NS4 and we are inside a layer
      //we have to call the div name first
      } else if (is_nav4up && imgLayer!=null) {
         changeSrc = eval('document.layers["'+imgLayer+'"].document.images["'+imgName+'"].src=this.mainImgOnSrc['+imgIndex+'].src');
      //otherwise if we are using IE5 or not accessing an image inside a layer
      } else {
         document.images[imgName].src = this.mainImgOnSrc[imgIndex].src;
      }
      break;
  }
  }
}

function menuImageSwap(menuIndex, imgIndex, imgState) {
  if (document.images) {

  //create string like "0_0", "0_1", etc
  var tmpIndex = "" + menuIndex + nameDelimiter + imgIndex + "";
  //piece together image name / ID: "img_0_0"
  var imgName  = "" + this.imageIDPrefix + tmpIndex;
  //grab menu layer name
  var imgLayer = this.getSubMenuLayerName(menuIndex);

  switch (imgState) {
    case 'off':
      //if we are using a newer browser
      if (is_nav6up || is_ie6up || is_gecko) {
         changeSrc = eval('document.getElementById(imgName).setAttribute("src",this.imagePath + this.menuImgPrefix + tmpIndex + this.imageOffSuffix + this.menuImgExt)');
      //if we are dealing with NS4 and we are inside a layer
      //we have to call the div name first
      } else if (is_nav4up && imgLayer!=null) {
         changeSrc = eval('document.layers["'+imgLayer+'"].document.images["'+imgName+'"].src=this.menuImgOffSrc["'+tmpIndex+'"].src');
      //otherwise if we are using IE5 or not accessing an image inside a layer
      } else {
         document.images[imgName].src = this.menuImgOffSrc[tmpIndex].src;
      }
      break;
    case 'on':
      //if we are using a newer browser
      if (is_nav6up || is_ie6up || is_gecko) {
         changeSrc = eval('document.getElementById(imgName).setAttribute("src",this.imagePath + this.menuImgPrefix + tmpIndex + this.imageOnSuffix + this.menuImgExt)');
      //if we are dealing with NS4 and we are inside a layer
      //we have to call the div name first
      } else if (is_nav4up && imgLayer!=null) {
         changeSrc = eval('document.layers["'+imgLayer+'"].document.images["'+imgName+'"].src=this.menuImgOnSrc["'+tmpIndex+'"].src');
      //otherwise if we are using IE5 or not accessing an image inside a layer
      } else {
         document.images[imgName].src = this.menuImgOnSrc[tmpIndex].src;
      }
      break;
  }
  }
}

//given menu index returns layer name
function getSubMenuLayerName(menuIndex) {
  return ("" + this.subMenuLayerPrefix + menuIndex + "");
}

//returns count of number of delimiters in menu index string
//which correlates to the submenu level
function getSubMenuLevel(menuIndex) {
  var i = 0;
  var subMenuLevel = 0;

  for (i=0; i < menuIndex.length; i++) {
    if (menuIndex.substr(i,1) == nameDelimiter) {
      subMenuLevel++;
    }
  }
  return subMenuLevel;
}

//pass 'none' to turn off all menus, pass a menu index to turn on that menu
function showMenu(menuIndex) {
  if (menuIndex == 'none') {
    var i = 0;
    for (i=0; i < numberOfMenus; i++) {
      toggleLayer(this.getSubMenuLayerName(i), 'hide');
      eval('this.mainImageSwap('+i+',"off")');
    }
  } else {
    //show layer
    toggleLayer(getSubMenuLayerName(menuIndex),'show');
    //hide other layers
    this.hideAllMenusExcept(menuIndex);
  }
}

//pass the subMenu index to close
function closeMenu(menuIndex) {
  toggleLayer(this.getSubMenuLayerName(menuIndex),'hide');
  eval('this.mainImageSwap('+menuIndex+',"off")');
}

//hides all menus except the one represented by the index passed
function hideAllMenusExcept(menuIndex) {
  var i = 0;
  for (i=0; i < numberOfMenus; i++) {
    if (this.getSubMenuLayerName(i) != this.getSubMenuLayerName(menuIndex)) {
      toggleLayer(this.getSubMenuLayerName(i), 'hide');
      eval('this.mainImageSwap('+i+',"off")');
    }
  }
}

//send xOffset and yOffset for submenu to be positioned relative to
//main menu item with same number, for pages where menu position can shift on resize
//call this function prior to using "showMenu(##)"
//NOTE: this currently only works for menus with no further submenu levels
function positionSubMenu(menuIndex,xOffset,yOffset){
  var item;
  var imgName    = this.imageIDPrefix + menuIndex + "";
  var imgLayer   = this.mainMenuLayer;
  var menuImageX = 0;
  var menuImageY = 0;
  var menuLayer  = "";
  var newX       = 0;
  var newY       = 0;

  //create string like "0_0", "0_1", etc
  //var tmpIndex = "" + menuIndex + nameDelimiter + imgIndex + "";
  //piece together image name / ID: "img_0_0"
  //var imgName  = "" + this.imageIDPrefix + tmpIndex;

  if (is_nav6up || is_ie6up || is_gecko) {
     item = eval('document.getElementById(imgName)');
  //if this is an old IE 5.x browser
  } else if (is_ie5up) {
     item = eval('document.all[imgName]');
  }

  //determine location of main menu item
  menuImageX = getPageOffsetLeft(item);
  menuImageY = getPageOffsetTop(item);

  //set new positions
  if (is_gecko) {
    //adjust offsets for mozilla
    xOffset = xOffset - 1;
    //yOffset = yOffset - 1;
  }

  newX = menuImageX + xOffset;
  newY = menuImageY + yOffset;

  menuLayer = this.getSubMenuLayerName(menuIndex)

  if (is_nav6up || is_ie6up || is_gecko) {
    //position submenu to appear correctly related to main menu item
    document.getElementById(menuLayer).style.left = newX  + "px";
    document.getElementById(menuLayer).style.top  = newY + "px";
  //if this is an old IE 5.x browser
  } else if (is_ie5up) {
    document.all[menuLayer].style.left = newX + "px";
    document.all[menuLayer].style.top  = newY + "px";
  }
}

//function takes a layer name and a toggle value
//works in both NS and IE to hide layer or make it visible
function toggleLayer(layerID,tog) {
   //if we want to hide the layer
   if (tog == 'hide') {
        //if this is new 6.0 Browser
        if (is_nav6up || is_ie6up || is_gecko) {
           document.getElementById(layerID).style.visibility = "hidden";
        //if this is an old Netscape 4.x browser
        }  else if (is_nav4up) {
           document.layers[layerID].visibility='hide';
      //if this is an old IE 5.x browser
        } else if (is_ie5up) {
           document.all[layerID].style.visibility='hidden';
        }
   //otherwise we want to show it
   } else {
        //if this is new 6.0 Browser
        if (is_nav6up || is_ie6up) {
           document.getElementById(layerID).style.visibility = "visible";
        //if this is an old Netscape 4.x browser
        }  else if (is_nav4up) {
           document.layers[layerID].visibility='show';
      //if this is an old IE 5.x browser
        } else if (is_ie5up) {
           document.all[layerID].style.visibility='visible';
        }
   }
}

function getPageOffsetLeft(el) {
  var x;

  // Return the x coordinate of an element relative to the page.
  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
}

function getPageOffsetTop(el) {
  var y;

  // Return the y coordinate of an element relative to the page.
  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}