// SiteHelm Menus, Version 1.0
// By David Leigh Christy
// www.dlchristy.com
//
// Derived from David Christy's SiteHelm, 
// a full-featured web site management system and CMS
// www.sitehelm.com
//
// JavaScript portion based on scripts by
// Project Seven, www.projectseven.com
//
// LICENSE:
// Copyright © 2004 David Leigh Christy
// All rights reserved.
// 
// Redistribution and use in source and binary forms,
// with or without modification, are permitted provided
// that the following conditions are met:
// 
// * Redistributions of source code must retain the above
//   copyright notice, this list of conditions and the
//   following disclaimer.
// * Redistributions in binary form must reproduce the above
//   copyright notice, this list of conditions and the 
//   following disclaimer in the documentation and/or other 
//   materials provided with the distribution.
// * Neither the name SiteHelm, David Leigh Christy, nor the 
//   names of its contributors may be used to endorse or
//   promote products derived from this software without
//   specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

function ddMenu(menuID)
{
 this.menuID         = menuID;

 this.menuTimer      = null;
 this.hideFlag       = false;
 this.menusOn        = new Array();
 this.menuClasses    = new Array();
 this.menuImages     = new Array();
 this.menuPos        = new Array();

//Configuration
 this.nameItem       = 'Item';
 this.nameImage      = 'Image';
 this.nameMenu       = '';
 this.imageON        = '';
 this.imageEmptyON   = '';
 this.menuPos[1]     = new Array(0,20);

 this.showMenu          = ddMenu_showMenu;
 this.hideMenus         = ddMenu_hideMenus;
 this.autoLayers        = ddMenu_autoLayers;
 this.snap              = ddMenu_snap;

 this._doHideMenus      = ddMenu_doHideMenus;
 this._resetItems       = ddmenu_resetItems;
}

function ddMenu_autoLayers() {
 var g,b,k,f,args=ddMenu_autoLayers.arguments,largs=new Array();
 a=parseInt(args[0]);
 if(isNaN(a))a=0;
 if(!document.p7setc){
  p7c=new Array();
  document.p7setc=true;
  for(var u=0;u<10;u++){
   p7c[u]=new Array();
  }
 }
 for(k=0;k<p7c[a].length;k++){
  if((g=ddMenu_findObj(p7c[a][k]))!=null){
  b=(document.layers)?g:g.style;b.visibility="hidden";
  }
 }
 largs = (typeof args[1] == "object") ? args[1] : args;
 for(k=1;k<largs.length;k++){
  if((g=ddMenu_findObj(largs[k]))!=null){
   b=(document.layers)?g:g.style;
   b.visibility="visible";f=false;
   for(var j=0;j<p7c[a].length;j++){
    if(largs[k]==p7c[a][j]) {
     f=true;
    }
   }
   if(!f){
    p7c[a][p7c[a].length++]=largs[k];
   }
  }
 }

}

