//
// copyright Xatcom.net
// All rights reserved
// Copy or modification not allowed
//
	var TMnav = (navigator.appName).search("Microsoft");

	var TMicones = new Array(17);
	var TMmapa = new Image();
	var TMloading = new Image();
	var TMItem = 0;
	
	TMloading.src = "imatges/loading.gif";
	
	function TMCarregaImatges() {
		for (i=0; i<17; i++ ) {
			TMicones[i] = new Image();
			TMicones[i].src = "meteo/mapa/" + i + ".png";
		} //FOR
		TMmapa.src = "meteo/mapa/" + "mapaSP.jpg";
	} //Function
	
	function TMCheckStart () {
		if ( !TMloading.complete ) {
			//en espera
			setTimeout("TMCheckStart()", 100);
		} else {
			//"Loading" carregat!
			CreaTXTloading();
			TMCarregaImatges();
			TMCheckImatges();
		} //IF_ELSE
	} //Function

	function TMCheckImatges () {
		var flagOK = 1;
		var i = 0;
		var l = TMicones.length;

		//comprova si les icones estàn carregades
		for (i=0; i<l; i++) {
			if ( !TMicones[i].complete ) {
				flagOK = 0; //la imatge encara no està carregada
			} //IF
		} //FOR

		//comprova si el mapa està carregat
		if ( !TMmapa.complete ) {
			flagOK = 0; //la imatge encara no està carregada
		} //IF

		if ( flagOK==0 ) {
			//En espera...
			setTimeout("TMCheckImatges()", 100);
		} else {
			//Imatges carregades!
			CreaMapa();
		} //IF_ELSE
	} //Function

	function CreaTXTloading() {
		var auxtxt = new Array(2);
		auxtxt[0] = "CARGANDO MAPA";
		auxtxt[1] = "Por favor, espere...";
		var NewObj = document.createElement("div");
		var NewImg = document.createElement("img");

		NewObj.setAttribute("id", "TXTloading"); //configura el nom de la capa "Loading"
		NewObj.appendChild(document.createElement("br")); //afegiex un salt de linia
		NewObj.appendChild(document.createElement("br")); //afegiex un salt de linia
		NewObj.appendChild(document.createTextNode(auxtxt[0])); //afegiex el text al paràgraf
		NewObj.appendChild(document.createElement("br")); //afegiex un salt de linia
		NewObj.appendChild(document.createTextNode(auxtxt[1])); //afegiex el text al paràgraf
		NewObj.appendChild(document.createElement("br")); //afegiex un salt de linia
		NewObj.appendChild(document.createElement("br")); //afegiex un salt de linia
		NewImg.src = TMloading.src; //configura la imatge

		NewObj.appendChild(NewImg); //afegeix la imatge al paràgraf

		document.getElementById("mapatemps").appendChild(NewObj);
	} //Function

	function CreaLlegenda() {
		var NewObj = document.createElement("div");

		NewObj.setAttribute("id", "TMllegenda");
		if ( TMnav == -1 ) {
			NewObj.setAttribute("class", "TMllegenda");
		} else {
			NewObj.setAttribute("className", "TMllegenda");
			//NewObj.setAttribute("style", "opacity:0.5;");
			//NewObj.style.position = "absolute";
		}

		document.getElementById("mapatemps").appendChild(NewObj);
	} //Function

	var TMtransparencia = 0;
	var TMincrtrans = 0.1;
	var TMeffecton = 0; //0:stop, 1:start 1:apareix, 2:desapareix
	var TMeffectdir = 2; //1:apareix, 2:desapareix
	var Tespera;
	var Tbaixa;
	var Tpuja;

	function LlegendaDesapareix() {
		var imgobj;
		TMeffecton = 1;
		TMtransparencia = TMtransparencia - TMincrtrans;
		if ( TMnav == -1 )
		{
			imgobj = document.getElementById('TMllegenda');
			imgobj.style.MozOpacity = TMtransparencia;
		} else {
			imgobj = document.all.TMllegenda;
			imgobj.filters.alpha.opacity = TMtransparencia*100;
		}
		if (TMtransparencia>0) { 
			Tbaixa = setTimeout("LlegendaDesapareix()", 70);
		} else {
			TMeffecton = 0;
		}
	} //Function

	function LlegendaApareix() {
		var imgobj;
		TMeffecton = 1;
		TMtransparencia = TMtransparencia + TMincrtrans;
		if ( TMnav == -1 )
		{
			imgobj = document.getElementById('TMllegenda');
			imgobj.style.MozOpacity = TMtransparencia;
		} else {
			imgobj = document.all.TMllegenda;
			imgobj.filters.alpha.opacity = TMtransparencia*100;
		}
		if (TMtransparencia<1) { 
			Tpuja = setTimeout("LlegendaApareix()", 70);
		} else {
			Tespera = setTimeout("LlegendaDesapareix()", 3000);
			TMeffecton = 0;
			TMeffectdir = 2;
		}
	} //Function

	function MostraLlegenda() {
		if ( TMeffecton==0 && TMeffectdir==2 ) {
			//parat baix
			TMeffectdir = 1;
			TMeffecton = 1;
			window.clearTimeout(Tespera);
			window.clearTimeout(Tbaixa);
			LlegendaApareix();
		} else if ( TMeffecton==1 && TMeffectdir==2 ) {
			//baixant
			TMeffectdir = 1;
			TMeffecton = 1;
			window.clearTimeout(Tbaixa);
			window.clearTimeout(Tespera);
			LlegendaApareix();
		}  else if ( TMeffecton==0 && TMeffectdir==2 ) {
			//parat dalt
			window.clearTimeout(Tespera);
			Tespera = setTimeout("LlegendaDesapareix()", 2000);
		}
	}

	function DadesLlegenda(location, temp, wind, presion, estadocielo) {
		var llegenda = document.getElementById("TMllegenda");
		llegenda.innerHTML = "<b>" + location + "</b><br>" + estadocielo + "<br>Temp: " + temp + "<br>Viento: " + wind + "<br>Presión Atm: " + presion;
		MostraLlegenda();
	}

	function GetRelativePos(obj, e) {
		x = e.offsetX?(e.offsetX):e.pageX-document.getElementById(obj).offsetLeft;
		y = e.offsetY?(e.offsetY):e.pageY-document.getElementById(obj).offsetTop;
		alert(x+","+y);
	}

	function CreaMapa() {
		var NewObj = document.createElement("img");
		var TXTloading = document.getElementById("TXTloading");
		
		//Elimina el text de "Loading..."
		document.getElementById("mapatemps").removeChild(TXTloading);
		
		//Crea la imatge del mapa
		NewObj.setAttribute("name","imgmapa");
		NewObj.setAttribute("id", "imgmapa");
		NewObj.src = TMmapa.src;
		
		//retorna la possició
		if ( TMnav==-1 ) {
			//NewObj.setAttribute("onmousedown", "GetRelativePos('mapatemps', event)");
		} else {
			//NewObj.detachEvent("onmousemove",  function (e) {} );
			//NewObj.attachEvent("onmousedown", function (e){ GetRelativePos('mapatemps', e); } );
		}
		
		document.getElementById("mapatemps").appendChild(NewObj);
		
		//Llegenda
		CreaLlegenda();
		
		//Afegeix llocs
		AfegeixLlocs();
	} //Function

	function AddItem(x, y, icon, location, temp, wind, presion, estadocielo) {
	
		if ( icon=="unknown" ) { icon=99; } //icona interrogant
	
		var NewObj = document.createElement("div");
		var NewImg = document.createElement("img");
		var imgW = TMicones[icon].width;
		var imgH = TMicones[icon].width;
		
		NewObj.setAttribute("id", "item"+TMItem); //Configura la capa de l'element
		NewObj.style.visibility = "visible";
		NewObj.style.width = imgW + "px";
		NewObj.style.height = imgH + "px";
		NewObj.style.position = "absolute";
		NewObj.style.left = (x*1 - (imgW/2)) + "px";
		NewObj.style.top = (y*1 - (imgH/2)) + "px";
		NewObj.style.cursor = "pointer";
		NewImg.src = TMicones[icon].src; //Configura la imatge de l'element
		
		//Crea el event de Click
		if ( TMnav==-1 ) {
			NewObj.setAttribute("onmousedown", "DadesLlegenda('" + location + "','" + temp + "','" + wind + "','" + presion + "','" + estadocielo + "')");
		} else {
			NewObj.attachEvent("onmousedown", function (e){ DadesLlegenda(location,temp,wind,presion); } );
		}
		
		//Afegeix la imatge a la capa de l'element
		NewObj.appendChild(NewImg);
		
		document.getElementById("mapatemps").appendChild(NewObj);
		
		TMItem++; //incrementa el nombre d'elements creats
		
	} //Function