var t_yr;
function update_yr_data() {
	ajaxYrFunction();
	var now = new Date();
	var sleepInterval = 1000 * 60 * (61 - now.getMinutes());
	t_yr = setTimeout("update_yr_data()", sleepInterval);
}

function ajaxYrFunction() {
	var xmlHttp = instantiateRequest();
	if(xmlHttp == null) {
		throw new Error( "This browser does not support XMLHttpRequest." )
	}
	var path = "sted/Norge/Telemark/Hjartdal/Stuverud/varsel.xml";
	var url = "/minyr/yr_proxy.php?yr_path=" + encodeURIComponent(path) + "&json=1";
	xmlHttp.open("GET", url, true);
	xmlHttp.setRequestHeader("If-Modified-Since", "Sun, 1 Mar 1970 00:00:00 GMT+1"); // For stupid IE caching problem

	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status != 200) {
				throw new Error( "The response from the server is not valid." )
			}
			var now = new Date();
			var data = eval('(' + xmlHttp.responseText + ')');
			data = data['weatherdata']
			var text = "";
			// Meta/lastupdate
			var lastupdate = data['meta']['lastupdate'];
			var datetime = lastupdate.split("T");
			text += "Data oppdatert " + now.toLocaleTimeString() + ". Varsel oppdatert " + datetime[1] + ". "
			// Credit/link
			var creditTagAttributes = data['credit']['link']['@attributes']
			text += "<a href=\"" + creditTagAttributes['url'] + "\">" + creditTagAttributes['text'] + "</a>"
			handleValue('yrLink', text);
			// Textbased forecast
			var forecastTag = data['forecast'];
			var textTags = forecastTag['text']['location']['time'];
			text = "";
			for(i = 0; i < textTags.length; i++) {
				text += "<h3>" + textTags[i]['title'] + "</h3>\n";
				text += textTags[i]['body'] + "\n";
			}
			handleValue('forecastText', text);

			var timeTag = forecastTag['tabular']['time'][0];
			var symbolTag = timeTag['symbol'];
			var precipitationTag = timeTag['precipitation'];
			var windDirectionTag = timeTag['windDirection'];
			var windSpeedTag = timeTag['windSpeed'];
			var temperatureTag = timeTag['temperature'];
			var pressureTag = timeTag['pressure'];
			// Temperature
			handleHotColdValue("forecastTemperature", temperatureTag['@attributes']['value']);
			// Pressure
			handleValue('forecastPressure', pressureTag['@attributes']['value']);
			// Description
			text = symbolTag['@attributes']['name'] + ". "
				+ windSpeedTag['@attributes']['name'] + ", "
				+ windSpeedTag['@attributes']['mps'] + " m/s fra "
				+ windDirectionTag['@attributes']['name'] + ". "
				+ precipitationTag['@attributes']['value'] + " mm nedbør i perioden.";
			handleValue('forecastDescription', text);
			// Time
			var dayTag = "d";
			var period = timeTag['@attributes']['period'];
			if(period == "0") { text = "Natt"; dayTag = "n"; }
			else if(period == "1") { text = "Morgen"; }
			else if(period == "2") { text = "Dag"; }
			else if(period == "3") { text = "Kveld"; }
			handleValue('forecastTime', text);
			// Symbol
			var symbolNumber = symbolTag['@attributes']['number'];
			if(symbolNumber.length == 1) {
				symbolNumber = "0" + symbolNumber;
			}
			if(symbolNumber == 4 || (symbolNumber > 8 && symbolNumber < 16 )) {
				dayTag = "";
			}
			var element = document.getElementById("forecastSymbol");
			if(element != null) {
				element.src = "http://fil.nrk.no/yr/grafikk/sym/b38/" + symbolNumber + dayTag + ".png";
			}
			// Hour by hour image
			element = document.getElementById("yrByHourImage");
			if(element != null) {
				element.src = "http://www.yr.no/sted/Norge/Telemark/Hjartdal/Stuverud/avansert_meteogram.png?" + now.getTime();
			}
		}
	}
	xmlHttp.send(null);
}

function buildWindFilename(windspeed, windbearing) {
	var filename = "http://fil.nrk.no/yr/grafikk/vindpiler/32/vindpil.";
	if(windspeed < 1) {
		windspeed = 0;
	} else if ((windspeed >= 1) && (windspeed < 4)) {
		windspeed = 25;
	} else if ((windspeed >= 4) && (windspeed < 6)) {
		windspeed = 50;
	} else if ((windspeed >= 6) && (windspeed < 9)) {
		windspeed = 75;
	} else if ((windspeed >= 9) && (windspeed < 11)) {
		windspeed = 100;
	} else if ((windspeed >= 11) && (windspeed < 14)) {
		windspeed = 125;
	} else if ((windspeed >= 14) && (windspeed < 16)) {
		windspeed = 150;
	} else if ((windspeed >= 16) && (windspeed < 19)) {
		windspeed = 175;
	} else if ((windspeed >= 19) && (windspeed < 21)) {
		windspeed = 200;
	}
	if(windspeed < 10) {
		filename += "000" + windspeed + ".";
	} else if(windspeedCalc < 100) {
		filename += "00" + windspeed + ".";
	} else {
		filename += "0" + windspeed + ".";
	}
	if(windbearing == 360) {
		windbearing = 0;
	}
	if(windbearing < 10) {
		filename += "00" + windbearing + ".png";
	} else if(windbearing < 100) {
		filename += "0" + windbearing + ".png";
	} else {
		filename += windbearing + ".png";
	}
	return filename;
}

