/* 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.

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();
				});
			} else {
			//Create the info HTML for the point
				//var infoHTML = '';
				//Removed title from popup info 11/2/2006
				//if (ptName != '') infoHTML += '<div class="gmaptitle">' + ptName + '</div>';
				//infoHTML += '<div class="gmapinfo">' + ptHTML + '</div>';
				//Create the click event for the point
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(ptHTML);
				});
			}
		}
	}
	//Return the point
	return marker;
}
