var map;
var marker;
var point;
var latLong;
var origLocationsTxt;

function initMap(gKey) {
	if(!gKey){
		var gKey = 'ABQIAAAAtpYeVDYboZlK5Mh3YYhscxSJxtRXaZXpOmHbjZ2B2jWwrWuakBRhWenMsg2wf0rc8jbEv38aMFNCMA';
	}
	var script = document.createElement("script");
	document.body.appendChild(script);
	script.src = "http://maps.google.com/maps?file=api&v=2&key=" + gKey + "&async=2&callback=loadMap";
}
function load(center,markers) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("gmap"));
		var centerExplode=center.split('|');
		point = new GLatLng(centerExplode[0], centerExplode[1]);
		map.setCenter(point,6);
		marker = new GMarker(point);
		map.addOverlay(marker);
	}
}
function loadMap(center,markers) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("gMap"));
		if(gLocation){
			var centerExplode=gLocation.split('|');
			point = new GLatLng(centerExplode[0], centerExplode[1]);
			map.setCenter(point,10);
			marker = new GMarker(point);
			map.addOverlay(marker);
		}
	}
}
function getLocations() {
	if (GBrowserIsCompatible()) {
		var address = document.getElementById('location').value;
		if(!origLocationsTxt) {
               		origLocationsTxt = window.document.getElementById('gmap_locations').innerHTML;
		}
		else{
               		window.document.getElementById('gmap_locations').innerHTML = origLocationsTxt;
		}
		window.document.getElementById('gmap_locations').style.display= '';
		window.document.getElementById('gmap_locations_failed').style.display= 'none';
		showAddress(address);
	}
}
function showAddress(address) {     
	geocoder = new GClientGeocoder();   
	geocoder.getLocations(address, addAddressToMap);
}   
function addAddressToMap(response) {
	document.getElementById('gmap_locations').innerHTML;
	if (!response || response.Status.code != 200) {   
		document.getElementById('gmap_locations').style.display= 'none';
		document.getElementById('gmap_locations_failed').style.display= '';
	} else {
		if(response.Placemark.length < 2){
			place = response.Placemark[0];
			var point = place.Point.coordinates;
			document.getElementById('location_google').value=place.Point.coordinates[1]+'|'+place.Point.coordinates[0];  
			document.getElementById('gmap_locations').innerHTML=place.address;//+ ' ' + place.Point.coordinates[1] + ' ' + place.Point.coordinates[0];
			map.setCenter(new GLatLng(parseFloat(point[1]),parseFloat(point[0])),10);
			map.addOverlay(new GMarker(new GLatLng(parseFloat(point[1]),parseFloat(point[0]))));
		}
		else{
			html = '';
			for(i=0, len = response.Placemark.length; i< len; i++){
				var place = response.Placemark[i];  
				var point = place.Point.coordinates;
				var coordinates = point[1]+'|'+point[0];
				map.addOverlay(new GMarker(new GLatLng(parseFloat(point[1]),parseFloat(point[0]))));
				address = new Array(place.address);
				var addressAAName = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
				if(addressAAName) address[address.length] = addressAAName;
				var addressAASAName = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
				if(addressAASAName) address[address.length] = addressAASAName;
				var address = address.join(', ');
				html += '<a href="#" onclick="map.setCenter(new GLatLng('+parseFloat(point[1])+','+parseFloat(point[0])+'),10); document.getElementById(\'location_google\').value=\'' + coordinates + '\'; return false;">'+address+'</a><br />';	
			}
			document.getElementById('gmap_locations').innerHTML=html;
		}
	}   
}