/**
 * @author avogadro
 * @author http://www.quirksmode.org/js/cookies.html
 */
var xmlFileUrl_Meteo = '/tools1024/includes/3a_colonna_boxmeteo/xml/meteo.xml';

$(function() {
	getMeteo(xmlFileUrl_Meteo,'milano',changeHtml);
	$('#meteotrigger').click( function() {
		changeLocalita($('select#select-localita option:selected').val());
		$('#meteolayer').addClass('hidden');
		return false;
	});
});

function MeteoBox() {
	this.location;
	this.weatherString;
	this.weatherImg;
	this.tempString;
	this.humidityString;
}
function changeLocalita(localita) {
	$.cookie('gazMeteo', localita, { path: '/', domain: 'gazzetta.it' });
	loadMeteoXml(xmlFileUrl_Meteo,localita,changeHtml);
}
function getMeteo(xmlFileUrl_Meteo, defaultDestinationName, changeHtmlFunction) {

	var dest = $.cookie('gazMeteo');
	if (!dest) {
		dest = defaultDestinationName;
	} 
	
	loadMeteoXml(xmlFileUrl_Meteo,dest,changeHtmlFunction)
	
}

function loadMeteoXml(xmlFileUrl_Meteo, destinationName, changeHtmlFunction){

	jQuery.get(xmlFileUrl_Meteo, 
			null, 
			function(xmlData,textStatus) { 
				parseMeteoXml(xmlData,textStatus,destinationName,changeHtmlFunction); 
			}, 
			"xml");								
}

function parseMeteoXml(xmlData, textStatus, destinationName, changeHtmlFunction) {
	
	var mb = new MeteoBox();
			
	if (textStatus == "success") {
	
		var dest = jQuery("loc[id='" + destinationName+"']", xmlData);

		if (dest.size() == 1) {
			
			mb.location = destinationName;											
			mb.weatherString = dest.find("tempo").text();
			mb.weatherImg = mb.weatherString.replace(" ","_") + ".png";
			mb.tempString = dest.find("temperatura > misurata").text();
			mb.humidityString = dest.find("umidita").text();	
      

      									
									
			if (mb.tempString != "NP") {	
				mb.tempString = mb.tempString + "&deg;C";
			}
			else {	
				mb.tempString = "ND";
			}
									
			if (mb.humidityString != "NP") {	
				mb.humidityString = mb.humidityString + "%";
			}
			else {	
				mb.humidityString = "ND";
			}
			
			changeHtmlFunction(mb);
								
		}	// if found destination.
	} // if load ok.

}
		

function changeHtml(meteoBox) {
	$("#localita").html(meteoBox.location);
	var img = meteoBox.weatherString.replace(" ", "_");
	if ($("body#sportweek").length > 0) {
		$("#wheather").attr("src","http://images.gazzettaobjects.it/libs/css/default_theme/assets/meteo/sportweek/" + img + ".png").css("display","block");
	}
	else {
		$("#wheather").attr("src","http://images.gazzettaobjects.it/libs/css/default_theme/assets/meteo/" + img + ".png").css("display","block");
	}
	$("#temperatura").html(meteoBox.tempString);	
	$("#umidita").html(meteoBox.humidityString);		
}

function changeHtmlSquadra(meteoBox) {
	$("#meteo-mappe .local").html(meteoBox.location);
	var img = meteoBox.weatherString.replace(" ", "_");
	$("#meteo-mappe #img-meteo").attr("src","http://images.gazzettaobjects.it/libs/css/default_theme/assets/meteo/" + img + ".png").css("display","block");
	$("#meteo-mappe .temperatura").html(meteoBox.tempString);	
	$("#meteo-mappe .umidita").html(meteoBox.humidityString);		

}

		