var xhr = createXHR();

function createXHR(){
	var xhr;
	try {
		xhr = new XMLHttpRequest();
	} catch(e) { //pro případ starší verze prohlížeče
		var MSXmlVerze = new Array('MSXML2.XML.Http.6.0','MSXML2.XML.Http.5.0','MSXML2.XML.Http.4.0','MSXML2.XML.Http.3.0','MSXML2.XML.Http.2.0','Microsoft.XML.Http');
		for(var i = 0; i < MSXmlVerze.lenght; i ++) {
			try {
				xhr = new ActiveXObject(MSXmlVerze[i]);
				alert(MSXmlVerze[i]);
			} catch(e) {
				//vzniklou chybu ignoruji a pokračuji nastavením další verze
			}
		}
	}
	if(!xhr) {
		alert("Došlo k chybě při vytváření objektu XMLHttpRequest!");
	} else {
		return xhr;
	}
}


function readXMLfile(file, replyHandler) {
	if(xhr){
		try {
			xhr.open("GET",file,true);
			xhr.onreadystatechange = replyHandler;
			xhr.send(null);
		} catch(e) {
			alert("Nelze se připojik k serveru:\n" + e.toString());
		}
	} else {
		alert("Funkce \"readXMLfile()\": chybí objekt XMLHttpRequest");
	}
}


function WeatherReplyHandler(){
	if(xhr.readyState == 4) {
		if(xhr.status == 200) {
			try {
				var XMLRes = xhr.responseXML;
				//zachycení chyb IE a Opery
				if(!XMLRes || !XMLRes.documentElement) {
					throw("Chybná struktura XML:\n"+xhr.responseText);
				}
				//zachycení chyb ohnivé lišky :-)
				var rootNodeName = XMLRes.documentElement.nodeName;
				if(rootNodeName == "parsereerror"){
					throw("Chybná struktura XML:\n"+xhr.responseText);
				}
				//čtu dokument, jelikož je vše ok :-)

				xmlRoot = XMLRes.documentElement;

				var xml_nodes=new Array("date","hour","temperature","wind_speed","wind_direction","rainfall","atm_pressure","atm_pressure_computed","max_wind_impact","humidity");
				for(i=0; i<xml_nodes.length; i++) {
					xml_node_data = xmlRoot.getElementsByTagName(xml_nodes[i]);
					span = document.getElementById("maruska_weatherinfo_" + xml_nodes[i]);
					if(span) {
						span.innerHTML = xml_node_data.item(0).firstChild.data;
					};
				};

			} catch(e) {
				alert("Chyba při čtení odpovědi:"+e.toString());
			}
		} else {
			alert("Požadavek HTTP není v pořádku.")
		}
	};
};

function UpdateWeatherInfo() {
	readXMLfile("_local/objects/U_MARUSKA_WEATHERINFO/maruska_weatherinfo_xml.php?", WeatherReplyHandler);
	window.setTimeout("UpdateWeatherInfo();", 1000 * 60 * 5);
};

//UpdateWeatherInfo();

