// Google Map Functions
//Aragon Air Quality

google.load("maps", "2",{"language" : "es"});
//Define defaults
var map;
var gx;
var zx;

var other_markers;

var centerLatitude = 41.599013;
var centerLongitude = -0.758057;
var startZoom = 7;
var marker_tracker = '';
var tab_tracker = "data";
var currentMarkerType = '';

var overlays_tracker = new Array();

var icons = new Array();


function directZoom(latitude, longitude) {
	var direct_zoom_level = 	16;
	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) {
	if (site_id == '9999') {
		return false;	
	} else {
		var tab_index = 1;
		panToSite(site_id);
		showSiteTabs(site_id, tab_index);
		marker_tracker = site_id;
		
		clearMarkers_custom();
		addGreyRegionOverlay();
		processMarkers('all');
	}
}

function resetZoom() {
	map.setMapType(G_NORMAL_MAP);
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
}//end function


function processMarkers(markerstype) {
	
	switch (markerstype) {
		case "all":
		default:
			for(id in aql_markers) {
				initializePoint(aql_markers[id]);
			}//end for
			for(id in other_markers) {
				initializeOtherPoint(other_markers[id]);
			}//end for
			break;
		case "aql":
			for(id in aql_markers) {
				initializePoint(aql_markers[id]);
			}//end for
			break;
		case "other":
			for(id in other_markers) {
				initializeOtherPoint(other_markers[id]);
			}//end for
			break;
	}


}


