//  Open up  popup windows when mapimg is clicked. 
//  Depending on the current tool and whether that tool has a popup_url set.
//  Appends mapimg.x and mapimg.y 
//  Depends on globals set by the mappopup component.

function mapPopup (e) {

	// get current tool
	var the_tool = document.mapform.tool.value;
	var window_name = the_tool + 'Results';

	// make sure it's valid
	if(valid_popup_tools.indexOf(the_tool) == -1 ){
		return true;	
	}
	// get the url and popup options
	var the_url = eval(the_tool + '_popup_url');
	var the_opt = eval(the_tool + '_popup_options');

	// if no url set return
	if(the_url == '')
	{
		return true;	
	}

	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
		//var el = 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;
	}	
	
	// append the mapimg click points
	var popup_url = the_url + '&mapimg.x=' + nX + '&mapimg.y=' + nY;
	// Could not get javascript to eval the qs function name
	// So the tests are hard coded for query and queryplot qs functions.
	if(the_tool == 'query' && window.query_popup_qs)
	{
		query_popup_qs(popup_url);
	}

	if(the_tool == 'queryplot' && window.queryplot_popup_qs)
	{
		popup_url = queryplot_popup_qs(popup_url);
	}

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

