// JavaScript Document (jQuery)

// START GOOGLE MAPS FUNCTIONS
var j = 0;
var geocoder = null;
var map = null;
var markers = null;
var infoBoxes = null;

function initializeGoogleMap() {				
	// Default coordinates and zoom
	// Puts the position between the three hotels
	geocoder = new google.maps.Geocoder();
	var latLng = new google.maps.LatLng(48.48000,2.20000);
	var zoomLevel = 11;

	markers = [];
	infoBoxes = [];
	
	var mapOptions = {
		zoom: zoomLevel,
		center: latLng,
		mapTypeId: google.maps.MapTypeId.ROADMAP, // 2D map
		disableDefaultUI: true, // limit the user's ability to adjust the map
		navigationControl: true, // zoom/pan
		navigationControlOptions: google.maps.NavigationControlStyle.SMALL, // allows only the + and - zoom buttons
		mapTypeControl: false, // no you can't change it to satellite
		scaleControl: false, // :(
		//draggable: false, // more :(
		other_params: "sensor=false" // v3 API needs this
	};
	
	// plot
	map = new google.maps.Map($('#map').get(0), mapOptions);
}

function setcode(a)
{
	geocoder.geocode( {address:address[a][2]}, function(results, status) {
		  if (status == google.maps.GeocoderStatus.OK && results.length) {
			// You should always check that a result was returned, as it is
			// possible to return an empty results object.
			if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
			 // map.set_center(results[0].geometry.location);
			  var marker = new google.maps.Marker({
				  position: results[0].geometry.location,
				  map: map,
				  title: address[a][1]
			  });
			  infowindow = new google.maps.InfoWindow({content:address[a][3],maxWidth:350});
			  markers[a] = marker;
			  infoBoxes[a] = infowindow;
			}
		  } else {
			alert("Geocode was unsuccessful due to: " + status);
		  }
		});
}

function codeAddress() {
	$(address).each(function(a,i) {
		if(a%5 == 0 && a != 0){
			//After send five request for geocode, we have to take a break for 10 seconds. otherwise, we will get OVER_QUERY_LIMIT error
			//because google allowed only five address at once.
			setTimeout("setcode("+a+")",10000);
		} else {
			setcode(a);
		}
    });
}
// END GOOGLE MAPS FUNCTIONS