var j20m = j20m || {};
j20m.config = j20m.config || {debug: false && typeof console == "object"};

j20m.config.load = j20m.config.load || {};
if (!j20m.config.load.GoogleFeed)
{
	google.load("feeds", "1");
	j20m.config.load.GoogleFeed = true;
}

j20m.feeds = j20m.feeds || {};
if (!j20m.feeds.MultipleFeeds)
{
	j20m.feeds.MultipleFeeds = function (urls, settings)
	{
		var urls = urls;
		var numFeedEntries = settings.numFeedEntries || 5;

		this.load = function(callback, callbackSettings)
		{
			var callback = callback || function(){};
			var callbackSettings = callbackSettings || {};

			if (urls.length ==  0)
			{
				callback([], callbackSettings);
				return;
			}

			var entries = [];
			var num = urls.length, numPending = num;
			for (var i = 0; i < num; i++)
			{
				var feed = new google.feeds.Feed(urls[i]);
				feed.setNumEntries(numFeedEntries);
				feed.setResultFormat(google.feeds.Feed.JSON_FORMAT);

				feed.load(function (result) {
					if (!result.error)
					{
						var numEntries = result.feed.entries.length;
						for (var j = 0; j < numEntries; j++)
						{
							var entry = result.feed.entries[j]
							entry.tmst = Date.parse(entry['publishedDate'])
							entry.feed = {
								'title': result.feed.title,
								'link': result.feed.link
							}
							entries.push(entry);
						}
					}

					numPending--;
					if (numPending == 0)
					{
						var numEntries = entries.length;

						entries.sort(function (a, b) {
							return b.tmst - a.tmst;
						});
						if (j20m.config.debug) console.log(entries);

						callback(entries, callbackSettings);
					}
				});
			}
		}

		this.display = function (entries, settings)
		{
			var totalEntries = settings.totalEntries || 5;
			var showContent = settings.showContent || false;
			var idElement = settings.idElement || 'j20m-feed';
			
			totalEntries = Math.min(totalEntries, entries.length);

			var html = '';
			if (totalEntries > 0)
			{
				html += '<ul>';
				for (var i = 0; i < totalEntries; i++)
				{
					var entry = entries[i];

					// TODO: Externalizar a una función en utilidades (utils)
					var date = new Date(entry.tmst);
					var day = date.getDate();
					if (day < 10) day = '0' + day;
					var month = date.getMonth() + 1;
					if (month < 10) month = '0' + month;
					var hours = date.getHours();
					if (hours < 10) hours = '0' + hours;
					var minutes = date.getMinutes();
					if (minutes < 10) minutes = '0' + minutes;
					var dateString = day + '.' + month + '.' + date.getFullYear() + ' ' + hours + ':' + minutes + 'h';

					// TODO: http://www.anieto2k.com/2006/12/18/dom-como-alternativa-a-innerhtml/
					html += '<ul><li>'
					html += '<p><a href="' + entry.link + '" target="_blank">' +  entry.title + '</a></p>';
					if (showContent)
					{
						html += '<p>' + entry.contentSnippet + '</p>';
					}
					html += '<span>' + dateString + '</span> | <span><a href="' + entry.feed.link + '" target="_blank">' +  entry.feed.title + '</a></span><br />';
					html += '</li></ul>';
				}
				html += '</ul>';
			}
			else
			{
				html += '<div class="c20m_feed_error"><p>Lo sentimos, no tenemos información disponible en estos momentos.</p></div>';
			}
			
			document.getElementById(idElement).innerHTML = html;
		}
	}
}
