
var Rss=new Class({Extends:Widget,initialize:function(){this.cfg={title:loc.text("rss_title"),module:"Rss"}
this.tags={};this.defaultProfile["cnt"]=10;this.defaultProfile["opend"]=0;this.defaultProfile["with_desc"]=0;this.defaultProfile["with_date"]=1;this.defaultProfile["url"]="";this.defaultProfile["icon"]="";this.defaultProfile["period"]=600;this.defaultProfile["md5"]=[];this.defaultProfile["bgcolor"]="orig";this.defaultProfile["id"]="";this.seturl="<div><span class='settings_label'>"+loc.text("rss_inp_url")+"</span><input type='text' name='select_url' size='15'></div>";this.lastRefresh=null;this.isLoading=false;this.isInReader=false;this.page=0;this.actRotateIdx=false;this.lastFeed={url:null,time:null}
this.iconLoaded=false;this.needLoad=true;},onBuildInterface:function(){this.settings="<form id='f"+this.id+"' onsubmit='return false'>"+" <div class='settings_section'>"+"  <span class='settings_label'>"+loc.text("inp_title")+"</span>"+"  <input type='text' name='title' size='15' class='settings_control'>"+" </div>"+" <div class='settings_section'>"+"  <span class='settings_label'>"+loc.text("rss_inp_ncount")+"</span>"+"  <select name='count' class='settings_control'>"+"   <option value='3'"+(this.profile.count=='3'?' selected':'')+">3</option>"+"   <option value='5'"+(this.profile.count=='5'?' selected':'')+">5</option>"+"   <option value='8'"+(this.profile.count=='8'?' selected':'')+">8</option>"+"   <option value='10'"+(this.profile.count=='10'?' selected':'')+">10</option>"+"   <option value='12'"+(this.profile.count=='12'?' selected':'')+">12</option>"+"   <option value='9999'"+(this.profile.count=='9999'?' selected':'')+">"+loc.text("rss_all")+"</option>"+"  </select>"+"</div>"+"<div class='settings_section'>"+" <span class='settings_label'>"+loc.text("rss_inp_refresh")+"</span>"+" <select name='period' class='settings_control'>"+"  <option value='120'"+(this.profile.period=='120'?' selected':'')+">"+loc.text("rss_p120")+"</option>"+"  <option value='300'"+(this.profile.period=='300'?' selected':'')+">"+loc.text("rss_p300")+"</option>"+"  <option value='600'"+(this.profile.period=='600'?' selected':'')+">"+loc.text("rss_p600")+"</option>"+"  <option value='1200'"+(this.profile.period=='1200'?' selected':'')+">"+loc.text("rss_p1200")+"</option>"+"  <option value='1800'"+(this.profile.period=='1800'?' selected':'')+">"+loc.text("rss_p1800")+"</option>"+"  <option value='3600'"+(this.profile.period=='3600'?' selected':'')+">"+loc.text("rss_p3600")+"</option>"+"  <option value='10800'"+(this.profile.period=='10800'?' selected':'')+">"+loc.text("rss_p10800")+"</option>"+" </select>"+"</div>"+"<div class='settings_section'>"+" <span class='settings_label'>"+loc.text("rss_inp_open_to")+"</span>"+" <input type='checkbox' size='15' name='opend'"+(this.profile.opend?' checked':'')+">"+"</div>"+"<div class='settings_section'>"+" <span class='settings_label'>"+loc.text("rss_inp_with_desc")+"</span>"+" <input type='checkbox' size='15' name='with_desc'"+(this.profile.with_desc?' checked':'')+">"+"</div>"+"<div class='settings_section'>"+" <span class='settings_label'>"+loc.text("rss_inp_with_date")+"</span>"+" <input type='checkbox' size='15' name='with_date'"+(this.profile.with_date?' checked':'')+">"+"</div></form>";var self=this;this.settings+=this.changeColors;if(this.profile.id==""){this.settings+=this.seturl;}
this.mid.firstChild.childNodes[1].innerHTML=this.settings;var frm=$('f'+this.id);this.tags['title']=$(frm.elements[0]);this.tags['count']=$(frm.elements[1]);this.tags['period']=$(frm.elements[2]);this.tags['opend']=$(frm.elements[3]);this.tags['with_desc']=$(frm.elements[4]);this.tags['with_date']=$(frm.elements[5]);this.tags['tip']=$('tooltip');if(this.profile.id==""){this.mid.getElement('input[name=select_url]').addEvent('change',function(){self.settingsAll();});}
this.tags['title'].addEvent('keydown',function(e){if(e.key=='enter')
self.settingsAll();});this.tags['title'].addEvent('blur',function(){self.settingsAll();});this.tags['count'].addEvent('change',function(){self.profile.cnt=self.getValue('count');self.save();self.showChannelTitle();self.page=0;self.renderChannel();});this.tags['period'].addEvent('change',function(){self.setPeriod();});this.tags['opend'].addEvent('click',function(){self.profile.opend=self.getProp('opend','checked')?1:0;self.save();});this.tags['with_desc'].addEvent('click',function(){self.profile.with_desc=self.getProp('with_desc','checked')?1:0;self.save();self.renderChannel();});this.tags['with_date'].addEvent('click',function(){self.profile.with_date=self.getProp('with_date','checked')?1:0;self.save();self.renderChannel();});this.colorsAction();this.setValue('count',this.profile.cnt);this.setValue('opend','check',this.profile.opend);this.setValue('with_desc','check',this.profile.with_desc);this.setValue('with_date','check',this.profile.with_date);if(this.profile.id==""){this.setValue('select_url',this.profile["url"]);}
if(this.profile.id==""){this.settings+=this.seturl;}
else{this.setTitle(this.profile.title);}
this.setBgColor(this.profile.bgcolor,1);},onOpen:function(){this.setTitle(loc.text("rss_title"));this.setContent(loc.text("msg_loading"));main.processTimer(this.id,this.profile.period*1000);},setPeriod:function(){this.profile.period=this.getValue('period');main.stopTimer(this.id);main.processTimer(this.id,this.profile.period*1000,true);this.save();},timerHandler:function(){if(!this.isInReader){this.refresh();}},refresh:function(){if(this.profile.id==""){if(this.profile.url!=""){var date=new Date();this.lastRefresh=date.getSeconds();this.setTitle(loc.text("msg_loading"));var wid=this.id;var iconEl=this.elements.icon;xmlRequest.send(this.profile.url,this,"showChannel");this.isLoading==true;}else{this.setContent(loc.text("msg_empty"));}}else{if(this.rss_item_data){this.setTitle(loc.text("msg_loading"));if(this.elements.change_url){if(this.elements.change_url.selectedIndex!=this.actUrlIdx)
this.page=0;this.actUrlIdx=this.elements.change_url.selectedIndex;}
if(this.profile.url!=this.rss_item_data.urls[this.actUrlIdx].url){this.profile.url=this.rss_item_data.urls[this.actUrlIdx].url;this.save();}
main.stopTimer(this.id);xmlRequest.send(this.profile.url,this,"showChannel");}else{this.setTitle(loc.text("msg_loading"));if(_RSS[this.profile.id]){this.rss_item_data=_RSS[this.profile.id];var got=0;if(this.profile.url!=""){for(var i=0;i<this.rss_item_data.urls.length;i++){if(this.rss_item_data.urls[i].url==this.profile.url){got=i;break;}}}
if(this.rss_item_data.urls[got]){this.actUrlIdx=got;if(_RSS_SOURCE[this.profile.id]&&this.needLoad){this.showChannelFrom(_RSS_SOURCE[this.profile.id]);this.needLoad=false;}else{xmlRequest.send(this.rss_item_data.urls[got].url,this,"showChannel");this.isLoading==true;}
if(this.profile.url!=this.rss_item_data.urls[got].url){this.profile.url=this.rss_item_data.urls[got].url;this.save();}}}else{request.send({act:"get_item",id:this.profile.id},this);this.setContent(loc.text("msg_loading"));}}}},openChannel:function(wid,fid){feedId=fid;if(this.profile.opend==1){window.open(this.data.items[feedId]["link"]);}else{if(!this.isPreview){this.isInReader=true;rssreader.title='';if(this.profile.title!='')
rssreader.title=this.profile.title;rssreader.channelLastRefresh=this.lastRefresh;rssreader.openChannel(wid,feedId);}}},getItemsCount:function(){return Math.min(this.data.items.length,this.profile.cnt);},getAllItemsCount:function(){return this.data.items.length;},settingsAll:function(){var t=trim(this.getValue('title'));if(t!=this.profile.title){this.profile.title=t;this.showChannelTitle();this.save();}
if(this.profile.id==""){if(this.isLoading){this.setValue('select_url',this.profile['url'])}else{var url=trim(this.getValue('select_url'));if(url!=""){if(url.indexOf("http://")==-1){url="http://"+url;}
if(this.profile["url"]!=url){this.profile["url"]=url;this.save();this.iconLoaded=false;main.stopTimer(this.id);main.processTimer(this.id,this.profile.period*1000,true);this.refresh();}}else{this.setContent(loc.text("msg_empty"));}}}},showChannelTitle:function(){if(this.data){var countAll=this.getAllItemsCount();var count=this.getItemsCount();var readed=this.getReadedItemsCount();var h='';if(this.profile.id==''){if(!this.profile.title){this.profile.title=this.data.title;}
if(this.data.link)
h+='<a href='+this.data.link+' target="_blank">'+this.profile.title.substr(0,25)+'</a>';else
h+=this.profile.title.substr(0,25);if(countAll>readed)
h+='<span>&nbsp;('+(countAll-readed)+')</span>';}
else{if(this.data.link)
h+='<a href='+this.data.link+' target="_blank">';if(this.profile.title!=''){h+=this.profile.title.substr(0,25);}
else{h+=this.rss_item_data.title;this.profile.title=this.rss_item_data.title;}
if(this.data.link)
h+='</a>';if(countAll>readed)
h+='<span>&nbsp;('+(countAll-readed)+')</span>';}
this.setValue('title',this.profile.title);this.setTitle(h);}},getItemMd5:function(idx){var content=this.data.items[idx]["content"]?this.data.items[idx]["content"]:this.data.items[idx]["description"];var str=this.data.items[idx].title+content;return crc32(str);},showChannel:function(response){this.isLoading==false;if(response.responseText){this.data=toValue(response.responseText);var newMd5=[];if(this.data){var count=this.data.items.length;var l=this.profile["md5"].length;for(var i=0;i<count;i++){var md5=this.data.items[i]["md5"]=this.getItemMd5(i);var j=this.isReaded(md5);if(j!=null){this.data.items[i].isRead=1;newMd5.push(md5);l--;}}
this.profile["md5"]=newMd5;if(l!=0){this.save();}}
this.renderChannel();this.showChannelTitle();}else{this.showError();}},showChannelFrom:function(response){this.isLoading==false;if(response){this.data=toValue(response);var newMd5=[];if(this.data){var count=this.data.items.length;var l=this.profile["md5"].length;for(var i=0;i<count;i++){var md5=this.data.items[i]["md5"]=this.getItemMd5(i);var j=this.isReaded(md5);if(j!=null){this.data.items[i].isRead=1;newMd5.push(md5);l--;}}
this.profile["md5"]=newMd5;if(l!=0){this.save();}}
this.renderChannel();this.showChannelTitle();}else{this.showError();}},renderChannel:function(){if(this.data){var countAll=this.data.items.length;var countItems=this.getItemsCount();var self=this;var row;var h="";var loader,ico;var self=this;if(this.rss_item_data){if(this.rss_item_data.urls[this.actUrlIdx].rotate==1){countItems=1;}}
var num=this.page*countItems;if(this.profile.id==''){this.mid.firstChild.firstChild.firstChild.childNodes[2].innerHTML='<img id="ico" src="favicon.php?url='+this.profile["url"]+'" width="16"/>';}
else{this.mid.firstChild.firstChild.firstChild.childNodes[2].innerHTML='<img id="ico" src="favicon.php?url='+this.data.link+'" width="16"/>';}
var count=this.getItemsCount();this.setContent("");if(this.rss_item_data){if(this.rss_item_data.urls.length>1){var tmp_options=[];for(var i=0;i<this.rss_item_data.urls.length;i++){tmp_options.push({value:this.rss_item_data.urls[i].url,text:this.rss_item_data.urls[i].title});}
this.buildDomModel(this.elements.content,{tag:"div",className:"settings_section",childs:[{tag:"span",className:"settings_label",innerHTML:loc.text("rss_inp_change_url")},{tag:"select",id:"change_url",className:"settings_control",events:{onchange:"refresh()"},options:tmp_options}]});if(this.actUrlIdx){this.elements.change_url.value=this.rss_item_data.urls[this.actUrlIdx].url;this.profile.url=this.rss_item_data.urls[this.actUrlIdx].url;}}}
var currDate=new Date();var liClass='',tmp;var block=new Element('ul',{'styles':{'margin':'0px','list-style':'none'}});for(var i=num;i<(num+countItems);i++){var diffTime=-1;var item=this.data.items[i];tmp=i;if(this.data.items[i]){if(this.data.items[i].pubDate){var brokenpubDate=this.data.items[i].pubDate.split(' ');var mypubDate=this.data.items[i].pubDate;var myDate=new Date(mypubDate);diffTime=(currDate.getTime()-myDate.getTime())/1000;diffTime=Math.floor(diffTime);if(diffTime==1)
diffText=loc.text("rss_second");else
diffText=loc.text("rss_seconds");if(diffTime>=60){diffTime=Math.floor(diffTime/60);if(diffTime==1)
diffText=loc.text("rss_minute");else
diffText=loc.text("rss_minutes");if(diffTime>=60){diffTime=Math.floor(diffTime/60);if(diffTime==1)
diffText=loc.text("rss_hour");else
diffText=loc.text("rss_hours");if(diffTime>=24){diffTime=Math.floor(diffTime/24);if(diffTime==1)
diffText=loc.text("rss_day");else
diffText=loc.text("rss_days");if(diffTime>=30){diffTime=Math.floor(diffTime/30);if(diffTime==1)
diffText=loc.text("rss_month");else
diffText=loc.text("rss_months");if(diffTime>=12){diffTime=Math.floor(diffTime/12);if(diffTime==1)
diffText=loc.text("rss_year");else
diffText=loc.text("rss_years");}}}}}}
if(diffTime<0){diffTime="";diffText="";}
if(!this.profile.with_desc)
var tip=item.tip;else
var tip='';if(this.profile.with_desc&&item.enclosure&&item.enclosure.type=='img'){var _img=item.enclosure.url;_img=_img.replace(/ /gi,'%20');h='<div style="width:80px;height:60px;overflow:hidden;float:left"><img src='+_img+' style="max-width:80px;max-height:60px"></div><div style="margin-left:90px;"><span>'+item.title+'</span>'+((this.profile.with_date==1&&diffTime&&diffText)?' - '+diffTime+' '+diffText:'')+'</div><div style="margin-left:90px;">'+stripHTML(item.description).wordWrap(150)+'</div>';liClass='rss-thumbs'+(item.isRead==1?'-visited':'');}else{h=((this.profile.with_desc==1)?'<b>':'')+item.title+((this.profile.with_desc==1)?'</b>':'')+((this.profile.with_date==1&&diffTime&&diffText)?' - '+diffTime+' '+diffText:'')+((this.profile.with_desc==1)?'<br>'+stripHTML(item.description.wordWrap(150)):'');liClass='rss-box'+((this.profile.with_desc==1?'-detail':'')+(item.isRead==1?'-visited':''));}
if(this.rss_item_data){if(this.rss_item_data.urls[this.actUrlIdx].rotate==1){row=new Element('div',{'id':i,'html':"<center><img src='"+item.media_thumbnail+"'/></center>"});}
else{row=new Element('li',{'class':liClass,'html':h});}}
else{row=new Element('li',{'class':liClass,'html':h});}
row.addEvent('click',function(e,tmp){self.openChannel(self.id,tmp);}.bindWithEvent(this,tmp));if(self.profile.with_desc==0){row.addEvent('mouseover',function(e,tmp){self.showTip(e,tmp);}.bindWithEvent(this,tmp));row.addEvent('mouseout',function(){self.hideTip();});row.addEvent('mousemove',function(e,tmp){if(self.profile.with_desc==0)
self.showTip(e,tmp);}.bindWithEvent(this,tmp));}
row.inject(block);if(this.rss_item_data){if(this.rss_item_data.urls[this.actUrlIdx].rotate==1){if(!this.isPreview){main.stopTimer(this.id);if(this.page==countAll-1){main.timers[this.id]=setTimeout("main.getWidget("+this.id+").rssNextAgain()",this.rss_item_data.urls[this.actUrlIdx].rotate_seconds*1000);}
else{main.timers[this.id]=setTimeout("main.getWidget("+this.id+").rssNext()",this.rss_item_data.urls[this.actUrlIdx].rotate_seconds*1000);}}}}}}
if(this.page>0){row=new Element('div',{'html':"<div class='window_prev'>"+loc.text("rss_prev")+"</div><div class='window_prev_arrow'/>",'events':{'click':function(e){self.rssPrev();}}});row.inject(block);}
if(countAll-(num+countItems)>0){row=new Element('div',{'html':"<div class='window_next'>"+loc.text("rss_next")+"</div><div class='window_next_arrow'/>",'events':{'click':function(e){self.rssNext();}}});row.inject(block);}
block.inject(this.mid.firstChild.childNodes[3]);}
else if(!this.isLoading){this.showError();}},rssPrev:function(){this.page--;this.renderChannel();},rssNext:function(){this.page++;this.renderChannel();},rssNextAgain:function(){this.page=0;this.renderChannel();},showError:function(){this.setTitle(loc.text("msg_error"));this.setContent(loc.text("rss_msg_error"));},openSite:function(){if(this.data){window.open(this.data.link);}},onClose:function(){if(!this.isPreview&&rssreader.widgetId&&rssreader.widgetId==this.id){rssreader.close();}},isReaded:function(md5){for(var i=0;i<this.profile["md5"].length;i++){if(this.profile["md5"][i]==md5){return i;}}
return null;},setReaded:function(idx){var md5=this.data.items[idx].md5;var i=this.isReaded(md5);if(i==null){this.profile["md5"].push(md5);}},unsetReaded:function(idx){var md5=this.data.items[idx].md5;var i=this.isReaded(md5);if(i!=null){delete(this.profile["md5"][i]);}},processItemRead:function(idx){this.data.items[idx].isRead=1;this.setReaded(idx);this.showChannelTitle();},processItemUnread:function(idx){this.data.items[idx].isRead=0;this.unsetReaded(idx);this.showChannelTitle();},getReadedItemsCount:function(){var count=this.getItemsCount();var res=0;for(var i=0;i<count;i++){if(this.data.items[i].isRead==1){res++;}}
return res;},dispatchMsg:function(msg){switch(msg.status){case"rss_item_data":this.rss_item_data=msg.rss_item;var got=0;if(this.profile.url!=""){for(var i=0;i<this.rss_item_data.urls.length;i++){if(this.rss_item_data.urls[i].url==this.profile.url){got=i;break;}}}
if(this.rss_item_data.urls[got]){xmlRequest.send(this.rss_item_data.urls[got].url,this,"showChannel");this.isLoading==true;this.actUrlIdx=got;if(this.profile.url!=this.rss_item_data.urls[got].url){this.profile.url=this.rss_item_data.urls[got].url;this.save();}}
break;case"rss_item_data_deleted":this.setContent("<center>"+loc.text("rss_item_data_deleted")+"</center>");break;}},showTip:function(event,id){var options=Object.extend({delta_x:10,delta_y:10,zindex:1000},arguments[1]||{});var tipobj=$('tooltip');var tip=this.data.items[id].tip;var mX=event.page.x;var mY=event.page.y;var dim=tipobj.getSize();var elW=dim.x;var elH=dim.y;var win=window.getSize();this.options=options;if((elW+mX)>=(win.x-this.options.delta_x)-10){mX=mX-elW;mX=mX-this.options.delta_x;}else{mX=mX+this.options.delta_x;}
if((elH+mY)>=(win.y-this.options.delta_y)-10){mY=mY-elH;mY=mY-this.options.delta_y;}else{mY=mY+this.options.delta_y;}
if(tip){this.tags['tip'].set({'html':tip,'styles':{'position':'absolute','top':mY+'px','left':mX+'px','display':'block','text-align':'left','z-index':this.options.zindex}});}},hideTip:function(){this.tags['tip'].style.display='none';this.tags['tip'].innerHTML='';}});