/*
 *	jQuery ttflickr
 *	copyright 2009
 *
 *	@author 	Boy van Amstel
 *	@version 	1.0
 *
 *	Changes 1.0:
 *	[06/01/2010]	- First release 
 *
 */
 
jQuery.ttflickr = 
{
	getUserInfo : function(user_id, options, callback) {
		
		// Call API			
		jQuery.ajax({
			type		:	'GET'
			,dataType	:	'jsonp'
			,url		: 	options.api_url
			,data		: 	"method=flickr.people.getInfo&user_id=" + user_id
			,success	:	function(data) {

				// Call callback
		        callback.call(this, data);

			}
			,error		:	function() {
				callback.call(this);
			}
		});				
		
	},
	createImageUrl : function(item, options) {
		return 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_' + options.photo_size + '.jpg';
	},
	build : function(options) {

		try {
		 
			// Default settings
			var defaults = {
				api_url				:	'http://api.flickr.com/services/rest/'
				,api_method			:	''
				,return_amount		:	10
				,photo_size			:	'm'	// mstb
				,safe_search		:	0	// 1 - safe, 2 - moderate, 3 - restricted
				,loading_text		:	'Loading..'
				,onReloadComplete	: 	false
				,onCheckComplete	: 	function(){}
				,onComplete			:	function(){}
				,onError			:	function(){}
				,limiter			:   22
			}
	
			// 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
					jQuery(object).html(options.loading_text);
		
					// Call API			
					jQuery.ajax({
						type		:	'GET'
						,dataType	:	'jsonp'
						,url		: 	options.api_url
						,data		: 	options.api_method + '&per_page=' + options.return_amount + '&safe_search=none' + options.safe_search
						,success	:	function(data) {
							
							jQuery(object).html("");
		
						    if(typeof options.onReloadComplete == 'function'){
		  						options.onReloadComplete.call(this, object, data, options);
							}
							else {
								object.last_id = data.photos.photo[0].id;
								// Loop through images
								jQuery.each(data.photos.photo, function(i,item) {
									
									var img = jQuery.ttflickr.createImageUrl(item, options);
							
							    	jQuery("<img/>").attr("src", img).attr("title", item.title).attr("alt", item.title).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 + '&per_page=' + options.return_amount + '&safe_search=none' + options.safe_search
						,success	:	function(data) {
							
							var new_count = object.new_count;
							object.last_id = data.photos.photo[0].id;
							
							// Loop through images
							jQuery.each(data.photos.photo, function(i,item) {
								if(item.id != 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({
		ttflickr: jQuery.ttflickr.build
});	