// Google Map Functions
// Northern Ireland Air Quality

//Load the maps API
google.load("maps", "2");

//Define defaults
var map;

var centerLatitude = 54.67;
var centerLongitude =  -6.79;
var startZoom = 8;
var marker_tracker = '';
var marker_selected = false;
var selected_marker_array = new Array();
var standard_marker_array = new Array();
var tab_tracker = "data";

var icons = new Array();


function zoomTo(newLat, newLong, newZoom) {
	map.setCenter(new GLatLng(newLat, newLong), newZoom);
}//end function


function directZoom(latitude, longitude) {
	var direct_zoom_level = 	14;
	map.setMapType(G_SATELLITE_MAP);
	zoomTo(latitude, longitude, direct_zoom_level);
}


function panToSite(site_id) {
	$.ajax({
	   type: "GET",
	   url: "ajax_process/get_site_location.php",
	   data: "doajax=true&site_id="+site_id,
	   success: function(lat_long){
			var arrLL = lat_long.split(",");
			var newLat = arrLL[0];
			var newLong = arrLL[1];
			if (map.getZoom() == 14) {
				map.panTo(new GLatLng(newLat, newLong));
			} else {
				zoomTo(newLat, newLong, 14);
			}
	   }
	 });
}//end function

function load_site_directly(site_id, pagename) {
	if (site_id == '9999') {
		return false;	
	} else {
		var tab_index = 1;
		panToSite(site_id);
		showSiteTabs(site_id, tab_index, pagename);
		marker_tracker = site_id;

		//Simulate a click event in order to re-colour the marker.
		triggerClickEvent(site_id);
		
	}
}

function resetZoom() {
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
}//end function


function processMarkers(markerstype, pagename) {
	
	switch (markerstype) {
		default:
		case "all":
			for(id in aql_markers) {
				initializePoint(aql_markers[id], pagename);
			}//end for
			
			if (document.form_pollutantfilter) {			
				for (i=0; i<document.form_pollutantfilter.elements.length; i++) { 
					el = document.form_pollutantfilter.elements[i];
					if (el.type=="checkbox"){
						if (el.checked == false) {
							el.checked = true;	
						}
					} 
				}
			}
			if (document.form_environmentfilter) {			
				for (i=0; i<document.form_environmentfilter.elements.length; i++) { 
					el = document.form_environmentfilter.elements[i];
					if (el.type=="checkbox"){
						if (el.checked == false) {
							el.checked = true;	
						}
					} 
				}
			}
			
			break;
		//Environment Selector Values
		case "1":
		case "2": 
		case "3": 
		case "4": 
		case "5":
		case "6": 
		case "7": 
		case "8": 
		case "9": 
		case "10": 
		case "11": 
		case "12": 
				if (document.getElementById('env_'+markerstype).checked == false) {
					for(id in aql_markers) {
						if (aql_markers[id].environment_id == markerstype) {
							aql_markers[id].hide();
						}//end if
					}//end for
					
				} else {
					for(id in aql_markers) {
						if (aql_markers[id].environment_id == markerstype) {
							aql_markers[id].show();
						}//end if
					}//end for
				}//end if
	
		break;
		//Pollutant Selector Values
		case "NO2": 
		case "CO": 
		case "O3": 
		case "SO2": 
		case "GE10":
		case "PM10": 
		case "PM25": 
			for(id in aql_markers) {
						
						var array_pollutants = aql_markers[id].pollutant_id.split(",");
						
						if (aql_markers[id].pollutant_id.indexOf(markerstype) > -1) {
						
							var marker_visible = false;
							
							for (m=0; m<=array_pollutants.length; m++) { 				
								var current_pollutant = array_pollutants[m];

								if (document.getElementById('poll_'+current_pollutant)) {
									if (document.getElementById('poll_'+current_pollutant).checked == true) {
										marker_visible = true;	
									}
								}
							}//end for
							
							
							if (marker_visible == true) {
								aql_markers[id].show();
							} else {
								aql_markers[id].hide();
							}
						}//end if
						
			}//end for
	
		break;
	}
	
}

function showSiteTabs(site_id, t_action, pagename) {
		tab_tracker = t_action;
	
		$.ajax({
		   type: "GET",
		   url: "ajax_process/show_site_tabs.php",
		   data: "doajax=true&site_id="+site_id+"&t_action="+t_action+"&pagename="+pagename,
		   beforeSend:	function(){
				$("#loading_content_message").show();
		   },
		   complete: function(){
				$("#loading_content_message").fadeOut("fast");
				$('#thumbnails a').lightBox();
		   },
		   success: function(html){
			 document.getElementById('site_data').innerHTML = html;
		   }
		 });
	
}


