﻿
/* Extern vals */
  
	var map;
	// Draw array of hotels onto google map and center map on hotels

	$(document).ready(function () {
		// $("#map_canvas").css({ height: 400, width: 800 });
		 var myOptions = {
			zoom: 12,
			/* center: <%= ViewData["centerPoint"] %>,  */
			mapTypeId: google.maps.MapTypeId.ROADMAP // TERRAIN // 
			}
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		AddPlaces(map);

		zoomToViewAll();

		ShowPin(1);

	});

	var infowindow = new google.maps.InfoWindow();

	var markers = [];

	function AddPlaces(map) {
		  $.each(places, function (val, place) {
			if (( place.p != null) && ( place.p.length >1)) {
			  AddMarker(map, place.p[0], place.p[1], place.n);
			  }
		  });
	}

	function AddMarker(map, lat, lng, msg) {
		  var marker = new google.maps.Marker({
			  position: new google.maps.LatLng(lat, lng),
			  map: map,
			  title: msg,
			  icon: '/content/hotel.png'
		  });

		  markers.push(marker);
		  calculateBorders( lat,lng );
		  google.maps.event.addListener(marker, 'click', function () {ShowPin(MarkerToIndex(marker));});
	}

    // When user clicks on an marker find it's ID
	function MarkerToIndex(marker){
	    var k = 0;
	    var res = 1;
			$(markers).each(function (index, m) {
			        if (m.title == marker.title) {res = (index + 1);}
                    });
			return res;

}


function SetNameValContent(marker) {
    $.each(places, function (val, place) {
        if (place.n == marker.title) {
            infowindow.content = "<div class='gi'><a title='" + place.v + " matches' href='" + prefix + place.n + "'>" + place.n + "</a><p>" + place.v + " hotels<p></div>";
        }
    });
}

	function ShowPin(i){
	    var marker = markers[i - 1];

	    if (nameVals == 1) {
	        SetNameValContent(marker);	    }
	    else {
	        infowindow.content = $("#h" + i).parent().html();}

      infowindow.open(map,marker);
	  return false;
	}

	var minLat= null;var maxLat= null;var minLng= null;var maxLng= null;
									
	function calculateBorders (latitude, longitude) {
		latitude = Number(latitude);
		longitude = Number(longitude); 
		if(typeof(latitude) == 'number' && typeof(longitude) == 'number') {
			if(minLat == null || minLat > latitude) {minLat = latitude;}
			if(maxLat == null || maxLat < latitude) {maxLat = latitude;}
			if(minLng == null || minLng > longitude) {minLng = longitude;}
			if(maxLng == null || maxLng < longitude) {maxLng = longitude;}
		}
	}

	function zoomToViewAll () {
	    var visibleBounds = getVisibleBounds();

	    if (( markers[0].n == "Baglioni Hotel London") || (markers[0].n == "Chesterfield Mayfair - A Red Carnation Hotel"))
	    {
		    map.setCenter(new google.maps.LatLng( markers[0].p[0], markers[0].p[1]), 15);
		    return;
	    }

	    if(visibleBounds) {map.fitBounds(visibleBounds);}
    }
									
   function getVisibleBounds () {
	    if(maxLng) {
		    var swLatLng = new google.maps.LatLng(minLat, minLng);
		    var neLatLng = new google.maps.LatLng(maxLat, maxLng);
		    var minBounds = new google.maps.LatLngBounds(swLatLng, neLatLng);
		    return minBounds;
	    } 
	    return null; 
	}
