

var defaultMenuWidth="150px" //set default menu width.


var ie5=document.all && !window.opera
var ns6=document.getElementById
var g_menuobj; 

function iecompattest()
{
	if (document.compatMode && document.compatMode.indexOf("CSS") != -1)
		return document.documentElement ; 
	else 
		return document.body; 
}

function showmenu(e, myDiv, link, optWidth)
{
	if (!document.all&&!document.getElementById)
		return; 
		
	if (ie5)
		link.setAttribute('menuobj', myDiv); 
	else 
		g_menuobj = myDiv; 
		
	
	var menuobj; 
	
	if (ie5)
		menuobj = link.getAttribute('menuobj'); 
	else 
		menuobj = g_menuobj; 
	
	clearhidemenu(link); 
	
	var iframeShim = document.getElementById('DivShim');
	if (menuobj.filters != null && menuobj.filters.length > 0)
	{
		if (iframeShim != null)
			iframeShim.filters[0].apply(); 
		menuobj.filters[0].apply(); 
	}
		
	if (ie5)
		menuobj.style.width=(typeof optWidth!="undefined")? optWidth : defaultMenuWidth; 
	
	if (ie5)
	{
		menuobj.contentwidth=menuobj.style.pixelWidth; 
		menuobj.contentheight=menuobj.style.pixelHeight; 
	}
	else 
	{
		menuobj.contentwidth=menuobj.offsetWidth; 
		menuobj.contentheight=menuobj.offsetHeight; 
	}
	eventX = ie5 ? event.clientX : e.clientX; 
	eventY = ie5 ? event.clientY : e.clientY; 

	//Find out how close the mouse is to the corner of the window
	var rightedge=ie5? iecompattest().clientWidth-eventX : window.innerWidth-eventX
	var bottomedge=ie5? iecompattest().clientHeight-eventY : window.innerHeight-eventY

	//if the horizontal distance isn't enough to accomodate the width of the context menu
	if (rightedge<menuobj.contentwidth)
		//move the horizontal position of the menu to the left by it's width
		menuobj.style.left=ie5? iecompattest().scrollLeft+eventX-menuobj.contentwidth+"px" : window.pageXOffset+eventX-menuobj.contentwidth+"px"
	else
		//position the horizontal position of the menu where the mouse was clicked
		menuobj.style.left=ie5? iecompattest().scrollLeft+eventX+"px" : window.pageXOffset+eventX+"px"
		
	//same concept with the vertical position
	if (bottomedge < menuobj.contentheight)
		menuobj.style.top=ie5? iecompattest().scrollTop+eventY-menuobj.contentheight+"px" : window.pageYOffset+eventY-menuobj.contentheight+"px"
	else
		menuobj.style.top=ie5? iecompattest().scrollTop+eventY+"px" : window.pageYOffset+eventY+"px"

	if (ie5)
	{
		if (menuobj.offsetParent.tagName != "BODY")
		{
			menuobj.style.left = menuobj.style.pixelLeft - menuobj.offsetParent.offsetLeft; 
			menuobj.style.top = menuobj.style.pixelTop - menuobj.offsetParent.offsetTop; 
		}
			
		menuobj.style.display = "block";
		if (iframeShim != null)
		{
			iframeShim.style.width = menuobj.offsetWidth;
			iframeShim.style.height = menuobj.offsetHeight;
		}
		
		if (iframeShim != null)
		{
			if (menuobj.offsetParent.tagName != "BODY")
			{
				iframeShim.style.top = menuobj.style.pixelTop + menuobj.offsetParent.offsetTop + "px";
				iframeShim.style.left = menuobj.style.pixelLeft + menuobj.offsetParent.offsetLeft + "px";
			}
			else
			{
				iframeShim.style.top = menuobj.style.top;
				iframeShim.style.left = menuobj.style.left;
			}
		}

		if (iframeShim != null)
		{
			iframeShim.style.zIndex = menuobj.style.zIndex - 1;
			iframeShim.style.display = "block";
		}
    }
    else 
		menuobj.style.visibility = "visible"; 

	if (menuobj.filters != null && menuobj.filters.length > 0)
	{
		if (iframeShim != null)
		{
			iframeShim.filters[0].play(0.3); 
			menuobj.filters[0].play(0.3); 
		}
	}

	return false
}

function contains_ns6(a, b) 
{
	//Determines if 1 element is contained in another- by Brainjar.com
	while (b.parentNode)
	{
		if ((b = b.parentNode) == a)
			return true;
	}
		
	return false;
}

function hidemenu(link)
{
	var iframeShim = document.getElementById('DivShim');
		
	var menuobj; 
	
	if (ie5 && link != null)
		menuobj = link.getAttribute('menuobj'); 
	else 
		menuobj = g_menuobj; 
	
	if (menuobj && menuobj.filters != null && menuobj.filters.length > 0)
	{
		menuobj.filters[0].apply(); 
		
		if (iframeShim != null)
			iframeShim.filters[0].apply(); 
	}
	
	if (menuobj)
	{
		if (ie5)
		{
			menuobj.style.display="none"; 
			
			if (iframeShim != null)
				iframeShim.style.display = "none";
		}
		else 
		{ 
			menuobj.style.visibility="hidden"; 
		}
	}
		
	if (menuobj && menuobj.filters != null && menuobj.filters.length > 0)
	{
		menuobj.filters[0].play(0.3); 
		if (iframeShim != null)
			iframeShim.filters[0].play(0.3); 
	}
}

function dynamichide(e, link)
{
	var menuobj; 
	
	if (ie5)
		menuobj = link.getAttribute('menuobj'); 
	else 
		menuobj = g_menuobj; 

	if (menuobj != null)
	{
		if (ie5&&!menuobj.contains(e.toElement))
			hidemenu(link)
		else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
			hidemenu(link)
	}
}

function delayhidemenu(e, link)
{
	link.setAttribute('delayhide', setTimeout('hidemenu(' + link.id + ')',100)) 
	
}

function clearhidemenu(link)
{
	if (link.getAttribute('delayhide'))
		clearTimeout(link.getAttribute('delayhide')); 

	link.setAttribute('delayhide', null)	
}


