//Pop-it menu- By Dynamic Drive
//For full source code and more DHTML scripts, visit http://www.dynamicdrive.com
//This credit MUST stay intact for use

var ie4 = document.all && navigator.userAgent.indexOf("Opera")==-1;
var ns6 = document.getElementById&&!document.all;


function showmenu(event, which, width, height) {
	showPopup(toEventXY(event), which, width, height);
}


function showPopup(eXY, which, width, height) {
   if (!document.all&&!document.getElementById&&!document.layers)
      return;

   clearhidemenu()

   menuobj =
      ie4? document.all.popmenu 
         : document.getElementById("popmenu");

   menuobj.thestyle = menuobj.style;

	menuobj.thestyle.width  = width;
	menuobj.thestyle.height = height;

	menuobj.innerHTML = which;

   menuobj.contentwidth = menuobj.offsetWidth;
   menuobj.contentheight = menuobj.offsetHeight;

   //Find out how close the mouse is to the corner of the window
   var rightedge =
      ie4? document.body.clientWidth-eXY.x : window.innerWidth-eXY.x;
   var bottomedge =
      ie4? document.body.clientHeight-eXY.y : window.innerHeight-eXY.y;


   var left = 0;
   if ( rightedge < menuobj.contentwidth ) {
      left = 
         ie4? document.body.scrollLeft+eXY.x-menuobj.contentwidth
            : window.pageXOffset+eXY.x-menuobj.contentwidth;
		if (ie4 && left < document.body.scrollLeft)
			left = document.body.scroolLeft;
		if ( !ie4 && top < window.pageXOffset)
			left = window.pageXOffset;
      left += 3;
   }
   else {
      left =
         ie4? document.body.scrollLeft+eXY.x : window.pageXOffset+eXY.x;
      if (left > 3)
         left -= 5;
   }
	if (left < -1)
		left = -1;

   menuobj.thestyle.left = left;


   // same concept with the vertical position
   var top = 0;
   if ( bottomedge < menuobj.contentheight ) {
      top = 
         ie4? document.body.scrollTop+eXY.y-menuobj.contentheight
            : window.pageYOffset+eXY.y-menuobj.contentheight;
		if (ie4 && top < document.body.scrollTop)
			top = document.body.scroolTop;
		if ( !ie4 && top < window.pageYOffset)
			top = window.pageYOffset;
      top += 3;
   }
   else {
      top =
         ie4? document.body.scrollTop+event.clientY
            : window.pageYOffset+eXY.y;
      if (top > 3)
         top -= 5;
   }
	top = top - menuobj.contentheight + 8;
	if (top < -1)
		top = -1;

	if (! top)
		top = 0;

   menuobj.thestyle.top = top;

   menuobj.thestyle.visibility="visible"

   return false;
}


function contains_ns6(a, b) {
   // Determines if 1 element in contained in another- by Brainjar.com
	if (!b)
		return false;
   while (b.parentNode)
      if ((b = b.parentNode) == a)
         return true;
   return false;
}


function hidemenu() {
   if (window.menuobj)
      menuobj.thestyle.visibility = "hidden";
}


function dynamichide(e) {
   if (ie4 && !menuobj.contains(e.toElement))
      hidemenu();
   else if ( ns6 && e.currentTarget != e.relatedTarget && !contains_ns6(e.currentTarget, e.relatedTarget))
      hidemenu();
}

function delayhidemenu() {
	delayhide=setTimeout("hidemenu()",500)
}

function clearhidemenu() {
   if (window.delayhide)
      clearTimeout(delayhide);
}


function highlightmenu(e,state) {
   if (document.all)
      source_el = event.srcElement;
   else if (document.getElementById)
      source_el = e.target;
   if (source_el.className=="menuitem")
      source_el.id = (state=="on")? "mouseoverstyle" : "";
   else{
      while (source_el.id != "popmenu") {
         source_el = document.getElementById?
            source_el.parentNode : source_el.parentElement;
         if (source_el.className == "menuitem")
            source_el.id = (state=="on")? "mouseoverstyle" : ""
      }
   }
}


function openmenu(event) {
   clearhidemenu();
   highlightmenu(event,'on');
}


function closemenu(event) {
   highlightmenu(event,'off');
   dynamichide(event);
}


document.onclick=hidemenu;


// define an object to encapsulate event location.
function EventXY(x, y) {
	this.x = x;
	this.y = y;
}


function toEventXY(event) {
	return new EventXY(event.clientX, event.clientY);
}

