var map = null;
var geocoder = null;

kDefaultZoom = 9;

url = document.URL;
if (url.search(/alsoft.com/i) != -1)
	{
	document.writeln('<script src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=ABQIAAAAC-ejH___SU8iBTAB_hjAiRRxXNj-uW52JWkeeJzvm5h3TTI79BTJfRY2OGExF7RRJb4HsAXCBTldrw" type="text/javascript"></script>');
	}
else if (url.search(/alsoftinc.com/i) != -1)
	{
	document.writeln('<script src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=ABQIAAAAC-ejH___SU8iBTAB_hjAiRRWNyoYPTybRB_6gGGFTmAm5t7WIRSCUhyK685-WXretbo8qoMNsL0_sA" type="text/javascript"></script>');
	}
else
	{
	document.writeln('<script src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=ABQIAAAAC-ejH___SU8iBTAB_hjAiRSTFJjv_MtYG7AAhHDFuEVrzBglCRRQBUcazXTdzXzZ-UrIAXrQ8sXZmw" type="text/javascript"></script>');
	}

function InitializeMap()
	{
	if (GBrowserIsCompatible())
		{
		map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
		geocoder = new GClientGeocoder();
		if (geocoder)
			FindStoresNearDefaultLocation();
		}
	} // InitializeMap


function FindLocations(address)
	{
	if (geocoder)
		{
		if ( (address == null) || (address.length == 0) )
			{
			alert("Please enter a City and State or Zip/Postal code.");
			}
		else
			{
			geocoder.getLatLng(
				address,
				function(point)
					{
					if (!point)
						{
						alert("We\'re sorry. " + address + " was not found (Google Maps may be unavailable).\n\nPlease enter a City and State or Zip/Postal code.");
						}
					else
						{
						FindStoresNearPoint(point, address);
						}
					}
				);
			}
		}
	} // FindLocations


function FindStoresNearDefaultLocation()
	{
	var searchURL = '../cgi-bin/StoreLocator.pl';

	GDownloadUrl(searchURL, function(data)
		{
		var xml = GXml.parse(data);

		var defaultLocation = xml.documentElement.getElementsByTagName("default_location")[0];
		var point = new GLatLng(defaultLocation.getAttribute("lat"), defaultLocation.getAttribute("lng"));
		var defaultAddress = defaultLocation.getAttribute("address");
		map.setCenter(point);
		FindStoresNearPoint(point, defaultAddress);
		});
	} // FindStoresNearDefaultLocation


function FindStoresNearPoint(center, addressAtPoint)
	{
	var distanceFromCenter = document.getElementById('distanceSelect').value;
	var searchURL = '../cgi-bin/StoreLocator.pl?address=' + encodeURIComponent(addressAtPoint) + '&lat=' + center.lat() + '&lng=' + center.lng() + '&distance=' + distanceFromCenter;

	GDownloadUrl(searchURL, function(data)
		{
		var xml = GXml.parse(data);
		var stores = xml.documentElement.getElementsByTagName('store');
		var bounds = new GLatLngBounds();
		map.clearOverlays();

		var marker = CreateYouAreHereMarker(center, addressAtPoint);
		map.addOverlay(marker);
		bounds.extend(center);

		var sidebar = document.getElementById('mapsidebar');
		sidebar.innerHTML = '';
		if (stores.length == 0)
			{
			sidebar.innerHTML = 'No stores found.';
			map.setCenter(center, kDefaultZoom);
			return;
			}

		for (var index = 0; index < stores.length; index++)
			{
			var storeType = stores[index].getAttribute('storeType');
			var name = stores[index].getAttribute('name');
			var address = stores[index].getAttribute('address');
			var city = stores[index].getAttribute('city');
			var state = stores[index].getAttribute('state');
			var zip = stores[index].getAttribute('zip');
			var phone = stores[index].getAttribute('phone');
			var lat = stores[index].getAttribute('lat');
			var lng = stores[index].getAttribute('lng');
			var url = stores[index].getAttribute('url');
			var distance = parseFloat(stores[index].getAttribute('distance'));

			var point = new GLatLng(parseFloat(stores[index].getAttribute('lat')),
				parseFloat(stores[index].getAttribute('lng')));

			var drivingDirectionsURL = 'http://maps.google.com/maps?q=' + lat + ',' + lng + '(' + encodeURIComponent(address) + ', ' + city + ', ' + state + ' ' + zip + ')';
			encodeURIComponent(drivingDirectionsURL);

			marker = CreateStoreMarker(point, storeType, name, address, city, state, zip, phone, url, distance, drivingDirectionsURL);
			map.addOverlay(marker);
			var sidebarEntry = CreateSidebarEntry(marker, storeType, name, address, city, state, zip, distance, phone, url, distance, drivingDirectionsURL);
			sidebar.appendChild(sidebarEntry);
			bounds.extend(point);
			}
		map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
		});
	} // FindStoresNearPoint


