var xmlstring;

var xmlitem;



var title;

var titles;

var ary_titles;



var counttitle;

var countcurrent;

var strRss;

var LastName;



var RssFirstReadTimer;

var RssReadTimer;





var rss_ary_List   = new Array();



var str_RefreshData;

var str_RefreshForecast;

var conn_ok;

var ReadTimerStart;



var rss_position;

var rss_timeupdate;

var rss_timescroll;

var rss_enable;

var rss_ary_Name  = new Array();

var rss_ary_Url   = new Array();

var rss_ary_Color = new Array();

var rss_ary_Max   = new Array();

var ary_news;



var forecast_enable;

var forecast_graphic;

var forecast_text;

var titleForecast;

var titlesForecast;

var descForecast;

var ary_titlesForecast;

var ary_titleForecast;

var ary_cityForecast;

var countcurrentForecast;

var ary_forecast;

var forecast_city;

var forecast_metric;

var forecast_color;

var ary_descForecast;

var str_forecast;

var str_imgforecast;



var TodayDate;



var ary_tmp_gradi = new Array();

var ary_gradi = new Array();

var ary_gradi2 = new Array();



ary_titles='';

countcurrent=0;

str_RefreshData='Mise à jour...';

str_RefreshForecast='refreshing...';



$(document).ready(function(){

	TodayDate = currentDate();

	conn_ok=false;

	ReadTimerStart=false;

	

	if(rss_position=='top'){

		$('#divnews').css({'top':'18px'})

		$('#divforecast').css({'top':'383px'})

	}else{

		$('#divnews').css({'top':'360px'})

		$('#divforecast').css({'top':'18px'})

	}

	

	if(rss_enable){

		$('#divnews').css({'color':'Red'});

		$('#divnews').html(str_RefreshData);

	}

	

	if(forecast_enable){

		$('#divforecast').css({'color':'Red'});

		$('#divforecast').html(str_RefreshForecast);

		

		$('#divdateforecast').css({'color':forecast_color});

		if(forecast_graphic==true){$('#divimgforecast').css({'top':'298px','left':'246px'});$('#divdateforecast').css({'top':'342px','left':'246px'})}

		

	}



	RssFirstReadTimer=setInterval("FirstReadRSS()",3500);	

});





function FirstReadRSS(){

	//if($('#divnews').html()==str_RefreshData){

	if(conn_ok==false){	

		if(forecast_enable){ReadForecast();}

		if(rss_enable){ReadRSS();}

	}else{

		clearInterval(RssFirstReadTimer);



	}

}



function ReadForecast(){

//if(forecast_enable){

	var iTitles;

	titlesForecast='';

	descForecast='';

	countcurrentForecast=0;

	

		$.ajax({

		url: "http://rss.accuweather.com/rss/liveweather_rss.asp?metric="+ forecast_metric +"&locCode=" + forecast_city,	

		cache: false,

		dataType: "html",

		success : function(html){

		conn_ok=true;

		clearInterval(RssFirstReadTimer);

		

		var xmlobject = (new DOMParser()).parseFromString(html, "text/xml");	

		var root = xmlobject.getElementsByTagName('channel')[0];

		

		var items = root.getElementsByTagName("item");

		

		for (i = 0 ; i < 3  ; i++) {

			xmlitem = items[i];

			titleForecast = xmlitem.getElementsByTagName("title")[0].firstChild.nodeValue;

			descForecast = xmlitem.getElementsByTagName("description")[0].firstChild.nodeValue;

			ary_descForecast=descForecast.split("img")	



			str_forecast=ary_descForecast[0]

			if(forecast_metric=='1'){str_forecast=str_forecast.replace(' C ','&deg; ').replace(' C ','&deg; ');}

			if(forecast_metric!='1'){str_forecast=str_forecast.replace(' F ','&deg; ').replace(' F ','&deg; ');}

			str_forecast=str_forecast.replace('High:','').replace('Low:',' - ');

			str_forecast=str_forecast.replace('Currently in ','');			

			titleForecast=titleForecast.replace(' Forecast','');			

			str_imgforecast=ary_descForecast[1].replace(' src="http://vortex.accuweather.com/phoenix2/images/common/icons/','').replace('" >','').replace('">','').replace('.gif','.png');

				

			titlesForecast=titlesForecast + '|^|' + titleForecast + '^|^' + str_forecast + '^|^' + str_imgforecast; 

			

		}		

			ary_titlesForecast=titlesForecast.split("|^|");

			if(ReadTimerStart==false){ReadTimerStart=true;RssReadTimer=setInterval("NewsUpdate()",rss_timescroll*1000);}

			if(forecast_text==false){$('#divforecast').html('');}



			createCookie('iWBRss_Forecast',textForCookie(titlesForecast),1);

		}

		});

		

}



