/* <![CDATA[ */
var actual = Array();
var previ = Array();

var tXHRmeteo=null;
function ById(who){
	return document.getElementById(who);
}

function f2c(t) {
	if(isNaN(t)){
		return t;
	}else return Math.round((t-32)*5/9); 
}// Conversion Fahrenheit->Celsius
function p2m(t) {
	if(isNaN(t)){
		return t;
	}else return Math.round((t)*33.865); 
}// Conversion Pouce de Mercure ->Millibars
function m2k(t) {
	if(isNaN(t)){
		return t;
	}else return Math.round((t)*1.609);
}// Conversion Mile->Km

function Temp(t,unit){
	if(isNaN(t)){
		return t;
	}else{
		var s=(unit=='m') ? f2c(t) : t;
		s+='&#176;';
		s+=(unit=='m') ? 'C' : 'F';
	}
	return s;
}

function Vitesse(t,unit){
	if(isNaN(t)){
		return strCalme;
	}else{
		var s=(unit=='m') ? m2k(t) : t;
		s+=(unit=='m') ? ' Kmh' : ' Mph';
	}
	return s;
}

function Distance(t,unit){
	if(isNaN(t)){
		return strUnlimited;
	}else{
		var s=(unit=='m') ? m2k(t) : t;
		s+=(unit=='m') ? ' Km' : ' Mi';
	}
	return s;
}

function Pression(t,unit){
	if(isNaN(t)){
		return t;
	}else{
		var s=(unit=='m') ? p2m(t) : t;
		s+=(unit=='m') ? ' mb' : ' po Hg';
	}
	return s;
}


function Pourcent(t){
	if(isNaN(t)){
		return t;
	}else{
		var s= t;
		s+=' %';
	}
	return s;
}


function indiceus2local(indice){ // Conversion indice UV US -> Local
   if(indice == 'Low'){
      return struv[0];
   }else if(indice == 'Moderate') {
      return struv[1];
   }else if(indice == 'High') {
      return struv[2];
   }else{
      return struv[3];
   }
}

function ventus2local(indice){
	var ret='';
	switch (indice){
		case 'N'	: ret=strvent[0];break;		/*0 deg */
		case 'NNE'	: ret=strvent[1];break;		/*22 deg */
		case 'NE'	: ret=strvent[2];break;		/*45 deg */
		case 'ENE'	: ret=strvent[3];break;		/*67 deg */
		case 'E'	: ret=strvent[4];break;		/*90 deg */
		case 'ESE'	: ret=strvent[5];break;		/*112 deg */
		case 'SE'	: ret=strvent[6];break;		/*135 deg */
		case 'SSE'	: ret=strvent[7];break;		/*157 deg */
		case 'S'	: ret=strvent[8];break;		/*180 deg */
		case 'SSW'	: ret=strvent[9];break;		/*202 deg */
		case 'SW'	: ret=strvent[10];break;	/*224 deg */
		case 'WSW'	: ret=strvent[11];break;	/*245 deg */
		case 'W'	: ret=strvent[12];break;	/*270 deg */
		case 'WNW'	: ret=strvent[13];break;	/*292 deg */
		case 'NW'	: ret=strvent[14];break;	/*314 deg */
		case 'NNW'	: ret=strvent[15];break;	/*336 deg */
		default		: ret=strvent[16];break;
	}
	return ret;
}


function Get_Meteo(id){
	if(id!=''){
		if(tXHRmeteo!=null) tXHRmeteo.abort();
		try{
		tXHRmeteo=getXMLHttpRequest();
		var url="xml/get_meteo.php";
		var params="city="+id;
		tXHRmeteo.open("POST", url, true);
		tXHRmeteo.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		tXHRmeteo.onreadystatechange =Result_Meteo;
		tXHRmeteo.send(params);
		}catch(e){};
	}
	return false;
}

