/*
*******************************************************************************************************
*******************************************************************************************************
*******************************************************************************************************
															LABELLED MARKER (must go before MAP functions)
*******************************************************************************************************
*******************************************************************************************************
*******************************************************************************************************
*/

/*
* LabeledMarker Class
*
* Copyright 2007 Mike Purvis (http://uwmike.com)
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*       http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This class extends the Maps API's standard GMarker class with the ability
* to support markers with textual labels. Please see articles here:
*
*       http://googlemapsbook.com/2007/01/22/extending-gmarker/
*       http://googlemapsbook.com/2007/03/06/clickable-labeledmarker/
*/

/* Constructor */
function LabeledMarker(latlng, options){
    this.latlng = latlng;
    this.labelText = options.labelText || "";
    this.labelClass = options.labelClass || "markerLabel";
    this.labelOffset = options.labelOffset || new GSize(0, 0);
    
    this.clickable = options.clickable || true;
    
    if (options.draggable) {
    	// This version of LabeledMarker doesn't support dragging.
    	options.draggable = false;
    }
    
    GMarker.apply(this, arguments);
}


/* It's a limitation of JavaScript inheritance that we can't conveniently
   extend GMarker without having to run its constructor. In order for the
   constructor to run, it requires some dummy GLatLng. */
LabeledMarker.prototype = new GMarker(new GLatLng(0, 0));


// Creates the text div that goes over the marker.
LabeledMarker.prototype.initialize = function(map) {
	// Do the GMarker constructor first.
	GMarker.prototype.initialize.apply(this, arguments);
	
	var div = document.createElement("div");
	div.className = this.labelClass;
	div.innerHTML = this.labelText;
	div.style.position = "absolute";
	map.getPane(G_MAP_MARKER_PANE).appendChild(div);

	if (this.clickable) {
		// Pass through events fired on the text div to the marker.
		var eventPassthrus = ['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout'];
		for(var i = 0; i < eventPassthrus.length; i++) {
			var name = eventPassthrus[i];
			GEvent.addDomListener(div, name, newEventPassthru(this, name));
		}

		// Mouseover behaviour for the cursor.
		div.style.cursor = "pointer";
	}
	
	this.map = map;
	this.div = div;
}

function newEventPassthru(obj, event) {
	return function() { 
		GEvent.trigger(obj, event);
	};
}

// Redraw the rectangle based on the current projection and zoom level
LabeledMarker.prototype.redraw = function(force) {
	GMarker.prototype.redraw.apply(this, arguments);
	
	// We only need to do anything if the coordinate system has changed
	if (!force) return;
	
	// Calculate the DIV coordinates of two opposite corners of our bounds to
	// get the size and position of our rectangle
	var p = this.map.fromLatLngToDivPixel(this.latlng);
	var z = GOverlay.getZIndex(this.latlng.lat());
	
	// Now position our DIV based on the DIV coordinates of our bounds
	this.div.style.left = (p.x + this.labelOffset.width) + "px";
	this.div.style.top = (p.y + this.labelOffset.height) + "px";
	this.div.style.zIndex = z + 1; // in front of the marker
}

// Remove the main DIV from the map pane, destroy event handlers
LabeledMarker.prototype.remove = function() {
	GEvent.clearInstanceListeners(this.div);
	this.div.parentNode.removeChild(this.div);
	this.div = null;
	GMarker.prototype.remove.apply(this, arguments);
}














/*
*******************************************************************************************************
*******************************************************************************************************
*******************************************************************************************************
																						MAP
*******************************************************************************************************
*******************************************************************************************************
*******************************************************************************************************
*/


//=====================================================================================================
// Set up icons
//=====================================================================================================

var icons = new Array();

var icon = new GIcon();
icon.image = root_path_url + "/images/icons/map_tube_station_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_grey"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/icons/map_tube_station.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_colored"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/1_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_1"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/2_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_2"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/3_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_3"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/4_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_4"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/5_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_5"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/6_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(35, 35);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_6"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/7_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_7"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/8_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_8"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/9_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_9"] = icon;



var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/10_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_10"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/11_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_11"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/12_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_12"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/14_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_14"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/15_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_15"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/landmark/16_grey.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["landmark_16"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/icons/map_hotel_not_selected_smal.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(20, 20);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(8, 16);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["hotel_small"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/icons/map_hotel_not_selected.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(27, 27);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["hotel_big"] = icon;


var icon = new GIcon();
icon.image = root_path_url + "/images/icons/hotel_home_not_selected.gif";
icon.shadow = root_path_url + "/images/icons/trans_pix.gif";
icon.iconSize = new GSize(28, 28);
icon.shadowSize = new GSize(1, 1);
icon.iconAnchor = new GPoint(14, 27);
icon.infoWindowAnchor = new GPoint(14, 14);
icons["hotel_home"] = icon;
	




//=====================================================================================================
// Map variables
//=====================================================================================================


var map_center_lat = ""; /*default values*/
var map_center_long = ""; /*default values*/
var map_zoom_level = 12; /*default values*/

var map_minimum_zoom = 1;
var map_maximum_zoom = 17;

