var t_realtime, t_history;
function update_current_data(prefix) {
    if(prefix == null) {
        prefix = "";
    }
	ajaxRealtimeFunction(prefix);
	ajaxSunAndMoonFunction(prefix);
	t_realtime = setTimeout("update_current_data(\"" + prefix + "\")", 30000);
}

function update_history_data(prefix) {
    if(prefix == null) {
        prefix = "";
    }
	ajaxRealtimeFunction(prefix);
	ajaxSunAndMoonFunction(prefix);
	updateHistory(prefix);
	t_history = setTimeout("update_history_data(\"" + prefix + "\")", 600000);
}

function instantiateRequest() {
	try { return new XMLHttpRequest() } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
	return null;
}

function handleValue(elementId, value) {
	var element = document.getElementById(elementId);
	if(element == null) {
		return;
	}
	element.innerHTML = value;
}

function handleHotColdValue(elementId, value) {
	var element = document.getElementById(elementId);
	if(element == null) {
		return;
	}
	element.innerHTML = value;
	if (value < 0) {
		element.style.color = "#00C";
	} else if (value > 0) {
		element.style.color = "#C00";
	} else {
		element.style.color = "#000";
	}
}

function ajaxRealtimeFunction(prefix) {
	var xmlHttp = instantiateRequest();
	if(xmlHttp == null) {
		throw new Error( "This browser does not support XMLHttpRequest." )
	}

    var now = new Date();
    var postfix = now.getTime(); // For stupid proxy @ vegvesenet

	xmlHttp.open("GET", prefix + "current.json?" + postfix, 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) {
			var data = eval('(' + xmlHttp.responseText + ')');

			handleValue('update_time', data["time"]);
			var tempout = data["temp_out"];
			handleHotColdValue('outsidetemperature', tempout);
			handleHotColdValue('outsidetemperatureS', tempout);
			var humout = data["hum_out"];
			handleValue('relativehumidity', humout);
			handleValue('relativehumidityS', humout);
			var dewpoint = data["dew_point"];
			handleHotColdValue('dewpoint', dewpoint);
			var windave = data["wind_ave"];
			handleValue('windspeedaverage', windave);
			handleValue('windspeedaverageS', windave);
			var rain = data["rain"];
			handleValue('raintoday', rain);
			handleValue('cloudbase', data["cloud_base"])
			var pressure = data["pressure"];
			handleValue('barometer', pressure);
			handleValue('barometerS', pressure);
			handleValue('winddirection', data["wind_dir_name"]);
			handleValue('winddirectionS', data["wind_dir_name"]);
			handleValue('windspeedbeaufort', data["wind_beaufort"]);
			handleValue('windspeedbeaufortDesk', data["wind_beaufort_name"]);
			handleValue('windspeedbeaufortDeskS', data["wind_beaufort_name"]);
			handleValue('pressuretrendname', data["pressure_trend_name"]);
			var pressuredelta1h = data["pressure_delta_1h"];
			handleHotColdValue('pressuretrendvalue', pressuredelta1h);
			handleHotColdValue('pressuretrendvalueS', pressuredelta1h);
			handleHotColdValue('pressuredelta1h', pressuredelta1h);
			handleHotColdValue('pressuredelta3h', data["pressure_delta_3h"]);
			var tempin = data["temp_in"];
			handleHotColdValue('insidetemperature', tempin);
			handleHotColdValue('insidetemperatureS', tempin);
			handleHotColdValue('insidetemperaturedelta1h', data["temp_in_delta_1h"]);
			handleHotColdValue('insidetemperaturedelta3h', data["temp_in_delta_3h"]);
			var humin = data["hum_in"];
			handleValue('insidehumidity', humin);
			handleValue('insidehumidityS', humin);
			var windchill = data["wind_chill"];
			handleHotColdValue('windchill', windchill);
			var tempoutdelta1h = data["temp_out_delta_1h"];
			handleHotColdValue('temperaturetrendvalue', tempoutdelta1h);
			handleHotColdValue('temperaturetrendvalueS', tempoutdelta1h);
			handleHotColdValue('tempoutdelta1h', tempoutdelta1h);
			handleHotColdValue('tempoutdelta3h', data["temp_out_delta_3h"]);
			var tempoutmax = data["temp_out_max"];
			handleHotColdValue('todayshightemp', tempoutmax);
			var tempoutmaxtime = data["temp_out_max_t"];
			handleValue('timeoftodayshightemp', tempoutmaxtime);
			var tempoutmin = data["temp_out_min"];
			handleHotColdValue('todayslowtemp', tempoutmin);
			var tempoutmintime = data["temp_out_min_t"];
			handleValue('timeoftodayslowtemp', tempoutmintime);
			var windavetoday = data["wind_ave_today"];
			handleValue('todayswindspeedaverage', windavetoday);
			handleValue('todayswindrun', data["wind_run_today"]);
			var windgusttoday = data["wind_gust_today"];
			handleValue('todayshighwindgust', windgusttoday);
			var windgusttodaytime = data["wind_gust_today_t"];
			handleValue('timeoftodayshighwindgust', windgusttodaytime);
			var pressuremax = data["pressure_max"];
			handleValue('todayshighpressure', pressuremax);
			var pressuremaxtime = data["pressure_max_t"];
			handleValue('timeoftodayshighpressure', pressuremaxtime);
			var pressuremin = data["pressure_min"];
			handleValue('todayslowpressure', pressuremin);
			var pressuremintime = data["pressure_min_t"];
			handleValue('timeoftodayslowpressure', pressuremintime);
			var rain1h = data["rain_1h"];
			handleValue('currentrainrate', rain1h);
		}
	}

	xmlHttp.send(null);
}

