/*
 * RSSWidget jQuery Plugin
 * Author: minabta.com (http://www.minabta.com)
 * Version: 0.5.0
 * Date: 2009/06/15
 * Licence: http://creativecommons.org/licenses/by-sa/3.0/
 * Description: 
 * 
 * This jQuery plug-in allows the users to embed an external RSS feed in 
 * their pages. In order to by-pass AJAX sandbox, the script uses an PHP 
 * script that retrieves the desired RSS feed and provides it to the 
 * plug-in as if the RSS feed was hosted in the same domain 
 * the plugin resides.
 * 
 * Based on previous work from:
 * 
 * http://visualrinse.com
 * 
 * See readme.txt for parameter usage, examples, license, know issues 
 * and more related info.
 */
(function ($) {
	$.fn.RSSWidget = function (options){
		var container = $(this);
		// Merge plugin parameters with default parameters
		var opts = $.extend({}, $.fn.RSSWidget.defaults, options);

		container.html($.fn.showLoading(opts));
		
		$.get(opts.proxyURI + '?' + opts.proxyURLParam + '=' + encodeURI(opts.url), function (xmlfeed) {
			var feed = new Object();
			// get channel data
			var resChannel = $(xmlfeed).find('channel');
			feed.channel = {title: $(resChannel).find('title').eq(0).text(), link: $(resChannel).find('link').eq(0).text(), description: $(resChannel).find('description').eq(0).text()};
			feed.items = [];
			//find each 'item' in the file and parse it
			$(xmlfeed).find('item').each(function() {
				//name the current found item this for this particular loop run
				var item = $(this);
				// grab the post title
				var title = $(item).find('title').text();
				// grab the post's URL
				var link = $(item).find('link').text();
				// next, the description
				var description = $(item).find('description').text();
				//don't forget the pubdate
				var pubDate = $(item).find('pubDate').text();
				feed.items.push({title: title, link: link, description: description,pubDate: pubDate});
			});
			container.html($.fn.RSSWidget.render(feed, opts));
		});
	};

	$.fn.RSSWidget.render = function (feed, opts) {
		// Render channel info
		
		var str = '';
		// Render items
		if (opts.maxItems != null)
		{
			if (feed.items.length > opts.maxItems)
			{
				// must truncate feed items array to max length
				feed.items.splice(opts.maxItems-1, feed.items.length - opts.maxItems);
			}
		}
		
		if (opts.showChannelInfo)
		{
			str += '<div class="' + opts.channelInfoClass + '">\n';
			str += '<a href="' + feed.channel.link + '" title="' + feed.channel.title+ '">'+ feed.channel.title + '</a>\n';
			str += '</div>\n';
		}
		
		$(feed.items).each(function () {
			var elem = this;
			var pubDt = new Date(Date.parse(elem.pubDate));
			str += '<div class="' + opts.itemClass + '">\n';
			str += '<a class="'+ opts.headingClass + '" href="' + elem.link + '" title="'+ elem.title + '">'+ elem.title +  '</a>\n';
			str += '<p class="' + opts.datePubClass + '">' +  pubDt.toLocaleString() + '</p>';
			str += '<p class="' + opts.descriptionClass + '">' + elem.description + '</p>\n';
			str += '</div>\n';
		});
		
		return str;
	};
	
	$.fn.RSSWidget.defaults = {
		url: 'http://vhg-design.com/blog/feed/',
		msgLoading: 'loading...',
		msgLoadingClass: 'rss_msgloading_class',
		showChannelInfo: true,
		channelInfoClass: 'rss_channelinfo_class',
		itemClass: 'rss_item_class',
		headingClass: 'rss_heading_class',
		descriptionClass: 'rss_description_class',
		datePubClass: 'rss_pubdate_class',
		proxyURI: 'proxy.php',
		proxyURLParam: 'url',
		maxItems: null
	};
	
	$.fn.showLoading = function (opts) {
		return '<div class="' + opts.msgLoadingClass + '">'+ opts.msgLoading +'</div>\n';
	};
}) (jQuery)