// Global variable for saving possible geocoded address 
geocodedAddress = null;
coordinates = new country("worldwide",0,0,1);
    function activateGeocoder() { 
       var address = document.forms['latlonLocatorForm'].stringaddress.value;
       if(address == "") { 
	alert("Please provide an address, select a country from the list \n or drag the marker get in closer");  
       } else { 
       showAddress(address);
       }
    }

function degreeFormatter(structure) { 
  var splitStructure = structure.split("\,");
  var latitudeRaw = splitStructure[0];
  var longitudeRaw = splitStructure[1];  
  // Prepare latitude
  var splitLatitude = latitudeRaw.split("\.");	 
  var latitudeMinutesRaw = parseFloat("0." + splitLatitude[1])*60;
  var splitLatitudeMinutes = latitudeMinutesRaw.toString().split("\.");
  var latitudeDegrees = Math.abs(parseInt(splitLatitude[0])); 
  var latitudeMinutes = splitLatitudeMinutes[0]; 
  var latitudeSeconds = Math.round(parseFloat("0." + splitLatitudeMinutes[1])*60);
  var latitudeOrientation = (parseInt(splitLatitude[0]) > 0) ? "N":"S";
  // Prepare longitude 
  var splitLongitude = longitudeRaw.split("\.");
  var longitudeMinutesRaw = parseFloat("0." + splitLongitude[1])*60;
  var splitLongitudeMinutes = longitudeMinutesRaw.toString().split("\.");
  var longitudeDegrees = Math.abs(parseInt(splitLongitude[0])); 
  var longitudeMinutes = splitLongitudeMinutes[0]; 
  var longitudeSeconds = Math.round(parseFloat("0." + splitLongitudeMinutes[1])*60);
  var longitudeOrientation = (parseInt(splitLongitude[0]) > 0) ? "E":"W";
  var degreeStructure = [latitudeDegrees,latitudeMinutes,latitudeSeconds,latitudeOrientation,longitudeDegrees,longitudeMinutes,longitudeSeconds,longitudeOrientation]
  return degreeStructure;
}


