/**
 * Get directions from google.
 * Google Map API: http://code.google.com/apis/maps/documentation/javascript/reference.html
 */

localedge.require('localedge.proad');

$(document).ready(function() {

	var geocoder = new google.maps.Geocoder();
	var directionsService = new google.maps.DirectionsService();
	var directionsRenderer = new google.maps.DirectionsRenderer();

	// Init map
	localedge.proad.global.directionsMap = new google.maps.Map(
		$("#section-directions .map").get(0),
		{
			zoom: 11,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
	);

	// Build list of destinations based on business listings.
	$(".proAdListing:visible").each(function() {
		var business = {
			latitude: $(this).attr("data-latitude"),
			longitude: $(this).attr("data-longitude"),
			street: $(this).find(".address .street").text(),
			city: $(this).find(".address .city").text(),
			state: $(this).find(".address .state").text(),
			zip: $(this).find(".address .zip").text()
		};
		$("#section-directions .destinations").append('<div class="item none"><div class="point"></div><div class="destination">' + business.street + " " + business.city + " " + business.state + " " + business.zip + '</div></div>');
		var $destination = $("#section-directions .destinations .destination:last");

		if (business.latitude != "0" && business.longitude != "0") {
			$destination.data("latLng", new google.maps.LatLng(business.latitude, business.longitude));
		} else {
			geocoder.geocode(
				{address: business.street + " " + business.city + " " + business.state + " " + business.zip},
				function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						var result = results[0];
						$destination.data("latLng", result.geometry.location);
						new google.maps.Marker({
							position: result.geometry.location,
							map: localedge.proad.global.directionsMap
						});
					}
				}
			);
		}
	});

	// Set map center to first destination.
	$("#section-directions .destinations .item:first").removeClass("none").addClass("b");
	localedge.proad.global.directionsMap.setCenter( $("#section-directions .destinations .destination:first").data("latLng") );

	// Add marker for each destination
	$("#section-directions .destinations .destination").each(function() {
		new google.maps.Marker({
			position: $(this).data("latLng"),
			map: localedge.proad.global.directionsMap
		});
	});

	// Add CTA if more than one destination
	if ( $("#section-directions .destinations .destination").length > 1) {
		$("#section-directions .destination-cta").show();
	}

	// Set map center to clicked destination
	$("#section-directions .destinations .destination").click(function() {
		$(this).parent().parent().find(".item").removeClass("b").addClass("none");
		$(this).parent().removeClass("none").addClass("b");
		localedge.proad.global.directionsMap.setCenter( $(this).data("latLng") );
	});

	// Direction form submit
	$("#section-directions .form form").submit(function(e) {
		$("#section-directions .status").hide();
		var $submit = $(this).find("input[type=submit]");

		$submit.attr("disabled", "disabled");
		
		e.preventDefault();
		$("#section-directions .steps").text("");

		var directionsRequest = {
			origin: $(this).find("input[name=start]").val(),
			destination: $("#section-directions .form .item.b .destination").text(),
			provideRouteAlternatives: false,
			travelMode: google.maps.DirectionsTravelMode.DRIVING,
			unitSystem: google.maps.DirectionsUnitSystem.IMPERIAL
		};

		directionsService.route(
			directionsRequest,
			function(directionsResult, directionsStatus) {
				if (directionsStatus == google.maps.DirectionsStatus.OK) {
					var dir = $("#section-directions");

					localedge.proad.global.directionsMap.setOptions({noClear: false});
					directionsRenderer.setMap(localedge.proad.global.directionsMap);
					directionsRenderer.setDirections(directionsResult);
					directionsRenderer.setPanel($("#section-directions .steps").get(0));
				} else if (google.maps.DirectionsStatus.NOT_FOUND) {
					$("#section-directions .status").text("Could not calculate directions").addClass("error").show();
				} else {
					$("#section-directions .status").text("There was an error getting directions: " + directionsStatus).addClass("error").show();
				}

				$submit.removeAttr("disabled");
			}
		);

	}); // End Submit
	
});