function ReadRSS(){

	var iTitles;

	titles='';

	countcurrent=0;

	LastName='';

	//clearInterval(RssReadTimer);

	for(iTitles=0 ; iTitles<rss_ary_Name.length ; iTitles++){

		$.ajax({

		url: rss_ary_Url[iTitles],

		async: false,

		cache: false,

		

		dataType: "html",

		success : function(html){

		

		conn_ok=true;

		clearInterval(RssFirstReadTimer);

		var xmlobject = (new DOMParser()).parseFromString(html, "text/xml");	

		var root = xmlobject.getElementsByTagName('channel')[0];

		

		var items = root.getElementsByTagName("item");

		

		if(rss_ary_Max[iTitles]==0){

			counttitle=items.length;

		}else{

			if(items.length>rss_ary_Max[iTitles]){

				counttitle=rss_ary_Max[iTitles];

			}else{

				counttitle=items.length;

			}

		}

		

		for (i = 0 ; i < counttitle  ; i++) {		

			xmlitem = items[i];

			title = xmlitem.getElementsByTagName("title")[0].firstChild.nodeValue + "<BR/>" + xmlitem.getElementsByTagName("description")[0].firstChild.nodeValue;

			titles=titles + '|^|' + title + '^|^' + rss_ary_Color[iTitles]+ '^|^' + rss_ary_Name[iTitles];

		}	



		}

		});

	}

	

		ary_titles=titles.split("|^|");

		

		if(ReadTimerStart==false){ReadTimerStart=true;RssReadTimer=setInterval("NewsUpdate()",rss_timescroll*1000);}



		createCookie('iWBRss_Rss',textForCookie(titles),1);
rss_timeupdate=setTimeout("ReadRSS()",refreshflux*60000);
}



function NewsUpdate(){

	if(rss_enable){

		if(countcurrent>=ary_titles.length){countcurrent=0;}	

		

		countcurrent = countcurrent+1;

		ary_news=ary_titles[countcurrent].split("^|^");

		

		if(LastName!=ary_news[2]){

			$('#divrssname').html('  <span style=color:#ffffff;background-color:orange;font-family:Helvetica;font-weight:bold;" > ' + ary_news[2] + '</span>');   

			LastName=ary_news[2]

		}

			strRss=ary_news[0];

			$('#divnews').css({'color':ary_news[1]})

			$('#divnews').html(strRss.substr(0,250));

	}

		

	if(forecast_enable){	

		if(countcurrentForecast>=3){countcurrentForecast=0;}

		countcurrentForecast=countcurrentForecast+1

		ary_forecast=ary_titlesForecast[countcurrentForecast].split("^|^");

		$('#divforecast').css({'color':forecast_color})

		if(countcurrentForecast>1){		

			data_forecast=formatDate(ary_forecast[0]);

			if(countcurrentForecast==2){str_data_forecast='Today';}

			if(countcurrentForecast==3){str_data_forecast='Tomorrow';}

			

			if(forecast_text==true){$('#divforecast').html(data_forecast +':  '+ formatLang(ary_forecast[1]));}

			

			tmp_str_forecast=data_forecast +':  '+ ary_forecast[1]

			ary_tmp_gradi=tmp_str_forecast.split('&deg;');

			ary_gradi=ary_tmp_gradi[0].split(':');

			ary_gradi[1]=ary_gradi[1] + '&deg;'



			ary_gradi2=ary_tmp_gradi[1].split('-');

			ary_gradi[1]=ary_gradi[1] + ' -' + ary_gradi2[1] + '&deg;'



		}else{

			str_data_forecast='Now' //formatDate(TodayDate)



			ary_titleForecast=ary_forecast[1].split(':');

			ary_cityForecast=ary_titleForecast[0].split(',');

			if(forecast_text==true){$('#divforecast').html(ary_cityForecast[0].capitalize() +': ' +formatLang(ary_titleForecast[1]));}

			

			tmp_str_forecast=ary_cityForecast[0].capitalize() +':  '+ ary_titleForecast[1]

			if(forecast_metric=='1'){ary_tmp_gradi=tmp_str_forecast.split('C ');}

			if(forecast_metric!='1'){ary_tmp_gradi=tmp_str_forecast.split('F ');}

			ary_gradi=ary_tmp_gradi[0].split(':');

		}

		

		if(forecast_graphic==true){

			$("#imgforecast").attr({'src':'icons_hd/' + ary_forecast[2]});

			$("#divdateforecast").html(ary_gradi[1] + '<br>' + formatLang(str_data_forecast));

			

		}

	}	

}