var markers = new Array(); /*keep the marker objects for landmarks*/
var landmark_details = new Array(); /*keep the landmark details*/

var hotel_details = new Array(); /*keep the hotel details*/

var map_move_enabled = false;

var map = new Object();

var labeled_marker_opts = {
		"icon": icon,
		"clickable": false,
		"draggable": false,
		"labelText": "",
		"labelOffset": new GSize(-14, -14)
	};

	
	




//=====================================================================================================
//=====================================================================================================

function load_map(latitude, longitude) 
{
	//alert ("Starting load_map() with latitude '" + latitude + "' and longitude '" + longitude + "'");

  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"));
	
	map.setCenter(new GLatLng(latitude, longitude), map_zoom_level);
	map.addControl(new GSmallMapControl());
	/*map.addControl(new GLargeMapControl());*/
	map.addControl(new GMapTypeControl());

	landmarkers_manager = new GMarkerManager(map, {trackMarkers: true});
	hotels_manager = new GMarkerManager(map, {trackMarkers: true});

	GEvent.addListener(map, "moveend", function() {
		/*this is to get other points on the map*/


		maps_center = map.getCenter();
		center_lat = maps_center.lat();
		center_long = maps_center.lng();
		
		try{
			
			input_coord_lat = $('coord_lat');
			input_coord_long = $('coord_long');
			input_coord_lat.value = center_lat;
			input_coord_long.value = center_long;
			
			input_coord = $('coord_value');
			input_coord.value = Math.round(center_lat*10000)/10000 + ', ' + Math.round(center_long*10000)/10000				
			
		}
		catch(e)
		{
			
		}
		
		if (hotels_manager.getMarkerCount() > 50)
		{
			return true;
		}
		if (!map_move_enabled)
		{
			return true;
		}

		show("map_loading");
		map_bounds = map.getBounds();
		sw = map_bounds.getSouthWest();
		ne = map_bounds.getNorthEast();
		
		min_lat = sw.lat();
		max_lat = ne.lat();

		min_long = sw.lng();
		max_long = ne.lng();
		
		var url = "ajax.php";
		var pars = 'action=landmark_map_moved&min_lat='+min_lat+'&max_lat='+max_lat+'&min_long='+min_long+'&max_long='+max_long;
		var myAjax = new Ajax.Request( url,
									{ method: 'get',
									  parameters: pars,
									  onComplete: add_json_markers,
									  onException: hide("map_loading"),
									  onSuccess: hide("map_loading")
									}
								 );
	});

	map.checkResize();

  }
}






//=====================================================================================================
//=====================================================================================================

function createLandMarker(point, number, type) {
  var marker = new GMarker(point, icons["landmark_"+type] );

  GEvent.addListener(marker, "click", function() {
	  try{
		landmark_open_info(number);
	  }
	  catch(err)
	  {
	  }
  });

  GEvent.addListener(marker, "mouseover", function() {
	  try{
		marker_set_color_icon(type, number);
	  }
	  catch(err)
	  {
	  }
  });

  GEvent.addListener(marker, "mouseout", function() {
	  try{
		marker_set_grey_icon(type, number);
	  }
	  catch(err)
	  {
	  }

  });

  markers[number] = marker;
  return marker;
}





//=====================================================================================================
//=====================================================================================================
		
function landmark_open_info(number)
{
	/*map.panTo( markers[number].getPoint() );*/
	if (markers[number])
	{
		name = landmark_details[number]["name"];
		if (landmark_details[number]["address"])
		{
			address = landmark_details[number]["address"];
		}
		else
		{
			address = '';
		}
		link = landmark_details[number]["link"];
		
		markers[number].openInfoWindowHtml("<div class='landmark_overlay'><a href='"+link+"' class='txt_hotel_blue'><b>" + name + "</b></a><br>" + address + "</div>", ({maxWidth:'100'}) );
		marker_set_color_icon(number);
	}
}




//=====================================================================================================
//=====================================================================================================

function marker_set_color_icon(landmark_type_id, number)
{
	markers[number].setImage(root_path_url + "/images/landmark/"+landmark_type_id+".gif");
}



//=====================================================================================================
//=====================================================================================================

function marker_set_grey_icon(landmark_type_id, number)
{
	markers[number].setImage(root_path_url + "/images/landmark/"+landmark_type_id+"_grey.gif");
}





//=====================================================================================================
//=====================================================================================================

function initialise_map(lat, lon, landmark_id, landmark_type_id)
{
	
	var landmark = createLandMarker( new GLatLng(lat, lon), landmark_id, landmark_type_id);
	landmarkers_manager.addMarker(landmark, map_minimum_zoom, map_maximum_zoom); 
	landmarkers_manager.refresh();
	
	hide('map_loading');

}





//=====================================================================================================
//=====================================================================================================

