var myZoom;
var map;
var geocoder;
if(mapInit) {
	$(document).bind('ready.map', mapInit);
}

if(mapInit) {
	$(document).bind('ready.address', addressInit);
}


if(GUnload) {
	$(document).bind('unload.map', GUnload);
}

function mapByAddress(clearAllFields){
	findAddress(clearAllFields);
	//mapInit("true", 3);
}
function addressInit() {
	//alert("addressInit");
	//$('#country').bind('change',findAddress);
	//$('#state_region_county').bind('change',findAddress);
	//$('#post_code').bind('change',findAddress);
	//$('#town_city').bind('change',findAddress);
	//$('#address1').bind('change',findAddress);
	//$('#address2').bind('change',findAddress);
}


function findAddress(clearAllFields) {
	if(!geocoder) {
		geocoder = new GClientGeocoder();
	}
	var address = new Array();
	
	var country = '';
	var selectTag = document.getElementsByTagName("select");
	for (s=0;s<selectTag.length;s++)
	{
		if(selectTag[s].name == "image[countryId]"){
			country = selectTag[s].options[selectTag[s].options.selectedIndex].text;
		}
	}
	var inputTag = document.getElementsByTagName("input");
	if(clearAllFields=="trueWithoutState" || clearAllFields=="trueWithState")
	{
		for (s=0;s<inputTag.length;s++)
		{
			if(clearAllFields=="trueWithState")
			{
				if(inputTag[s].name == "image[county]"){
					inputTag[s].value = "";
				}
			}
			if(inputTag[s].name == "image[postcode]"){
				inputTag[s].value = "";
			}
			if(inputTag[s].name == "image[city]"){
				inputTag[s].value = "";
			}
			if(inputTag[s].name == "image[address1]"){
				inputTag[s].value = "";
			}
			if(inputTag[s].name == "image[address2]"){
				inputTag[s].value = "";
			}
		}	
	}
	for (s=0;s<inputTag.length;s++)
	{
		if(inputTag[s].name == "image[county]"){
			state = inputTag[s].value;
		}
		else if(inputTag[s].name == "image[postcode]"){
			postcode = inputTag[s].value;
		}
		else if(inputTag[s].name == "image[city]"){
			city = inputTag[s].value;
		}
		else if(inputTag[s].name == "image[address1]"){
			address1 = inputTag[s].value;
		}
		else if(inputTag[s].name == "image[address2]"){
			address2 = inputTag[s].value;
		}
	}
	if(country != '' || country != ' -- please select -- ') 
	{
		myZoom = 2;
		if(country != '') {
			address[address.length] = country;
		}
		if(state != '')
		{
			myZoom = 6;
			address[address.length] = state;
		}
		if(postcode != '') {
			myZoom = 14;
			address[address.length] = postcode;	//(postcode.replace(/ /, " "));
		}
		if(address1 != '') 
		{
			myZoom = 14;
			if(address2=="")
			{
				address1 += ", " + address2;
			}
			address[address.length] = address1 + " " + address2;
		}
		address = address.join(', ');
		geocoder.getLocations(address, addAddressToMap);
	}
}

function addAddressToMap(response) {
	if (response && response.Status.code == 200) {
		map.clearOverlays();
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1],	place.Point.coordinates[0]);
        marker = new GMarker(point);
		//alert(myZoom);
		map.setCenter(point, myZoom);
        map.addOverlay(marker);
		$('#addressLatitude').attr('value',place.Point.coordinates[1]);
		$('#addressLongitude').attr('value',place.Point.coordinates[0]);
		
		var inputTag = document.getElementsByTagName("input");
		for (s=0;s<inputTag.length;s++)
		{
			if(inputTag[s].name == "image[latitude]"){
				inputTag[s].value = place.Point.coordinates[1];
			}
			if(inputTag[s].name == "image[longitude]"){
				inputTag[s].value = place.Point.coordinates[0];
			}
			
		}	
	
	}
}
var latlng;
function getAddressByLatitudeAndLongitude(type){
	if(type=='lat'){
		document.getElementById('lat').value = document.getElementById('latitude').value;	
	}else if(type=='lng'){
		document.getElementById('lng').value = document.getElementById('longitude').value;	
	}
	latitude = document.getElementById('latitude').value;
	lat = document.getElementById('lat').value;
	longitude = document.getElementById('longitude').value;
	lng = document.getElementById('lng').value;

	if(latitude == lat && longitude == lng){
		if (GBrowserIsCompatible()) {
			latlng = new GLatLng(latitude, longitude);
			geocoder = new GClientGeocoder();
			geocoder.getLocations(latlng, getAddressByLatitudeAndLongitudeResult);
		}
		document.getElementById('lat').value = '';	
		document.getElementById('lng').value = '';	
	}
}
var latLngPostcode = '';
var latLngTown = '';
var latLngAddress = '';
var latLngCountry = '';
function iterate(obj){
	for(itm in obj){
		if(typeof(obj[itm])=='object'){
			iterate(obj[itm]);
		}else{
			if(itm=='PostalCodeNumber'){
				latLngPostcode = obj[itm];
			}else if(itm=='address'){
				latLngAddress = obj[itm];
			}else if(itm=='CountryName'){
				latLngCountry = obj[itm];
			}else if(itm=='LocalityName'){
				latLngTown = obj[itm];
			}
		}
	}
}
function getAddressByLatitudeAndLongitudeResult(response){
	if (response && response.Status.code == 200) {
		//map = new GMap2(document.getElementById("map"));
		map.setCenter(latlng, 13);
/*		map.addControl(new GSmallZoomControl());
*/		map.clearOverlays();
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
		marker = new GMarker(point);
		map.addOverlay(marker);
		marker.openInfoWindowHtml( place.address );
		iterate(place);

		var inputTag = document.getElementsByTagName("input");
		for (s=0;s<inputTag.length;s++)
		{
			if(inputTag[s].name == "image[county]"){
				inputTag[s].value = latLngTown;
			}/*else if(inputTag[s].name == "image[countryId]"){
			//inputTag[s].value = latLngCountry;
			}*/else if(inputTag[s].name == "image[postcode]"){
				inputTag[s].value = latLngPostcode;
			}else if(inputTag[s].name == "image[address1]"){
				inputTag[s].value = latLngAddress;
			}
		}
		
		countryIds = document.getElementById("countryId");
		countries = '';
		for(i = 0; i < countryIds.length; i++){
			if(countryIds.options[i].text == latLngCountry){
				countryIds.selectedIndex = i;
			}
		}
	}else{
		alert('Longitude & Latitude are not right. Status code '+response.Status.code);
	}
}

