jQuery.fn.extend({
	feed: function( settings ){
		settings = jQuery.extend({}, jQuery.fn.feed.settings, settings );
	
		var self = this;
	    if(settings.url) {
	    	$.ajax({
	            type: "GET",
	            url: "download.php?id=" + settings.url,
	            dataType: "xml",
	            success: function( data ) {
	    			self.each(function() {
	    				if( $("feed", data).length == 1 ) 
	    					var feed = new AtomFeed( data );      
	    				else if( $("channel", data).length == 1 ) 
	    					var feed = new RSSFeed( data ); 
	    				
	    				if( feed && jQuery.isFunction( settings.success ) ) 
	    					settings.success( this, feed, settings );
	    			});
	            }
	        });
	    }	
	    
		function FeedItem(){
			this.title = "";
	        this.link = "";
	        this.description = "";
	        this.updated = "";
	        this.id = "";
		}
		
	    function Feed(data){
	    	this.items = new Array();
	    	this.title = $(data).find("title:first").text();
	    	this.link = $(data).find("link:first").text();
	    }
	    
		function RSSFeed(data){
			this.constructor(data);
			this.description = $(data).find("description:first").text();
	        this.updated = $(data).find("lastBuildDate:first").text() ? $(data).find("lastBuildDate:first").text() : $(data).find("pubDate:first").text();
	        
	        var feed = this;
	        $("item", data).each( function() {
	        	var item = new FeedItem();
	            
	            item.title = $(this).find("title").eq(0).text();
	            item.link = $(this).find("link").eq(0).text();
	            item.description = $(this).find("description").eq(0).text();
	            item.updated = $(this).find("pubDate").eq(0).text() ? $(this).find("pubDate").eq(0).text() : feed.updated;
	            
	            feed.items.push(item);
	        });			
		}
		RSSFeed.prototype = new Feed();
		
		function AtomFeed(data){
			this.constructor(data);
	        this.description = $(channel).find("subtitle:first").text();
	        this.updated = $(channel).find("updated:first").text();
	        
	        var feed = this;
	        $("entry", data).each( function() {
	        	var item = new FeedItem();
	            
	            item.title = $(this).find("title").eq(0).text();
	            item.link = $(this).find("link").eq(0).text();
	            item.description = $(this).find("content").eq(0).text();
	            item.updated = $(this).find("updated").eq(0).text() ? $(this).find("updated").eq(0).text() : feed.updated;
	            
	            feed.items.push(item);
	        });	
		}
		AtomFeed.prototype = new Feed();
	}
});



/**
 * Default settings.
 * Already implements a cycle through all feed-items on success.
 */
jQuery.fn.feed.settings = {
	
	url: null,
	timeout: 10000,
	
	success: function( target, feed, settings ){
		var index = 0;
		var mouseover = 0;
		
		function cycleItems(){
			if( mouseover )
				var timeout = 1000;
			else{
				var timeout = settings.timeout;

				if( ++index >= feed.items.length )
					index = 0;
			
				$(target).fadeOut( 500, function(){ 
					$(target).html( settings.getItemHTML( feed, index ) ).fadeIn( 500 ) 
				});
			}
			
			setTimeout( cycleItems, timeout );
		}

		$(target).mouseenter( function(){ mouseover = 1 } );
		$(target).mouseleave( function(){ mouseover = 0 } );
		cycleItems();
	},
	
 	getItemHTML: function( feed, index ){
		var html = "";		
		var date = new Date( feed.items[index].updated );
		
		html += "<div><a href='"+ feed.items[index].link +"' target='_blank'>" + feed.items[index].title + "</a></div>";
		html += !isNaN(date) ? "<div style='font-size:10px'>"+ date.toLocaleString(  ) + "</div>" : "";
		html += "<div>"+ feed.items[index].description + "</div>";
		
		return html;
	}
	
};