// JavaScript Document

function Map_Details(markers, task)
{
	this.markers = markers;
	this.task = task
	
	this.get_zoom_level = function(){
		if (zoom>0)
			return zoom;
			
		//getting marker list based on non user-entered values:
		marker_count = 0;
		for(var i=0;i<this.markers.length;i++)
		{
			if (Number(this.markers[i].latitude)!=-100)
				marker_count++;
		}
		switch(marker_count)
		{
			case 0:
				return 4;
			case 1:
				return 16;
			default:
				//based on distance apart of items, set zoom so all are visible:
				var min_lat = min_lng = 1000000;
				var max_lng = max_lat = 0;
				
				for (i=0;i<this.markers.length;i++)
				{
					if (Number(this.markers[i].latitude)!=-100)
					{
						if (this.markers[i].latitude>max_lat)
						{
							max_lat = this.markers[i].latitude; 
						}
						if (this.markers[i].latitude<min_lat)
						{
							min_lat = this.markers[i].latitude; 
						}
						
						if (this.markers[i].latitude>max_lng)
						{
							max_lng = this.markers[i].longitude; 
						}
						if (this.markers[i].latitude<min_lng)
						{
							min_lng = this.markers[i].longitude; 
						}
					}
				}
				
				//get differences in size:
				var lng_dif = max_lng - min_lng;
				var lat_dif = max_lat - min_lat;
				
				if (lng_dif<0)
					lng_dif*=-1;
				
				if (lat_dif<0)
					lat_dif*=-1;
				
				//evaluate sizes:
				
				if (lat_dif<0.001 || lng_dif<0.001)
					zoom = 16;
				if ((0.001<lat_dif && lat_dif<0.0025) || (0.001<lng_dif &&lng_dif<0.0025))
					zoom = 15;
				if ((0.0025<lat_dif && lat_dif<0.007) || (0.0025<lng_dif &&lng_dif<0.007))
					zoom = 14;
				if ((0.007<lat_dif && lat_dif<0.03) || (0.007<lng_dif &&lng_dif<0.03))
					zoom = 13;
				if ((0.03<lat_dif && lat_dif<0.06) || (0.03<lng_dif &&lng_dif<0.06))
					zoom = 12;
				if ((0.06<lat_dif && lat_dif<0.2) || (0.06<lng_dif &&lng_dif<0.2))
					zoom = 11;
				
				if (lat_dif>=0.2 || lng_dif>=0.2)
					zoom = 10;

				return zoom;
		}
	}

	this.get_centre_point = function(){
		//work out centre point from map based on all of the markers.
		
		//get number of relevant markers:
		marker_count = 0;
		for(var i=0;i<this.markers.length;i++)
		{
			if (Number(this.markers[i].latitude)!=-100)
				marker_count++;
		}
		
		switch(marker_count)
		{
			case 0://centre point for centre of uk:
				return new GLatLng(54.72462, -3.3228125,false);
			case 1:
				for(var i=0;i<this.markers.length;i++)
				{
					if (Number(this.markers[i].latitude)!=-100)
						return new GLatLng(this.markers[i].latitude, this.markers[i].longitude,false);
				}
				
			default:
			{
				if (task=="find_nearby")
					//if task is view nearest first venue is selected venue so centre on this
				{
					if (Number(this.markers[0].latitude)!=-100)
						return new GLatLng(this.markers[0].latitude, this.markers[0].longitude,false);
					else
						return new GLatLng(54.72462, -3.3228125,false);
				}
				else //otherwise return average latitude and longitude:
				{
					var count = marker_count;
					var latitude_sum = 0;
					var longitude_sum = 0;
					for (var i=0;i<this.markers.length;i++)
					{
						if (Number(this.markers[i].latitude)!=-100)
						{
							latitude_sum += parseFloat(this.markers[i].latitude);
							longitude_sum += parseFloat(this.markers[i].longitude);
						}

					}
					
					return new GLatLng(latitude_sum/count, longitude_sum/count,false);
				}
			}
		}
	};

}

function set_map_centre(latitude, longitude)
{
	window.setTimeout(function(){
  		map.panTo(new GLatLng(latitude, longitude));
	}, 1000);
}

function re_centre_map()
{
	var centre_point = map_info.get_centre_point();
	set_map_centre(centre_point.lat(),centre_point.lng());
}

function Object_Marker(id, latitude, longitude, info, marker_class)
{
	this.id = id;
	this.latitude = latitude;
	this.longitude = longitude;
	this.info_text = info;
	this.marker_class = marker_class;
	this.get_id = function()
	{
		return this.id;
	}
	
	this.get_latlon = function()
	{
		return new GLatLng(this.latitude, this.longitude, false);
	}
	
	this.get_info = function()
	{
		return this.info_text;
	}

}


//function for creating venue markers
var marker_number;

var allow_reposition;


