/**
 * 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: 10,
			header: true,
			titletag: 'h4',
			date: true,
			content: true,
			snippet: true,
			showerror: true,
			linkToSource: false,
			errormsg: '',
			pageContext:'',
			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&output=json_xml&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,data.responseData.xmlString, 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,xmlString ,options) {
		if (!feeds) {
			return false;
		}
		var html = '';
		var row = 'odd';

		// Add header if required
		if (options.header)
			//For GC only modification
			/*html +=	'<div class="rssHeader">' +
				'<a href="'+feeds.link+'" title="'+ feeds.description +'">'+ feeds.title +'</a>' +
				'</div>';*/
			html +=	'<div class="rssHeader">' +
				'<a href="'+feeds.link+'" title="'+ feeds.description +'">News Headlines provided by The Golf Channel</a>' +
				'</div>';
		// Add body
		html += '<div class="rssBody">' +
			'<ul>';
		var guid_temp = "";
		var storyIdArray = new Array();
	
		
		// Add feeds
		for (var i=0; i<feeds.entries.length; i++) {
			guid_temp = "";
			
			// Get individual feed
			var entry = feeds.entries[i];

			// Format published date
			var entryDate = new Date(entry.publishedDate);
			var pubDate = entryDate.toLocaleDateString() + ' ' + entryDate.toLocaleTimeString();

			// Add feed row
			if (!options.linkToSource){
				if(typeof(entry.guid)!='undefined'){
					guid_temp = (entry.guid.split(":"));
				}else{
					if(entry.link.indexOf("id=")>-1){
						guid_temp = (entry.link.split("id=")[1]);
					}else if(entry.link.indexOf("select=")>-1){
						guid_temp = (entry.link.split("select=")[1]);
					}
				}
				if(guid_temp=='' && storyIdArray.length==0){
					var tempArray = xmlString.split("<gc:storyID>");
					for(var xmlIndex=0;xmlIndex<tempArray.length;xmlIndex++){
						var tempIndex = tempArray[xmlIndex].indexOf("</gc:storyID>");
						if(tempIndex>-1){
							storyIdArray.push(tempArray[xmlIndex].substring(0,tempIndex));
						}
					}
				}
			}

			if(guid_temp!=''){
				html += '<li class="rssRow '+row+'">' +
				'<'+ options.titletag +'><a href="'+options.pageContext+'/rssDetail.html?feedId='+escape(guid_temp)+'" title="View this feed at '+ feeds.title +'">'+ entry.title +'</a></'+ options.titletag +'>';
			}else if(storyIdArray.length>=i){
				html += '<li class="rssRow '+row+'">' +
				'<'+ options.titletag +'><a href="'+options.pageContext+'/rssDetail.html?feedId='+escape(storyIdArray[i])+'" title="View this feed at '+ feeds.title +'">'+ entry.title +'</a></'+ options.titletag +'>'
			}else{
				html += '<li class="rssRow '+row+'">' +
				'<'+ options.titletag +'><a href="'+ entry.link +'" title="View this feed at '+ feeds.title +'">'+ entry.title +'</a></'+ options.titletag +'>';
			}

			if (options.date) html += '<div>'+ pubDate +'</div>'
			if (options.content) {

				// Use feed snippet if available and optioned
				if (options.snippet && entry.contentSnippet != '') {
					var content = entry.contentSnippet;
				} else {
					var content = entry.content;
				}

				html += '<p>'+ content +'</p>'
			}

			html += '</li>';

			// Alternate row classes
			if (row == 'odd') {
				row = 'even';
			} else {
				row = 'odd';
			}
		}

		html += '</ul>' +
			'</div>'

		$(e).html(html);
	};
})(jQuery);

