/*******************************************************************************
 * ChaseMenu Script version 1.0
 * Author: Chris Whalin
 * Copyright 2008 Chase Software Solutions
 *
 *
 * ChaseMenu IS NOT Freeware!
 * Licensing information: http://www.fjevans.com/cmenu/
 ******************************************************************************/

// MENU SETTINGS
var LinkColor           ='#ffffff';
var LinkBGColor         ='transparent';
var LinkHoverColor      ='#000000';
var LinkHoverBGColor    ='#ffffee';
var SubLinkColor        ='#000000';
var SubLinkBGColor      ='#eeeeee';
var SubLinkHoverColor   ='#000000';
var SubLinkHoverBGColor ='#ffffee';

var MenuElementId       ='menu';
var MenuTopPosition     =40;
var MenuLeftPosition    =5;
var SubMenuTopOffset    =2;
var SubMenuLeftOffset   =-10;
var MenuCloseDelay      =420;
var MenuArrowImg        ='v_arrow.gif'
// END MENU SETTINGS





// script variables
var isNS=(navigator.appName=="Netscape"?true:false);
var activeMainMenu;
var activeMenu;
var activeTimer;


function debug(str){
  var d=document.getElementById('debug_txt');
  d.innerHTML+='<br>'+str.replace(/\n/g, "<br>");
  d.scrollTop=d.scrollHeight;
}


function ulinit() {
  var m = document.getElementById(MenuElementId);
  var li = m.getElementsByTagName("LI");
  for (i=0; i<li.length; i++) {
    li[i].onmouseover=ulmenuOpen;
    li[i].onmouseout=ulmenuClose;
    if (li[i].childNodes.length>1){
      if (li[i].getElementsByTagName("UL").length > 0){
        li[i].firstChild.style.background='url('+MenuArrowImg+') no-repeat';
        li[i].firstChild.style.backgroundPosition='99% 5px';
      }
    }
  }
  document.getElementById(MenuElementId).style.visibility='visible';
  var h=document.getElementById(MenuElementId).offsetHeight;
  if (isNS){var mb=document.getElementById("menubox");mb.style.top=(mb.offsetTop+1)+'px';}
  if (document.getElementById("menu_area"))
  {
    document.getElementById("menu_area").style.height=(h+29)+'px';
  }
}
window.onload=ulinit;

function ulmenuOpen(e){
  if(document.all)window.event.cancelBubble=true;
  else e.stopPropagation();
  if (activeTimer)clearTimeout(activeTimer);
  this.getElementsByTagName("A")[0].style.backgroundColor=LinkHoverBGColor;
  this.getElementsByTagName("A")[0].style.color=LinkHoverColor;
  var isSub=(activeMenu?document.getElementById(activeMenu).parentNode.id==MenuElementId:null);
  if (!isSub) {
    try {
      this.parentNode.parentNode.style.backgroundColor=LinkBGColor;
      this.parentNode.parentNode.style.color=LinkColor;
    } catch(e){}
  }
  if (activeMenu!=this.id) {
    if (this.parentNode.id==MenuElementId) {
      if (activeMenu!=null){
        var apid=document.getElementById(activeMenu).parentNode.id.replace("_sub","");
        if (apid!=this.id) {
          var obj=document.getElementById(activeMenu);
          var m=getMenuParent(obj);
          hideChildMenus(m[0],m[1]);
        } else {
          var a=document.getElementById(activeMenu);
          a.getElementsByTagName("A")[0].style.backgroundColor=SubLinkBGColor;
          a.getElementsByTagName("A")[0].style.color=SubLinkColor;
        }
      }
    }
    else {
      var am=document.getElementById(activeMenu);
      am.getElementsByTagName("A")[0].style.backgroundColor=isSub?LinkBGColor:SubLinkBGColor;
      am.getElementsByTagName("A")[0].style.color=isSub?LinkColor:SubLinkColor;
      //try {
        if (this.parentNode.id.replace("_sub","") != activeMenu){
          var app=am.parentNode.parentNode;
          if (app.id!=this.parentNode.id.replace("_sub","")) {
            //debug(app.id + ' - ' + this.id);
            if (app.id==this.id && this.parentNode.id!=MenuElementId) {
              this.getElementsByTagName("A")[0].style.backgroundColor=SubLinkHoverBGColor;
              this.getElementsByTagName("A")[0].style.color=SubLinkHoverColor;
              var ul = am.getElementsByTagName("UL");
              if (ul.length>0) {
                for (x=(ul.length-1); x>=0; x--){
                  ul[x].style.display='none';
                  if(x==0)break;
                }
              }
            } else {
              app.getElementsByTagName("A")[0].style.backgroundColor=isSub?LinkBGColor:SubLinkBGColor;
              app.getElementsByTagName("A")[0].style.color=isSub?LinkColor:SubLinkColor;
              app.getElementsByTagName("UL")[0].style.display='none';
            }
          }
          else {
            var ul = am.getElementsByTagName("UL");
            if (ul.length>0) {
              for (x=(ul.length-1); x>=0; x--){
                ul[x].style.display='none';
                if(x==0)break;
              }
            }
          }
            
        }
      //} catch(e){}
    }
  }
  activeMenu=this.id;
  try {
    var ul=this.getElementsByTagName("UL")[0];
    var dh=(isNS?document.body.clientHeight:document.documentElement.clientHeight);
    var dw=(isNS?document.body.clientWidth:document.documentElement.clientWidth);
    var dsh=(isNS?document.body.scrollHeight:document.documentElement.scrollHeight);
    var dsw=(isNS?document.body.scrollWidth:document.documentElement.scrollWidth);
    ul.style.left=this.offsetWidth+SubMenuLeftOffset+'px';
    ul.style.top=this.offsetTop+SubMenuTopOffset+'px';
    ul.style.display='block';
    var pos;
    if (ul!=null)pos=getMenuPosition(ul);
    if (pos[2]>dh){
      var t=pos[2]-dh;
      var ntop=ul.offsetTop-(t+10);
      ul.style.top=ntop+'px';
    }
    if (pos[1]>dw){
      var l=pos[1]-dw;
      var nleft=ul.offsetLeft-(ul.offsetWidth*2)+20;
      ul.style.left=nleft+'px';
    }
  } catch(e){}
  selectRoots(this);
}

