
 
var RssFeeder = new Class(
{
      options: {
                  rss_periodical    : null
               },
 
      initialize: function(options)
                  {
 
                    this.setOptions(options); 
                    this.ajax          = null;
 
                    if(!this.options.url ||  this.options.url ==null) { alert('No RSS url given.');return;}
                    if(!this.options.rssdiv ||  this.options.rssdiv ==null) { alert('No element given to inject RSS feed in.');return;}
                    this.rssdiv = $(this.options.rssdiv);
                    if(!this.rssdiv) { alert('RSS div "'+this.options.rssdiv+'" does not exist.');return;}
 
                    // click events
                    //reload event
                    if(this.options.reload && this.options.reload!=null && (this.reload = $(this.options.reload))) 
                    { 
                      this.reload.addEvent('click',  this.refresh.bind(this));
                    }
                    //start auto-reloading event
                    if(this.options.start && this.options.start!=null && (this.start = $(this.options.start))) 
                    { 
                      this.start.addEvent('click',  this.refresh.bind(this));
                        // stop  auto-reloading event
                        if(this.options.stop && this.options.stop!=null && (this.stop = $(this.options.stop))) 
                        { 
                          this.stop.addEvent('click',  this.refresh.bind(this));
                        }
                    }
 
                     this.startajax();
                  },
 
        startit: function(){
                            	$clear(this.options.rss_periodical); 	// prevent numerous requests
 
                            	/* styles */
                            	this.start.setStyle('font-weight', 'bold');
                            	this.stop.setStyle('font-weight', 'normal');
                            	this.rssdiv.addClass('ajax-loading'); 
                            	this.rssdiv.setStyle('opacity', 0.2);
 
                              /* request new page  */                             
                            	this.options.rss_periodical = this.refresh.periodical(this.options.timer * 1000, this); 	// the refresh period starts here. time in milliseconds 
                            	this.ajax.request($time()); // 1st request
                             },          
 
        stopit: function()
                {
                     	this.start.setStyle('font-weight', 'normal');
                    	this.stop.setStyle('font-weight', 'bold');
 
                    	$clear(this.options.rss_periodical); 	// stop timed ajax
                    	this.ajax.cancel(); 	// stop request - in case it was waiting for a response
 
                 },
 
        startajax: function()
                   {
                    var ajax2 = new Ajax(this.options.url, { 
                                            	/*update: rssdiv,*/
                                            	method: 'get',
                                            	evalScripts: false,
                                            	onComplete: this.createTable.bind(this),
                                            	onCancel: function() 
                                            	          {
                                                	           this.rssdiv.removeClass('ajax-loading'); 
                                                           	this.rssdiv.setStyle('opacity', 1);
                                            	          }
                                                }, this);
 
                      this.ajax = ajax2;  
                      randomUrlElement = $time() + $random(0, 100);
                	  this.rssdiv.addClass('ajax-loading');	// activate 'loading' spinner
                   	this.rssdiv.setStyle('opacity', 0.2);
                    this.ajax.request(randomUrlElement); 	
                    },
 
        refresh : function(bar)
                  {
                      randomUrlElement = $time() + $random(0, 100);
                 	    this.rssdiv.addClass('ajax-loading');	// activate 'loading' spinner
                     	this.rssdiv.setStyle('opacity', 0.2);
                	    this.ajax.request(randomUrlElement); 	
                  },
 
        createTable: function(responseText, responseXML) 
                    {		
                        var tabel = null;
                        if (window.ActiveXObject) // responseXML is leeg in IE7 !
                        {   
                            var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                            xmlDoc.loadXML(responseText);
                            responseXML = xmlDoc;
                        }    
 
                        // feed data
                    		var feed_title       = ( responseXML.getElementsByTagName("title")[0].firstChild.data);
                    		var feed_description = ( responseXML.getElementsByTagName("description")[0].firstChild.data);
                    		var feed_link        = ( responseXML.getElementsByTagName("link")[0].firstChild.data);
 
                        // build new html table
 
                    		tabel = new Element('table');
 
                        tabel.addClass('rsstable');
//                        tabel.setStyles({display: 'block',border: '1px solid black', width:'auto !important'});
 
 
                     		var tabelheader = new Element('thead').injectInside(tabel);                            		
                     		var headerrow = new Element('tr',{ id: 'rss_hrow'}).injectInside(tabelheader);                            		
                     		var headeritem = new Element('th',{}).setHTML(feed_title).injectInside(headerrow);                            		
                     		var tabelbody = new Element('tbody').injectInside(tabel);                            		
 
 
 
 
                        // walk all items
                        newsitems = responseXML.getElementsByTagName('item');
                        alternate=0;    
                        for (i=0; i < newsitems.length; i++)
                        {
                            if (alternate) alternate=0; else alternate=1;
 
                            //read data
                    	    	var item_title = newsitems[i].getElementsByTagName("title")[0].firstChild.data;
                    	    	var item_description = newsitems[i].getElementsByTagName("description")[0].firstChild.data;
                    	    	var item_guid = newsitems[i].getElementsByTagName("guid")[0].firstChild.data;
                    	    	var item_link = newsitems[i].getElementsByTagName("link")[0].firstChild.data;
                    	    	var item_pubdate = newsitems[i].getElementsByTagName("pubDate")[0].firstChild.data;
 
                            // inject in table
                            var itemrow = new Element('tr',{
                                                          id: 'rssrow'+i
                                        			}).injectInside(tabelbody);  
                            var itemcell = new Element('td',{
                                                }).injectInside(itemrow);  
                                	itemcell.addClass('alternate'+alternate);
                         		new Element('a',{
                                        			'href' : item_link,
                                        			'styles' : { 
                                        			}
                                        		}).setHTML(item_title).injectInside(itemcell);
                        }
 
                     this.rssdiv.empty().removeClass('ajax-loading'); 
                    	this.rssdiv.setStyle('opacity', 1);
                     tabel.injectInside(this.rssdiv);
 
                    }
 
 
});