/***************************************************************************
* Context Menu
* Copyright 2002 by David Schontzler | www.stilleye.com
* Free to use under GNU General Public License as long as this message
*  remains intact.
* Description:  Create a custom context (right-click) menu.
* URI: http://www.stilleye.com/projects/dhtml/contextMenu/
***************************************************************************
* Version: 1.0
***************************************************************************/
function checkDom()
{
	this.name=navigator.appName.toLowerCase()
	this.ver=navigator.appVersion.toLowerCase()
	this.opera=(this.name=="opera")?1:0
	this.dom=(document.getElementById&&!this.opera)?1:0
	this.ie=(document.all)?1:0
	this.ie6=(this.dom&&this.ie&&this.ver.indexOf('msie 6'))?1:0
	this.ie5=(this.dom&&this.ie&&!this.ie6)?1:0
	this.ns4=(document.layers)?1:0
	this.ns6=(this.dom&&!this.ie)?1:0
	this.ns=(this.ns4||this.ns6)?1:0
	this.bw=(this.dom||this.ie||this.ns||this.opera)?1:0
}
bw = new checkDom()

function getPageSize()
{
	this.x = document.getElementsByTagName('html').item(0).clientWidth||document.getElementsByTagName('html').item(0).offsetWidth||document.body.offsetWidth||innerWidth
	this.y = document.getElementsByTagName('html').item(0).clientHeight||document.getElementsByTagName('html').item(0).offsetHeight||document.body.offsetHeight||innerHeight
	this.x2 = parseInt(this.x/2)||0
	this.y2 = parseInt(this.y/2)||0
	this.sx = document.body.scrollWidth||0
	this.sy = document.body.scrollHeight||0
}
var pg

function pgScrollAt()
{
	this.x = document.getElementsByTagName('html').item(0).scrollLeft||document.body.scrollLeft||(bw.ns6?pageXOffset:0)||0
	this.y = document.getElementsByTagName('html').item(0).scrollTop||document.body.scrollTop||(bw.ns6?pageYOffset:0)||0
}

function getCoords(e)
{
	if(bw.ie) e = event
	this.x = e.clientX
	this.y = e.clientY
}

function makeDom(name)
{
	if(!document.getElementById) return null
	this.name = name
	this.evnt = document.getElementById(name)
	this.css = this.evnt.style
	this.x = this.css.pixelLeft || this.evnt.offsetLeft
	this.y = this.css.pixelTop || this.evnt.offsetTop
	this.w = this.css.pixelWidth || this.evnt.offsetWidth
	this.h = this.css.pixelHeight || this.evnt.offsetHeight

	makeDom.prototype.moveTo = function(x,y)
	{
		x = parseInt(x); y = parseInt(y)
		this.css.left = x+'px'
		this.css.top = y+'px'
		this.x = x
		this.y = y
	}
	makeDom.prototype.show = function()
	{
		this.css.visibility = 'visible'
	}
	
	makeDom.prototype.hide = function()
	{
		this.css.visibility = 'hidden'
	}
	makeDom.prototype.clipTo = function(t,r,b,l,sW)
	{
		this.cT = t; this.cR = r; this.cB = b; this.cL = l
		a = arguments
		for(k in a) a[k] = a[k]>=0 ? a[k]+'px' : a[k]
		this.css.clip = 'rect('+t+','+r+','+b+','+l+')'
		if(sW) this.css.width=r
	}
}


function showContMenu(e)
{
	ms = new getCoords(e)
	cX = ms.x+aContMenu.w<pg.x ? ms.x : ms.x-aContMenu.w
	cY = ms.y+aContMenu.h<pg.y ? ms.y : ms.y-aContMenu.h
	sc = new pgScrollAt()
	aContMenu.moveTo(cX+sc.x,cY+sc.y)
	aContMenu.show()
	cDirX = cX==ms.x ? 1:0
	cDirY = cY==ms.y ? 1:0
	doClip(cDirX,cDirY)
	return false
}

cM = 0
function doClip(dirX,dirY)
{
	if(cM<100)
	{
		cT=cR=cB=cL='auto'
		if(dirX) cR = cM+'%'
			else cL = (100-cM)+'%'
		if(dirY) cB = cM+'%'
			else cT = (100-cM)+'%'
		aContMenu.clipTo(cT,cR,cB,cL)
		cM+=40
		setTimeout('doClip('+dirX+','+dirY+')',35)
	}
	else
	{
		aContMenu.clipTo('auto','auto','auto','auto')
		cM = 0
	}
}

function initContMenu(e)
{
	if(bw.dom)
	{
		pg = new getPageSize()
		aContMenu = new makeDom('contMenu')
		isLoaded = 1
	}
}

onload = initContMenu
document.oncontextmenu=showContMenu
document.onclick=new Function('aContMenu.hide()')
