/**********************
 * SP_Rss
 *
 * @description Utility class for Rss portlet
  * @params Object
  * Depending on the prototip 2.0 library
 */
var SP_Rss = Class.create({
    initialize : function() {
        this.id = "";
        this.url = "";
        this.feed = null;
        this.skin = null;
        this.skin_id = "";
        this.sp_portlet = null;
        this.loader = null;
        this.content = null;
    },
    /* init this on demand */
    setup : function() {
        if(this.feed) return;
        var props = arguments[0] || {};           
        this.id = props.id || this.id;
        this.url = props.url || this.url;

        this.feed = $('feed_' + this.id) || new Element('div');
        this.skin = this.feed.up('.SP_TopContainer') || new Element('div');
        this.skin_id = this.skin.identify();
        this.sp_portlet = props.sp_portlet || new SP_Portlet(this.skin_id);
        this.loader = this.feed.down('.f_loader');
        this.content = this.feed.down('.f_content');
    },
    /* initialize loading of feed */
    load : function() {
        this.setup();
        this.loader.show();
        var options = {
            method     : 'get',
            parameters : {},
            onSuccess  : this.onSuccess.bind(this),
            onFailure  : this.onFailure.bind(this)
        };
        new Ajax.Request(this.url, options);
    },
    onSuccess : function(response) {
        // Update feed container with ajax response
        this.content.update(response.responseText);
              
        // Build all tooltips and format title for this portlet
        $$('li.item_' + this.id).each(function(element){
            var title = element.down('.title');
            var titleHtml = title.innerHTML.strip();
            title.innerHTML = titleHtml.truncate(35);
            var description = element.down('.description');
            descriptionHtml = description.innerHTML.strip();
            
            var descriptionHtml = descriptionHtml.replace(/((&lt;embed)|(&lt;object)|(&lt;img)|(&lt;div)|(&lt;br)|(&lt;table)|(&lt;tr)|(&lt;td))(?:.|\s)*?&gt;/g, ' ').replace(/(&lt;a)(?:.|\s)*?\/a&gt;/g, '').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&').truncate(1500);
            var tipWidth = Math.max(300, Math.round(descriptionHtml.length/3));
            element.tip = new Tip(element, descriptionHtml, {
                width: tipWidth + 'px', 
                border: 4, 
                radius: 4,
                hideOthers: true,
                delay: 0,
                title: titleHtml
            });
        });
        this.loader.fade({duration: 0.3, delay: 0});
        this.content.blindDown({duration: 0.5, delay: 0.4}); 
        this.updateTitle(this.feed.down('.feed_title'));
    },
    onFailure : function(response) {
        this.content.hide();
        this.loader.fade({duration: 0.3, delay: 0});
        this.feed.down('.f_error_content').blindDown({duration: 0.5, delay: 0.4}); 
        this.updateTitle(this.feed.down('.f_error_title'));
    },
    updateTitle : function(element) {
        if(!element || element.innerHTML.length == 0) return;
        this.skin.down('.portlet_title').innerHTML = element.innerHTML;
        this.sp_portlet.setIcon(SP_IMG_DIR + '/serviceportal/feed.png');
        //this.sp_portlet.setColor('3e3e3e'); 
        this.sp_portlet.setTitle();    
    }
});
