/**
@author djjeck
*/

(function($) {
	if($ == undefined)
		; //TODO
	
	// TODO require jquery.jfeed
	
	if(window.Blacktrend == undefined)
		window.Blacktrend = {};
	
	if(Blacktrend.remote == undefined)
		Blacktrend.remote = function() {
			// private
			
			// public
			return {
				// getJSON: function(url, callback) // not necessary?
				
				get: function(url, data, success, dataType) {
					$.ajax({
						url: 'same_origin_policy.php?url='+url, // TODO move url to data
						data: data, // TODO add dataType, for php script to set headers? or should they be retrieved from original file?
						dataType: dataType,
						success: success
					});
				},
				
				// TODO document
				
				each: function(url, each_callback, limit) {
					$.getFeed({
						url: 'same_origin_policy.php?url='+url,
						success: function(feed_data) {
							var entries = feed_data.items;
							if(limit != undefined)
								entries = entries.slice(0, limit);
							entries.forEach(function(entry) {
								each_callback(entry);
							});
						}
					});
					/*
					$.get(url, function(feed_data) {
						var entries = $(feed_data).find('entry, item');
						if(limit != undefined)
							entries = entries.slice(0, limit);
						entries.each(function() {
							var $item = $(this);
							each_callback({
								link: $item.find('link').text() ? $item.find('link').text() : $item.find('link').attr('href'),
								title: $item.find('title').text(),
								description: $item.find('summary, description').text(),
								updated: $item.find('pubDate, updated').text()
							});
						});
					});
					*/
				},
				
				place: function(url, container, formatter_callback, limit) {
					$(container).empty();
					Blacktrend.remote.each(url, function(item) {
						$(container).append(formatter_callback(item));
					}, limit);
				}
			};
		}();
})(jQuery);
