var lateroomsArray = [];
var infoWindow = null;
var map = null;
var pinArray = [];

function initialize() {	
	$('#googlemapContainer').show();
	
	var myLatlng = new google.maps.LatLng(googlemap_data['latitude'], googlemap_data['longitude']);
	var myOptions = {
		zoom: googlemap_data['zoom'],
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map(document.getElementById("googlemap"), myOptions);
	
	infoWindow = new google.maps.InfoWindow({
		width: 225
	});
	
	pinArray[laterooms] = new google.maps.MarkerImage('/images/icons/googlemap-hotel-icon.png',
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(31, 25),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(12, 19));

	
	laterooms (myLatlng);
	
	$('#googlemap').after('<a href="#" class="hotels">hotels in this area</a>');
	$('a.hotels').click(function () {
	  laterooms (map.getCenter());
	});
	
	
}

function laterooms (myLatlng) {
	$.get('/api/laterooms/geocode/', 
		{
			'lat' : myLatlng.lat(),
			'lon' : myLatlng.lng()
		},
		function(data) {
			data = eval(data);
			for (var i = 0; i < data.length; i++) {
				addLateroomsMarker(data[i]);				
			}
		});		
	return false;
}

function addLateroomsMarker ( hotel )
{
	if (hotel['hotel_ref'] in lateroomsArray == false && hotel['geo_code'] != undefined)
	{
		var contentString = '<div id="lateroomsWindow">';
		
		if (hotel['images']) contentString += '<a href="'+hotel['hotel_link']+'" target="_blank"><img src="'+hotel['images']+'" alt=""  /></a>';
		contentString += '<h2><a href="'+hotel['hotel_link']+'" target="_blank">'+hotel['hotel_name']+'</a></h2>'+
			'<div id="bodyContent">'+
			'<p>' + hotel['hotel_address'];
		if (hotel['prices_from'] && hotel['star_accomodation_type']) contentString += '<br />'+ hotel['hotel_star'] + ' Star '+ hotel['star_accomodation_type'];
		if (hotel['prices_from']) contentString += '<br />Rooms from: &pound;'+ hotel['prices_from'];
		if (hotel['hotel_no_of_rooms']) contentString += '<br />No. of rooms: '+ hotel['hotel_no_of_rooms'];
		contentString += '<br /><a href="'+hotel['hotel_link']+'" target="_blank">View details</a></p>'+
			'</div>'+
			'</div>';
		
		var myLatLng = new google.maps.LatLng(hotel['geo_code']['lat'], hotel['geo_code']['long']);
		var marker = new google.maps.Marker({
			position: myLatLng,
			map: map,
			icon: pinArray[laterooms],
			title: hotel['hotel_name'],
			zIndex: 1
		});
		
		google.maps.event.addListener(marker, 'click', function() {
			infoWindow.setContent(contentString);
			infoWindow.open(map,marker);
		});
		
		lateroomsArray[hotel['hotel_ref']] = marker;
	}
}


$(document).ready(function () {
	initialize();
})
