﻿function GoogleGeocode(apiKey) {
	this.apiKey = apiKey;
	this.geocode = function(address, callbackFunction) {
		jQuery.ajax({
			dataType: 'jsonp',
			url: 'http://maps.google.com/maps/geo?output=json&oe=utf8&sensor=false&key=' + this.apiKey + '&q=' + address,
			cache: false,
			async: true,
			success: function(data){
				var result = {};
				var bHasValue = false;
				if(data.Status.code==200) {
					if(data.Placemark && data.Placemark.length>0){
						var place = data.Placemark[0];
						if(place.AddressDetails && place.AddressDetails.Country && place.AddressDetails.Country.AdministrativeArea){
							ad = place.AddressDetails.Country.AdministrativeArea;
							if(ad.Locality && ad.Locality.PostalCode && ad.Locality.PostalCode.PostalCodeNumber){
								result.zip = ad.Locality.PostalCode.PostalCodeNumber;
								bHasValue = true;
							}else if(ad.SubAdministrativeArea && ad.SubAdministrativeArea.Locality && ad.SubAdministrativeArea.Locality.PostalCode && ad.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber){
								result.zip = ad.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
								bHasValue = true;
							}
						}
						if(place.Point && place.Point.coordinates && place.Point.coordinates.length >= 2){
							result.longitude = place.Point.coordinates[0];
							result.latitude = place.Point.coordinates[1];
							bHasValue = true;
						}
					}
				}
				if(bHasValue){
					eval(callbackFunction(result));
				} else{
					eval(callbackFunction(null));
				}
			}
		  });
	};
}

var googleMapsApiKey = '';
var geoCoder = null;
var mapIcons = {GarageSmall:'/css/img/map_markers/garage_small.png',GarageLarge:'/css/img/map_markers/garage.png'};

if(window.location.hostname.indexOf('.cms.seek4cars.local',0) > 0){
	googleMapsApiKey = 'ABQIAAAAtfaVcwi1OKzBnhS-kNbMNxSSqGYLURAdm8r590F28dCB1fQ3SxQ1sR31prB08U3VEO5NjYqp68oq6w';//	*.cms.seek4cars.local
}else if(window.location.hostname.indexOf('.cms.seek4cars.net',0) > 0){
	googleMapsApiKey = 'ABQIAAAAtfaVcwi1OKzBnhS-kNbMNxSynuBX8aSShz_O5MbSB7k-EkYLJhQ99Yk52fldHRE4KPXeiGuYbQAoeQ';//	*.cms.seek4cars.net
}
