/* ------------------------------------------------------------------------
 * Plugin: RSSFeed plugin works in conjunction with jQuery feed parser, jFeed
 * Author: Ryan Molley
 * Company: Red Hawk Technologies (http://www.redhawk-tech.com)
 * Version: 1.0
 * Completion: September 10, 2011
 * ------------------------------------------------------------------------- */

(function(jQuery) {
		  
	jQuery.fn.rssFeed = function(rssURL, options) {
	
		var o = jQuery.extend({}, jQuery.fn.rssFeed.defaults, options, jQuery.expr[':'], 
		{
			
			outputTable: function(object) {
				jQuery.getFeed({
					url: rssURL,
					success: function(feed) {
						var html = '<table class="rssTable">';
						var	end;
						
						(feed.items.length > o.count && !o.showall) ? end = o.count : end = feed.items.length;
						
						for (var i = 0; i < end; i++) {
							var rssClass, showtitle, showdescription, images, showreadmore;
							
							var item = feed.items[i];
								
							o.showtitle ? showtitle = o.getTitle(item) : showtitle = '';
							o.images ? images = o.getImages(item) : images = '';
							o.showdescription ? showdescription = o.getDescription(item) : showdescription = '';
							
							i == (end - 1) ? rssClass = 'last' : rssClass = '';
							
							if(o.images) {
								images != '' ? html += '<tr class="rssContainer">' + images + '<td>' + showtitle + showdescription + '</td></tr>' : html += '<tr class="rssContainer"><td colspan="2">' + showtitle + showdescription + '</td></tr>';
								html += '<tr><td colspan="2" class="rssBottomLine ' + rssClass + '"></td></tr>';
							}
							else {
								html += '<tr class="rssContainer"><td>' + showtitle + showdescription + '</td></tr>';
								html += '<tr><td class="rssBottomLine ' + rssClass + '"></td></tr>';
							}
							
						}
						
						html += '</table>';
						
						jQuery(object).append(html);
					}
				});
			},
			
			outputList: function(object) {		
				jQuery.getFeed({
					url: rssURL,
					success: function(feed) {
						var html = '<ul class="rssList">';
						var	end;
							
						(feed.items.length > o.count && !o.showall) ? end = o.count : end = feed.items.length;
						
						for (var i = 0; i < end; i++) {
							var rssClass, showtitle, showdescription, images, showreadmore;
								
							var item = feed.items[i];
								
							o.showtitle ? showtitle = o.getTitle(item) : showtitle = '';
							o.images ? images = o.getImages(item) : images = '';
							o.showdescription ? showdescription = o.getDescription(item) : showdescription = '';
							
							i == (end -1) ? rssClass = 'last' : rssClass = '';
							
							html += '<li class="rssContainer ' + rssClass + '">' + images + showtitle + showdescription + '</li>';
						}
						
						html += '</ul>';
						
						jQuery(object).append(html);
					}
				});
			},
			
			getTitle: function(item) {
				var title, showtitlelink;
				
				o.titlelength != null && (item.title.length > o.titlelength) ? title = item.title.substring(0, o.titlelength) + ' &#8230;' : title = item.title;
				o.showtitleaslink ? showtitlelink = '<a class="rssTitle" target="_self" href="' + item.link + '">' + title + '</a>' : showtitlelink = title;
				o.wraptitle != null ? showtitle = '<' + o.wraptitle + '>' + showtitlelink + '</' + o.wraptitle + '>' : showtitle = showtitlelink;
					
				return showtitle;
					
			},
			
			getImages: function(item) {
				var width;
				var imgSrc = jQuery(item.description).find('img:first').attr('src');
				var actualimgwidth = jQuery(item.description).find('img:first').attr('width');
				
				((actualimgwidth != null && actualimgwidth != 0) && actualimgwidth < o.imagewidth) ? width = actualimgwidth : width = o.imagewidth;
				
				if(o.table)
					imgSrc != null ? images = '<td><img class="rssImage" alt="' + item.title + '" src="' + imgSrc + '" width="' + width + '" /></td>' : images = '';
				else
					imgSrc != null ? images = '<img class="rssImage" alt="' + item.title + '" src="' + imgSrc + '" width="' + width + '" />' : images = '';
					
				return images;
				
			},
			
			getReadMore: function(item) {
				var readmorenewline;
				
				if(o.showreadmore) {
					o.readmorenewline ? readmorenewline = '<br />' : readmorenewline = '';
					showreadmore = readmorenewline + ' <a class="rssLink" target="_self" href="' + item.link + '">Read More</a>';
				}
				else
					showreadmore = '';
					
				return showreadmore;
					
			},
			
			getDescription: function(item) {
				var description;
				
				if(o.showdescriptionhtml)
					showdescription = item.description;
				else {
					(o.descriptionlength == null || o.descriptionlength > jQuery(item.description).text().length) ? description = jQuery(item.description).text() : description = jQuery(item.description).text().substring(0, o.descriptionlength) + ' &#8230;';
					o.removeptags ? showdescription = description + o.getReadMore(item) : showdescription = '<p class="rssDescription">' + description + o.getReadMore(item) + '</p>';
				}
					
				return showdescription;
					
			}
			
		});
		
		o.table ? o.outputTable(jQuery(this)) : o.outputList(jQuery(this));
		
	};
	
	jQuery.fn.rssFeed.defaults = {
		showall: false,
		count: 5,
		showtitle: true,
		showtitleaslink: true,
		wraptitle: null,
		titlelength: null,
		showdescription: false,
		showdescriptionhtml: false,
		descriptionlength: 165,
		table: false,
		showreadmore: true,
		readmorenewline: true,
		removeptags: false,
		images: false,
		imagewidth: 180
	}

})(jQuery);