function showAddress(address) {
  var geocoder = new GClientGeocoder();
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert("* " + address + " Not Found\n Suggestions: \n 1) Introduce a format like : Street, City, Country, Zip Code\n 2) Leave off street level details if you keep getting unresolvable addresses.\n 3) Use City, Country addresses and drag the marker & zoom contols \n to determine street level coordinates.");
      } else {
	geocodedAddress = point;
        load();
      }
    }
  );
}

    function country(code,lat,lon,zoom) { 
      this.code = code;
      this.lat = lat; 
      this.lon = lon; 
      this.zoom = zoom;
    }
    function load() { 
// Piggy back of load for text box message 
document.Form2.stringaddress.onblur = function()
{
  var field = document.Form2.stringaddress;
  if (field.value == '') field.value = 'Search for address';
}

document.Form2.stringaddress.onfocus = function()
{
  var field = document.Form2.stringaddress;
   if (field.value == 'Search for address') field.value = '';
}
if (document.Form2.stringaddress.value == '') document.Form2.stringaddress.value = 'Search for address';

     // Start countries
     var ctyus = new country("ctyus",37.50972584293751,-96.943359375,4);
     var ctyuk = new country("ctyuk",53.69670647530323,-1.58203125,5);
     var ctycanada = new country("ctycanada",54.7246201949245,-101.953125,4);
     var ctyaustria = new country("ctyaustria",47.234489635299184,13.64501953125,7);
     var ctyaustralia = new country("ctyaustralia",-29.075375179558346,137.28515625,4);
     var ctybelgium = new country("ctybelgium",50.80593472676908,4.405517578125,7);
     var ctybrazil = new country("ctybrazil",-11.092165893502,-51.50390625,4);
     var ctychina = new country("ctychina",30.977609093348686,105.1171875,4);
     var ctydenmark = new country("ctydenmark",55.52863052257191,11.689453125,6);
     var ctyfinland = new country("ctyfinland",64.47279382008166,26.54296875,4);
     var ctyfrance = new country("ctyfrance",46.92025531537451,2.5927734375,5);
     var ctygermany = new country("ctygermany",51.86292391360244,10.2392578125,5);
     var ctyhongkong = new country("ctyhongkong",22.39071391683855,114.19189453125,8);
     var ctyitaly = new country("ctyitaly",42.74701217318067,13.359375,5);
     var ctyjapan = new country("ctyjapan",36.52729481454624,137.98828125,4);
     var ctynetherlands = new country("ctynetherlands",52.26815737376817,5.3173828125,6);
     var ctynorway = new country("ctynorway",61.01572481397616,8.525390625,5);
     var ctyportugal = new country("ctyportugal",39.50404070558415,-8.1298828125,6);
     var ctysingapore = new country("ctysingapore",1.4939713066293239,103.4912109375,5);
     var ctysouthkorea = new country("ctysouthkorea",36.59788913307022,127.96875,5);
     var ctyspain = new country("ctyspain",40.38002840251183,-3.8232421875,5);
     var ctysweden = new country("ctysweden",60.326947742998414,15.29296875,4);
     var ctyswitzerland = new country("ctyswitzerland",46.98025235521883,8.0419921875,6);
     var ctytaiwan = new country("ctytaiwan",23.845649887659352,120.948486328125,7);
     var worldwide = new country("worldwide",0,0,1);
     var box = document.forms['latlonLocatorForm'].country;
     var valueformat = document.forms['latlonLocatorForm'].valueformat.value;
     var targetpoint = eval(box.options[box.selectedIndex].value);
     if(geocodedAddress == null) { 
       if(valueformat == "degrees") { 
	 var introStructure = "(" + targetpoint.lat + "," + targetpoint.lon + ")"
         var structure = "" + introStructure.toString().replace(/ /g,"").replace(/\(/g,"").replace(/\)/g,"");
	 var degreeStructure = degreeFormatter(structure);
         document.getElementById("latitudelongitude").innerHTML = "(" + degreeStructure[0] +"<sup>°</sup>" + degreeStructure[1] +"<sup>'</sup> "+ degreeStructure[2] +"<sup>''</sup> "+  degreeStructure[3] +" , " + degreeStructure[4] +"<sup>°</sup>" + degreeStructure[5] +"<sup>'</sup> "+ degreeStructure[6] +"<sup>''</sup> "+  degreeStructure[7] +") <br/> [ " + document.forms["latlonLocatorForm"].stringaddress.value + " ]";
       } else { 
         document.getElementById("latitudelongitude").innerHTML = "(" + targetpoint.lat + "," + targetpoint.lon + ")";
       }
     } else { 
       if(valueformat == "degrees") { 
         var structure = "" + geocodedAddress.toString().replace(/ /g,"").replace(/\(/g,"").replace(/\)/g,"");
	 var degreeStructure = degreeFormatter(structure);
         document.getElementById("latitudelongitude").innerHTML = "(" + degreeStructure[0] +"<sup>°</sup>" + degreeStructure[1] +"<sup>'</sup> "+ degreeStructure[2] +"<sup>''</sup> "+  degreeStructure[3] +" , " + degreeStructure[4] +"<sup>°</sup>" + degreeStructure[5] +"<sup>'</sup> "+ degreeStructure[6] +"<sup>''</sup> "+  degreeStructure[7] +") <br/> [ " + document.forms["latlonLocatorForm"].stringaddress.value + " ]";
       } else {
         document.getElementById("latitudelongitude").innerHTML = "" + geocodedAddress.toString().replace(/ /g,"") + "<br/> [ " + document.forms["latlonLocatorForm"].stringaddress.value + " ]";
       }
     }
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("latlocator"));
	var center = new GLatLng(0,0);
	if(geocodedAddress == null) { 
        center  = new GLatLng(targetpoint.lat,targetpoint.lon);
        map.setCenter(center,targetpoint.zoom);
        } else { 
        center = geocodedAddress;
        map.setCenter(center,10);
	}
        map.addControl(new GMapTypeControl());

        var marker = new GMarker(center, {draggable: true});


        GEvent.addListener(marker, "dragstart", function() {
          map.closeInfoWindow();
        });

        GEvent.addListener(marker, "dragend", function() {
         coordinates = marker.getPoint();
           if(valueformat == "degrees") { 
             var structure = coordinates.toString().replace(/ /g,"").replace(/\(/g,"").replace(/\)/g,"");
             var degreeStructure = degreeFormatter(structure);
             marker.openInfoWindowHtml("(" + degreeStructure[0] +"<sup>°</sup>" + degreeStructure[1] +"<sup>'</sup> "+ degreeStructure[2] +"<sup>''</sup> "+  degreeStructure[3] +" , " + degreeStructure[4] +"<sup>°</sup>" + degreeStructure[5] +"<sup>'</sup> "+ degreeStructure[6] +"<sup>''</sup> "+  degreeStructure[7] +")");
	     document.getElementById("latitudelongitude").innerHTML = "(" + degreeStructure[0] +"<sup>°</sup>" + degreeStructure[1] +"<sup>'</sup> "+ degreeStructure[2] +"<sup>''</sup> "+  degreeStructure[3] +" , " + degreeStructure[4] +"<sup>°</sup>" + degreeStructure[5] +"<sup>'</sup> "+ degreeStructure[6] +"<sup>''</sup> "+  degreeStructure[7] +")";
            } else {
             marker.openInfoWindowHtml(coordinates.toString().replace(/ /g,""));
             document.getElementById("latitudelongitude").innerHTML = coordinates.toString().replace(/ /g,"");
            }
          var currentZ = map.getZoom();
          if(currentZ <= 4) { 
          map.setCenter(coordinates,4);
          }
         });

        GEvent.addListener(marker, "click", function() {
          coordinates = marker.getPoint();
         if(valueformat == "degrees") { 
             var structure = coordinates.toString().replace(/ /g,"").replace(/\(/g,"").replace(/\)/g,"");
             var degreeStructure = degreeFormatter(structure);
             marker.openInfoWindowHtml("(" + degreeStructure[0] +"<sup>°</sup>" + degreeStructure[1] +"<sup>'</sup> "+ degreeStructure[2] +"<sup>''</sup> "+  degreeStructure[3] +" , " + degreeStructure[4] +"<sup>°</sup>" + degreeStructure[5] +"<sup>'</sup> "+ degreeStructure[6] +"<sup>''</sup> "+  degreeStructure[7] +")");
            } else {
              marker.openInfoWindowHtml(coordinates.toString().replace(/ /g,""));
            }
         });

        map.addOverlay(marker);
        map.enableDoubleClickZoom();
        map.enableContinuousZoom();
        map.setMapType(map.getMapTypes()[0]);
        map.addControl(new GLargeMapControl());
        map.addControl(new GOverviewMapControl());
        // Place latitude/longitude and description address in window 
	if(geocodedAddress == null) { 
        coordinates = marker.getPoint();
         if(valueformat == "degrees") { 
             var structure = coordinates.toString().replace(/ /g,"").replace(/\(/g,"").replace(/\)/g,"");
             var degreeStructure = degreeFormatter(structure);
             marker.openInfoWindowHtml("(" + degreeStructure[0] +"<sup>°</sup>" + degreeStructure[1] +"<sup>'</sup> "+ degreeStructure[2] +"<sup>''</sup> "+  degreeStructure[3] +" , " + degreeStructure[4] +"<sup>°</sup>" + degreeStructure[5] +"<sup>'</sup> "+ degreeStructure[6] +"<sup>''</sup> "+  degreeStructure[7] +") <br/> [ " + box.options[box.selectedIndex].innerHTML + "]");
            } else {
              marker.openInfoWindowHtml(coordinates.toString().replace(/ /g,"") + "<br/> [ " + box.options[box.selectedIndex].innerHTML + "]");
            }
	} else { 
        coordinates = marker.getPoint();
         if(valueformat == "degrees") { 
             var structure = coordinates.toString().replace(/ /g,"").replace(/\(/g,"").replace(/\)/g,"");
             var degreeStructure = degreeFormatter(structure);
             marker.openInfoWindowHtml("(" + degreeStructure[0] +"<sup>°</sup>" + degreeStructure[1] +"<sup>'</sup> "+ degreeStructure[2] +"<sup>''</sup> "+  degreeStructure[3] +" , " + degreeStructure[4] +"<sup>°</sup>" + degreeStructure[5] +"<sup>'</sup> "+ degreeStructure[6] +"<sup>''</sup> "+  degreeStructure[7] +") <br/> [ " + document.forms["latlonLocatorForm"].stringaddress.value + "]");
            } else {
             marker.openInfoWindowHtml(coordinates.toString().replace(/ /g,"") + "<br/> [ "  + document.forms["latlonLocatorForm"].stringaddress.value + "]");
	    }
	}
      }
      // Reset geocoded address for new calls.
      geocodedAddress = null;
     }
     addLoadEvent(load);
     addUnloadEvent(GUnload);


