var FeedWindow;
var rssItemsCount;
var rssItemsURL;
var rssItemsName;

function RSSItem () {
    /* rss List */
    this.rssList  = [];
    this.rssCount = 0;
    this.rssTpl   = new Ext.Template(
            '<p><strong>{pubDate} - <a href="{link}" target="_blank">{title}</a></strong><br />',
            '{description}',
            '</p>'
    );
    this.headerTpl = new Ext.Template(
        '<p><strong>{pubDate} - <a href="{link}" target="_blank">{title}</a></strong><br />',
            '{description}',
        '</p>'
    );
}

 function PluginRSSFeed(url,count){
     rssItemsURL = url;
     rssstore.setBaseParam('feed',url);
     rssItemsCount = count;
     rssstore.load();
 }

function cleanDate(str){
    var mill = Date.parse(str);
    var date = new Date(mill);
    return date.format("m/d/Y");
}

RSSItem.prototype.process = function(e) {
    this.rssCount = e.items.length;

    var maxcount = (e.items.length > rssItemsCount) ? rssItemsCount : e.items.length;

    for(var x=0;x<maxcount;x++){
        e.items[x].data.pubDate = cleanDate(e.items[x].data.pubDate);
        this.rssTpl.append("rssfeed", e.items[x].data);
    }
}

Ext.onReady(function(){
   
    rssstore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: 'index.php?fuse=home&action=DoDashboardPluginAction&plugin=rssreader&pluginAction=feedproxy'
        }),
        reader: new Ext.data.XmlReader(
            {record: 'item'},
            ['title', 'author', {name:'pubDate', type:'date'}, 'link', 'description', 'content']
        )
    });
    rssstore.setDefaultSort('pubDate', "DESC");
    rssstore.on('load', function(sm, index, record){
        rssItem = undefined;
	rssItem = new RSSItem();
        rssItem.process(sm.data);

        if(rssItemsName!=undefined){
            document.getElementById('rssfeedlink').innerHTML = "<a href='"+rssItemsURL+"'>"+rssItemsName+"</a>";
            document.getElementById('rssfeedlinkimage').innerHTML = "<a href='"+rssItemsURL+"' target='_blank'><img alt='"+rssItemsName+"' src='../images/rss.gif' width='16' height='11' border='0' /></a>";
        }
    }, this, {buffer:250}); 
});