function contextMenu(cssHide,cssShow,menuCssId,doc,cmShowDelay,cmWidth,cmHeight,showEvent) {var self=this;var d;var isCmVisible;var cmEl;var mouseOverCm;var isIe;var pointerX;var pointerY;var timeoutId;var hideMenu=_hideContextMenu();self.objName = 'contextMenu';self.initialised = false;self.version = '0.9';self.copyright = 'Copyright (C) 2006 Paul Mackinlay (info@webotech.co.uk)';self.hide=function(){_hideContextMenuImpl('force');};self.destroy=function(){_hideContextMenuImpl('force');if(self.initialised){_removeEventListeners();}if(timeoutId){clearTimeout(timeoutId);}for(var p in self){delete self[p];}};self.reset=function(cssHide,cssShow,menuCssId,doc,cmShowDelay,cmWidth,cmHeight,showEvent){if(self.initialised){_removeEventListeners();_hideContextMenuImpl('force');}self.initialised = false;var errorMsg='';d = doc;if(!d){d = window.document;}if(!cmShowDelay){cmShowDelay = 800;}if(!cmWidth){cmWidth = 150;}if(!cmHeight){cmHeight = 180;}if(!cssHide){errorMsg += "cssHide input is mandatory in object "+self.objName+"\n";}if(!cssShow){errorMsg += "cssShow input is mandatory in object "+self.objName+"\n";}if(!menuCssId){errorMsg += "menuCssId input is mandatory in object "+self.objName+"\n";}if(errorMsg){self.destroy();return(-1);}if(!showEvent){showEvent = 'oncontextmenu';}self.showDelay = cmShowDelay;self.cmWidth = cmWidth;self.cmHeight = cmHeight;self.cssHide = cssHide;self.cssShow = cssShow;self.menuCssId = menuCssId;self.showEvent = showEvent;cmEl = d.getElementById(self.menuCssId);isIe = d.all?true:false;pointerX = 0;pointerY = 0;isCmVisible = false;mouseOverCm = false;if(!isIe){var event=null;}if(!isIe){d.captureEvents(Event.MOUSEMOVE);}cmEl.style.width = self.cmWidth+"px";cmEl.style.height = self.cmHeight+"px";if(isIe){d.attachEvent('onmousemove', _isPointOverCm);}else{d.addEventListener('mousemove', _isPointOverCm, false);}if(showEvent.toLowerCase()=='ondblclick'){if(isIe){d.attachEvent('ondblclick',_setPointPos);}else{d.addEventListener('dblclick', _setPointPos, false);}}else{if(isIe){d.attachEvent('oncontextmenu',_setPointPos);}else{d.addEventListener('contextmenu', _setPointPos, false);d.oncontextmenu=function() {return false};}}if(isIe){window.attachEvent('onunload',self.destroy);}else{window.addEventListener('unload',self.destroy,false);}self.initialised = true;return false;};self.reset(cssHide,cssShow,menuCssId,doc,cmShowDelay,cmWidth,cmHeight,showEvent);function _removeEventListeners() {if(isIe){d.detachEvent('onmousemove',_isPointOverCm);d.detachEvent('oncontextmenu',_setPointPos);d.detachEvent('ondblclick',_setPointPos);window.detachEvent('onunload',self.destroy);}else{d.removeEventListener('mousemove', _isPointOverCm, false);d.removeEventListener('contextmenu', _setPointPos, false);d.removeEventListener('dblclick', _setPointPos, false);window.removeEventListener('unload',self.destroy,false);delete d.oncontextmenu;}}function _showContextMenu() {if(!self.initialised){self.reset(self.cssHide,self.cssShow,self.menuCssId,d,self.showDelay,self.cmWidth,self.cmHeight,self.showEvent);}cmEl.className = self.cssShow;cmEl.style.left = pointerX+"px";cmEl.style.top = pointerY+"px";mouseOverCm = true;isCmVisible = true;}function _hideContextMenu() {return (function() {_hideContextMenuImpl();});}function _hideContextMenuImpl(type) {if(type&&type=='force'){mouseOverCm = false;}if(isCmVisible&&!mouseOverCm){cmEl.className = self.cssHide;mouseOverCm = false;isCmVisible = false;}}function _setPointPos(e) {var coords=_getPointPos(e);pointerX = coords[0];pointerY = coords[1];_showContextMenu();return false;}function _isPointOverCm(e) {var pad=10;var cmLeftPos=pointerX - pad;var cmRightPos=cmLeftPos+self.cmWidth+pad;var cmTopPos=pointerY - pad;var cmBottomPos=cmTopPos+self.cmHeight+pad;var coords=_getPointPos(e);var x=coords[0];var y=coords[1];if (x > cmLeftPos&&x < cmRightPos&&y > cmTopPos&&y < cmBottomPos) {mouseOverCm = true;}else{mouseOverCm = false;if(isCmVisible){timeoutId = setTimeout(hideMenu, self.showDelay);}}return false;}function _getPointPos(e) {var coords=new Array();if(!isIe){coords[0] = e.pageX;coords[1] = e.pageY;}if(isIe){coords[0] = event.clientX+d.body.scrollLeft;coords[1] = event.clientY+d.body.scrollTop;}return coords;}}