function ajaxSunAndMoonFunction(prefix) {
	var xmlHttp = instantiateRequest();
	if(xmlHttp == null) {
		throw new Error( "This browser does not support XMLHttpRequest." )
	}

    var now = new Date();
    var postfix = now.getTime(); // For stupid proxy @ vegvesenet

	xmlHttp.open("GET", prefix + "sun_moon.json?" + postfix, 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) {
		    var now = new Date();
			var data = eval('(' + xmlHttp.responseText + ')');

			handleValue('sun_up', data["sun_up"]);
			handleValue('sun_down', data["sun_down"]);
			handleValue('day_length', data["day_length"]);

			handleValue('moon_up', data["moon_up"]);
			handleValue('moon_down', data["moon_down"]);
			handleValue('moon_phase', data["moon_phase"]);
			handleValue('moon_phase_percent', data["moon_phase_percent"] + '%');
			element = document.getElementById("moon_phase_img");
			if(element != null) {
				element.src = prefix + "static/moon_phase_" + data["moon_phase_idx"] + ".jpg?" + now.getTime();
			}
		}
	}

	xmlHttp.send(null);
}

function updateHistory(prefix) {
    var now = new Date();
    var postfix = now.getTime();
    updateHistoryImage(prefix, "24_temp", postfix);
    updateHistoryImage(prefix, "24_pressure", postfix);
    updateHistoryImage(prefix, "24_wind", postfix);
    updateHistoryImage(prefix, "24__temp__in", postfix);
    updateHistoryImage(prefix, "7_temp", postfix);
    updateHistoryImage(prefix, "7_pressure", postfix);
    updateHistoryImage(prefix, "7_wind", postfix);
    updateHistoryImage(prefix, "7__temp__in", postfix);
    updateHistoryImage(prefix, "28_temp", postfix);
    updateHistoryImage(prefix, "28_pressure", postfix);
    updateHistoryImage(prefix, "28_wind", postfix);
    updateHistoryImage(prefix, "28__temp__in", postfix);
}

function updateHistoryImage(prefix, img, postfix) {
    element = document.getElementById(img + "_img");
    if(element != null) {
    	element.src = prefix + img + ".png?" + postfix;
    }
}

var c = 0;

var beaufortArray = [];
beaufortArray[0] = "Stille";
beaufortArray[1] = "Flau vind";
beaufortArray[2] = "Svak vind";
beaufortArray[3] = "Lett bris";
beaufortArray[4] = "Laber bris";
beaufortArray[5] = "Frisk bris";
beaufortArray[6] = "Liten kuling";
beaufortArray[7] = "Stiv kuling";
beaufortArray[8] = "Sterk kuling";
beaufortArray[9] = "Liten storm";
beaufortArray[10] = "Full storm";
beaufortArray[11] = "Sterk storm";
beaufortArray[12] = "Orkan";

var winddirectionArray = [];
winddirectionArray["N"] = "Nord";
winddirectionArray["NNE"] = "Nord-nordøst";
winddirectionArray["NE"] = "Nordøst";
winddirectionArray["ENE"] = "Øst-nordøst";
winddirectionArray["E"] = "Øst";
winddirectionArray["ESE"] = "Øst-sørøst";
winddirectionArray["SE"] = "Sørøst";
winddirectionArray["SSE"] = "Sør-sørøst";
winddirectionArray["S"] = "Sør";
winddirectionArray["SSW"] = "Sør-sørvest";
winddirectionArray["SW"] = "Sørvest";
winddirectionArray["WSW"] = "Vest-sørvest";
winddirectionArray["W"] = "Vest";
winddirectionArray["WNW"] = "Vest-nordvest";
winddirectionArray["NW"] = "Nordvest";
winddirectionArray["NNW"] = "Nord-nordvest";

