//onload function
function WindowOnload(f) {
	var prev=window.onload;
	window.onload=function(){ if(prev)prev(); f(); }
}

function load() {
	if (GBrowserIsCompatible()) {
		//Map definition
		var content = new Array();
		var map = new GMap2(document.getElementById("axa_map"));
		// Set map center
		map.setCenter(new GLatLng(36.78591667994276, -4.164676666259766), 12, G_SATELLITE_MAP);
		
		// Change to Hybrid (Types: G_NORMAL_MAP or G_SATELLITE_MAP or G_HYBRID_MAP)
		map.setMapType(G_HYBRID_MAP);
		
		// CAdd map controls
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		//map.enableScrollWheelZoom();
		
		// Show position of the map center (below the map)
		//GEvent.addListener(map, "moveend", function() {
		//  var center = map.getCenter();
		//  document.getElementById("message").innerHTML = center.toString();
		//});
		
		// ************************************************************************
		
		//Add clean label
		function add_label(id, name, lat, lng ) {	
			var content = '<a href="http://www.axaragua.com/Información/tiempo'+id+'" style="text-decoration: none;" target="_new"><div class="widget" id="'+id+'"><h1>'+name+'</h1><span class="temp">Temperatura <span class="value">x</span></span><span class="sensacion">Sensación<span class="value">---</span></span><span class="wind">Viento <span class="value">x</span><div class="arrow"><img class="grades" src="img/arrow.gif"></div></span><span class="humidity">Humedad <span class="value">x</span></span><span class="qrain">Pluviómetro <span class="value">x</span></span></div></a>';
			
			eval ("var label"+id+"= new TLabel()" );
			eval ("label"+id).id = 'label'+id;
			eval ("label"+id).anchorLatLng = new GLatLng (lat,lng);
			eval ("label"+id).anchorPoint = 'bottomCenter';
			eval ("label"+id).content = content;
			eval ("label"+id).percentOpacity = 100;
			
			map.addTLabel(eval ( "label"+id));
		}
		
		$.get("getdata.php",{question:"position"},function(data) {
			results=data.split("#");
			for (var i = 0; i < results.length; i++) {
				tuple=results[i].split(",");
				id=tuple[0];
				name=tuple[1];
				x=tuple[2];
				y=tuple[3];
				add_label(id,name,x,y);
			}
		});
		
		//Refresh data in label
		function refresh_label(id, wspeed, dwind, humidity, qrain, temp ) {
			var arrow = "img/arrow"+dwind+".png";
			if( jQuery.browser.msie && (jQuery.browser.version <= 7) ) {			
				var arrow = "img/arrow"+dwind+".gif";
			}
			if (temp!="") {
				$("#"+id).children(".temp").children(".value").empty();
				$("#"+id).children(".temp").children(".value").append(temp+"&deg;C");
				// Calculate the temperature feeling
				if ((temp<=27) || (humidity<=40)) {
					if (temp<=10) {
						if (wspeed!="") {
							// New Wind Chill (http://www.tutiempo.net/silvia_larocca/Temas/nuevo_windchill.htm)
							sensacion=13.12 + 0.6215 * temp - 11.37 * Math.pow(wspeed,0.16) + 0.3965 * temp * Math.pow(wspeed,0.16);
							sensacion=sensacion.toFixed(2);
						}
					} else {
						sensacion=temp;
					}
				} else {
					if (humidity!="") {
						// Heat Index (http://www.tutiempo.net/silvia_larocca/Temas/Met21.htm)
						tempf= 1.80 * temp + 32;
						c1 = -42.379;
						c2 =   2.04901523;
						c3 =  10.14333127;
						c4 =  -0.22475541;
						c5 =  -6.83783   * Math.pow(10,-3);
						c6 =  -5.481717  * Math.pow(10,-2);
						c7 =   1.22874   * Math.pow(10,-3);
						c8 =   8.5282    * Math.pow(10,-4);
						c9 =  -1.99      * Math.pow(10,-6);
						heat_index=c1+c2*tempf+c3*humidity+c4*tempf*humidity+c5*Math.pow(tempf,2)+c6*Math.pow(humidity,2)+c7*Math.pow(tempf,2)*humidity+c8*tempf*Math.pow(humidity,2)+c9*Math.pow(tempf,2)*Math.pow(humidity,2);
						sensacion_termica=(heat_index-32)*0.556;
						sensacion=sensacion_termica.toFixed(2);
					}
				}
				// Show the temperature feeling
				$("#"+id).children(".sensacion").children(".value").empty();
				$("#"+id).children(".sensacion").children(".value").append(sensacion+"&deg;C");
			} else {
				sensacion="---";
			}
			if (wspeed!="") {
				$("#"+id).children(".wind").children(".value").empty();
				$("#"+id).children(".wind").children(".value").append(wspeed+" km/h");
			}
			if (dwind!="") {
				$("#"+id).children().children(".arrow").children().rotate(dwind,'abs');
			}
			if (humidity!="") {
				$("#"+id).children(".humidity").children(".value").empty();
				$("#"+id).children(".humidity").children(".value").append(humidity+" %");
			}
			if (qrain!="") {
				$("#"+id).children(".qrain").children(".value").empty();
				$("#"+id).children(".qrain").children(".value").append(qrain+" mm/h");
			}
		}
		
		//Get data every N seconds
		function refresh_map() {
			$.get("getdata.php",{question:"data"}, function(data){
			results=data.split("#");
			for (var i = 0; i < results.length; i++) {
				tuple=results[i].split(",");
				id=tuple[0];
				speed=tuple[1];
				direction=tuple[2];
				humidity=tuple[3];
				rain=tuple[4];
				temperature=tuple[5];
				refresh_label(id,speed,direction,humidity,rain,temperature);
			}
			});
		}
		refresh_map(); //do refresh on page load
		diagram_interval = setInterval(refresh_map,1000); //change numerical number to chang refresh time
	}
}

// Load GMaps on page load
WindowOnload( load );