function initializePoint(pointData, pagename) {

	var visible = false;
	var overall_index = pointData.overall_index;
	
	//get the selected site id - from the url or internal
	var site_id_url = get_selected_siteid();
	var site_id_int = marker_tracker;

	//check whether this marker should be displayed
	if (site_id_int != '' && site_id_int == pointData.site_id) {
		overall_index = overall_index+'_Selected';
		var ex_load = false;
	} else if (site_id_url != '' && site_id_url == site_id_int && site_id_url == pointData.site_id) {
		overall_index = overall_index+'_Selected';
		var ext_load = true;
	} else if (site_id_url != '' && site_id_url == pointData.site_id && site_id_int == '') {
		overall_index = overall_index+'_Selected';
		var ex_load = true;
	}//end if

	var mpoint = new GPoint(pointData.longitude, pointData.latitude);
	//var marker = new GMarker(mpoint, {icon: eval("IconMarker"+overall_index) } );
	var marker = new GMarker(mpoint, {icon: icons[overall_index],zIndexProcess:importanceOrder } );
	marker.importance = overall_index.replace("_Selected", "");
	
	//Store external load selected site
	if (ex_load == true) {
		selected_marker_array[0] = new Array();
		selected_marker_array[0]['marker'] = marker;
		selected_marker_array[0]['overall_index'] = overall_index;
	}


	var is_selected = false;

	var focusPoint = function() {
		marker_tracker = pointData.site_id;
				
		//Re-colour selected marker
		createSelectedMarker(marker, overall_index);
		is_selected = true;
		
		showSiteTabs(pointData.site_id, tab_tracker, pagename);

		return false;
	}//end function
	
	
	GEvent.addListener(marker, 'click', focusPoint);
	
	GEvent.addListener(marker, 'mouseover', function () {
		marker.openToolTip(pointData.name);
	});
	
	GEvent.addListener(marker, 'mouseout', function () {
			marker.closeToolTip();
	});

	//Add to the array
	standard_marker_array[pointData.site_id]= marker;
	//Add to the map
	map.addOverlay(marker);
	
	pointData.show = function() {
		if (!visible) {
			marker.show();
			visible = true;
		}//end if
	}//end function

	pointData.hide = function() {
		if (visible) {
			marker.hide();
			visible = false;
		}//end if
	}//end function

	pointData.show();
	
}//end function

//Z-Index Order function
function importanceOrder (marker,b) {
   return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
}

// This function picks up the click and opens the corresponding info window
function triggerClickEvent(site_id) {
	GEvent.trigger(standard_marker_array[site_id], "click");
}


function createSelectedMarker(marker, overall_index) {

	//Catch non-numeric content
	overall_index = overall_index.replace("_Selected", "");
	
	//Clear existing "selected" markers
	for (id in selected_marker_array) {
		//Reset Icon
		var restoreMarker = selected_marker_array[id]['marker'];
		var restoreIndex = selected_marker_array[id]['overall_index'];
		
		//Catch non-numeric content
		restoreIndex = restoreIndex.replace("_Selected", "");
		restoreMarker.setImage('images/mapmarkers/marker_'+restoreIndex+'.png');
	}
	selected_marker_array = new Array();
	
	selected_marker_array[0] = new Array();
	selected_marker_array[0]['marker'] = marker;
	selected_marker_array[0]['overall_index'] = overall_index;

	marker.setImage('images/mapmarkers/marker_'+overall_index+'_selected.png');

}


function changeLoadingClass(from, to) {
	if (document.getElementById('map_loading')) {
		document.getElementById('map_loading').className = document.getElementById('map_loading').className.replace(from,to);
	}
	return false;
}//end function 


