var map;
var markers;
var locsAry;
var minZoom = 7;


function init()
{
	map = new GMap2(document.getElementById("map_canvas"));
	map.setCenter(new GLatLng( 33.8280,-84.2200), 9); 
	map.setMapType(G_NORMAL_MAP);
	map.addControl( new GSmallZoomControl3D());
	G_NORMAL_MAP.getMinimumResolution = function() { return minZoom };
	document.addressForm.address.focus()
	
	
	GEvent.addListener(map, "moveend", handleMove);
	processLocations(null);
}

function handleMove ()
{
	if ( locsAry )
	{
		var point;
		var info;

		for ( i=0; i<locsAry.length; i++ )
		{
			point = new GLatLng( locsAry[i].getAttribute("lat"), locsAry[i].getAttribute("lng") );
			info = document.getElementById( "location" + locsAry[i].getAttribute("id") );
			info.style.display = map.getBounds().containsLatLng(point) ? "block" : "none";
		}
	}
}

function processLocations ( center )
{
	GDownloadUrl("locations.xml", function(doc)
	{
		var xmlDoc = GXml.parse(doc);
		var locations = xmlDoc.getElementsByTagName("location");
		markers = new Array();

		var point;

		locsAry = new Array(locations.length);
		
		for ( i=0; i<locations.length; i++ )
		{
			locsAry[i] = locations[i];

			point = new GLatLng( locsAry[i].getAttribute("lat"), locsAry[i].getAttribute("lng") );
			if ( center != null ) locsAry[i].setAttribute( "distance", getDistance( point, center) );
		}
		locsAry.sort(sortfunction);

		writeLocations();
	});	
}

function writeLocations()
{
	var locs = document.getElementById("location");
	var letter;

	for ( i=0; i<locsAry.length; i++ )
	{
		letter = String.fromCharCode("A".charCodeAt(0) + i);

		writeLocationHTML(locs, locsAry[i].getAttribute("id"), locsAry[i].getAttribute("name"), locsAry[i].getAttribute("distance"), letter, locsAry[i].getAttribute("address"), locsAry[i].getAttribute("city"), locsAry[i].getAttribute("phone"), locsAry[i].getAttribute("url"), locsAry[i].getAttribute("lat"), locsAry[i].getAttribute("lng"));


	}
	handleMove();
}

function writeLocationHTML ( locs, id, name, distance, letter, address, city, phone, url, lat, lng )
{

	var point;
	var marker;

		locs.innerHTML += "<div class=\"data\" id=\"location" + id + "\">" +
						  "<h3>" + name + " <br/> " + (distance!=null ? ("<span class=\"distance\">Distance: " + parseFloat(distance).toFixed(2) + " miles</span><br/>\n") : "") + " </h3>\n" +
						  "<div class=\"pin\"><img src=\"http://www.google.com/mapfiles/marker" + letter + ".png\" width=\"20\" height=\"34\"/></div>\n" +        

						  " <p>" + address + "<br/>\n" +
						  city + " <br/><strong>CALL\n" +
						  phone + "</strong><br/>\n" +
						  "   <a href=\"" + url + "\" target=\"_blank\">Visit Our Site!</a> </p>"
						  "   <div class=\"clear\"></div>";

		point = new GLatLng( lat, lng );
		marker = createMarker( point, letter, name, (distance!=null ? ("<span class=\"distance\">Distance: " + parseFloat(distance).toFixed(2) + " miles</span><br/>\n") : ""), phone, url );
		map.addOverlay(marker);
}


function clickMarker( letter )
{
	GEvent.trigger(markers[letter], "click");
}

function createMarker ( point, letter, name, distance, phone, url )
{
	var letteredIcon = new GIcon(G_DEFAULT_ICON);
	letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
	markerOptions = { icon:letteredIcon };

	var marker = new GMarker(point, markerOptions);
	marker.name= name;
	GEvent.addListener(marker, "click", function()
	{
		marker.openInfoWindowHtml("<div class=\"map\" style=\"width:240px;font-family:arial;font-size:12px;\"><h3><span>Two Men And A Truck</span><br/>"+ name +"</h3>\n" + distance + "\nCall: " + phone + "<br/>\n<a href=\"" + url + "\" target=\"_blank\">Visit Our Site</a></div>");
	});
	// save the info we need to use later for the side_bar
	markers[letter] = marker;

	return marker;
}

function addressButtonClick()
{
	var address = document.addressForm.address.value;
	var geocoder = new GClientGeocoder();
	locsAry = null;
	document.getElementById("location").innerHTML = "";


	map.clearOverlays();

	geocoder.getLatLng( address, function(point)
	{
		if (!point)
		{
			alert(address + " not found");
		}
		else
		{
			var blueIcon = new GIcon(G_DEFAULT_ICON);
			blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/icons/blue-dot.png";
			blueIcon.iconSize = new GSize(32, 32);
			var markerOptions = { icon:blueIcon }; 
			var marker = new GMarker( point, markerOptions );
			map.setCenter(point, 8);
			map.addOverlay(marker);



			GDownloadUrl("locations.xml", function(doc)
			{
				var xmlDoc = GXml.parse(doc);
				var locations = xmlDoc.getElementsByTagName("location");
				var found = false;
				
				for ( i=0; i<locations.length; i++ )
				{
					var zips = locations[i].getElementsByTagName("zip");
					
					for ( j=0; j<zips.length; j++ )
					{
						if ( address == zips[j].getAttribute("code") )
						{
							writeLocationHTML(document.getElementById("location"), locations[i].getAttribute("id"), locations[i].getAttribute("name"), getDistance( new GLatLng(locations[i].getAttribute("lat"), locations[i].getAttribute("lng")), point), "A", locations[i].getAttribute("address"), locations[i].getAttribute("city"), locations[i].getAttribute("phone"), locations[i].getAttribute("url"), locations[i].getAttribute("lat"), locations[i].getAttribute("lng"));						  
							found = true;
						}
					}
				}

				if ( !found )
					processLocations( point );


			});



		}
	});
}

function sortfunction(a, b)
{
	return (a.getAttribute("distance") - b.getAttribute("distance")) //causes an array to be sorted numerically and ascending
}
