/**
 * Plugin: jquery.zRSSFeed
 * 
 * Version: 1.0.1
 * (c) Copyright 2010, Zazar Ltd
 * 
 * Description: jQuery plugin for display of RSS feeds via Google Feed API
 *              (Based on original plugin jGFeed by jQuery HowTo)
 * 
 * History:
 * 1.0.1 - Corrected issue with multiple instances
 *
 **/

(function($){

	var current = null; 
	
	$.fn.rssfeed = function(url, options) {	
	
		// Set pluign defaults
		var defaults = {
			limit: 3,
			header: false,
			footer: false,
			date: false,
			content: true,
			snippet: false,
			showerror: false,
			errormsg: '',
			key: null
		};  
		var options = $.extend(defaults, options); 
		
		// Functions
		return this.each(function(i, e) {
			var $e = $(e);
			
			// Add feed class to user div
			if (!$e.hasClass('rssFeed')) $e.addClass('rssFeed');
			
			// Check for valid url
			if(url == null) return false;

			// Create Google Feed API address
			var api = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + url;
			if (options.limit != null) api += "&num=" + options.limit;
			if (options.key != null) api += "&key=" + options.key;

			// Send request
			$.getJSON(api, function(data){
				
				// Check for error
				if (data.responseStatus == 200) {
	
					// Process the feeds
					_callback(e, data.responseData.feed, options);
				} else {

					// Handle error if required
					if (options.showerror) {
						if (options.errormsg != '') {
							var msg = options.errormsg;
						} else {
							var msg = data.responseDetails;
						};
						$(e).html('<div class="rssError"><p>'+ msg +'</p></div>');
					}
				};
			});				
		});
	};
	
	// Callback function to create HTML result
	var _callback = function(e, feeds, options) {
	
		if (!feeds) {
			return false;
		}
		
		var html = '';	
		var row = 'odd';	
		
		// Add header if required
		if (options.header)
			html +=	'<h2><a href="'+feeds.link+'" title="'+ feeds.description +'">More</a>'+ feeds.title +'</h2>';

		// Add feeds
		for (var i=0; i<feeds.entries.length; i++) {
			
			// Get individual feed
			var entry = feeds.entries[i];
			
			// Format published date
			var entryDate = new Date(entry.publishedDate);
			var pubDate = entryDate.toLocaleDateString() + ' ' + entryDate.toLocaleTimeString();
			
			// Image Vars
			var entryThumbnail = '';
			var entryImage = entry.content.match(/<img[^>+]*>/i);
			
			// Create Thumbnail from Images
			if (entryImage) {
				var entryImageSrc = $(entryImage[0]).attr("src");
				var entryImageAlt = $(entryImage[0]).attr("alt");
				var entryImageWidth = $(entryImage[0]).attr("width");
				var entryImageHeight = $(entryImage[0]).attr("height");
				if (entryImageWidth || entryImageHeight > 50) {
					var entryThumbnail = '<div class="image"><a href="'+entry.link+'"><img src="'+entryImageSrc+'" alt="'+entryImageAlt+'" width="100%" height="100%" /></a></div>';
				}
			}
			
			// Create New Row
			html += '<div class="row '+row+'">'+entryThumbnail+'<div class="description"><h3 class="heading"><a href="'+entry.link+'"">'+entry.title+'</a></h3>';
			
			// Add Date
			if (options.date) {
				html += '<div>'+pubDate+'</div>';
			}
			
			// Add Content
			if (options.content) {
				if (options.snippet && entry.contentSnippet != '') {
					var content = entry.contentSnippet;
				} else {
					var content = entry.content;
				}
				var strippedContent = $.trim(content.replace(/(<([^>]+)>)/ig,""));
				html += '<p class="blurb">'+ strippedContent +'</p>';
			}
			
			// End Row
			html += '</div></div>';
			
			// Update Class
			if (row == 'odd') {
				row = 'even';
			} else {
				row = 'odd';
			}
			
		}
		
		if(options.footer) {
			html +=	'<div class="more"><a href="'+feeds.link+'" title="'+ feeds.description +'">'+ feeds.title +'</a></div>';
		}
		
		$(e).html(html);
		
		$("div.widget").each(function(){
			$("div.row", this).hover(function(){
				$(this).addClass("hover");
			}, function(){
				$(this).removeClass("hover");
			});
		});
	
	};
	
})(jQuery);

