/*

	Libreria para el mapa de cataluña

*/



		function listaEstacions(){

  			var obj = new Object();

  			//atributos

  			obj.aEstaciones = new Array();

  			obj.aNumEstacions = 0;

  			// metodos

  			obj.afegirEstacio = listaEstacions_afegirEstacio;

  			obj.getEstacio = listaEstacions_getEstacio;

			obj.getEstacions = listaEstacions_getEstacions;

			return obj;

  		}

		//implementacion de metodos

  		function listaEstacions_afegirEstacio(_estacio){	

  			this.aEstaciones[this.aEstaciones.length] = _estacio;

  			this.aNumEstacions++;

  			return true;

  		}

  		function listaEstacions_getEstacio(_identificador){	

  			return this.aEstaciones[_identificador];

  		}

  		function listaEstacions_getEstacions(){

  			return this.aEstaciones;

  		}

  		///////////////////////////////////

  		///////////////////////////////////

  		function estacio(_nomEstacio,_identificador,_minuts){

  			var obj = new Object();

  			//atributos

  			

  			obj.aNom = _nomEstacio;

  			obj.aIdentificador = _identificador;

  			obj.aMinuts = _minuts;

  			obj.aTemperatures = new Array();

  			obj.aHumitats = new Array();

  			obj.aPrecipitacions = new Array();  			  		

  			obj.aVVent = new Array();

  			obj.aDVent = new Array();

  			obj.aVentSrc = new Array();

  			obj.aSensorVent = new Array();

  			//metodos

  			obj.getNom = estacio_getNom;

  			obj.afegirLectures = estacio_afegirLectures;

  			obj.getIdentificador = estacio_getIdentificador;

  			obj.getMinuts = estacio_getMinuts;

  			obj.getTemperatures = estacio_getTemperatures;

  			obj.getHumitats = estacio_getHumitats;

  			obj.getPrecipitacions = estacio_getPrecipitacions;

  			obj.getVVent = estacio_getVVent;

  			obj.getDVent = estacio_getDVent;

  			obj.getVentSrc = estacio_getVentSrc;

  			obj.getSensorVent = estacio_getSensorVent;

  			return obj;

  		}

  		

  		function estacio_afegirLectures(_temperatura,_humitat,_precipitacio,_vvent,_dvent,_srcvent,_key,_sensorVent){

  			this.aTemperatures[_key] = _temperatura;

  			this.aHumitats[_key] = _humitat;

  			this.aPrecipitacions[_key] = _precipitacio;

  			this.aVVent[_key] = _vvent;

  			this.aDVent[_key ] = _dvent;

  			this.aVentSrc[_key] = _srcvent;

  			this.aSensorVent[_key] = _sensorVent;

  			return true;

  		}

		function estacio_getNom(){

			return this.aNom;

		}  		

		function estacio_getIdentificador(){

			return this.aIdentificador;

		}

  		function estacio_getMinuts(){

  			return this.aMinuts;

  		}

  		function estacio_getTemperatures(_key){

			

  			if (_key == null){

	  			return this.aTemperatures;

  			}else{

	  			return this.aTemperatures[_key];

	  		}



  		}

  		function estacio_getHumitats(_key){

  			if (_key == null){  	

	  			return this.aHumitats;

	  		}else{

	  			return this.aHumitats[_key]

	  		}

  		}

  		function estacio_getPrecipitacions(_key){

  			if (_key == null){  		

	  			return this.aPrecipitacions;

	  		}else{

	  			return this.aPrecipitacions[_key];

	  		}

  		}

  		function estacio_getVVent(_key){

  			if (_key == null){  		

	  			return this.aVVent;

	  		}else{

	  			return this.aVVent[_key];

	  		}

  		}

  		function estacio_getDVent(_key){

  			if (_key == null){  		

	  			return this.aDVent;

	  		}else{

	  			return this.aDVent[_key];

	  		}

  		}

  		function estacio_getVentSrc(_key){

	  			return this.aVentSrc[_key];

  		}

  		function estacio_getSensorVent(_key){

	  		return this.aSensorVent[_key];

  		}

		function actualizaVariable(v){

			var data = getHora(hora);

			document.getElementById("tablaViento").style.visibility = 'hidden';

			switch (v){

				case 1:			//TEMPERATURA

					for(var i = 0; i < estacions.getEstacions().length;i++){

						var e = estacions.getEstacio(i);

						if (document.getElementById(e.getIdentificador()) != null ){

							var div = document.getElementById(e.getIdentificador());

							div.title = e.getNom();

							div.innerHTML = "";

							if (e.getTemperatures(data) != null){

								div.innerHTML = leerLectura(e.getTemperatures(data));

								div.title =  e.getNom() + " ("+ e.getIdentificador()+") " +  e.getTemperatures(data) + "ºC";

							}else if(!tipoBaseTemporal(e)){		//comprueba si la estación ha de tener lectura para el peridodo consultado.

								var x = new Date();

								x = cloneDate(hora);

								x.setMinutes(0);

								if (e.getTemperatures(getHora(x)) != null){

									div.title = e.getNom() + " ("+ e.getIdentificador()+") " + e.getTemperatures(getHora(x)) + " ºC";

									div.innerHTML = leerLectura(e.getTemperatures(getHora(x)));

								}

							}

						}

					}

					variable = 1;

					break;

					

				case 2:			//HUMITAT

					for(var i = 0; i < estacions.getEstacions().length;i++){

						var e = estacions.getEstacio(i);

						if (document.getElementById(e.getIdentificador()) != null ){

							var div = document.getElementById(e.getIdentificador());

							div.title = e.getNom();

							div.innerHTML = "";

							if (e.getHumitats(data) != null){

								div.title = e.getNom()+ " ("+ e.getIdentificador()+") " + e.getHumitats(data) + "%";

								div.innerHTML = leerLectura(e.getHumitats(data));

							}else if(!tipoBaseTemporal(e)){		//comprueba si la estación ha de tener lectura para el peridodo consultado.

								var x = new Date();

								x = cloneDate(hora);

								x.setMinutes(0);

								if (e.getHumitats(getHora(x)) != null && e.getHumitats(getHora(x)) != "-"){

								div.title = e.getNom()+ " ("+ e.getIdentificador()+") " + e.getHumitats(getHora(x)) + "%";

									div.innerHTML = leerLectura(e.getHumitats(getHora(x)));

								}

							}

						}

					}

					variable = 2;

					break;

				case 3:			//PRECIPITACIO ACC

					for(var i = 0; i < estacions.getEstacions().length;i++){

						var e = estacions.getEstacio(i);

						if (document.getElementById(e.getIdentificador()) != null ){

							var div = document.getElementById(e.getIdentificador());

							div.title = e.getNom();

							div.innerHTML = "";							

							if (e.getPrecipitacions(data) != null){

								div.title = e.getNom() + " ("+ e.getIdentificador()+") " +e.getPrecipitacions(data) + "mm";							

								div.innerHTML = leerLectura(e.getPrecipitacions(data));

							}else if(!tipoBaseTemporal(e)){		//comprueba si la estación ha de tener lectura para el peridodo consultado.

								var x = new Date();

								x = cloneDate(hora);

								x.setMinutes(0);

								if (e.getPrecipitacions(getHora(x)) != null && e.getPrecipitacions(getHora(x)) != "-"){

									div.title = e.getNom() + " ("+ e.getIdentificador()+") " + e.getPrecipitacions(getHora(x)) + "mm";															

									div.innerHTML = leerLectura(e.getPrecipitacions(getHora(x)));

								}

							}

						}

					}

					variable = 3;

					break;

				case 4:			//VELOCITAT VENT

					for(var i = 0; i < estacions.getEstacions().length;i++){

						var e = estacions.getEstacio(i);

						if (document.getElementById(e.getIdentificador()) != null ){

							var div = document.getElementById(e.getIdentificador());

							div.title = "";							

							div.innerHTML = "";							

							if (e.getVentSrc(data) != null){

								div.innerHTML="";		

								if (e.getVentSrc(data) != "-"){

									var img = document.createElement('img');

									img.src = e.getVentSrc(data);

									img.border = "0";

									img.title = e.getNom()+" ("+ e.getIdentificador()+")"+ " [Altura "+ e.getSensorVent(data) + "] " +  e.getVVent(data)+"m/s " + e.getDVent(data) + "º";

									div.appendChild(img);

								}

							}else if(!tipoBaseTemporal(e)){		//comprueba si la estación ha de tener lectura para el peridodo consultado.

								var x = new Date();

								x = cloneDate(hora);

								x.setMinutes(0);

								var div = document.getElementById(e.getIdentificador());								

								div.innerHTML = "";

								if (e.getVentSrc(getHora(x)) != null && e.getVentSrc(getHora(x)) != "-"){

									var img = document.createElement('img');

//									img.alt = e.getNom() + e.getDVent(getHora(x)) + "º" + " - " + e.getVVent(getHora(x))+"m/s";																	

									img.border = "0";

									img.src = e.getVentSrc(getHora(x));

									img.title = e.getNom() + " ("+ e.getIdentificador()+")" + " [Altura "+ e.getSensorVent(getHora(x)) + "] "  + e.getVVent(getHora(x)) + "m/s "+ e.getDVent(getHora(x))+"º";								

									div.appendChild(img);

								}

							}

						}

					}

					document.getElementById("tablaViento").style.visibility = 'visible';

					variable = 4;

					break;

			};

			actualizarHora(hora);

			document.getElementById('block').style.visibility='hidden';

		}

		

		

		function anterior(){

			var objImgPrev = document.getElementById("previous");

			var objImgNext = document.getElementById("next");
			
			if (hora <= yesterday){

//				alert("desactivo");

				objImgPrev.src = "../Images/Botons/flecha_izquierda_desactivada.gif";

				objImgPrev.disabled = "disabled";

				objImgPrev.style.cursor = "default";

				return false;

			}else{

				if (hora <= horaRefrescoInf){

					var DOMComboObj = document.getElementById("control");

					DOMComboObj.value = 0; 

					var DomVariable = document.getElementById("variable");

					DomVariable.value = variable;
					hora.setMinutes(hora.getUTCMinutes() - 30);

					envia(getHora(hora),"data");
					//document.getElementById('block').style.visibility='visible';
					
					return true

				}

				if (objImgNext.disabled == true){

					objImgNext.disabled= false;

					objImgNext.src="../Images/Botons/ico_flechadere.gif";

					objImgNext.style.cursor = "pointer";

				}	

				hora.setMinutes(hora.getUTCMinutes() - 30);

				actualizaVariable(variable);

				return true;

			}

		}

		

		//Avanza un periodo

		function siguiente(){

			var objImgPrev = document.getElementById("previous");

			var objImgNext = document.getElementById("next");
			
			if (getHora(hora) == getHora(now)){

//				var obj = document.getElementById("next");

				objImgNext.src = "../Images/Botons/flecha_derecha_desactivada.gif";

				objImgNext.disabled = "disabled";

				objImgNext.style.cursor = "default";

				return false;

			}else{

				if (hora >= horaRefrescoSup){

					var DOMComboObj = document.getElementById("control");

					DOMComboObj.value = 1; 

					var DomVariable = document.getElementById("variable");

					DomVariable.value = variable;
					hora.setMinutes(hora.getUTCMinutes() + 30);
					envia(getHora(hora),"data");
					//document.getElementById('block').style.visibility='visible';
					
					return true;

				}

				if (objImgPrev.disabled == true){

					objImgPrev.disabled = false;

					objImgPrev.src="../Images/Botons/ico_flechaizquierd.gif";

					objImgPrev.style.cursor = "pointer";

				}

				hora.setMinutes(hora.getUTCMinutes() + 30);

				actualizaVariable(variable);

				return true;

			}

		}	

		



		

	// ilumina el cuandrante seleccionado en el cuadnro de navegacion

