__SimpleRSS = DefineClass({

    url: null,
    NewsInRSS: 5,

    init: function(id) {
        this.lang = Locale.Gadgets.SimpleRSS;
        this.Core = new __StdModuleCore(this, id, 'novosti2', this.lang.title, this.lang.desc);
        var Item = this.Core.AddOption(this.lang['FeedAddress']);
        this.URL= ce('input', Item, { type: 'text' } );
        ce('br', Item);
        ce('input', Item, { type: 'button', value: 'Добавить RSS', className: 'button', onclick: this.AddRSS.bind(this) });
        this.Content.className += ' MultiRSS';
        this.Content.update('<div class="message">'+ this.lang.msg['FeedIsUndefine'] +' <a href="javascript:void(0)">'+ Locale['ToAdd'] +'?</a></div>');
        var OpenSettingsLink = this.Content.getElementsByTagName('a')[0];
        OpenSettingsLink.onclick = this.OpenSettings.bind(this);
        oThis = this;
        OpenSettingsLink.onmouseover = function() { Tooltip.Show(oThis.lang['ShowOptions'], this, 200) };
        OpenSettingsLink.onmouseout = function() { Tooltip.Hide() };

        var Item = this.Core.AddOption(this.lang['NumOfNews']);
        this.ItemCount = ce('select', Item, { innerHTML: '', onchange: function() {
            this.NewsInRSS = parseInt(this.ItemCount.value);
            if (this.RSSContent) this.RedrawContent(this.RSSContent);
            Update.request('SimpleRSS', 'module', 'SetItemsCount', { id: this.id }, { count: this.NewsInRSS });
        }.bind(this)});
        ce('option', this.ItemCount, { value: '1', innerHTML: '1' });
        ce('option', this.ItemCount, { value: '3', innerHTML: '3' });
        ce('option', this.ItemCount, { value: '5', innerHTML: '5' });
        ce('option', this.ItemCount, { value: '10', innerHTML: '10' });
        ce('option', this.ItemCount, { value: '15', innerHTML: '15' });
        ce('option', this.ItemCount, { value: '20', innerHTML: '20' });
        ce('option', this.ItemCount, { value: '0', innerHTML: this.lang['All']});
                
       if(this.FromProfile) this.Content.update('<div class="Error">'+this.lang['Loading']+'</div>');
                
      this.Core.AddUpdater('UpdateRSS'+this.id, this.Update.bind(this), 60);
    },

    onCreateFromProfile: function() {    	   	
       Update.request('SimpleRSS', 'module', 'RequestEnvironment', { id: this.id });                
    },

    OpenSettings: function() {
        this.Core.ToggleOptions();
        this.Options.getElementsBySelector('[type="text"]')[0].focus();
    },

    Update: function() {
        if (this.url) Update.request('SimpleRSS', 'module', 'ForceUpdate', { id: this.id }, {});
    },

    AddRSS: function() {
        
        var url = $F(this.URL);
        if(!url) return false;        
        this.Core.ToggleOptions();        
        this.Content.innerHTML = '<div class="Error">'+this.lang['Loading']+'</div>';
        
        if (!(/http:\/\//).test(url)) {
            url = 'http://'+url;
            this.URL.value = url;
        }
        Update.request('SimpleRSS', 'module', 'GetRSS', { id: this.id }, { url: url }, true);
    },

    RedrawContent: function(item, url) {
    	
    	if(!item.channel || !item.channel.item) return false;
    	
        var title = item.channel.title;
        if (title.length>13) title = title.substr(0,13)+'...';
        this.Core.SetHeader(title);
        var oThis = this;
        var ItemsCount = item.channel.item.length;
        var Current = this.NewsInRSS == 0 ? ItemsCount : this.NewsInRSS;
        this.Content.innerHTML = '<b>'+item.channel.title+' ('+Current+'/'+ItemsCount+')</b>';
        var ItemsBlock = ce('ul', this.Content, { className: 'ItemsBlock' });

        var count = 0;
        for (var j=0;j!=item.channel.item.length;j++) {

            if (count>this.NewsInRSS-1 && this.NewsInRSS!=0) return;
            count++;
            var OneItem = item.channel.item[j];
            var time = getFullDate(OneItem.pubDate * 1000)
            var title = OneItem.title + ', <small>'+time+'</small>';
            var aaa = ce('li', ItemsBlock, {});
            ce('a', aaa, { id: 'Module.'+this.id+'.'+j, href: '#', innerHTML:  title,
                onmouseover: function() {
                	
                	if(!oThis.RSSContent.channel) return false;
                	
                    var id = this.id.split('.');
                    var text = '<h1>'+oThis.RSSContent.channel.item[id[2]].title+'</h1><small>'+getFullDate(oThis.RSSContent.channel.item[id[2]].pubDate * 1000)+'</small><p>'+oThis.RSSContent.channel.item[id[2]].description+'</p>';
                    if (oThis.RSSContent.channel.item[id[2]].description) Tooltip.Show(text, this, 300);
                },
                onmouseout: function() {
                    Tooltip.Hide();
                },
                onclick: function() {
                    var id = this.id.split('.');
                    var TabID = Desktop.NewTab('cMain', oThis.RSSContent.channel.item[id[2]].link.substr(0,15)+'...', null, null, oThis.RSSContent.channel.item[id[2]].link);
                    Desktop.SetActiveByID('cMain', TabID);
                    return(false);
                }
            });
        }
    },

    ParseErrors: function(data) {
        for (var i in data) {
            this.url = '';
            this.RSSContent = null;
            this.Content.innerHTML = '<div class="Error">'+ this.lang.msg['FeedTempNA'] +'</div>';
        }
        return(false);
    },

    Relay: function(r) {
        if (r.errors) return(this.ParseErrors(r.errors));
        switch (r.event.action) {
            case 'GetRSS':
                if (r.variables.content) {
                    this.RedrawContent(r.variables.content, r.variables.url);
                    this.URL.value = r.variables.url;
                    this.RSSContent = r.variables.content;
                    this.url = r.variables.url;
                }
            break;
            case 'RequestEnvironment':            
            	
                this.NewsInRSS = r.variables.count;
                for (var i=0;i!=this.ItemCount.childNodes.length;i++) {
                    var current = this.ItemCount.childNodes[i];
                    if (current.value == this.NewsInRSS) current.selected = 'selected';
                }
                if (r.variables.content) {
                    this.RedrawContent(r.variables.content, r.variables.url);
                    this.URL.value = r.variables.url;
                    this.RSSContent = r.variables.content;
                    this.url = r.variables.url;
                }else{
					this.Content.update('<div class="message">'+ this.lang.msg['FeedIsUndefine'] +' <a href="javascript:void(0)">'+ Locale['ToAdd'] +'?</a></div>');
        			var OpenSettingsLink = this.Content.getElementsByTagName('a')[0];
        			OpenSettingsLink.onclick = this.OpenSettings.bind(this);
                }
            break;
        }
    }
});