/*------------------------------------------------------------------------------*/
function codeAddress(address,okCB,koCB) 
{
	geocoder = new google.maps.Geocoder();
	geocoder.geocode( { 'address': address,language:'json'}, function(results, status) 
	{
    	if (status == google.maps.GeocoderStatus.OK) 
		{
			if(okCB != undefined)
				okCB(results[0].geometry.location.lat(),results[0].geometry.location.lng());
	     }
    	else 
		{
			if(koCB != undefined)
				koCB(status);
		}
	});
}

/*------------------------------------------------------------------------------*/
function codeAddressAllResults(address,okCB,koCB) 
{
	geocoder = new google.maps.Geocoder();
	geocoder.geocode({'address': address,language:'xml'}, function(results, status) 
	{
    	if (status == google.maps.GeocoderStatus.OK) 
		{
			if(okCB != undefined)
				okCB(results);
	     }
    	else 
		{
			if(koCB != undefined)
				koCB(status);
		}
	});
}

/*------------------------------------------------------------------------------*/
function getInfosFromGeocodeResult(result)
{
	lat = result.geometry.location.lat();
	lng = result.geometry.location.lng();
	locality = undefined;
	country = undefined;
	postcode = "";
	addressComponents = result.address_components;
	for(j=0; j < addressComponents.length; j++)
	{
		addressComponent = addressComponents[j];
		var tmp = new String(addressComponent.types);
		types = tmp.split(",");
		for(k=0; k < types.length; k++)	
		{
			type = types[k];
			if(type == "locality")
			{
				locality = addressComponent.long_name;
			}
			if(type == "country")
			{
				country = addressComponent.long_name;
			}
			if(type == "postal_code")
			{
				postcode = addressComponent.long_name;
			}
		}
	}
	if(country != undefined && locality != undefined)
		return {"lat":lat,"lng":lng,"locality":locality,"country":country,"postcode":postcode};
	else
		return undefined;
}

/*------------------------------------------------------------------------------*/
function centerAddress(map,lat,lng,zoom) 
{
	var myLatlng = new google.maps.LatLng(lat,lng);
	map.setCenter(myLatlng);
	map.setZoom(zoom);
}

/*------------------------------------------------------------------------------*/
function codeCenterAddress(map,address,zoom) 
{
	geocoder = new google.maps.Geocoder();
	geocoder.geocode( { address: address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK && results.length) {
			if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
				map.set_center(results[0].geometry.location);
				map.set_zoom(zoom);
			}
		} 
		else {
			alert("Geocode was unsuccessful due to: " + status);
		}
	});
}

/*------------------------------------------------------------------------------*/
function makeInfoWindow(map,lat,lng,content,icon,zoom)
{
	var myLatLng = new google.maps.LatLng(lat,lng);
	var theMarker = new google.maps.Marker({
	      position: myLatLng,
	      map: map,
	      icon: icon
	  });
	
	 var theInfowindow = new google.maps.InfoWindow({
	        content: content
	    });

	google.maps.event.addListener(theMarker, 'click', function() {
		 theInfowindow.open(map,theMarker);
		 centerAddress(map,lat,lng,zoom);
	});
}
