//http://luke.breuer.com/tutorial/javascript-context-menu-tutorial.htm

var enableContainer = document.getElementById('content_column');
var enableMenu = document.createElement('div');
enableContainer.appendChild(enableMenu);
enableMenu.innerHTML = "<a id='aEnable' style='display: none;' href='#'>Enable context menus</a>";
 
var contextMenu = 
	"<ul class='cmenu'>" +
	"<li><a id='aGoogle' href='http://www.google.com/'>Google</a></li>" +
	"<li><a id='aWikipedia' href='http://en.wikipedia.org/'>Wikipedia</a></li>" +
	"<li><a id='aSummitPost' href='http://www.summitpost.org/'>SummitPost</a></li>" +
	"<li><a id='aMountainProject' href='http://www.mountainproject.com/'>Mountain Project</a></li>" +	
	"<li><a id='aFlickr' href='http://www.flickr.com/'>Flickr</a></li>" +
	"<li><a id='aPicasa' href='http://picasa.google.com/'>Picasa</a></li>" +
	"<li><a id='agMaps' href='http://maps.google.com/'>Google Maps</a></li>" +
	"<li class='topSep'>" +
	"<a id='aDisable' href='#'>disable this menu</a>" +
	"</li>" +
	"</ul>";

var enableMenu = document.createElement('div');
var enableMenuId = document.createAttribute('id');
enableMenuId.nodeValue = "divContext";
enableMenu.setAttributeNode(enableMenuId);
enableContainer.appendChild(enableMenu);
enableMenu.innerHTML = contextMenu;

var _replaceContext = false;		// replace the system context menu?
var _mouseOverContext = false;		// is the mouse over the context menu?
var _noContext = false;				// disable the context menu?
var _divContext = $('divContext');	// makes my life easier

InitContext();

function InitContext()
{
	_divContext.onmouseover = function() { _mouseOverContext = true; };
	_divContext.onmouseout = function() { _mouseOverContext = false; };

	$('aDisable').onclick = DisableContext;
	$('aEnable').onclick = EnableContext;

	document.body.onmousedown = ContextMouseDown;
	document.body.oncontextmenu = ContextShow;
}

// 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() == 'div')
	//if (event.button == 2 && target.tagName.toLowerCase() == 'div' && target.getAttribute('class') == 'link')
	if (event.button == 2 && target.getAttribute('class') == 'link')
	//if (event.button == 2 && target.getAttribute('class') == 'link'||'vcard')
		_replaceContext = true;
	else if (!_mouseOverContext)
		_divContext.style.display = 'none';
}

function CloseContext()
{
	_mouseOverContext;
	_divContext.style.display = 'none';
}

// 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;

	// 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)
	{
		var term = '';
		var googleSearch = 'http://www.google.com/search?hl=en&q=';
		var wikipediaSearch = 'http://en.wikipedia.org/wiki/';
		var summitPostSearch = 'http://www.summitpost.org/object_list.php?object_type=0&object_name_0=';
		var mountainProjectSearch = 'http://www.mountainproject.com/scripts/Search?query='
		var flickrSearch = 'http://www.flickr.com/search/?q=';
		var picasaSearch = 'http://picasaweb.google.com/lh/view?q=';
		var gMapsSearch = 'http://maps.google.com/maps?q=';

		var menuClass = target.getAttribute('class');
		switch (menuClass)
		{
			case 'link':
				//remove alternative names and height (anything in parenthesis) from search term
				term = target.innerHTML.replace(/\(.*\)/,"");
				term = term.replace(/\./,"");
				term = term.replace(/\s<\/.*>/,"");
				break;
			case 'vcard':
				term = target.innerHTML.replace(/<\/a>.*/,"");
				break;	
			default:
				break;
		}		
		
		//remove alternative names and height (anything in parenthesis) from search term
		//term = target.innerHTML.replace(/\(.*\)/,"");
		
		googleSearch = googleSearch + term;
		wikipediaSearch = wikipediaSearch + term.replace(/\s/,"_");
		summitPostSearch = summitPostSearch + term.replace(/\s/,"+");
		mountainProjectSearch = mountainProjectSearch + term.replace(/\s/,"+");
		flickrSearch = flickrSearch + term.replace(/\s/,"+");
		picasaSearch = picasaSearch + term;
		gMapsSearch = gMapsSearch + term;	
		
		switch (menuClass)
		{
			case 'link':
				$('aGoogle').href = googleSearch;
				$('aWikipedia').href = wikipediaSearch;
				$('aSummitPost').href = summitPostSearch;
				$('aMountainProject').href = mountainProjectSearch;
				$('aFlickr').href = flickrSearch;
				$('aPicasa').href = picasaSearch;
				$('agMaps').href = gMapsSearch;
				break;			
			default:
				break;
		}
		
		//$('aContextNav').href = target.href;
		//$('aAddWebmark').href = 'http://luke.breuer.com/webmark/?addurl=' +
		//	encodeURIComponent(target.href) + '&title=' +
		//	encodeURIComponent(target.innerHTML);

		// 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';

		_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);
}
