/* ****************************************************************************

	CJ GMaps Javascript framework v1.0

	Copyright (c) 2008, Doug Jones. All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:
	
	a) Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	  
	b) Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution. 
	  
	c) Neither the name of the Creative Juices, Bo. Co. nor the names of its
	   contributors may be used to endorse or promote products derived from
	   this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
	A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
	OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
	LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
	OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	
	For more information please visit www.cjboco.com.

**************************************************************************** */

var CJ_GMap = {
	
	version: '1.0',
	
	settings: {
		id: null,
		map: null,
		gdir: null,
		geocoder: null,
		imageDir: null,
		icon: null,
		baseIcon: null,
		markers: []
	},
	handleDriveErrors: function () {
		if (CJ_GMap.settings.gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
			alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\n\nError code: " + CJ_GMap.settings.gdir.getStatus().code);
		} else if (CJ_GMap.settings.gdir.getStatus().code == G_GEO_SERVER_ERROR) {
			alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n\nError code: " + CJ_GMap.settings.gdir.getStatus().code);
		} else if (CJ_GMap.settings.gdir.getStatus().code == G_GEO_MISSING_QUERY) {
			alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n\nError code: " + CJ_GMap.settings.gdir.getStatus().code);
		} else if (CJ_GMap.settings.gdir.getStatus().code == G_GEO_BAD_KEY) {
			alert("The given key is either invalid or does not match the domain for which it was given.\n\nError code: " + CJ_GMap.settings.gdir.getStatus().code);
		} else if (CJ_GMap.settings.gdir.getStatus().code == G_GEO_BAD_REQUEST) {
			alert("A directions request could not be successfully parsed.\n\nError code: " + CJ_GMap.settings.gdir.getStatus().code);
		} else {
			alert("An unknown error occurred.");
		}
	},
	onGDirectionsLoad: function () { 
	  // Use this function to access information about the latest load() results.
	  // document.getElementById("getStatus").innerHTML = CJ_GMap.settings.gdir.getStatus().code;
	},
	setDirections: function () {
		var df = document.DrivingDirForm;
		if (df.fromAddress.value !== "" && df.toAddress.value !== "" && df.locale.selectedIndex >= 0) {
			CJ_GMap.settings.gdir.load("from: " + df.fromAddress.value + " to: " + df.toAddress.value, {
				"locale": df.locale.options[df.locale.selectedIndex].value
			});
		} else {
			alert ("You must provide valid address information in order to receive driving directions.\n\nPlease try again.");
		}
	},
	getAddressPoint: function (address) {
		if (CJ_GMap.settings.geocoder) {
			CJ_GMap.settings.geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					return("undefined");
				} else {
					// Doesn't work, this is delayed on purpose by google.
					// return(point);
				}
			});
		}
	},
	addMarker: function (info,point,idx,icn) {
		var icon = new GIcon(CJ_GMap.settings.baseIcon);
		if (icn) {
			var icnSrc = CJ_GMap.settings.icon.split('.');
			icon.image = CJ_GMap.settings.imageDir + icnSrc[0] + "_" + icn + "." + icnSrc[1];
		} else {
			icon.image = CJ_GMap.settings.imageDir + CJ_GMap.settings.icon;
		}
		var latlng = new GLatLng(point.y,point.x,true);
		var marker = new GMarker(latlng,icon);
		GEvent.addListener(marker, "click",
		function() {
			marker.openInfoWindowHtml(info);
			// custom function
			if (typeof marker.__addr_str !== "undefined" && document.DrivingDirForm) {
				document.DrivingDirForm.toAddress.value = marker.__addr_str;
			}
		});
		CJ_GMap.settings.markers['cj_marker'+idx] = marker;
		return marker;
	},
	init: function(settings) {
		if (!document.getElementById || !document.createElement || !GBrowserIsCompatible || !settings || !settings.id) {
			return;
		}
		CJ_GMap.settings.id = settings.id;
		
		// create the map
		CJ_GMap.settings.map = new GMap2(document.getElementById(CJ_GMap.settings.id));
		CJ_GMap.settings.geocoder = new GClientGeocoder();
		CJ_GMap.settings.imageDir = settings.imageDir;
		CJ_GMap.settings.icon = settings.icon.src;
		
		// Create a base icon for all of our markers that specifies the shadow, icon dimensions, etc.
		
		CJ_GMap.settings.baseIcon = new GIcon();
		CJ_GMap.settings.baseIcon.iconSize = new GSize(settings.icon.width, settings.icon.height);
		CJ_GMap.settings.baseIcon.iconAnchor = new GPoint(settings.icon.anchor_x, settings.icon.anchor_y);
		CJ_GMap.settings.baseIcon.shadow = settings.imageDir + settings.shadow.src;
		CJ_GMap.settings.baseIcon.shadowSize = new GSize(settings.shadow.width, settings.shadow.height);
		CJ_GMap.settings.baseIcon.infoWindowAnchor = new GPoint(settings.infoWin.anchor_x, settings.infoWin.anchor_y);
	}
};