function activarCuandrante(obj){

	var img = document.getElementById("navegacion");

	img.src = obj;

}

		

		//Indica si la fecha especificada por parametro esta dentro del horario de verano o no.

function horarioVerano(_date){

	myDays= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];

	var d1 = new Date();

	var d2 = new Date();

	//Octubre

	d2.setMonth(9);

	d2.setDate(31);

	//Marzo

	d1.setMonth(2);

	d1.setDate(31);

			

	while (myDays[d1.getDay()] == "Sunday"){

		d1.setDate(d1.getDate()-1);

	}

	while (myDays[d2.getDay()] == "Sunday"){

		d2.setDate(d2.getDate()-1);

	}

	if (d1<=_date && _date <=d2){		//horario verano

		return true;

	}else			

		return false;					

}





//llama al cuadrante especificado

function enviaMapa(c){

	var DOMComboObj = document.getElementById("data");

	DOMComboObj.value = getHora(hora); 



	var DOMComboObjCuandra = document.getElementById("cuadrante");

	DOMComboObjCuandra.value = c;

	var DomVariable = document.getElementById("variable");

	DomVariable.value = variable;

	var DOMFormObj=DOMComboObj.form;
	document.getElementById('block').style.visibility='visible';
	if (enviado() == true) {

		DOMFormObj.submit();

		return true;

	}

}	