function createMarker(object) 
{
	
	marker_class = object.marker_class;
	title = object.get_info();
	
	var icon = new GIcon(G_DEFAULT_ICON);
	
	if (marker_class == 'numbered')
	{
		icon.image = './images/marker_'+marker_number+".gif";
		marker_number++;
	}
	else
	{  
		icon.image = "./images/marker_selected.gif";
	}
	if (Number(object.latitude)==-100)
		return false;
	
	var marker = new GMarker(object.get_latlon(),icon);
	
	
	

	if (allow_reposition!=true)
	{
		marker.tooltip = '<div class="tooltip">'+title+'</div>';
		GEvent.addListener(marker, "click", function(){
			window.location = "index.php?module=Venue&cmd=view&venue="+object.get_id();
		});
	
		//creating hover over event functions:
		GEvent.addListener(marker,"mouseover", function() {
		  showTooltip(marker);
		});        
		GEvent.addListener(marker,"mouseout", function() {
			tooltip.style.visibility="hidden";
		});   
	}
		
     
 
	return marker;
}

 function showTooltip(marker) {

	tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";
  }

var map;
var mapOverlay; 
var markers;
var map_info;
var tooltip;

function map_load(map_data,task)
{
	if (GBrowserIsCompatible()) 
  	{
		map = new GMap2(document.getElementById("map"));
		marker_number = 1;
		tooltip = document.createElement("div");
		document.getElementById("map").appendChild(tooltip);
		tooltip.style.visibility="hidden";
		
		function TextualZoomControl() {}
		TextualZoomControl.prototype = new GControl();
		
		TextualZoomControl.prototype.initialize = function(map) 
		{
			var container = document.createElement("div");
			
			var zoomInDiv = document.createElement("div");
			this.setButtonStyle_(zoomInDiv);
			container.appendChild(zoomInDiv);
			zoomInDiv.appendChild(document.createTextNode("Zoom In"));
			GEvent.addDomListener(zoomInDiv, "click", 
			function() 
			{
				map.zoomIn();
			});
			
			var zoomOutDiv = document.createElement("div");
			this.setButtonStyle_(zoomOutDiv);
			container.appendChild(zoomOutDiv);
			zoomOutDiv.appendChild(document.createTextNode("Zoom Out"));
			GEvent.addDomListener(zoomOutDiv, "click", 
			function()
			{											  
				map.zoomOut();
		  	});
			
		  if (!allow_reposition)
		  {
			var recentreDiv = document.createElement("div");
			this.setButtonStyle_(recentreDiv);
			container.appendChild(recentreDiv);
			recentreDiv.appendChild(document.createTextNode("Re-Centre"));
			GEvent.addDomListener(recentreDiv, "click", 
			function() 
			{
				re_centre_map();
			});
		  }
		  
		
		  map.getContainer().appendChild(container);
		  return container;
		}
	
	// By default, the control will appear in the top left corner of the
	// map with 7 pixels of padding.
		TextualZoomControl.prototype.getDefaultPosition = function() 
		{
		  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(3, 3));
		}
	
	// Sets the proper CSS for the given button element.
		TextualZoomControl.prototype.setButtonStyle_ = function(button) 
		{
			button.style.textDecoration = "underline";
			button.style.color = "#000000";
			button.style.backgroundColor = "white";
			button.style.font = "small Arial";
			button.style.fontWeight = "bold";
			button.style.border = "1px solid black";
			button.style.padding = "2px";
			button.style.marginBottom = "3px";
			
			button.style.textAlign = "center";
			button.style.width = "6em";
			button.style.cursor = "pointer";
		}
		
	
		map_info = new Map_Details(map_data,task);

		markers = map_info.markers;
		

		if (longitude!=0 && latitude!=0)
		{
			
			map.setCenter(new GLatLng(latitude,longitude), map_info.get_zoom_level());
		}
		else
		{
			map.setCenter(map_info.get_centre_point(), map_info.get_zoom_level());
		}
		if (task=='home' || task=='town_edit_process')
		{
			
		}
		else
		{
			map.addControl(new TextualZoomControl());
		}
		//adding marker for each object:
		if (allow_reposition==true)
		{
			GEvent.addListener(map,"click", 
			function(overlay,point) 
			{
				map.clearOverlays();
				var icon = new GIcon(G_DEFAULT_ICON);
				icon.image = "./images/marker_selected.gif";
				map.addOverlay(new GMarker(point,icon));
				
				longitude = document.getElementById("longitude_holder");
				longitude.value = point.lng();
				latitude = document.getElementById("latitude_holder");
				latitude.value = point.lat();
			});
		}
	
				
		for (var i=0;i<markers.length;i++)
		{	
//				var point = markers[i].get_latlon();
				var marker = createMarker(markers[i]);
				
				if (marker)
				{
					mapOverlay = map.addOverlay(marker);
				}
		}
		
 	}
}

function map_update(new_markers)
{

	marker_number = 1;
	map.clearOverlays();
	map_info.markers = new_markers;
	
	map.setCenter(map_info.get_centre_point(), map_info.get_zoom_level());

	for (var i=0;i<new_markers.length;i++)
	{
		
		var point = new_markers[i].get_latlon();
	
		var marker = createMarker(new_markers[i]);
	
		if (marker)
			mapOverlay = map.addOverlay(marker);
	}
	
	
}


function allowUserUpdate(){
	allow_reposition = true;
}

var zoom = 0;
var latitude = 0;
var longitude = 0;

function set_zoom(new_zoom)
{
	zoom = new_zoom
}

function set_centre(new_lat, new_lon)
{
	latitude = new_lat;
	longitude = new_lon;
}