/*  Route map click event according to selected tool.
    Open up popup windows if appropriate.
    Appends click location as mapimg.x and mapimg.y 
*/

function mapClick (mapImage,e) {

	// get current tool
	var the_tool = document.mapform.tool.value;

	/*
	Normal click & options, or fast return
	*/
	var the_url = eval("document.mapform." + the_tool + '_popup_url');
	var the_opt = eval("document.mapform." + the_tool + '_popup_options');
	if (the_url == undefined || the_opt == undefined){
		return true;
	}

	// Get the click location
	var nX;
	var nY;
	if (window.event) { 
		// IE
		var nX = window.event.offsetX;
		var nY = window.event.offsetY;
	} else if (e.target) { 
		// Mozilla/Firefox
		// Could also use document.getElementById('mapDiv');
		var el = e.target;
		var offsetLeft = 0;
		var offsetTop = 0;
		while (el) {
			offsetLeft += el.offsetLeft;
			offsetTop += el.offsetTop;
			el = el.offsetParent;
		}
		nX = e.pageX - offsetLeft;
		nY = e.pageY - offsetTop;
	}	
	
	/* get the current URL */
	var misrc = mapImage.src;
	var qs = misrc.substring( misrc.indexOf('?'), misrc.length);
	var popup_url = the_url.value + qs + '&mapimg.x=' + nX + '&mapimg.y=' + nY;

	/* Call qs manip function */
	eval(
		"if (window.qs_append_" + the_tool + "){" +
			" popup_url = qs_append_" + the_tool + "(nX,nY);" +
		"}"
	);

	var popup_win = window.open( popup_url, the_tool + 'Results', the_opt.value ); 
	popup_win.focus(); 
	// don't submit the map.
	return false;
}


