// Google Map Functions
// Welsh Air Quality

google.load("maps", "2.146");
//Define defaults
var map;

var centerLatitude = 52.588;
var centerLongitude = -3.77;
var startZoom = 7;
var marker_tracker = '';

var overlays_tracker = new Array();

var icons = new Array();



function directZoom(latitude, longitude) {
	var direct_zoom_level = 	17;
	map.setMapType(G_SATELLITE_MAP);
	zoomTo(latitude, longitude, direct_zoom_level)
}


function zoomTo(newLat, newLong, newZoom) {
	map.setCenter(new GLatLng(newLat, newLong), newZoom);
}//end function

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;
		
		clearMarkers_custom();
		processMarkers('all',pagename);
	}
}

function resetZoom() {
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
}//end function


function processMarkers(markerstype, pagename) {
	for(id in aql_markers) {
		initializePoint(aql_markers[id], pagename);
	}//end for
}

function importanceOrder (marker) {
   return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
}


function showSiteTabs(site_id, t_action, pagename, lang) {
		$.ajax({
		   type: "GET",
		   url: "ajax_process/show_site_tabs.php",
		   data: "doajax=true&site_id="+site_id+"&t_action="+t_action+"&pagename="+pagename+"&lg="+lang,
		   beforeSend:	function(){
				$("#loading_content_message").show();
		   },
		   complete: function(){
				$("#loading_content_message").fadeOut("fast");
		   },
		   success: function(html){
			 document.getElementById('site_tabs').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;
	var lang =  get_querystring_lang();
	
	//check whether this marker should be displayed
	if (site_id_int != '' && site_id_int == pointData.site_id) {
		overall_index = overall_index+'selected';
	} else if (site_id_url != '' && site_id_url == site_id_int && site_id_url == pointData.site_id) {
		overall_index = overall_index+'selected';
	} else if (site_id_url != '' && site_id_url == pointData.site_id && site_id_int == '') {
		overall_index = overall_index+'selected';
	}//end if

	var mpoint = new GPoint(pointData.longitude, pointData.latitude);
	//var marker = new GMarker(mpoint, {icon: eval("icons['"+overall_index+"']")} );
	var marker = new GMarker(mpoint, {icon: eval("icons['"+overall_index+"']"),zIndexProcess:importanceOrder } );
	//marker.importance= 1;
	var mipt = overall_index.replace("selected", "");
	marker.importance= mipt*1;
	var is_selected = false;

	overlays_tracker[overlays_tracker.length] = marker;

	var focusPoint = function() {
		
		marker_tracker = pointData.site_id;
				
		//clear overlays and then re-make them 
		clearMarkers_custom();
		processMarkers('all', pagename);
		is_selected = true;

		showSiteTabs(pointData.site_id, "data", pagename, lang);
	
		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();
	});
	


	pointData.show = function() {
		if (!visible) {
			map.addOverlay(marker);
			visible = true;
		}//end if
	}//end function

	pointData.hide = function() {
		if (visible) {
			map.removeOverlay(marker);
			visible = false;
		}//end if
	}//end function
	
	pointData.show();
	
}//end function

function clearMarkers_custom() {
	for(id in overlays_tracker) {
		map.removeOverlay(overlays_tracker[id]);	
	}
}
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(21, 31);
	icons['1'].iconAnchor = new GPoint(10, 31);
	icons['1'].infoWindowAnchor = new GPoint(9, 2);
	
	icons['1selected'] = new GIcon();
	icons['1selected'].image = 'images/mapmarkers/marker_1_selected.png';
	icons['1selected'].iconSize = new GSize(21, 31);
	icons['1selected'].iconAnchor = new GPoint(10, 31);
	icons['1selected'].infoWindowAnchor = new GPoint(9, 2);
	
	
	icons['2'] = new GIcon();
	icons['2'].image = 'images/mapmarkers/marker_2.png';
	icons['2'].iconSize = new GSize(21, 31);
	icons['2'].iconAnchor = new GPoint(10, 31);
	icons['2'].infoWindowAnchor = new GPoint(9, 2);
	
	
	icons['2selected'] = new GIcon();
	icons['2selected'].image = 'images/mapmarkers/marker_2_selected.png';
	icons['2selected'].iconSize = new GSize(21, 31);
	icons['2selected'].iconAnchor = new GPoint(10, 31);
	icons['2selected'].infoWindowAnchor = new GPoint(9, 2);
	
	icons['3'] = new GIcon();
	icons['3'].image = 'images/mapmarkers/marker_3.png';
	icons['3'].iconSize = new GSize(21, 31);
	icons['3'].iconAnchor = new GPoint(10, 31);
	icons['3'].infoWindowAnchor = new GPoint(9, 2);
	
	icons['3selected'] = new GIcon();
	icons['3selected'].image = 'images/mapmarkers/marker_3_selected.png';
	icons['3selected'].iconSize = new GSize(21, 31);
	icons['3selected'].iconAnchor =new GPoint(10, 31);
	icons['3selected'].infoWindowAnchor = new GPoint(9, 2);
	
	
	icons['4'] = new GIcon();
	icons['4'].image = 'images/mapmarkers/marker_4.png';
	icons['4'].iconSize = new GSize(21, 31);
	icons['4'].iconAnchor = new GPoint(10, 31);
	icons['4'].infoWindowAnchor = new GPoint(9, 2);
	
	icons['4selected'] = new GIcon();
	icons['4selected'].image = 'images/mapmarkers/marker_4_selected.png';
	icons['4selected'].iconSize = new GSize(21, 31);
	icons['4selected'].iconAnchor = new GPoint(10, 31);
	icons['4selected'].infoWindowAnchor = new GPoint(9, 2);
	
	icons['5'] = new GIcon();
	icons['5'].image = 'images/mapmarkers/marker_5.png';
	icons['5'].iconSize = new GSize(21, 31);
	icons['5'].iconAnchor = new GPoint(10, 31);
	icons['5'].infoWindowAnchor = new GPoint(9, 2);
	
	
	icons['5selected'] = new GIcon();
	icons['5selected'].image = 'images/mapmarkers/marker_5_selected.png';
	icons['5selected'].iconSize = new GSize(21, 31);
	icons['5selected'].iconAnchor = new GPoint(10, 31);
	icons['5selected'].infoWindowAnchor = new GPoint(9, 2);


	icons['6'] = new GIcon();
	icons['6'].image = 'images/mapmarkers/marker_6.png';
	icons['6'].iconSize = new GSize(21, 31);
	icons['6'].iconAnchor = new GPoint(10, 31);
	icons['6'].infoWindowAnchor = new GPoint(9, 2);
	
	
	icons['6selected'] = new GIcon();
	icons['6selected'].image = 'images/mapmarkers/marker_6_selected.png';
	icons['6selected'].iconSize = new GSize(21, 31);
	icons['6selected'].iconAnchor = new GPoint(10, 31);
	icons['6selected'].infoWindowAnchor = new GPoint(9, 2);
	icons['6selected'].shadow = "images/mapmarkers/shadow.png";
    icons['6selected'].shadowSize = new GSize(52, 29);
	
	icons['7'] = new GIcon();
	icons['7'].image = 'images/mapmarkers/marker_7.png';
	icons['7'].iconSize = new GSize(21, 31);
	icons['7'].iconAnchor = new GPoint(10, 31);
	icons['7'].infoWindowAnchor = new GPoint(9, 2);
	
	
	icons['7selected'] = new GIcon();
	icons['7selected'].image = 'images/mapmarkers/marker_7_selected.png';
	icons['7selected'].iconSize = new GSize(21, 31);
	icons['7selected'].iconAnchor = new GPoint(10, 31);
	icons['7selected'].infoWindowAnchor = new GPoint(9, 2);


	icons['8'] = new GIcon();
	icons['8'].image = 'images/mapmarkers/marker_8.png';
	icons['8'].iconSize = new GSize(21, 31);
	icons['8'].iconAnchor = new GPoint(10, 31);
	icons['8'].infoWindowAnchor = new GPoint(9, 2);
	
	
	icons['8selected'] = new GIcon();
	icons['8selected'].image = 'images/mapmarkers/marker_8_selected.png';
	icons['8selected'].iconSize = new GSize(21, 31);
	icons['8selected'].iconAnchor = new GPoint(10, 31);
	icons['8selected'].infoWindowAnchor = new GPoint(9, 2);


	icons['9'] = new GIcon();
	icons['9'].image = 'images/mapmarkers/marker_9.png';
	icons['9'].iconSize = new GSize(21, 31);
	icons['9'].iconAnchor = new GPoint(10, 31);
	icons['9'].infoWindowAnchor = new GPoint(9, 2);
	
	
	icons['9selected'] = new GIcon();
	icons['9selected'].image = 'images/mapmarkers/marker_9_selected.png';
	icons['9selected'].iconSize = new GSize(21, 31);
	icons['9selected'].iconAnchor = new GPoint(10, 31);
	icons['9selected'].infoWindowAnchor = new GPoint(9, 2);

	icons['10'] = new GIcon();
	icons['10'].image = 'images/mapmarkers/marker_10.png';
	icons['10'].iconSize = new GSize(21, 31);
	icons['10'].iconAnchor = new GPoint(10, 31);
	icons['10'].infoWindowAnchor = new GPoint(9, 2);
	
	
	icons['10selected'] = new GIcon();
	icons['10selected'].image = 'images/mapmarkers/marker_10_selected.png';
	icons['10selected'].iconSize = new GSize(21, 31);
	icons['10selected'].iconAnchor = new GPoint(10, 31);
	icons['10selected'].infoWindowAnchor = new GPoint(9, 2);


	icons['0'] = new GIcon();
	icons['0'].image = 'images/mapmarkers/marker_0.png';
	icons['0'].iconSize = new GSize(21, 31);
	icons['0'].iconAnchor = new GPoint(10, 31);
	icons['0'].infoWindowAnchor = new GPoint(9, 2);
	
	icons['0selected'] = new GIcon();
	icons['0selected'].image = 'images/mapmarkers/marker_0_selected.png';
	icons['0selected'].iconSize = new GSize(21, 31);
	icons['0selected'].iconAnchor = new GPoint(10, 31);
	icons['0selected'].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_.innerHTML = this.html_;
		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 = '#FFFF99';
		this.container_.style.padding = '4px';
	
		//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) {
		if(this.ToolTipInstance == null) {
			this.ToolTipInstance = new ToolTip(this,content)
			overlays_tracker[overlays_tracker.length] = this.ToolTipInstance;
			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 google.maps.Map2(document.getElementById("google_map"));        
		map.setCenter(new google.maps.LatLng(centerLatitude, centerLongitude), startZoom);  
		//map.addControl(new GLargeMapControl());
		//map.addControl(new GMapTypeControl());

		
		var customUI = map.getDefaultUI();
		customUI.zoom.scrollwheel = true;
		customUI.zoom.doubleclick = true;
		customUI.maptypes.physical= true;
		customUI.controls.maptypecontrol  = true;
		customUI.controls.largemapcontrol3d  = true;
		customUI.controls.menumaptypecontrol = false;
		map.setUI(customUI);
		
		
		processMarkers('all', pagename);
	}
	
	
	
	
	
	
}

