// JavaScript Document

//Midland, Midland, Michigan, United States
//Midland City, Dale, Alabama, United States
//Pinckard, AL
var centerLatitiude = 41.387900;
var centerLongitude = 2.171500;
var startZoom = 12;
var map;
var description = "<div class='infowindow'>Barcelona, Spain</div>";
var geocoder;

var descArray = new Array();
var infoLat = new Array();
var infoLong = new Array();
var counterArr = 0;

		for(var t=0;t<5;t++)
		{
			infoLat[t] = centerLatitiude;
			infoLong[t] = centerLongitude;
			descArray[t] = description;
			description = "<div class='infowindow'>Description #"+ t +"<br></div>";
			centerLatitiude = centerLatitiude+0.010100
			centerLongitude = centerLongitude+0.020100
			
		}

function addMarker(latitude,longitude,description)
{
	var iconImage = 'images/mapIcon.gif';
	if(iconImage!='')
	{
		var icon = new GIcon();
		icon.image = iconImage;
		icon.iconSize = new GSize(25,35);
		icon.iconAnchor = new GPoint(16,36);
		icon.infoWindowAnchor = new GPoint(15,14);
	
		var marker = new GMarker(new GLatLng(latitude,longitude),icon);
	}
	else
	{
		var marker = new GMarker(new GLatLng(latitude,longitude));
	}
	
	GEvent.addListener(marker, "mouseover",  function()
									   {
											marker.openInfoWindowHtml(description);   
									   }
					   );


	GEvent.addListener(marker, 'click',function()
									   {
											marker.openInfoWindowHtml(description);   
									   }
					   );
	map.addOverlay(marker);
	
}
function init()
 {
	if(GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("mapDiv"));
		var location = new GLatLng(centerLatitiude,centerLongitude);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(location,startZoom);
		geocoder = new GClientGeocoder();

		for(var h=0;h<descArray.length;h++)
		{
		//	alert(infoLat[h]);
		//	addMarker(infoLat[h],infoLong[h],descArray[h]);
		}
		showLocation('Midland, Midland, Michigan, United States');
		showLocation('Midland City, Dale, Alabama, United States');
		showLocation('Pinckard, AL');
	}
 }
 
	function addAddressToMap(response) {
     // map.clearOverlays();
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to locate that address.");
      } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);

		
		//alert(point);
		var iconImage = 'images/mapIcon.gif';
		if(iconImage!='')
		{
			var icon1 = new GIcon();
			icon1.image = iconImage;
			icon1.iconSize = new GSize(25,35);
			icon1.iconAnchor = new GPoint(16,36);
			icon1.infoWindowAnchor = new GPoint(15,14);
	
			var marker1 = new GMarker(point,icon1);
		}
		else
		{
			var marker1 = new GMarker(point);
		}

		//var desc = '<div class="infowindow">' + place.address + '<br><i><u>Country code:</u></i> ' + place.AddressDetails.Country.CountryNameCode + '<div';
		var desc = descArray[counterArr];
		
		GEvent.addListener(marker1, "mouseover",  function()
									   {
											marker1.openInfoWindowHtml(desc);   
									   }
					   );

		GEvent.addListener(marker1, 'click',function()
									   {
											marker1.openInfoWindowHtml(desc);   
									   }
					   );
	   map.addOverlay(marker1);
      // marker1.openInfoWindowHtml('<div class="infowindow">' + place.address + '<br><i><u>Country code:</u></i> ' + place.AddressDetails.Country.CountryNameCode + '<div');
       marker1.openInfoWindowHtml(desc);
      
	 	counterArr = counterArr+1;
	  }
    }
	
    function showLocation(address) {
     // var address = document.formAddresses.txtAddress.value;
      geocoder.getLocations(address, addAddressToMap);
    }

    function findLocation(address) {
      document.formAddresses.txtAddress.value = address;
	  showLocation();
    } 

 
 window.onload = init;
 window.onunload = GUnload;
