var ie5 = document.all && document.getElementById
var ns6 = document.getElementById && !document.all

if (ie5||ns6)
{
	var menuobj = document.getElementById("cmenu")
	var x = document.getElementById('x')
	document.oncontextmenu = showcmenu
	document.onclick = hidecmenu
}

function otevri(adresa, tar, attr)
{
	window.open(adresa, tar, attr)
}

function showcmenu(e)
{
	//Find out how close the mouse is to the corner of the window
	var rightedge = ie5 ? document.documentElement.clientWidth-event.clientX : document.documentElement.clientWidth-e.clientX
	var bottomedge = ie5 ? document.documentElement.clientHeight-event.clientY : document.documentElement.clientHeight-e.clientY
	
	var firingobj=ie5 ? event.srcElement : e.target
	if(firingobj.nodeName == "A")
	{
		var y = document.getElementById('y')
			
		y.firstChild.nodeValue = "Otevřít v novém okně..."
		y.getAttributeNode('href').nodeValue = "javascript:otevri('" + firingobj.getAttributeNode('href').nodeValue + "', '', '')";
		x.style.display = 'list-item';
	}
	else if(firingobj.nodeName == "IMG")
	{
		var y = document.getElementById('y')
			
		y.firstChild.nodeValue = "Otevřít obrázek..."
		y.getAttributeNode('href').nodeValue = "javascript:otevri(\"" + firingobj.getAttributeNode('src').nodeValue + "\", \"Obrázek\", \"width="+firingobj.width+",height="+firingobj.height+"\")";
		x.style.display = 'list-item';
	}

	//if the horizontal distance isn't enough to accomodate the width of the context menu
	if (rightedge < menuobj.offsetWidth)
		//move the horizontal position of the menu to the left by it's width
		menuobj.style.left = ie5 ? event.clientX+document.documentElement.scrollLeft-menuobj.offsetWidth+"px" : e.clientX+document.documentElement.scrollLeft-menuobj.offsetWidth+"px"
	else
		//position the horizontal position of the menu where the mouse was clicked
		menuobj.style.left = ie5 ? document.documentElement.scrollLeft+event.clientX+"px" : document.documentElement.scrollLeft+e.clientX+"px"

	//same concept with the vertical position
	if (bottomedge < menuobj.offsetHeight)
		menuobj.style.top = ie5 ? event.clientY+document.documentElement.scrollTop-menuobj.offsetHeight+"px" : e.clientY+document.documentElement.scrollTop-menuobj.offsetHeight+"px"
	else
		menuobj.style.top = ie5 ? document.documentElement.scrollTop+event.clientY+"px" : document.documentElement.scrollTop+e.clientY+"px"
	
	menuobj.style.visibility = "visible"
	
	return false			
}

function hidecmenu(e)
{
	menuobj.style.visibility = "hidden"
	x.style.display = "none"
}