/*
 *	jQuery ttyoutube
 *	copyright 2009
 *
 *	@author 	Boy van Amstel
 *	@version 	1.0
 *
 *	Changes 1.0:
 *	[06/01/2010]	- First release 
 *
 */
 
jQuery.ttyoutube = 
{
	build : function(options) {

		try {
		 
			// Default settings
			var defaults = {
				api_url				:	'http://gdata.youtube.com/feeds/api/videos?v=2&alt=json-in-script&callback=?'
				// TO DO: http://gdata.youtube.com/feeds/api/users/nocnsf1/favorites
				,api_method			:	''
				,return_amount		:	10
				,safe_search		:	'none'	// none, moderate, strict
				,loading_text		:	'Loading..'				
				,onReloadComplete	: 	false
				,onCheckComplete	: 	function(){}
				,onComplete			:	function(){}
				,onError			:	function(){}
				,limiter			:   22
				,clearBefore		:	true
			}
	
			// Move to options
			var options = jQuery.extend(defaults, options);
			
			var object = this;
			
			// Add custom functions
			var extensions = {
				new_count	:	0
				,last_id	:	0
				,reload		: 	function() { 

					// Display loading
					if(options.clearBefore) jQuery(object).html(options.loading_text);
					
					// Call API			
					jQuery.ajax({
						type		:	'GET'
						,dataType	:	'jsonp'
						,url		: 	options.api_url
						,data		: 	options.api_method + '&max-results=' + options.return_amount + '&safeSearch=' + options.safe_search
						,success	:	function(data) {
						
							if(options.clearBefore) jQuery(object).html("");
						
						    if(typeof options.onReloadComplete == 'function'){
		  						options.onReloadComplete.call(this, object, data, options);
							}
							else {
								// Loop through videos
								jQuery.each(data.feed.entry, function(i,item) {
		
									var img = item.media$group.media$thumbnail[0].url;
							    	jQuery("<img/>").attr("src", img).attr("title", item.title.$t).attr("alt", item.title.$t).appendTo(object);
		
								});
							}

							// Call callback
							options.onComplete.call(this, data);
					        
						}
						,error		:		function(xhr, ajaxOptions, thrownError) {
		
							// Call callback
					        options.onError.call(this);
							
						}
					});
			
				}
				,checkUpdates	:	function() {
				
					// Call API			
					jQuery.ajax({
						type		:	'GET'
						,dataType	:	'jsonp'
						,url		: 	options.api_url
						,data		: 	options.api_method + '&max-results=' + options.return_amount + '&safeSearch=' + options.safe_search
						,success	:	function(data) {
						
							var new_count = object.new_count;
							object.last_id = data.feed.entry[0].id.$t;
							
							// Loop through images
							jQuery.each(data.feed.entry, function(i,item) {
								if(item.id.$t != object.last_id) {
									new_count++;
								} else {
									return false;
								}
							});
							object.new_count = new_count;
							
							// Call callback
							options.onCheckComplete.call(this, data);							
					        
						}
						,error		:		function(xhr, ajaxOptions, thrownError) {
		
							// Call callback
					        options.onError.call(this);
							
						}
					});				
				
				}
			}
			jQuery.extend(object, extensions);
			
			// Load
			object.reload();

			return this;
		}
		catch(err) {
			return false;
		}
	}
};

jQuery.fn.extend({
		ttyoutube: jQuery.ttyoutube.build
});	