
var Weather=new Class({Extends:Widget,initialize:function(){this.cfg={title:loc.text("weather_title"),module:"Weather"};this.tags={};this.defaultProfile["city"]="HUXX0002";this.defaultProfile["unit"]="c";this.defaultProfile["bgcolor"]="orig";this.defaultProfile["view"]="e";this.loading=false;},onBuildInterface:function(){var t=this;t.settings="<form id='f"+t.id+"' onsubmit='return false'><div class='settings_section'>"+" <span class='settings_label'>"+loc.text('weather_inp_unit')+"</span>"+" <select name='unit'>"+"  <option value='c'"+(t.profile.unit=='c'?' selected':'')+">"+loc.text('weather_celsius')+"</option>"+"  <option value='f'"+(t.profile.unit=='f'?' selected':'')+">"+loc.text('weather_fahrenheit')+"</option>"+" </select>"+"</div>"+"<div class='settings_section'>"+" <span class='settings_label'>"+loc.text('weather_view')+"</span>"+" <select name='view'>"+"  <option value='e'"+(t.profile.view=='e'?' selected':'')+">"+loc.text('weather_e')+"</option>"+"  <option value='n'"+(t.profile.view=='n'?' selected':'')+">"+loc.text('weather_n')+"</option>"+" </select>"+"</div>"+"<div class='settings_section'>"+" <span class='settings_label'>"+loc.text('weather_inp_town')+"</span>"+" <input type='text' name='city'>"+" <input type='button' name='btn' value='"+loc.text('weather_btn')+"' class='module_btn'>"+loc.text('weather_tip')+"</div><div id='cities' style='display:none'></div></form>"+t.changeColors;t.setSettings(t.settings)
t.colorsAction();var f=$('f'+t.id),n=f.elements;t.tags['unit']=$(n.unit);t.tags['city']=$(n.city);t.tags['cities']=t.mid.getElement('div[id=cities]');t.tags['btn']=$(n.btn);t.tags['view']=$(n.view);t.tags['unit'].addEvent('change',function(){t.profile.unit=t.getValue('unit');t.renderWeather();t.save();});t.tags['view'].addEvent('change',function(){t.profile.view=t.getValue('view');t.renderWeather();t.save();});t.tags['city'].addEvent('keydown',function(e){if(e.key=='enter')
t.loadCities();});t.tags['btn'].addEvent('click',function(e){t.loadCities();});t.setBgColor(t.profile.bgcolor,1);},onOpen:function(){this.refresh();main.stopTimer(this.id);main.processTimer(this.id,1800*1000,true);},refresh:function(){if(!this.loading){this.loading=true;this.setTitle(loc.text("msg_loading"));if(typeof _WEATHER!="undefined"&&_WEATHER[this.profile.city]){this.data=toValue(unescape(_WEATHER[this.profile.city]));if(this.data)
this.renderWeather();this.loading=false;if(!this.isSettingsReduced)
this.hideSettings();}else{xmlRequest.send("weatherproxy.php?city="+this.profile.city,this,"showWeather");}}},loadCities:function(){this.tags['cities'].innerHTML=loc.text("msg_loading");this.tags['cities'].style.display='block';xmlRequest.send("weatherproxy.php?where="+escape(this.getValue('city')),this,"showCities");},showCities:function(response){var data,row,x,t=this;data=toValue(response.responseText);t.tags['cities'].innerHTML='';if(!data.error){if(data.cnt==1){t.setCity(data.c[0].id);}else{for(var i=0;i<data.c.length;i++){row=new Element('div',{'id':data.c[i].id,'html':data.c[i].title,'styles':{'cursor':'pointer','margin':'5px 0px 5px 5px','text-decoration':'underline'}});x=data.c[i].id;row.addEvent('click',function(e,x){t.setCity(x);}.bindWithEvent(t,x));row.inject(t.mid.getElementById('cities'));}}}else{t.mid.getElementById('cities').set({'html':loc.text("weather_no_cities")});}},setCity:function(id){this.tags['cities'].style.display='none';this.tags['cities'].innerHTML='';this.tags['city'].value='';this.profile.city=id;this.tags['cities'].style.display='block';this.save();this.refresh();},toCelcius:function(n){var calc=Math.round((n-32)*5/9);return(isNaN(calc))?'':calc;},getTemp:function(hi,lo){if(this.getValue('unit')=="c"){hi=this.toCelcius(hi);lo=this.toCelcius(lo);}
return lo+'&deg;'+(hi!=''?' / '+hi+'&deg;'+this.getValue('unit').toUpperCase():'');},renderWeather:function(){var d=this.data,cur,U,ico,day,h="";U=this.getValue('unit').toUpperCase();if(U=="C"){cur=this.toCelcius(d.cur.tmp);}else{cur=d.cur.tmp;}
h+="<div class='weather'>"+" <div><div style='float:left;width:33%'><h3>"+loc.text('weather_'+d.days[0].d.toLowerCase())+"</h3></div><div style='float:left;width:33%;text-align:center'><h2>"+d.loc+"</h2></div><div style='float:left;width:33%;text-align:right'><h3>"+d.today+"</h3></div></div>"+" <div style='clear:both;overflow:hidden;min-width: 378px;'>"+"  <div style='float:left;width:93px'><img src='pix/weather/93/"+d.cur.ico+".png'></div>"+"  <div style='float:left;margin-left:5px;margin-top:20px;width:50px'><div><h1>"+cur+"&deg;"+U+"</h1></div></div>"+"  <div style='float:right;margin-left:10px;margin-top:35px;width:220px'>"+"   <div style='float:left;margin-right:15px'>"+"    <div><span style='width:62px;float:left'>"+loc.text('weather_sunr')+"</span>"+d.days[0].sr+"</div>"+"    <div><span style='width:62px;float:left;'>"+loc.text('weather_suns')+"</span>"+d.days[0].ss+"</div>"+"   </div>"+"   <div style='float:left'>"+"    <div><span style='width:70px;float:left;'>"+loc.text('weather_wind')+"</span>"+d.cur.wspeed+" km/h</div>"+"    <div><span style='width:70px;float:left;'>"+loc.text('weather_hum')+"</span>"+d.cur.hum+"%</div>"+"    <div><span style='width:70px;float:left;'>"+loc.text('weather_uv')+"</span>"+d.cur.uvc+"</div>"+"   </div>"+"  </div>"+" </div>"+"</div>";if(this.profile.view=='e'){h+="<div style='border-top:1px solid #ddd;padding-top:4px'>";h+="<table cellpadding='0' cellspacing='0' border='0' width='100%'><tr>";for(var i=0;i<d.days.length;i++){ico=parseInt(d.days[i].ico);ico=((ico<10)?"0":"")+ico+".png";day=d.days[i].d;h+="<td align='center'><div><b>"+loc.text('weather_'+day.toLowerCase())+"</b></div>";h+="<img src='pix/weather/61/"+ico+"' title='"+loc.text('weather_sunr')+d.days[i].sr+";"+loc.text('weather_suns')+d.days[i].ss+"' width='52'>";h+="<div>"+this.getTemp(d.days[i].hi,d.days[i].lo)+"</div></td>";}
h+="</tr></table>";}
this.setContent(h);this.setTitle(this.cfg.title+" : "+d.loc);},showWeather:function(response){this.data=toValue(unescape(response.responseText));if(this.data)
this.renderWeather();this.loading=false;if(!this.isSettingsReduced){this.hideSettings();this.isSettingsReduced=true;}},timerHandler:function(){this.refresh();}});