//Chrome Drop Down Menu v2.01- Author: Dynamic Drive (http://www.dynamicdrive.com)
//Last updated: November 14th 06- added iframe shim technique
// Tutorial: http://www.dynamicdrive.com/dynamicindex1/chrome/
// EXTENSIVELY enhanced with additional features 03-14-08, Christine Jamison
// (http://www.askthetechnobabe.com)

// Here's the syntaxt to do 2 menus:
// <script type="text/javascript">
// cssdropdown.startchrome("chromemenu", "chromemenu2")
// </script>

var menuitems='';
var cssdropdown={
// when user clicks on a menu item with a drop down menu,
// disable menu item's link?
disablemenuclick: true,
enableswipe: 1, //enable swipe effect? 1 for yes, 0 for no
// enable "iframe shim" technique to get drop down menus to correctly appear
// on top of controls such as form objects in IE5.5/IE6? 1 for yes, 0 for no
enableiframeshim: 1,
// Enable click of anchor menu elements? 1 for yes, 0 for No.
anchormenuclick: 1,
// Anchor menu horizontal(0) or vertical (width in px)?
anchormenusize: 0,
// Delay in miliseconds before anchor menu attributes "unhover" onmouseout
// (0 to not disappear)
anchormenudelay: 100,
// If Anchor menu vertical, drop menu(s) vertical(0) or horizontal(width in px)?
// (If Horizontal, dropmenuwidth 'auto' is highly recommended!)
dropmenusize: 0,
// width of drop menu; can be '' (match anchormenusize), 'auto', 'parent' or
// '000px' (which fixes the width for all dropmenus)
dropmenuwidth: '',
// left offset of drop menu in px, 0 for auto place,
// 'center' to override everything and center it in the browser or
// -1 to match first field of anchormenu, etc.
dropmenuleft: 0,
// offset to left in px from results of above field *or*
// absolute offset in the UOM of your choice AS A STRING *or*
// 'center' to override everything and center it in the browser.
dropmenuleftoffset: 5,
dropmenuleftoffset_ie: 6,
// set delay in miliseconds before menu disappears onmouseout
// (0 to not disappear)
dropmenudelay: 250,

//No need to edit beyond here////////////////////////
dropmenuobj: null,
ie: document.all,
firefox: document.getElementById&&!document.all,
swipetimer: undefined,
bottomclip:0,
anchormenuX: 0,
anchorBkgndColor: '',
anchorHoverColor: '',

getposOffset:function(what, offsettype)
  { var work1, work2;
    var totaloffset=0;
    var parentobj=what.offsetParent;
    var browser_width=0;
    if (this.ie) browser_width=document.body.offsetWidth;
    if (this.firefox) browser_width=window.innerWidth;

//    var totaloffset=(offsettype == "left")? what.offsetLeft : what.offsetTop;
    if (offsettype == 'left')
      { if (typeof(this.dropmenuleft) == 'string')
	  { if (this.dropmenuleft == 'center')
	      { work1=parentobj.offsetWidth;
		work2=(browser_width-work1)/2-0;
		totaloffset=work2;
	      }
	 else { return this.dropmenuleft;
	      }
	  }
     else { if (this.dropmenuleft != 0)
	      { if (this.dropmenuleft > 0)
		  {totaloffset=this.dropmenuleft;}
	     else {totaloffset=this.anchormenuX;}
	      }
	 else { if (this.anchormenusize == 0)
		  {totaloffset=what.offsetLeft-5;}
	     else {totaloffset=this.anchormenusize+2;}
	      }
	  }
	work1=this.dropmenuleftoffset;
	if (this.ie)
	  {work1=this.dropmenuleftoffset_ie;}
	totaloffset=totaloffset+work1;
      }
// offsettype == "top"
 else { if (this.anchormenusize == 0)
	  { totaloffset=parseInt(what.offsetTop);
//  if (this.ie)
// Bottom SM 0, Top SM -2, Bottom IE +1, top IE +2
	  }
     else { totaloffset=parseInt(what.offsetTop)-23;	// Magic Number (18+5?)
	  }
      }
    var parentEl=what.offsetParent;
    while (parentEl != null)
      { 
//work1=(offsettype == "left")? parentEl.offsetLeft : parentEl.offsetTop;
	if (offsettype == "left")
	  { work1=parentEl.offsetLeft;
	  }
     else { work1=parentEl.offsetTop;
	  }
	totaloffset +=work1;
	parentEl=parentEl.offsetParent;
      }
    return totaloffset;
  },

swipeeffect:function()
  { if (this.bottomclip < parseInt(this.dropmenuobj.offsetHeight))
//unclip drop down menu visibility gradually
      { this.bottomclip +=10+(this.bottomclip/10);
	this.dropmenuobj.style.clip="rect(0 auto "+this.bottomclip+"px 0)";
      }
   else return;
    this.swipetimer=setTimeout("cssdropdown.swipeeffect()", 10);
  },

showhide:function(obj, e)
  { if (this.ie || this.firefox)
      this.dropmenuobj.style.left=this.dropmenuobj.style.top="-500px";
    if (e.type == "click" && obj.visibility == hidden || e.type == "mouseover")
      { if (this.enableswipe == 1)
	  { if (typeof this.swipetimer != "undefined")
	      clearTimeout(this.swipetimer);
	    obj.clip="rect(0 auto 0 0)"; //hide menu via clipping
	    this.bottomclip=0;
	    this.swipeeffect();
	  }
	obj.visibility="visible";
      }
   else if (e.type == "click")
	  obj.visibility="hidden";
  },

iecompattest:function()
  { return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
  },

clearbrowseredge:function(obj, whichedge)
  { var edgeoffset=0
    if (whichedge=="rightedge")
      { var windowedge=this.ie && !window.opera? this.iecompattest().scrollLeft+this.iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
	this.dropmenuobj.contentmeasure=this.dropmenuobj.offsetWidth
//move menu to the left?
	if (windowedge-this.dropmenuobj.x < this.dropmenuobj.contentmeasure)
	   edgeoffset=this.dropmenuobj.contentmeasure-obj.offsetWidth
      }
 else { var topedge=this.ie && !window.opera? this.iecompattest().scrollTop : window.pageYOffset
	var windowedge=this.ie && !window.opera? this.iecompattest().scrollTop+this.iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
	this.dropmenuobj.contentmeasure=this.dropmenuobj.offsetHeight
	if (windowedge-this.dropmenuobj.y < this.dropmenuobj.contentmeasure) //move up?
	  { edgeoffset=this.dropmenuobj.contentmeasure+obj.offsetHeight
//up no good either?
	    if ((this.dropmenuobj.y-topedge)<this.dropmenuobj.contentmeasure)
	       edgeoffset=this.dropmenuobj.y+obj.offsetHeight-topedge
	  }
      }
    return edgeoffset
  },

dropit:function(obj, e, dropmenuID)
  { if (this.dropmenuobj != null) //hide previous menu
       this.dropmenuobj.style.visibility="hidden" //hide menu
    this.clearhidedropdownmenu()
    if (this.ie || this.firefox)
      { obj.onmouseout=function() {cssdropdown.delayhidedropdownmenu()}
	if (this.anchormenuclick == 0)	//disable main menu item link onclick?
	  { obj.onclick=function() {return !cssdropdown.disablemenuclick}
	  }
	var parentobj=obj.offsetParent;

	this.dropmenuobj=document.getElementById(dropmenuID)
	this.dropmenuobj.onmouseover=function() {cssdropdown.clearhidedropdownmenu()}
	this.dropmenuobj.onmouseout=function(e) {cssdropdown.dynamichide(e)}
	this.dropmenuobj.onclick=function() {cssdropdown.delayhidedropdownmenu()}
	this.showhide(this.dropmenuobj.style, e)
	if (this.dropmenuwidth == 'parent')
	  { work1=parentobj.offsetWidth-10;
	    this.dropmenuobj.style.width=work1+'px';
	  }
     else { if (this.dropmenuwidth != '')
	      {this.dropmenuobj.style.width=this.dropmenuwidth;}
	  }
	this.dropmenuobj.x=this.getposOffset(obj, "left")
	this.dropmenuobj.y=this.getposOffset(obj, "top");
	if (typeof(this.dropmenuobj.x) == 'string')
	  { this.dropmenuobj.style.left=this.dropmenuobj.x;
	  }
     else { this.dropmenuobj.style.left=
	      this.dropmenuobj.x-this.clearbrowseredge(obj, "rightedge")+"px";
	  }
	this.dropmenuobj.style.top=
	  this.dropmenuobj.y-this.clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+1+"px"
	this.positionshim() //call iframe shim function
      }
  },

positionshim:function()		//display iframe shim function
  { if (this.enableiframeshim && typeof this.shimobject != "undefined")
      { if (this.dropmenuobj.style.visibility == "visible")
	  { this.shimobject.style.width=this.dropmenuobj.offsetWidth+"px";
	    this.shimobject.style.height=this.dropmenuobj.offsetHeight+"px";
	    this.shimobject.style.left=this.dropmenuobj.style.left;
	    this.shimobject.style.top=this.dropmenuobj.style.top;
	  }
	this.shimobject.style.display=
	    (this.dropmenuobj.style.visibility=="visible")? "block" : "none";
      }
  },

hideshim:function()
  { if (this.enableiframeshim && typeof this.shimobject != "undefined")
       this.shimobject.style.display='none';
  },

contains_firefox:function(a, b)
  { while (b.parentNode)
      if ((b = b.parentNode) == a)
	 return true;
    return false;
  },

dynamichide:function(e)
  { var evtobj=window.event? window.event : e
    if (this.ie && !this.dropmenuobj.contains(evtobj.toElement))
       this.delayhidedropdownmenu()
  else if (this.firefox && e.currentTarget != evtobj.relatedTarget &&
	   !this.contains_firefox(evtobj.currentTarget, evtobj.relatedTarget))
	  this.delayhidedropdownmenu()
  },

//hide dropdown menu after delay
delayhidedropdownmenu:function()
  { if (this.dropmenudelay > 0)
      { this.delayhidedropdown=
          setTimeout("cssdropdown.dropmenuobj.style.visibility='hidden'; cssdropdown.hideshim()",
	    this.dropmenudelay)
      }
  },

clearhidedropdownmenu:function()
  { if (this.delayhidedropdown != "undefined")
       clearTimeout(this.delayhidedropdown)
  },

changecss:function(className, elementName, newValue)
  { var cssRules;
    if (this.ie)
      {cssRules='rules';}
    if (this.firefox)
      {cssRules='cssRules';}
    for (var sLoop=0; sLoop < document.styleSheets.length; sLoop++)
      { for (var rLoop=0; rLoop < document.styleSheets[sLoop][cssRules].length; rLoop++)
          { if (document.styleSheets[sLoop][cssRules][rLoop].selectorText.toLowerCase() == className.toLowerCase())
              { document.styleSheets[sLoop][cssRules][rLoop].style[elementName]=newValue;
	      }
	  }
      }	
  },

getcss:function(className, elementName)
  { var cssRules;
    if (this.ie)
      {cssRules='rules';}
    if (this.firefox)
      {cssRules='cssRules';}
    for (var sLoop=0; sLoop < document.styleSheets.length; sLoop++)
      { for (var rLoop=0; rLoop < document.styleSheets[sLoop][cssRules].length; rLoop++)
          { if (document.styleSheets[sLoop][cssRules][rLoop].selectorText.toLowerCase() == className.toLowerCase())
              { return document.styleSheets[sLoop][cssRules][rLoop].style[elementName];
	      }
	  }
      }	
    return false;
  },

startchrome:function()
  { var styleType;

function onhover(localmenuobj, event2)
  { var loop1, work1;
    var element_name='backgroundColor';
    var flag1=0;
    if (cssdropdown.anchormenudelay == 0)
      { for (loop1=0; loop1 < menuitems.length; loop1++)
	  { if (typeof(cssdropdown.dropmenuleft) == 'number' &&
		cssdropdown.dropmenuleft == -loop1-1)
	      {cssdropdown.anchormenuX=menuitems[loop1].offsetLeft;}
//work2="TEST Pt 1B:"+loop1+'::'+localmenuobj.id;
//alert(work2);
	    if (flag1 == 0 && menuitems[loop1].id == localmenuobj.id)
	      { flag1=1;
		loop1=0;
	      }
	    if (flag1 != 0)
	      { if (menuitems[loop1].id == localmenuobj.id)
		  { menuitems[loop1].style[element_name]=cssdropdown.anchorHoverColor;
		  }
	     else { menuitems[loop1].style[element_name]=cssdropdown.anchorBkgndColor;
		  }
	      }
	  }
	if (cssdropdown.dropmenuobj != null)
	  {cssdropdown.dropmenuobj.style.visibility='hidden';}
	cssdropdown.clearhidedropdownmenu();
      }

// {document.getElementId(arguments[ids]).style.width=this.anchormenusize+'px';}
// var relvalue=menuitems[i].getAttribute('rel');
    if (localmenuobj.getAttribute('rel'))
      {cssdropdown.dropit(localmenuobj,event2,localmenuobj.getAttribute('rel'));}
  }

    if (this.anchormenusize != 0)
      {this.changecss('.chromestyle ul', 'width', this.anchormenusize+'px');}
    if (this.dropmenusize != 0)
      { this.changecss('.dropmenudiv a', 'display', 'inline');
	this.changecss('.dropmenudiv', 'width', this.dropmenusize+'px');
      }
    this.anchorBkgndColor=this.getcss('.chromestyle ul', 'backgroundColor');
    this.anchorHoverColor=this.getcss('.chromestyle ul li a:hover', 'backgroundColor');

    for (var ids=0; ids<arguments.length; ids++)
      { menuitems=
	    document.getElementById(arguments[ids]).getElementsByTagName('a');
	for (var i=0; i<menuitems.length; i++)
	  { menuitems[i].onmouseover=function(e)
	      { var event=typeof e != "undefined"? e : window.event
		onhover(this, event);
	      }
	  }
      }
//if IE5.5 to IE6, create iframe for iframe shim technique
    if (typeof this.shimobject == 'undefined' &&
        window.createPopup && !window.XmlHttpRequest)
      { document.write('<IFRAME id="iframeshim"  src="" style="display: none;'+
	    ' left: 0; top: 0; z-index: 90; position: absolute; '+
	    'filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)"'+
	    ' frameBorder="0" scrolling="no"></IFRAME>')
	this.shimobject=document.getElementById("iframeshim") //reference iframe object
      }
  }
}
