﻿var map;
var panorama;
var marker_array = new Array();
var sv = new google.maps.StreetViewService();

/* initializeMap not used in this instance */
/* function initializeMap() {
	var tampaLoc = new google.maps.LatLng(27.985341, -82.481690);
	var myOptions = {
      zoom: 8,
      center: tampaLoc,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    setMarkers();
}
*/

/**
* Data for the markers consisting of a name, a LatLng.
* var locations = [
*  ['Bondi Beach', -33.890542, 151.274856, infoWindowHtml, iconUrl],
*  ['Coogee Beach', -33.923036, 151.259052, infoWindowHtml, iconUrl],
*  ['Cronulla Beach', -34.028249, 151.157507, infoWindowHtml, iconUrl],
*  ['Maroubra Beach', -33.950198, 151.259302, infoWindowHtml, iconUrl]
* ];
*/

function setMarkers(isMobile) {

	if (locations) {

		for (var i = 0; i < locations.length; i++) {
			var loc = locations[i];

			// Add markers to the map
			var myLatLng = new google.maps.LatLng(loc[1], loc[2]);
			var marker;
			if (isMobile == "false") {
				// Don't animate markers on mobile browsers
				marker = new google.maps.Marker({
					position: myLatLng,
					map: map,
					title: loc[0],
					htmlContent: loc[3],
					icon: loc[4]
				});
			} else {
				marker = new google.maps.Marker({
					position: myLatLng,
					map: map,
					title: loc[0],
					htmlContent: loc[3],
					animation: google.maps.Animation.DROP,
					icon: loc[4]
				});
			}

			marker_array.push(marker);

			var infowindow = new google.maps.InfoWindow({
				content: "No content"
			});
			google.maps.event.addListener(marker, 'click', function () {
				infowindow.setContent(this.htmlContent);
				infowindow.open(map, this);
			});
		}

		// Auto-center the map on the markers
		//  Create a new viewpoint bound
		var bounds = new google.maps.LatLngBounds();

		//  Go through each...
		for (i = 0; i < marker_array.length; i++) {
			bounds.extend(marker_array[i].position);
		}

		//apply user-defined zoom level to points on map 
		if (marker_array.length <= 1) {
			//get center of extended points and set it on the map 
			map.setCenter(bounds.getCenter());
			map.setZoom(16);
		} else {
			//fit map using map api function 
			map.fitBounds(bounds);
		}

	}
}

function ShowStreetView(lat, lng) {

	// We get the map's default panorama and set up some defaults.
	// Note that we don't yet set it visible.

	var markerLatLng = new google.maps.LatLng(lat, lng);

	//panorama = map.getStreetView();
	//panorama.setPosition(markerLatLng);
	//panorama.setPov({
	//	heading: 265,
	//	zoom: 1,
	//	pitch: 0
	//}
    //);
	//panorama.setVisible(true);

	// Custom panorama using 150 meter radius to insure every location gets a streetview (default radius of 50 was too small)
	sv.getPanoramaByLocation(markerLatLng, 150, processSVData);
}

function processSVData(data, status) {
	if (status == google.maps.StreetViewStatus.OK) {
		//		var marker = new google.maps.Marker({
		//			position: data.location.latLng,
		//			map: map,
		//			title: data.location.description
		//		});

		panorama = map.getStreetView();

		// Set the Pano to use the passed panoID
		var markerPanoID = data.location.pano;
		panorama.setPano(markerPanoID);

		panorama.setPov({
			heading: 0,
			pitch: 0,
			zoom: 1
		});

		panorama.setVisible(true);

	} else {

		alert("Street View data not available at this location.");
	}
}


function SearchFromCurrentLocation()
{
	// Try W3C Geolocation (Preferred)
	if (navigator.geolocation) {
		browserSupportFlag = true;
		navigator.geolocation.getCurrentPosition(function (position) {
			location.href = "/locations.aspx?lat=" + position.coords.latitude + "&lng=" + position.coords.longitude;
		}, function () { alert("Geolocation service failed."); });
	} else {
		browserSupportFlag = false;
		alert("Your device does not support automatic location services.  Please enter a City, State or Postal Code.");
	}
}

function ShowHideMap(showMap) {

	if (showMap) {
		document.getElementById("map_canvas").style.display = '';
		document.getElementById("locationsList").style.display = 'none';
		document.getElementById("btnListImg").src = '/App_Themes/Mobile/Images/list_btn.jpg';
		document.getElementById("btnMapImg").src = '/App_Themes/Mobile/Images/map_btn_on.jpg';
	} else {
		document.getElementById("map_canvas").style.display = 'none';
		document.getElementById("locationsList").style.display = '';
		document.getElementById("btnListImg").src = '/App_Themes/Mobile/Images/list_btn_on.jpg';
		document.getElementById("btnMapImg").src = '/App_Themes/Mobile/Images/map_btn.jpg';
	}

	return false;
}

function GeocodeLocation(inputfield, latout, lngout, postbackStr) {

	var addressStr = document.getElementById(inputfield).value;
	if (addressStr.length > 2) {
		geocoder = new google.maps.Geocoder();
		geocoder.geocode({ 'address': addressStr }, function (results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				document.getElementById(latout).value = results[0].geometry.location.lat();
				document.getElementById(lngout).value = results[0].geometry.location.lng();

				//alert("Geocode was successful! " + results[0].geometry.location.lat() + " " + results[0].geometry.location.lng());

				__doPostBack(postbackStr, '');

			} else {

				alert("Unable to locate City, State/Province or Postal Code, please try again. (status: " + status + ")");
			}
		});

		return false;

	} else {

		// Allow control to postback to do special two character state abbreviation lookup.
		return true;
	}
}