function createLabeledMarker(point, number, icon, label)
{
	opts = {
		"icon": icon,
		"clickable": false,
		"draggable": false,
		"labelText": label,
		"labelOffset": new GSize(-9, -22)
		/*
		width - if the number goes to zero, the number is placed at the right of the icon.
		 height if the height goes towards zero, the number will be placed under the icon
		*/
	};
	
	var marker = new LabeledMarker(point, opts);
	
	GEvent.addListener(marker, "click", function() {
		hotel_open_info(number);
	  });
	  
  GEvent.addListener(marker, "mouseover", function() {
	  hotel_set_selected_icon(number);
  });

	GEvent.addListener(marker, "mouseout", function() {
		hotel_set_not_selected_icon(number);
  });

	  
	markers[number] = marker;
	return marker;
}








//=====================================================================================================
//=====================================================================================================

function hotel_open_info(hotel_id)
{
	if (markers[hotel_id])
	{
		name = marker_details[hotel_id]["name"];
		link = "#";/*marker_details[hotel_id]["link"];*/
		onclick = "toggle_hotel_details("+hotel_id+"); return false;";
		thumb_url = marker_details[hotel_id]["thumb_url"];
		address = marker_details[hotel_id]["address"];
		distance = marker_details[hotel_id]["distance"];
		star_rating_img = marker_details[hotel_id]["star_rating_img"];
		guest_rating = marker_details[hotel_id]["guest_rating"];
		book_link = marker_details[hotel_id]["book_link"];
		supplier_image = marker_details[hotel_id]["supplier_image"];
		
		if (book_link == "") {
			best_price_div = '';
			more_link_text = "more info";
		}
		else {
			best_price_div = '<div class="infowindow_best_price">Best price: '+book_link+'&nbsp;</div><div class="infowindow_supplier_link">'+supplier_image+'</div><div class="spacer"></div>';
			more_link_text = "more info and prices";
		}

		if (guest_rating == null) {
			guest_rating_string = '';
		}
		else {
			guest_rating_string = 'Reviews: '+guest_rating+' / 5';
		}

		if (star_rating_img == "") {
			star_rating_div = '';
		}
		else {
			star_rating_div = '<div class="infowindow_stars">'+star_rating_img+'</div>';
		}

		markers[hotel_id].openInfoWindowHtml(
		'<div class="infowindow_container">'+
			'<div class="infowindow_hotel_name">'+name+'</div>'+
			'<div class="spacer"></div>'+
  	  star_rating_div+
  	  '<div class="infowindow_subtext" style="float:left;">'+guest_rating_string+'</div>'+
  	  '<div class="spacer"></div>'+
			best_price_div +
			'<div><a class="infowindow_more_link" href="#" onclick="hotel_details_overlay('+hotel_id+'); return false;">'+more_link_text+'</a></div>'+
		'</div>');
	
		

		hotel_set_selected_icon(hotel_id);
		
	}
}





//=====================================================================================================
//=====================================================================================================

function hotel_set_selected_icon(hotel_id)
{
	try{
		if (div = document.getElementById("hotel_"+hotel_id))
		{
			  div.style.backgroundColor = mouse_over_background_color;
		}
	}
	catch(err)
	{
	}
	try{
		if (div = document.getElementById("hotel_details_"+hotel_id))
		{
			  div.style.backgroundColor = mouse_over_background_color;
		}
	}
	catch(err)
	{
	}
	
	
	try{
		if (div = document.getElementById("hotel_"+hotel_id+"_number"))
		{
		
  				  div.style.backgroundImage = "url(" + root_path_url + "/images/icons/map_hotel_selected_smal.gif)";
		}
	}
	catch(err)
	{
	}
	
	try{
		if (markers[hotel_id])
		{
		    
		    image_url = markers[hotel_id].getIcon().image;
		    
		    if (image_url == root_path_url + "/images/icons/map_hotel_not_selected_smal.gif")
		    {
			    markers[hotel_id].setImage(root_path_url + "/images/icons/map_hotel_selected_smal.gif");
		    }
		    else
		    {
		    
		        markers[hotel_id].setImage(root_path_url + "/images/icons/hotel_home_selected.gif");
		    }
		}
	}
	catch(err)
	{
	}
}
	








//=====================================================================================================
//=====================================================================================================

function hotel_set_not_selected_icon(hotel_id)
{
	try{
		if (div = document.getElementById("hotel_"+hotel_id))
		{
			  div.style.backgroundColor = "";  				 
		}
	}
	catch(err)
	{
	}
	try{
		if (div = document.getElementById("hotel_details_"+hotel_id))
		{
			  div.style.backgroundColor = "";
		}
	}
	catch(err)
	{
	}
	try{
		if (div = document.getElementById("hotel_"+hotel_id+"_number"))
		{
			  div.style.backgroundImage = "url(" + root_path_url + "/images/icons/map_hotel_not_selected_smal.gif)";
		}
	}
	catch(err)
	{
	}
	try{
		if (markers[hotel_id])
		{
		
			
		    image_url = markers[hotel_id].getIcon().image;
		    
		    
		    if (image_url == root_path_url + "/images/icons/map_hotel_not_selected_smal.gif")
		    {
			    markers[hotel_id].setImage(root_path_url + "/images/icons/map_hotel_not_selected_smal.gif");
		    }
		    else
		    {
		    
		        markers[hotel_id].setImage(root_path_url + "/images/icons/hotel_home_not_selected.gif");
		    }
		    
			
		}
	}
	catch(err)
	{
	}

}

