var BASE_PATH = 'http://ugo.co.ug/';
/**
 * 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)
 *
 * Modified by Richard Mackney for Instagram images
 **/
(function($){

	var current = null;

	$.fn.rssfeed = function(url, options) {

		// Set pluign defaults
		var defaults = {
			limit: 3,
			header: false,
			titletag: 'h4',
			date: true,
			content: false,
			snippet: false,
			showerror: true,
			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=" + encodeURIComponent(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 +=	'<div class="rssHeader">' +
				'<a href="'+feeds.link+'" title="'+ feeds.description +'">'+ feeds.title +'</a>' +
				'</div>';
            */
		// Add body
		html += '<div class="rssBody">' +
			'<ul>';

		// Add feeds
		for (var i=0; i < 4; i++) {

    	// Get individual feed
			var entry = feeds.entries[i];


            //check if the feed is loaded
            if(entry)
            {
			var entryDate = new Date(entry.publishedDate);


			var pubDate = entryDate.toLocaleDateString() + ' ' + entryDate.toLocaleTimeString();
            if(pubDate === 'Invalid Date')
            {
            options.date = false;
            }

            //display an image for the first feed
            if(getImageSrcFromString(entry.content))
            {
            /*

           html += '<li><div class="rsstopcontainer"><div class="rsstopcontainerheader"><div class="rsstopcontainertitle"><a href="'+entry.link+'" title="'+ entry.title +' " target="_blank">'+ entry.contentSnippet.trunc(55,true) +'</a></div>';
           if (options.date) html += '<div class="rsstopcontainertime">'+ pubDate +'</div></div>';
           var imgsrc = getImageSrcFromString(entry.content);
           html +='<div class="rsstopcontainercontent">'+ entry.contentSnippet.trunc(200,true)  +'</div>';
           html +='<div class="rsstopcontainerImage"><img src="'+ imgsrc +'"></div></div></li>';
*/
     html += '<li><div class="rsstopcontainer">';
     if (options.date) html += '<div class="rsstopcontainercontent" style="font-size:11px;"><span class="rssTime">'+ pubDate +'</span><br/>';
     html +=  entry.contentSnippet.substring(0,195) +'<br/>';


    html += '<span style="float:right;width:100%;display:block;margin-top:3px;font-size:10px;color:#CCCCCC !important;"><a href="'+ entry.link +'" title="View this feed at '+ feeds.title +'">full story</a></span></div>';


           var imgsrc = getImageSrcFromString(entry.content);
           //html +='<div class="rsstopcontainercontent">'+ entry.contentSnippet.trunc(200,true)  +'</div>';
           html +='<div class="rsstopcontainerImage"><a href="'+ entry.link +'" title="View this feed at '+ feeds.title +'"><img src="'+ imgsrc +'"></a></div></div></li>';

            }else
            {






			// Add feed row
			html += '<li class="rssRow">';
                if(options.header)
                {
				 html += '<div class="rssTitle"><a href="'+ entry.link +'" title="View this feed at '+ feeds.title +'">'+ feeds.title +'</a></'+ "div" +'>'
                }



				// Use feed snippet if available and optioned
				if (options.snippet) {

          	html += '<div class="rssContent"><a href="'+entry.link+'" title="'+ entry.title +' " target="_blank">'+ entry.title  +'</a>';

				}



             //if (options.date) html += '<div class="rssTime">'+ pubDate +'</div>';
             //generate a random div di
             var hidediv = "rssfeedhidediv_"+ Math.floor(Math.random()*11111);
             var showdiv = "rssfeedshowdiv_"+ Math.floor(Math.random()*11111);
             var contentdiv = "rssfeedcontentdiv_"+ Math.floor(Math.random()*11111);

            html += '<span style="display:none;" id="'+ hidediv +'"><img src="'+BASE_PATH+'images/min2.png" OnClick=tabtoolbarMinimise("'+contentdiv+'","'+showdiv+'","'+hidediv+'"); ></span><span id="'+ showdiv +'"><img src="'+BASE_PATH+'images/max2.png" OnClick=tabtoolbarMaximise("'+contentdiv+'","'+hidediv+'","'+showdiv+'");></span></div>';


             if (options.date) html += '<div class="rssTime">'+ pubDate +'</div>';


            html +='<div class="rssImage" id="'+ contentdiv +'" style="display:none;">'+ entry.content.substring(0,785)  +'';

    html += '<span style="float:right;width:100%;display:block;margin-top:3px;font-size:10px;color:#CCCCCC !important;"><a href="'+ entry.link +'" title="View this feed at '+ feeds.title +'">full story</a></span></div>';

			html += '</li>';
        }

    }

		}

//alert(html);

		html += '</ul>' +'</div>';

//alert(e);

		$(e).html(html);
		$(e).find("a").attr("target","_blank"); // change target here
	};
})(jQuery);


//get an image atrribute from a string
function getImageSrcFromString(data)
{
var imgsrc;
var imgsrcraw;
var data = "<div class='getimgsrc'>"+data+"</div>";
jQuery(function($){
//var imgsrc = $("img",$(data)).attr("src");
imgsrcraw = $("img", $(data)).attr ("src");
if(imgsrcraw)
{
var ext = imgsrcraw.split('.').pop();
ext = ext.toLowerCase();
if(ext == 'jpg' || ext == 'jpeg' || ext == 'gif' || ext == '')
{
imgsrc = imgsrcraw;
}
}
 });
return imgsrc;
}


