/* GOOGLEMAP.JS
   Utilities for working with Google Maps.

   Version Information
    20060510a = Created.
    20061211a = Added tooltip to icon images.
    20070321a = Added support for local URLs (don't appear in popup window) as map point clickactions.
    20110225a = Added support for content supplied by AJAX call to createPoint HTML.

Copyright © 2006, Land Information Access Association
*/

//Utility functions
function isArray(a) {
    return isObject(a) && a.constructor == Array;
}
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function isFunction(a) {
    return typeof a == 'function';
}
//Global variables
var ccLocMapWin

function createPoint(ptID, ptLat, ptLon, ptName, ptIcon, ptIconX, ptIconY, ptHTML) {
	//Create an icon object for the icon image
	var picon = new GIcon();
	picon.image = ptIcon;
	picon.iconSize = new GSize(ptIconX, ptIconY);
	var centerX = parseInt(ptIconX / 2.0);
	var centerY = parseInt(ptIconY / 2.0);
	picon.iconAnchor = new GPoint(centerX, centerY);
	picon.infoWindowAnchor = new GPoint(centerX, centerY);
	//Create the coordinates for the point
	var point = new GLatLng(ptLat, ptLon);
	//Create the marker object for the point
	var marker = new GMarker(point, {icon: picon, title: ptName});
	//Create the popup for the point if requested
	if (ptHTML) {
		//If ptHTML is an array, then prepare for tabs
		if (isArray(ptHTML)) {
			//Create the click event for the point
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowTabsHtml(ptHTML);
			});
		} else {
			//Check if a JavaScript is to be run
			if (ptHTML.substr(0, 4) == 'scr|') {
				GEvent.addListener(marker, "click", function() {
					eval(unescape(ptHTML.substr(4)));
				});
			//Check if a URL is provided
			} else if (ptHTML.substr(0, 4) == 'url|') {
				GEvent.addListener(marker, "click", function() {
					ccLocMapWin = window.open(unescape(ptHTML.substr(4)), 'LocMapWin');
					if (ccLocMapWin) ccLocMapWin.focus();
				});
			//Check if a local URL is provided
			} else if (ptHTML.substr(0, 5) == 'lurl|') {
				GEvent.addListener(marker, "click", function() {
					window.location = unescape(ptHTML.substr(5));
					if (ccLocMapWin) ccLocMapWin.focus();
				});
			//Check if an ajax URL is provided
			} else if (ptHTML.substr(0, 5) == 'ajax|') {
				GEvent.addListener(marker, "click", function() {
					//Display wait message
					var winContent = document.createElement('div');
					winContent.innerHTML = '<div class="locpopup"><img src="' + tbSiteRoot + 'cc/images/wait26trans.gif" border="0" width="26" height="26" align="absmiddle" alt="Please Wait" title="Please Wait">Loading...</div>';
					marker.openInfoWindowHtml(winContent);
					//Replace wait message with AJAX content when ready
            		GDownloadUrl(unescape(ptHTML.substr(5)), function(data) {
            			marker.openInfoWindowHtml('<div class="locpopup">' + data + '</div>');
            		});
				});
			} else {
				//Display the text when the point is clicked
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(ptHTML);
				});
			}
		}
	}
	//Return the point
	return marker;
}