function getHoraActual(){

	var d = new Date();

  	d.setSeconds(0);

  	if (horarioVerano(d)){

		n=2;

  	}else{

  		n=1;

	}

		d.setHours(d.getHours()-n);

	 		

	if (d.getMinutes() > 0 && d.getMinutes() < 30){

		d.setMinutes(00);

	}else{

		d.setMinutes(30);

	}

		return d;

}



//comprueba si la estacio ha de tener lectura en el periodo de lectura.

function tipoBaseTemporal(estacio){

	var base = estacio.getMinuts();

	var m = hora.getMinutes();

	var ret = false;

	switch(m){

		case 30:

			if (base == 30){

				ret = true;	

			}

			break;

		case 0:

			ret = true;

		}

	return ret;				

}



function cloneDate(date){

	var a = new Date(y2n(date.getYear()),

		date.getMonth(),

		date.getDate(),

		date.getHours(),

		date.getMinutes(),

		date.getSeconds());

	return a;

}  		



function anidarCero(nr){

	if (nr < 10){  			

		nr = "0" + nr;

	}

		return nr;

	}

  		

	//Actualiza el element HTML con la hora modificada.

function actualizarHora(h){

	document.getElementById("hora").innerHTML = getHora(h) + " (T.U.)";

}

		

//Devuelve la hora(el periodo que se esta consultando) en el formato dd/mm/yyyy hh24/mi

function getHora(fecha){

	var str = anidarCero(fecha.getDate()) + "/" + anidarCero(fecha.getMonth()+1)  + "/" + y2n(fecha.getYear()) + " " + 

			anidarCero(fecha.getHours()) + ":" + anidarCero(fecha.getMinutes());

	return str;

}



function getParseDate(fecha){

	var str = anidarCero(fecha.getDate()) + "/" + anidarCero(fecha.getMonth()+1)  + "/" + y2n(fecha.getYear()) + " " + 

			anidarCero(fecha.getHours()) + ":" + anidarCero(fecha.getMinutes());

	return str;

}  		

  		//La mayoria de  navegadores al hacer getYear() develve el año a partir de 1900. Este método es para calcular el año de la fecha especificada por parametro

function y2n(y){

	return (y < 1000) ? y + 1900 : y; 

}