﻿function log(str)
{
	//console.log(str);
}

var geocoder;
var map;

google.load("maps", "2");
google.setOnLoadCallback(initialize);

function initialize()
{
	map = new google.maps.Map2(document.getElementById("map_canvas"));
	geocoder = new google.maps.ClientGeocoder();
	map.enableScrollWheelZoom();
	map.addControl(new google.maps.MapTypeControl());
	map.addControl(new google.maps.SmallMapControl());
	map.setCenter(new google.maps.LatLng(43.5164221, -84.9057943), 5);
}

jQuery(function() {

	jQuery("span.enlarge").click(function()
	{
		if (jQuery(this).html() == "View Larger Map")
		{
			jQuery(this).html('View Smaller Map');
			jQuery("#map_canvas").css('height', '300px');
		}
		else
		{
			jQuery(this).html('View Larger Map');
			jQuery("#map_canvas").css('height', '160px');
		}
	});

	jQuery("button[id$='btnGo']").click(function() {
		var loc = jQuery("input[id$='txtStreetAddress']").val() + " " +  jQuery("input[id$='txtCityState']").val() + " " +  jQuery("input[id$='txtZipCode']").val();
		var radius = jQuery("select[$='ddlRadius'] option:selected").val()

		geocoder.getLatLng(
			loc,
			function(point)
			{
				if (point)
				{
				
				    jQuery("div#message").html("");
				    jQuery("<p>Searching... Please wait.</p>").appendTo("div#message");
				    jQuery('div#results-pane').html("");
				    
                    /*pageTracker._trackPageview('/locationSearch/' + loc + '/' + radius);*/
                    
					var locData = {
						latitude	: point.lat(),
						longitude	: point.lng(),
						radius		: radius
					}
					
					jQuery.ajax({
						type: "POST",
						contentType: "application/json; charset=utf-8",
						url: "modules/GoogleMaps/GService.asmx/GetLocations",
						data: jQuery.toJSON(locData),
						dataType: "json",
						success: function(response)
						{
							map.clearOverlays();
							var data = jQuery.evalJSON(response.d);

							jQuery("div#message").html("");

							var bounds = new GLatLngBounds;
							if (data.length > 0)
							{
							    if (data.length >= 20)
							    {
								    jQuery("<p class='results'>Top " + data.length + " closest results found.</p>").appendTo("div#message").effect("highlight", {}, 2000);
								}
								else
                                {
                                    jQuery("<p class='results'>" + data.length + " results found.</p>").appendTo("div#message").effect("highlight", {}, 2000);
                                }    
                                
								for (var i = 0; i < data.length; i++)
								{
									addToMap(data[i]);
									bounds.extend(new GLatLng(data[i].Lat, data[i].LongX));
								}
								
								bounds.extend(point);
								map.setZoom(map.getBoundsZoomLevel(bounds));
								map.panTo(bounds.getCenter());
							}
							else
							{
								// show none found message.
								jQuery("<p class='error'>No results found.  Please expand your search radius.</p>").appendTo("div#message").effect("highlight", { color: 'red' }, 2000);
							}
						}
					});
				}
				else
				{
				    jQuery("div#message").html("");
					jQuery("<p class='error'>No results found.  Please adjust your search criteria and try again.</p>").appendTo("div#message").effect("highlight", { color: 'red' }, 2000);
				}
			}
		);
		return false;
	});

});

function addToMap(item) {

    var point = new google.maps.LatLng(item.Lat, item.LongX);
    var marker = new google.maps.Marker(point);
    var fromQuery = escape(jQuery("input[id$='txtStreetAddress']").val() + " " + jQuery("input[id$='txtCityState']").val() + " " + jQuery("input[id$='txtZipCode']").val());
    var directionsQuery = escape(item.Address1 + " " + item.City + ", " + item.State + "  " + item.Zip + "  (" + item.Name + ")");
	var markerHtml = "<div class='bubble-content'>" +
		"<h3>" + item.Name + "</h3>" +
		"<p class='address'>" + item.Address1 + "<br />" +
		item.City + ", " + item.State + "&nbsp;&nbsp;" + item.Zip + "</p>";
	
	if (item.Phone != '' && item.Phone != null) 
	{
	    markerHtml += "<p class='phone'>Phone: " + item.Phone + "</p>";
	}	
	if (item.Web != '' && item.Web != null)
	{
		var linkHref = item.Web;
		if (item.Web.indexOf("http://") == -1)
		{
			linkHref = "http://" + item.Web;
		}
		markerHtml += "<p class='website'><a target='_blank' href='" + linkHref + "'>" + item.Web + "</a></p>";
	}
	
	markerHtml += "<p class='directions'><a href='http://maps.google.com/maps?saddr=" + fromQuery + "&daddr=" + directionsQuery + "&hl=en' target='_blank'>Get directions to here.</a></p>";

	markerHtml += "</div>";
	GEvent.addListener(marker, 'click', function() 
	{
		marker.openInfoWindowHtml(markerHtml);
		map.panTo(point);
	});
	map.addOverlay(marker);
	jQuery('div#results-pane').append(markerHtml);
}


