//
// jQuery Flickr Photostream Lib
// Version: 1.0
// Author Name: emarketed
// Author Website: http://www.emarketed.com
//
// ----------------------------
// HOW TO USE:
// ----------------------------
//
// Settings:
//  api_key - the flickr api key
//  photoset_id - your flickr photoset id
//  width - canvas width
//  height - canvas height
//  show - the number of photos to display
//  lightbox - the lightbox script function to call (example: "lightbox", "facebox")
//		(note) - script must be installed and included prior to including this jquery plugin

var _photostream_cache = {
	page : 1,
	total_page : 1,
	settings : {}
};

jQuery.fn.photostream = function(action,options,update) {
	switch(action) {
		case "prev":
			//begin previous button processing
			return this.each(function() {
				var div_main = $(this);
				div_main.click(function() {
					if(_photostream_cache.page>1) {
						_photostream_cache.page--;
						$('.photostream-canvas').photostream('display',_photostream_cache.settings,true);
					}
				});
			});
			//end previous button processing
			break;
		case "next":
			//begin next button processing
			return this.each(function() {
				var div_main = $(this);
				div_main.click(function() {
					_photostream_cache.page++;
					$('.photostream-canvas').photostream('display',_photostream_cache.settings,true);
				});
			});
			//end next button processing
			break;
		case "display":
			//begin load default settings
			var settings = jQuery.extend({
				api_key : '',
				photoset_id : '',
				width : 200,
				height : 200,
				show : 1,
				lightbox : false
			},options);
			//end load default settings
		
			//begin variables
			var photolib = new Array();
			//end variables
		
			//begin plugin process
			return this.each(function() {
				var div_main = $(this);
				var originalHTML = div_main.html();
				if(update) { div_main.html(""); }
				var div_canvas = $("<div />")
					.attr('class','photostream-canvas')
					.css({
						'width' : settings.width + 'px',
						'height' : settings.height + 'px',
						'margin-bottom' : '10px',
						'overflow' : 'hidden'
					})
					.appendTo(div_main);
					//begin retrieve json data
					var request_url = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=' + settings.api_key + '&photoset_id=' + settings.photoset_id + '&per_page=' + settings.show + '&page=' + _photostream_cache.page + '&extras=url_t,url_m,url_o&format=json&jsoncallback=?';
					$.getJSON(request_url,function(data) {
						//begin each json object
						$.each(data.photoset.photo,function(i,image) {
							photolib.push({
								photo_id : image.id,
								title : image.title,
								description : image.title,
								fullUrl : image.url_m,
								thumbUrl : image.url_t
							});
							if(photolib.length==0) { obj.html(originalHTML); }
							else {
							}
						});
						//end each json object
						//begin each photo in library
						if(photolib.length>0) {
							$.each(photolib,function(i,photo) {
								var div_image = $("<div />")
									.attr('class','photostream-image')
									.css({
										 'float' : 'left',
										 'width' : ((settings.width/settings.show)-10) + 'px',
										 'height' : settings.height,
										 'margin-left' : '10px'
									})
									.appendTo(div_canvas);
									var anchor_image = $("<a />")
										.attr('href',photo.fullUrl);
										var img = new Image();
										$(img)
											.hide()
											.load(function() {
												$(this).fadeIn(500);
											})
											.attr('src',photo.thumbUrl)
											.attr('width',((settings.width/settings.show)-10))
											.attr('height',settings.height)
											.attr('alt',photo.title)
											.attr('border',0)
											.appendTo(anchor_image);
									if(settings.lightbox) { eval("anchor_image."+settings.lightbox+"();"); }
									anchor_image.appendTo(div_image);
							});
							_photostream_cache.total_page = Math.ceil(photolib.length / settings.show);
						}
						//end each photo in library
					});
					//end retrieve json data
				_photostream_cache.settings = settings;
			});
			//end plugin process
		break; //end case display
	}

};