function Result_Meteo(){
	if(tXHRmeteo!=null && tXHRmeteo.readyState == 4 && tXHRmeteo.status==200){
		var div_meteo=ById('ctn-meteo');
		var s='';
		var response = cleanXML(tXHRmeteo.responseXML.documentElement);
		var curent=response.getElementsByTagName('courant');
		//alert(actual['icon']=curent[0].getElementsByTagName('icon')[0].firstChild.data);
		actual['icon']=curent[0].getElementsByTagName('icon')[0].firstChild.data;
		if(actual['icon']<10) actual['icon']='0'+actual['icon'];
		if( isNaN(actual['icon'])) actual['icon']='NA';
		actual['temp']=curent[0].getElementsByTagName('temp')[0].firstChild.data;
		actual['flik']=curent[0].getElementsByTagName('flik')[0].firstChild.data;
		actual['max']=curent[0].getElementsByTagName('max')[0].firstChild.data;
		actual['min']=curent[0].getElementsByTagName('min')[0].firstChild.data;
		actual['dvent']=curent[0].getElementsByTagName('dvent_now')[0].firstChild.data;
		actual['vvent']=curent[0].getElementsByTagName('vvent_now')[0].firstChild.data;

		actual['uv']=curent[0].getElementsByTagName('uv_now')[0].firstChild.data;
		actual['uv_echelle']=curent[0].getElementsByTagName('uv_echelle_now')[0].firstChild.data;
		actual['hmid']=curent[0].getElementsByTagName('hmid')[0].firstChild.data;
		actual['vis']=curent[0].getElementsByTagName('vis')[0].firstChild.data;
		actual['pression']=curent[0].getElementsByTagName('pression')[0].firstChild.data;
		actual['maj']=curent[0].getElementsByTagName('maj')[0].firstChild.data;

		var prev=response.getElementsByTagName('prev');
		var nb=prev[0].childNodes.length;
		for(i=0; i<nb; i++){
			try{
				var day=prev[0].childNodes[i];
				previ[i] = Array();
				previ[i]['day']=day.getElementsByTagName('d')[0].firstChild.data;
				previ[i]['month']=day.getElementsByTagName('m')[0].firstChild.data;
				previ[i]['year']=day.getElementsByTagName('y')[0].firstChild.data;
				previ[i]['icon']=day.getElementsByTagName('icon')[0].firstChild.data;
				if(previ[i]['icon']<10) previ[i]['icon']='0'+previ[i]['icon'];
				previ[i]['night_icon']=day.getElementsByTagName('night_icon')[0].firstChild.data;
				if(previ[i]['night-icon']<10) previ[i]['night-icon']='0'+previ[i]['night-icon'];
				previ[i]['temp_max']=day.getElementsByTagName('temp_max')[0].firstChild.data;
				previ[i]['temp_min']=day.getElementsByTagName('temp_min')[0].firstChild.data;
				previ[i]['humid']=day.getElementsByTagName('humid')[0].firstChild.data;
				previ[i]['dvent']=day.getElementsByTagName('dvent')[0].firstChild.data;
				previ[i]['vvent']=day.getElementsByTagName('vvent')[0].firstChild.data;
				previ[i]['ppcp']=day.getElementsByTagName('ppcp')[0].firstChild.data;
				previ[i]['sunrise']=day.getElementsByTagName('sunrise')[0].firstChild.data;
				previ[i]['sunset']=day.getElementsByTagName('sunset')[0].firstChild.data;
				previ[i]['day_alt']=day.getElementsByTagName('day_alt')[0].firstChild.data;
				previ[i]['night_alt']=day.getElementsByTagName('night_alt')[0].firstChild.data;
			}catch(e){}
		}
		tXHRmeteo=null;
		Show_Meteo(SYSTEM);

		ById('donnee-meteo').style.display='block';
// 		ById('lk-metric').style.display='block';
	}
}

function Show_Meteo(unit){
	var s='';

	var d=new Date();
	d.setTime(actual["maj"] * 1000);
	var h=d.getHours();
	if(h<10) h='0'+h;
	var m=d.getMinutes();
	if(m<10) m='0'+m;
	if(unit=='m'){
		var yo=d.getDate()+' '+strmois[d.getMonth()+1]+' '+d.getFullYear()+' '+h+':'+m;
	}else{
		if(h>12){
			suff='pm';
			h=h-12;
		}else{
			suff='am';
		}
		if(h<10) h='0'+h;
		var yo=d.getDate()+' '+strmois[d.getMonth()+1]+' '+d.getFullYear()+' '+h+':'+m+' '+suff;
	}

	ById('actual-icon').innerHTML='<img src="images/meteo/'+actual['icon']+'.png" alt=\"'+stretatciel+'('+actual['icon']+')'+'\" title=\"Update '+yo+'\"/>';
	s='<h2>';
	s+=Temp(actual['temp'],unit);
	s+='</h2>';
	s+='<p>\n';

	s+='<span class="temp-max">'+strmax+' : ';
	s+=Temp(actual['max'],unit);
	s+='</span><br />\n';

	s+='<span class="temp-min">'+strmin+' : ';
	s+=Temp(actual['min'],unit);
	s+='</span><br />\n';

	s+='<span>'+strressentie+' : ';
	s+=Temp(actual['flik'],unit);
	s+='</span>\n';
	s+='</p>\n';

	ById('actual-info').innerHTML=s;
	/*
	ById('actual-uv-data').innerHTML=' '+actual['uv']+' '+indiceus2local(actual['uv_echelle']);
	ById('actual-wind-dir-data').innerHTML=' '+ventus2local(actual['dvent']);
	ById('actual-wind-speed-data').innerHTML=Vitesse(actual['vvent'],unit);

	ById('actual-hmid-data').innerHTML=' '+Pourcent(actual['hmid']);

	ById('actual-pression-data').innerHTML=' '+Pression(actual['pression'],unit);

	ById('actual-vis-data').innerHTML=' '+Distance(actual['vis'],unit);
	*/
// 	ById('actual-maj-data').innerHTML=' : weather.com';

	for(i=0;i<previ.length-1;i++){
		var d=new Date(previ[i]['year'],previ[i]['month']-1,previ[i]['day']);
		ById('prev-day-title'+i).innerHTML=strjour[d.getDay()]+' '+d.getDate();
		ById('prev-day-icon'+i).innerHTML='<img src="images/meteo/'+previ[i]['icon']+'.png" width="64" height="64" alt=\"'+stretatciel+'\"/>';

		s='<span class="temp-min">Low : ';
		s+=Temp(previ[i]['temp_min'],unit);
		s+='</span>';
		s+=' <br /> ';
		s+='<span class="temp-max">High : ';
		s+=Temp(previ[i]['temp_max'],unit);
		s+='</span>';
		ById('prev-day-temp'+i).innerHTML=s;
	}
	return false;
}

/* ]]> */

