localedge.require('localedge.map');
localedge.require('localedge.map.impl.google');

$(document).ready(function() {

	var isGeolocationEstimated = Boolean( $("meta[name=geolocation-estimated]").attr("content") == "true" );

	if ($("#proAdMap").length > 0 && !isGeolocationEstimated) {
		var mapService = new localedge.map.MapService(localedge.map.impl.google);
		var markers = [];

		$(".proAdListing:visible").each(function(i) {
			var latitude = $(this).attr("data-latitude") && $(this).attr("data-latitude") != 0 ? $(this).attr("data-latitude") : null;
			var longitude = $(this).attr("data-longitude") && $(this).attr("data-longitude") != 0 ? $(this).attr("data-longitude") : null;
			var $address = $(this).find(".address");
			var address = null;
			if ( $address.find(".street").text() != "" && (($address.find(".city").text() != "" && $address.find(".state").text() != "") || $address.find(".zip").text() != "") ) {
				address = $address.find(".street").text()+" "+$address.find(".city").text()+", "+$address.find(".state").text()+" "+$address.find(".zip").text();
			}

			if ( (latitude && longitude) || address) {
				$(this).data("haveCoord", true);
				var coord = {};

				if (latitude && longitude) {
					coord.latitude = latitude;
					coord.longitude = longitude;
				} else {
					coord.address = address;
				}

				markers.push({
					coord : coord,
					useThemeIcon: true
				});

			}
		});

		if (markers.length > 0) {

			mapService.map(
				{
					map: $("#proAdMap").get(0),
					mapThemeKey: $("meta[name=mapThemeKey]").attr("content"),
					markers: markers,
					mapView: localedge.map.MapView.ROAD_VIEW
				},
				function(response) {
					$(".proAdListing:visible").each(function(i) {
						if ( $(this).data("haveCoord") && response.markers.length > 0 ) {
							$(this).data("marker", response.markers[i]);
							$(this).find(".mapMarker img").click(function() {
								mapService.centerMapOnMarker(response.markers[i]);
							}).attr("src", response.markers[i].iconUri);
						} else {
							$(this).find(".mapMarker img").remove();
						}
					});
				}
			);
			if (mapService.showAllMarkersOnMap) {
				mapService.showAllMarkersOnMap();
			}

			// Comment out the above and uncomment the below to use street view.

//			mapService.map(
//			{
//				map: $("#proAdMap").get(0),
//				mapThemeKey: $("meta[name=mapThemeKey]").attr("content"),
//				markers: markers,
//				mapView: localedge.map.MapView.STREET_VIEW
//			},
//			function(streetViewResponse, streetViewStatus) {
//				if (streetViewStatus != localedge.map.MapStatus.OK) {
//					mapService.map(
//						{
//							map: $("#proAdMap").get(0),
//							mapThemeKey: $("meta[name=mapThemeKey]").attr("content"),
//							markers: markers,
//							mapView: localedge.map.MapView.ROAD_VIEW
//						},
//						function(mapResponse) {
//							$(".proAdListing:visible").each(function(i) {
//								if ( $(this).data("haveCoord") && mapResponse.markers.length > 0 ) {
//									$(this).data("marker", mapResponse.markers[i]);
//									$(this).find(".mapMarker img").click(function() {
//										mapService.centerMapOnMarker(mapResponse.markers[i]);
//									}).attr("src", mapResponse.markers[i].iconUri);
//								} else {
//									$(this).find(".mapMarker img").remove();
//								}
//							});
//						}
//					);
//				}
//			});

		} else {
			$("#proAdMapContainer").remove();
		}
	} else {
		$("#proAdMapContainer").remove();
	}
});