function ddMenu_snap() {
 var x,y,ox,bx,oy,p,tx,a,b,k,d,da,e,el,tw,q0,xx,yy,w1,pa='px',args=ddMenu_snap.arguments;a=parseInt(a);
 if(document.layers||window.opera){pa='';}for(k=0;k<(args.length);k+=4){
 if((g=ddMenu_findObj(args[k]))!=null){if((el=ddMenu_findObj(args[k+1]))!=null){
 a=parseInt(args[k+2]);b=parseInt(args[k+3]);x=0;y=0;ox=0;oy=0;p="";tx=1;
 da="document.all['"+args[k]+"']";if(document.getElementById){
 d="document.getElementsByName('"+args[k]+"')[0]";if(!eval(d)){
 d="document.getElementById('"+args[k]+"')";if(!eval(d)){d=da;}}
 }else if(document.all){d=da;}if(document.all||document.getElementById){while(tx==1){
 p+=".offsetParent";if(eval(d+p)){x+=parseInt(eval(d+p+".offsetLeft"));y+=parseInt(eval(d+p+".offsetTop"));
 }else{tx=0;}}ox=parseInt(g.offsetLeft);oy=parseInt(g.offsetTop);tw=x+ox+y+oy;
 if(tw==0||(navigator.appVersion.indexOf("MSIE 4")>-1&&navigator.appVersion.indexOf("Mac")>-1)){
  ox=0;oy=0;if(g.style.left){x=parseInt(g.style.left);y=parseInt(g.style.top);}else{
  w1=parseInt(el.style.width);bx=(a<0)?-5-w1:-10;a=(Math.abs(a)<1000)?0:a;b=(Math.abs(b)<1000)?0:b;
  x=document.body.scrollLeft+event.clientX+bx;y=document.body.scrollTop+event.clientY;}}
 }else if(document.layers){x=g.x;y=g.y;q0=document.layers,dd="";for(var s=0;s<q0.length;s++){
  dd='document.'+q0[s].name;if(eval(dd+'.document.'+args[k])){x+=eval(dd+'.left');y+=eval(dd+'.top');
  break;}}}e=(document.layers)?el:el.style;xx=parseInt(x+ox+a),yy=parseInt(y+oy+b);
 if(navigator.appVersion.indexOf("MSIE 5")>-1 && navigator.appVersion.indexOf("Mac")>-1){
  xx+=parseInt(document.body.leftMargin);yy+=parseInt(document.body.topMargin);}
 e.left=xx+pa;e.top=yy+pa;}}}
}

function ddMenu_hideMenus() {
  this.menuTimer = setTimeout(this.menuID+"._doHideMenus()", 300);
  this.hideFlag = true;
}

function ddMenu_doHideMenus() {
  if (!this.hideFlag) return;
  this.autoLayers(0);
  this._resetItems();
}

function ddmenu_resetItems() {
 var mit, mim;
 if (this.menusOn.length > 0) {
  for(key in this.menusOn) {
   mit = this.menuID + this.nameItem + this.menusOn[key];
   mim = this.menuID + this.nameImage + this.menusOn[key];
   if ( (h = ddMenu_findObj(mit) ) != null ) {
    h.className = this.menuClasses[key];
   }
   if ( this.menuImages[key] && (h = ddMenu_findObj(mim) ) != null ) {
    h.src = this.menuImages[key];
   }
  }
 }
}

function ddMenu_showMenu() {
  var bn, ml, mit, mim, mm, img, x=0, y=0, mex="", al, args=ddMenu_showMenu.arguments, bns=new Array(), bnsc=new Array(), bnsi=new Array(), menus=new Array();
// More Levels?
  ml =  args[0];
// Turn off all items
  this._resetItems();
// Build Menus
  al = args.length;
  for(k=1; k<al; k++) {
   bn = args[k];
   mex += "_" + bn;
   mit = this.menuID + this.nameItem + mex;
   mim = this.menuID + this.nameImage + mex;
   mm  = this.menuID + this.nameMenu + mex;
   if ( (g = ddMenu_findObj(mit) ) != null ) {
    bns[k]  = mex;    
    bnsc[k] = g.className;    
    g.className = bnsc[k] + "ON";
   }
   if (k<(al-1) || ml) {
    menus[k] = mm;
    if (this.menuPos[k]) {
     x=this.menuPos[k][0];
     y=this.menuPos[k][1];
    }
    this.snap(mim,mm,x,y);
    img=this.imageON;
   } else {
    img=this.imageEmptyON;
   }
   if ( img && (g = ddMenu_findObj(mim) ) != null ) {
    bnsi[k] = g.src;    
    g.src = img;
   }
  }
  this.menusOn     = bns;
  this.menuClasses = bnsc;
  this.menuImages  = bnsi;
// Reset Menu Hiding
  if (this.menuTimer) {
   clearTimeout(this.menuTimer);
  }
  this.menuTimer = null;
  this.hideFlag  = false;

  if (menus.length > 0) {
   this.autoLayers(0, menus);
  } else {
   this.autoLayers(0);
  }
}

function ddMenu_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=ddMenu_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