function showSiteTabs(site_id, t_action, pref) {
	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+"&pref="+pref,
		   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) {

	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';
	} 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 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');
		is_selected = true;

		showSiteTabs(pointData.site_id, tab_tracker);
		//marker.openToolTip(pointData.name);
	
		return false;
	}//end function
	
	GEvent.addListener(marker, 'click', focusPoint);
	
	GEvent.addListener(marker, 'mouseover', function () {
		var tooltip_index = overall_index.replace('selected', '');
		currentMarkerType = 'aql';
		marker.openToolTip(pointData.name+' (Índice '+tooltip_index+')', 'aql');
	});
	
	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 initializeOtherPoint(pointData) {

	var visible = false;

	var mpoint = new GPoint(pointData.longitude, pointData.latitude);
	var marker = new GMarker(mpoint, {icon: icons['other'] } );
	var is_selected = false;

	overlays_tracker[overlays_tracker.length] = marker;

	GEvent.addListener(marker, 'mouseover', function () {
		currentMarkerType = 'other';
		marker.openToolTip(pointData.name, 'other');
	});
	
	GEvent.addListener(marker, 'mouseout', function () {
		marker.closeToolTip();
	});
	
	GEvent.addListener(marker, 'click', function () {
		document.location = 'site_information.php?site_id='+pointData.site_id;
	});


	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() {

	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['1'].shadow = "images/mapmarkers/shadow.png";
    icons['1'].shadowSize = new GSize(52, 29);
	
	icons['1selected'] = new GIcon();
	icons['1selected'].image = 'images/mapmarkers/marker_1_selected.png';
	icons['1selected'].iconSize = new GSize(25, 40);
	icons['1selected'].iconAnchor = new GPoint(12.5, 40);
	icons['1selected'].infoWindowAnchor = new GPoint(9, 2);
	icons['1selected'].shadow = "images/mapmarkers/shadow.png";
    icons['1selected'].shadowSize = new GSize(52, 29);
	
	
	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['2'].shadow = "images/mapmarkers/shadow.png";
    icons['2'].shadowSize = new GSize(52, 29);
	
	
	icons['2selected'] = new GIcon();
	icons['2selected'].image = 'images/mapmarkers/marker_2_selected.png';
	icons['2selected'].iconSize = new GSize(25, 40);
	icons['2selected'].iconAnchor = new GPoint(12.5, 40);
	icons['2selected'].infoWindowAnchor = new GPoint(9, 2);
	icons['2selected'].shadow = "images/mapmarkers/shadow.png";
    icons['2selected'].shadowSize = new GSize(52, 29);
	
	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['3'].shadow = "images/mapmarkers/shadow.png";
    icons['3'].shadowSize = new GSize(52, 29);
	
	icons['3selected'] = new GIcon();
	icons['3selected'].image = 'images/mapmarkers/marker_3_selected.png';
	icons['3selected'].iconSize = new GSize(25, 40);
	icons['3selected'].iconAnchor =new GPoint(12.5, 40);
	icons['3selected'].infoWindowAnchor = new GPoint(9, 2);
	icons['3selected'].shadow = "images/mapmarkers/shadow.png";
    icons['3selected'].shadowSize = new GSize(52, 29);
	
	
	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['4'].shadow = "images/mapmarkers/shadow.png";
    icons['4'].shadowSize = new GSize(52, 29);
	
	icons['4selected'] = new GIcon();
	icons['4selected'].image = 'images/mapmarkers/marker_4_selected.png';
	icons['4selected'].iconSize = new GSize(25, 40);
	icons['4selected'].iconAnchor = new GPoint(12.5, 40);
	icons['4selected'].infoWindowAnchor = new GPoint(9, 2);
	icons['4selected'].shadow = "images/mapmarkers/shadow.png";
    icons['4selected'].shadowSize = new GSize(52, 29);
	
	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['5'].shadow = "images/mapmarkers/shadow.png";
    icons['5'].shadowSize = new GSize(52, 29);
	
	
	icons['5selected'] = new GIcon();
	icons['5selected'].image = 'images/mapmarkers/marker_5_selected.png';
	icons['5selected'].iconSize = new GSize(25, 40);
	icons['5selected'].iconAnchor = new GPoint(12.5, 40);
	icons['5selected'].infoWindowAnchor = new GPoint(9, 2);
	icons['5selected'].shadow = "images/mapmarkers/shadow.png";
    icons['5selected'].shadowSize = new GSize(52, 29);
	
	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['0'].shadow = "images/mapmarkers/shadow.png";
    icons['0'].shadowSize = new GSize(52, 29);
	
	icons['0selected'] = new GIcon();
	icons['0selected'].image = 'images/mapmarkers/marker_0_selected.png';
	icons['0selected'].iconSize = new GSize(25, 40);
	icons['0selected'].iconAnchor = new GPoint(12.5, 40);
	icons['0selected'].infoWindowAnchor = new GPoint(9, 2);
	icons['0selected'].shadow = "images/mapmarkers/shadow.png";
    icons['0selected'].shadowSize = new GSize(52, 29);

	icons['other'] = new GIcon();
	icons['other'].image = 'images/mapmarkers/marker_other.png';
	icons['other'].iconSize = new GSize(20, 20);
	icons['other'].iconAnchor = new GPoint(10, 20);
	icons['other'].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.font = 'bold 10px/10px verdana, arial, sans';
		this.container_.style.width = this.width_;
		this.container_.style.border = '1px solid black';
		this.container_.style.padding = '4px';

		if (currentMarkerType == 'aql') {
			this.container_.style.left = (pixelLocation.x+19) + "px";
			this.container_.style.top = (pixelLocation.y-33) + "px";
			this.container_.style.background = '#FFFF99';
		} else {
			this.container_.style.left = (pixelLocation.x+10) + "px";
			this.container_.style.top = (pixelLocation.y-30) + "px";
			this.container_.style.color = '#FFFFFF';
			this.container_.style.background = '#0066CC';
		}
		//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, type) {
		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= false;
		customUI.controls.maptypecontrol  = true;
		customUI.controls.largemapcontrol3d  = true;
		customUI.controls.menumaptypecontrol = false;
		map.setUI(customUI);
		
		gx = new GGeoXml("http://www.aragonaire.es/spain_overlay.kmz");

		zx = new GGeoXml("http://www.aragonaire.es/zaragoza_overlay.kmz");
	
		addGreyRegionOverlay();

		processMarkers('all');
	}
	
	
	
	
	
	
}

function addGreyRegionOverlay() {
	//Add Overlay
	map.addOverlay(gx);
	map.addOverlay(zx);
}

