Xsn.RssElements.FeedPanel = function(el) {
    Xsn.RssElements.FeedPanel.superclass.constructor.call(this, el, {
        rootVisible:false,
        loader: new Ext.tree.TreeLoader({dataUrl:'index.php?action=getFeeds'}),
        lines:false,
        collapseFirst:false
    });

    this.setRootNode(new Ext.tree.AsyncTreeNode({
        text: 'Rss feeds', 
        draggable:false, 
        id:'rss-feeds-root'
    }));
	this.getLoader().on('load', this.selectFirstFeed,this);					

    this.getSelectionModel().on({
        'beforeselect' : function(sm, node){
             return node.isLeaf();
        },
        'selectionchange' : function(sm, node){
            if(node){
                this.fireEvent('feedselect', node.attributes);
            }
        },
        scope:this
    });

    this.addEvents({feedselect:true});
	this.render();

};

Ext.extend(Xsn.RssElements.FeedPanel, Ext.tree.TreePanel, {

	selectFirstFeed : function(){
	    if (this.root.firstChild){
			this.getSelectionModel().select(this.root.firstChild);
		}
	    this.getLoader().removeListener('load', this.selectFirstFeed);			
	},

    addFeed : function(attrs, inactive, preventAnim){
        var exists = this.getNodeById(attrs.url);
        if(exists){
            if(!inactive){
                exists.select();
                exists.ui.highlight();
            }
            return;
        }
        Ext.apply(attrs, {
            iconCls: 'feed-icon',
            leaf:true,
            cls:'feed',
            id: attrs.url
        });
        var node = new Ext.tree.TreeNode(attrs);
        this.root.appendChild(node);
        if(!inactive){
            if(!preventAnim){
                Ext.fly(node.ui.elNode).slideIn('l', {
                    callback: node.select, scope: node, duration: .4
                });
            }else{
                node.select();
            }
        }
        return node;
    }

});