function CreateYouAreHereMarker(point, address)
	{	  
	var icon = new GIcon(G_DEFAULT_ICON);
	icon.image = "Maps/YouAreHereMarker.png";
	icon.iconSize = new GSize(21, 35);
	icon.iconAnchor = new GPoint(6, 21);
	icon.infoWindowAnchor = new GPoint(9, 5);
	var marker = new GMarker(point, icon);

	//var marker = new GMarker(point);

	var html = '<font STYLE="font-face:arial; font-size:9pt; color:0155A7"><b>';
	html = html + address + '</font></b>';

	GEvent.addListener(marker, 'click', function()
		{
		marker.openInfoWindowHtml(html);
		});
	return marker;
    } // CreateYouAreHereMarker


function CreateStoreMarker(point, storeType, name, address, city, state, zip, phone, url, distance, drivingDirectionsURL)
	{
	var icon = new GIcon(G_DEFAULT_ICON);
	if (storeType == "Apple")
		icon.image = "Maps/AppleStoreMarker.png";
	else
		icon.image = "Maps/OtherStoreMarker.png";
	icon.iconSize = new GSize(21, 35);
	icon.iconAnchor = new GPoint(6, 21);
	icon.infoWindowAnchor = new GPoint(9, 5);
	var marker = new GMarker(point, icon);

	var html = '<font STYLE="font-face:arial; font-size:9pt; color:0155A7"><b>';
	html = html + '<a href="' + url + '" target="_blank">' + name + '</a>' + '</font></b><font STYLE="font-face:arial; font-size:8pt;"><br/>' + address;
	html = html + '<br/>' + city + ', ' + state + '  ' + zip + '<br/><b>Phone:</b>' + phone + '<br/></font><a href="' + drivingDirectionsURL + '" target="_blank" STYLE="font-face:arial; font-size:7pt;">Get Directions</a>';

	GEvent.addListener(marker, 'click', function()
		{
		marker.openInfoWindowHtml(html);
		});
	return marker;
    } // CreateStoreMarker


function CreateSidebarEntry(marker, storeType, name, address, city, state, zip, distance, phone, url, distance, drivingDirectionsURL)
	{
	var div = document.createElement('div');
	var html = '<font STYLE="font-face:arial; font-size:8pt; color:0155A7"><b>';

	html = html + name + '</font></b><font STYLE="font-face:arial; font-size:8pt;"><br/>' + address;
	html = html + '<br/>' + city + ', ' + state + '  ' + zip + '<br/>';
	html = html + '<b>Phone:</b> ' + phone + '<br/>';
	html = html + '<b>Distance:</b> ' + (Math.round(distance * 10) / 10) + ' Miles <br/>';
	html = html + '<a href="' + drivingDirectionsURL + '" target="_blank" STYLE="font-face:arial; font-size:7pt;">Get Directions</a>';

	div.innerHTML = html;
	div.style.cursor = 'pointer';
	div.style.marginBottom = '10px'; 
	div.style.marginLeft = '4px'; 
	div.style.marginRight = '3px'; 
	GEvent.addDomListener(div, 'click', function()
		{
		GEvent.trigger(marker, 'click');
		});
	GEvent.addDomListener(div, 'mouseover', function()
		{
		div.style.backgroundColor = '#eee';
		});
	GEvent.addDomListener(div, 'mouseout', function() {
		div.style.backgroundColor = '#fff';
		});
	return div;
	} // CreateSidebarEntry
