/**
 * Plugin: jquery.zRSSFeed
 * 
 * Version: 1.1.2
 * (c) Copyright 2010-2011, Zazar Ltd
 * 
 * Description: jQuery plugin for display of RSS feeds via Google Feed API
 *              (Based on original plugin jGFeed by jQuery HowTo. Filesize function by Cary Dunn.)
 * 
 * History:
 * 1.1.2 - Added user callback function due to issue with ajaxStop after jQuery 1.4.2
 * 1.1.1 - Correction to null xml entries and support for media with jQuery < 1.5
 * 1.1.0 - Added support for media in enclosure tags
 * 1.0.3 - Added feed link target
 * 1.0.2 - Fixed issue with GET parameters (Seb Dangerfield) and SSL option
 * 1.0.1 - Corrected issue with multiple instances
 *
 **/

(function($){

	$.fn.rssfeed_article = function(url, options, fn) {	
	
		// Set pluign defaults                  
		var defaults = {
			limit: 10,
			header: false,
			titletag: 'h4',
			date: true,
			content: true,
			snippet: true,
			showerror: true,
			errormsg: '',
			key: null,
			ssl: false,
			linktarget: '_self',
			type: "videos"
		};  
		var options = $.extend(defaults, options); 
		
		// Functions
		return this.each(function(i, e) {
			var $e = $(e);
			var s = '';

			// Check for SSL protocol
			if (options.ssl) s = 's';
			
			// 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"+ s +"://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + encodeURIComponent(url);
			if (options.limit != null) api += "&num=" + options.limit;
			if (options.key != null) api += "&key=" + options.key;
			api += "&output=json_xml"

			// Send request
			$.getJSON(api, function(data){
				
				// Check for error
				if (data.responseStatus == 200) {
	
					// Process the feeds
					_process(e, data.responseData, options);

					// Optional user callback function
					if ($.isFunction(fn)) fn.call(this,$e);
					
				} 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>');
				};
			});				
		});
	};
	
	// Function to create HTML result
	var _process = function(e, data, options) {

		// Get JSON feed data
		var feeds = data.feed;
		//alert(JSON.stringify(options));
		if (!feeds) {
			return false;
		}
		var html = '';	
		var row = 'odd';
		
		// Get XML data for media (parseXML not used as requires 1.5+)
		var xml = getXMLDocument(data.xmlString);
		var xmlEntries = xml.getElementsByTagName('item');
		
		// Add header if required
		if (options.header)
			html +=	'<div class="rssHeader">' +
				'<a href="'+feeds.link+'" title="'+ feeds.description +'">'+ feeds.title +'</a>' +
				'</div>';
			
		// Add body
		
		// Add feeds
//		if(options.type == "news-content"){
//			html += "<div style=\"height:17px;width:100%;font:normal 12px arial;\">";
//			html += "<img src=\"images/icons/yahoosearch.ico\" style=\"float:left\" />";
//            html += "<div style=\"margin-left:4px;margin-bottom:0px;float:left\">From Yahoo News</div></div>";
//			html += "<table class=\"content\">";
//		}
//		else{
			html += "<table class=\"widget\">";
		//}
		
		for (var i=0; i<feeds.entries.length; i++) {			
			// Get individual feed
			var entry = feeds.entries[i];
			
			//console.log(entry);
			var entryDate = new Date(entry.publishedDate);
			var pubDate = entryDate.toLocaleDateString() + ' ' + entryDate.toLocaleTimeString();
			
			// Add feed row
			var videolink = "";
			var relLink = "";
			var contentLink = entry.link;
			if(options.type == "videos"){
				relLink = "shadowbox;width=505;height=400;player=swf";
				videolink = "video-link";
				contentLink = getVideo(entry.link);
				
				html += "<tr><td><a id=\"image\" rel=\"" + relLink + "\" href=\"" + getVideo(entry.link) + "\" title=\"" + entry.title + "\">";
				html += "<img id=\"image-display\" src=\"" + getScreen(entry.link, "small") + "\" alt=\"" + entry.title + "\" ";
				html += "style=\"cursor:pointer;width:140px;padding:2px;border:1px solid #ccc\" /></a></td></tr>";				
			}			
			else if(options.type == "shopping"){
				html += "<tr><img src=\"\" /></tr>";
			}
			else if(options.type == "news-content"){
			    //html += '<tr><td style=\"margin-bottom:10px;\"><a rel=\"' + relLink + '\" href="'+ contentLink +'" class=\"normal-link\">' + entry.title +'</a>';
			    html += '<tr><td style=\"margin-bottom:10px;\"><div style=\"color:blue\">' + entry.title + '</div>';
				html += '<div style=\"margin-top:3px;margin-bottom:7px\">' + entry.content + '</div></td></tr>';  
			}
			else{
				html += '<tr><td style=\"margin-bottom:10px;\"><a rel=\"' + relLink + '\" href="'+ contentLink +'" class=\"video-link\">' + entry.title +'</a>';
				html += '<div style=\"margin-top:3px;margin-bottom:7px\">' + entry.content + '</div></td></tr>';  
			}
		}
		
		html += "</table>";
		
		$(e).html(html);
	};
	
	function getScreen( url, size )
	{
	  if(url === null){ return ""; }

	  size = (size === null) ? "big" : size;
	  var vid;
	  var results;

	  results = url.match("[\\?&]v=([^&#]*)");

	  vid = ( results === null ) ? url : results[1];

	  if(size == "small"){
		return "http://img.youtube.com/vi/"+vid+"/2.jpg";
	  }else {
		return "http://img.youtube.com/vi/"+vid+"/0.jpg";
	  }
	}
	
	function getVideo(url)
	{
	  if(url === null){ return ""; }
	  
	  var vid;
	  var results;

	  results = url.match("[\\?&]v=([^&#]*)");

	  vid = ( results === null ) ? url : results[1];

	  return "http://www.youtube.com/v/" + vid + "&amp;hl=en&amp;fs=1&amp;rel=0&amp;autoplay=1";
	}
	
	function formatFilesize(bytes) {
		var s = ['bytes', 'kb', 'MB', 'GB', 'TB', 'PB'];
		var e = Math.floor(Math.log(bytes)/Math.log(1024));
		return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
	}

	function getXMLDocument(string) {
		var browser = navigator.appName;
		var xml;
		if (browser == 'Microsoft Internet Explorer') {
			xml = new ActiveXObject('Microsoft.XMLDOM');
			xml.async = 'false'
			xml.loadXML(string);
		} else {
			xml = (new DOMParser()).parseFromString(string, 'text/xml');
		}
		return xml;
	}

})(jQuery);