function selectRoots(obj){
  var pnode=obj.parentNode;
  while (pnode) {
    if (pnode.id==MenuElementId)break;
    if (pnode.nodeName=="LI") {
      pnode.getElementsByTagName("A")[0].style.backgroundColor=LinkHoverBGColor;
      pnode.getElementsByTagName("A")[0].style.color=LinkHoverColor;
    }
    pnode=pnode.parentNode;
  }
}

function getMenuPosition(obj) {
  var pnode=obj;
  var root=document.getElementById('menubox');
  var top=0,left=0,right=0,bottom=0;
  var w=obj.offsetWidth;
  var h=obj.offsetHeight;
  while (pnode) {
    if (pnode.nodeName=="UL"){
      if (pnode.id==MenuElementId){
        top+=pnode.offsetTop+root.offsetTop;
        left+=pnode.offsetLeft+root.offsetLeft;
        bottom=top+h+SubMenuTopOffset;
        right+=pnode.offsetWidth+root.offsetLeft;
        return [top,right,bottom,left];
        break;
      }
      top+=pnode.offsetTop;
      left+=pnode.offsetLeft;
      right+=pnode.offsetWidth+SubMenuLeftOffset;
    }
    pnode=pnode.parentNode;
  }
}

function hideChildMenus(id,parent){
  var m=document.getElementById(id);
  var p=document.getElementById(parent);
  var c=p.getElementsByTagName('UL');
  m.getElementsByTagName("A")[0].style.backgroundColor=SubLinkBGColor;
  m.getElementsByTagName("A")[0].style.color=SubLinkColor;
  p.getElementsByTagName('A')[0].style.backgroundColor=LinkBGColor;
  p.getElementsByTagName('A')[0].style.color=LinkColor;
  var i=(c.length-1);
  for (x=i; x>=0; x--){
    c[x].style.display='none';
    var a=c[x].parentNode.getElementsByTagName('A')[0];
    a.style.backgroundColor=(c[x].parentNode.parentNode.id==MenuElementId?LinkBGColor:SubLinkBGColor);
    a.style.color=(c[x].parentNode.parentNode.id==MenuElementId?LinkColor:SubLinkColor);
    if (x==0) break;
  }
  activeMenu=null;
}

function getMenuParent(obj){
  var p=obj;
  var m=obj.id
  while (p){
    if (p.parentNode.id==MenuElementId){
      return [obj.id,m]; break;
    }
    p=p.parentNode;
    m=p.id;
  }
}

function ulmenuClose(e){
  if(document.all)window.event.cancelBubble=true;
  else e.stopPropagation();
  var m=getMenuParent(this);
  activeTimer=setTimeout("hideChildMenus('"+m[0]+"','"+m[1]+"')",MenuCloseDelay);
}