function init(pagename) {

	icons['1']= new GIcon();
	icons['1'].image = 'images/mapmarkers/marker_1.png';
	icons['1'].iconSize = new GSize(16, 32);
	icons['1'].iconAnchor = new GPoint(8, 32);
	icons['1'].infoWindowAnchor = new GPoint(9, 2);

	icons['1_Selected']= new GIcon();
	icons['1_Selected'].image = 'images/mapmarkers/marker_1_selected.png';
	icons['1_Selected'].iconSize = new GSize(16, 32);
	icons['1_Selected'].iconAnchor = new GPoint(8, 32);
	icons['1_Selected'].infoWindowAnchor = new GPoint(9, 2);

	icons['2'] = new GIcon();
	icons['2'].image = 'images/mapmarkers/marker_2.png';
	icons['2'].iconSize = new GSize(16, 32);
	icons['2'].iconAnchor = new GPoint(8, 32);
	icons['2'].infoWindowAnchor = new GPoint(9, 2);

	icons['2_Selected']= new GIcon();
	icons['2_Selected'].image = 'images/mapmarkers/marker_2_selected.png';
	icons['2_Selected'].iconSize = new GSize(16, 32);
	icons['2_Selected'].iconAnchor = new GPoint(8, 32);
	icons['2_Selected'].infoWindowAnchor = new GPoint(9, 2);
	
	icons['3'] = new GIcon();
	icons['3'].image = 'images/mapmarkers/marker_3.png';
	icons['3'].iconSize = new GSize(16, 32);
	icons['3'].iconAnchor = new GPoint(8, 32);
	icons['3'].infoWindowAnchor = new GPoint(9, 2);

	icons['3_Selected']= new GIcon();
	icons['3_Selected'].image = 'images/mapmarkers/marker_3_selected.png';
	icons['3_Selected'].iconSize = new GSize(16, 32);
	icons['3_Selected'].iconAnchor = new GPoint(8, 32);
	icons['3_Selected'].infoWindowAnchor = new GPoint(9, 2);

	icons['4']  = new GIcon();
	icons['4'].image = 'images/mapmarkers/marker_4.png';
	icons['4'].iconSize = new GSize(16, 32);
	icons['4'].iconAnchor = new GPoint(8, 32);
	icons['4'].infoWindowAnchor = new GPoint(9, 2);
	
	icons['4_Selected']= new GIcon();
	icons['4_Selected'].image = 'images/mapmarkers/marker_4_selected.png';
	icons['4_Selected'].iconSize = new GSize(16, 32);
	icons['4_Selected'].iconAnchor = new GPoint(8, 32);
	icons['4_Selected'].infoWindowAnchor = new GPoint(9, 2);

	icons['5'] = new GIcon();
	icons['5'].image = 'images/mapmarkers/marker_5.png';
	icons['5'].iconSize = new GSize(16, 32);
	icons['5'].iconAnchor = new GPoint(8, 32);
	icons['5'].infoWindowAnchor = new GPoint(9, 2);
	
	icons['5_Selected']= new GIcon();
	icons['5_Selected'].image = 'images/mapmarkers/marker_5_selected.png';
	icons['5_Selected'].iconSize = new GSize(16, 32);
	icons['5_Selected'].iconAnchor = new GPoint(8, 32);
	icons['5_Selected'].infoWindowAnchor = new GPoint(9, 2);

	icons['6'] = new GIcon();
	icons['6'].image = 'images/mapmarkers/marker_6.png';
	icons['6'].iconSize = new GSize(16, 32);
	icons['6'].iconAnchor = new GPoint(8, 32);
	icons['6'].infoWindowAnchor = new GPoint(9, 2);
	
	icons['6_Selected']= new GIcon();
	icons['6_Selected'].image = 'images/mapmarkers/marker_6_selected.png';
	icons['6_Selected'].iconSize = new GSize(16, 32);
	icons['6_Selected'].iconAnchor = new GPoint(8, 32);
	icons['6_Selected'].infoWindowAnchor = new GPoint(9, 2);

	icons['7'] = new GIcon();
	icons['7'].image = 'images/mapmarkers/marker_7.png';
	icons['7'].iconSize = new GSize(16, 32);
	icons['7'].iconAnchor = new GPoint(8, 32);
	icons['7'].infoWindowAnchor = new GPoint(9, 2);

	icons['7_Selected']= new GIcon();
	icons['7_Selected'].image = 'images/mapmarkers/marker_7_selected.png';
	icons['7_Selected'].iconSize = new GSize(16, 32);
	icons['7_Selected'].iconAnchor = new GPoint(8, 32);
	icons['7_Selected'].infoWindowAnchor = new GPoint(9, 2);

	icons['8'] = new GIcon();
	icons['8'].image = 'images/mapmarkers/marker_8.png';
	icons['8'].iconSize = new GSize(16, 32);
	icons['8'].iconAnchor = new GPoint(8, 32);
	icons['8'].infoWindowAnchor = new GPoint(9, 2);

	icons['8_Selected']= new GIcon();
	icons['8_Selected'].image = 'images/mapmarkers/marker_8_selected.png';
	icons['8_Selected'].iconSize = new GSize(16, 32);
	icons['8_Selected'].iconAnchor = new GPoint(8, 32);
	icons['8_Selected'].infoWindowAnchor = new GPoint(9, 2);


	icons['9'] = new GIcon();
	icons['9'].image = 'images/mapmarkers/marker_9.png';
	icons['9'].iconSize = new GSize(16, 32);
	icons['9'].iconAnchor = new GPoint(8, 32);
	icons['9'].infoWindowAnchor = new GPoint(9, 2);

	icons['9_Selected']= new GIcon();
	icons['9_Selected'].image = 'images/mapmarkers/marker_9_selected.png';
	icons['9_Selected'].iconSize = new GSize(16, 32);
	icons['9_Selected'].iconAnchor = new GPoint(8, 32);
	icons['9_Selected'].infoWindowAnchor = new GPoint(9, 2);

	icons['10'] = new GIcon();
	icons['10'].image = 'images/mapmarkers/marker_10.png';
	icons['10'].iconSize = new GSize(16, 32);
	icons['10'].iconAnchor = new GPoint(8, 32);
	icons['10'].infoWindowAnchor = new GPoint(9, 2);

	icons['10_Selected']= new GIcon();
	icons['10_Selected'].image = 'images/mapmarkers/marker_10_selected.png';
	icons['10_Selected'].iconSize = new GSize(16, 32);
	icons['10_Selected'].iconAnchor = new GPoint(8, 32);
	icons['10_Selected'].infoWindowAnchor = new GPoint(9, 2);

	icons['0'] = new GIcon();
	icons['0'].image = 'images/mapmarkers/marker_0.png';
	icons['0'].iconSize = new GSize(16, 32);
	icons['0'].iconAnchor = new GPoint(8, 32);
	icons['0'].infoWindowAnchor = new GPoint(9, 2);

	icons['0_Selected']= new GIcon();
	icons['0_Selected'].image = 'images/mapmarkers/marker_0_selected.png';
	icons['0_Selected'].iconSize = new GSize(16, 32);
	icons['0_Selected'].iconAnchor = new GPoint(8, 32);
	icons['0_Selected'].infoWindowAnchor = new GPoint(9, 2);
	

	//-------------------------------------------------
	 //Tooltips
	//create the tooltip overlay
	function ToolTip(marker,html,width) {
		this.html_ = html;
		this.width_ = (width ? width + 'px' : 'auto');
		this.marker_ = marker;
	}
	
	ToolTip.prototype = new GOverlay();
	
	ToolTip.prototype.initialize = function(map) {
		var div = document.createElement("div");
		div.style.display = 'none';
		map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
		
		this.map_ = map;
		this.container_ = div;
	}
	
	ToolTip.prototype.remove = function() {
		this.container_.parentNode.removeChild(this.container_);
	}
	
	ToolTip.prototype.copy = function() {
		return new ToolTip(this.html_);
	}
	
	ToolTip.prototype.redraw = function(force) {
		if (!force) return;
		
		var pixelLocation = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
		this.container_.style.position = 'absolute';
		this.container_.style.left = (pixelLocation.x+15) + "px";
		this.container_.style.top = (pixelLocation.y-33) + "px";
		this.container_.style.width = this.width_;
		this.container_.style.font = 'bold 10px/10px verdana, arial, sans';
		this.container_.style.border = '1px solid black';
		this.container_.style.background = '#FFFFFF';
		this.container_.style.color= '#212063';
		this.container_.style.padding = '4px';
		this.container_.innerHTML = this.html_;
	
		//one line to desired width
		this.container_.style.whiteSpace = 'nowrap';
		if(this.width_ != 'auto') this.container_.style.overflow = 'hidden';
		this.container_.style.display = 'block';
	}
	
	GMarker.prototype.ToolTipInstance = null;
	
	GMarker.prototype.openToolTip = function(content) {
		//don't show the tool tip if there is acustom info window
		if(this.ToolTipInstance == null) {
			this.ToolTipInstance = new ToolTip(this,content)
			map.addOverlay(this.ToolTipInstance);
		}
	}
	
	GMarker.prototype.closeToolTip = function() {
		if(this.ToolTipInstance != null) {
			map.removeOverlay(this.ToolTipInstance);
			this.ToolTipInstance = null;
		}
	}
	//-------------------------------------------------

	if (GBrowserIsCompatible() && document.getElementById("google_map")) {
		map = new GMap2(document.getElementById("google_map"));
		//map.addControl(new GLargeMapControl());
		//map.addControl(new GMapTypeControl());
		map.setUIToDefault();
		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	
		processMarkers('all', pagename);
		//changeLoadingClass('loading', 'standby');
		insertResetLink();

	}
	
	
	
	
	
	
}

window.onload = init;
