var _replaceContext = true; // replace the system context menu?
var _mouseOverContext = false; // is the mouse over the context menu? 
var _noContext = false; // disable the context menu?
var _divContext = null;
var _aQuestionWeb = null;
var _aQuestionClub = null;
if (document.oncontextmenu)
   document.oncontextmenu = function() { return false; }
else if (document.body)
   document.body.oncontextmenu = function() { return false; }

// call from the onMouseDown event, passing the event if standards compliant 
function ContextMouseDown(event) {
	if (_noContext || _mouseOverContext)
		return;
// IE is evil and doesn't pass the event object 
	if (event == null) 
		event = window.event;
// we assume we have a standards compliant browser, but check if we have IE 
	var target = event.target != null ? event.target : event.srcElement;
// only show the context menu if the right mouse button is pressed 
// and a hyperlink has been clicked (the code can be made more selective) 
	if (event.button == 2 && target.tagName.toLowerCase() == 'a')
		_replaceContext = true;
	else if (!_mouseOverContext && _replaceContext)
		_divContext.style.display = 'none';
} 

function CloseContext() {
	_mouseOverContext = false;
	_divContext.style.display = 'none';
	document.body.removeChild(_divContext);
} 
// call from the onContextMenu event, passing the event 
// if this function returns false, the browser's context menu will not show up 

function ContextShow(event) {
	if (_noContext || _mouseOverContext)
		return false; 
// IE is evil and doesn't pass the event object 
	if (event == null) 
		event = window.event;
// we assume we have a standards compliant browser, but check if we have IE
//	var target = event.target != null ? event.target : event.srcElement;
	if (_replaceContext) {
//              $('aContextNav').href = target.href;
		//$('aQuestionWeb').href = 'mailto:webmaster@scpb.org'
		_aQuestionWeb.href = 'mailto:webmaster@scpb.org'
		
		//$('aQuestionClub').href = 'mailto:contact@scpb.org'
		_aQuestionClub.href = 'mailto:contact@scpb.org'
// document.body.scrollTop does not work in IE
		var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
		var scrollLeft = document.body.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft;
// hide the menu first to avoid an "up-then-over" visual effect 
		_divContext.style.display = 'none';
		_divContext.style.left = event.clientX + scrollLeft + 'px';
		_divContext.style.top = event.clientY + scrollTop + 'px';
		_divContext.style.display = 'block';
		document.body.appendChild(_divContext);
//		_replaceContext = false;
	}
        return false;
}

function DisableContext() {
	_noContext = true;
	CloseContext();
//	$('aEnable').style.display = '';
	return false; 
} 

function EnableContext() {
	_noContext = false;
	_mouseOverContext = false;
// this gets left enabled when "disable menus" is chosen 
//	$('aEnable').style.display = 'none';
	return false; 
} 
// comes from prototype.js; this is simply easier on the eyes and fingers 

function $(id) {
	return document.getElementById(id);
}
   
function InitContext(replace_context_param) {
         _replaceContext = replace_context_param;
		 
         if (_replaceContext) {
			_divContext = document.createElement('div');
			_divContext.id='divContext';
			
			var _ul = document.createElement('ul');
			_divContext.appendChild(_ul);
			
			var _li = document.createElement('li');
			_ul.appendChild(_li);
			_aQuestionWeb = document.createElement('a');
			_aQuestionWeb.id='aQuestionWeb';
			_aQuestionWeb.href='#';
			_aQuestionWeb.innerHTML='Envoyer un mail au webmaster&nbsp;';
			_li.appendChild(_aQuestionWeb);
			
			_li = document.createElement('li');
			_ul.appendChild(_li);
			_aQuestionClub = document.createElement('a');
			_aQuestionClub.id='aQuestionClub';
			_aQuestionClub.href='#';
			_aQuestionClub.innerHTML='Poser une question au club&nbsp;';
			_li.appendChild(_aQuestionClub);
			
			_li = document.createElement('li');
			_ul.appendChild(_li);
			 var _a = document.createElement('a');
			_a.href='http://www.scpb.org';
			_a.innerHTML='Retourner &agrave; la page d\'accueil';
			_li.appendChild(_a);
			
          //  _divContext = $('divContext'); // makes my life easier
			_divContext.onmouseover = function() { _mouseOverContext = true; };
			_divContext.onmouseout = function() { _mouseOverContext = false; };
         } else {
			document.body.appendChild(_divContext);
			/*document.body.oncontextmenu = ContextShow;
			document.body.onmousedown = ContextMouseDown;*/
		 }
//	$('aDisable').onclick = DisableContext;
//	$('aEnable').onclick = EnableContext;
	//	if (document.onmousedown) 
			document.onmousedown = ContextMouseDown;
		/*else
			document.body.onmousedown = ContextMouseDown;*/
        
		//if (document.oncontextmenu)
			document.oncontextmenu = ContextShow;
		/*else if (document.body.oncontextmenu)
			document.body.oncontextmenu = ContextShow;*/
}


InitContext(true);