/*! Copyright (c) 2010 James Guest (http://jamesguest.co.uk)
 * Dual licensed under the MIT (MIT_LICENSE.txt)
 * and GPL Version 2 (GPL_LICENSE.txt) licenses.
 *
 * Version: 0.1
 * Requires jQuery 1.4.2+, Fancybox 1.3.1+
 *
 * Modified version of fancy-photoset 0.4.0, Copyright (c) 2010 Phil Cohen (http://phlippers.net)
 * Docs: http://phlippers.net/code/fancy-photoset
 *
 * Adds 'displayNum' property - a maximum number of photos to be displayed
 *
 * Requires jQuery 1.4.2+, Fancybox 1.3.1+
 * Docs: http://phlippers.net/code/fancy-photoset
 */
(function($) {
 $.fn.fancyPhotoset = function(options) {
   var opts    = $.extend($.fn.fancyPhotoset.defaults, options);
   var domId   = 'fancyPhotoset-' + opts.photosetId;
   var displayNum   =  opts.displayNum;
   var jsonUrl = 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key={apiKey}&photoset_id={photosetId}&extras=url_sq,url_t,url_s,url_m,url_l,url_o&format=json&jsoncallback=?'.replace(/\{\w+\}/g, function(match) {
     return opts[match.replace(/\{|\}/g, '')];
   });

   // Create a fancyPhotoset for each selector
   return this.each(function() {
     var obj = $(this);
     $(obj).append(
       $('<ul/>').attr('id', domId).addClass('fancyPhotoset')
     );
     
     // Add photos to each selector
     $.getJSON(jsonUrl, function(data) {
		 if(displayNum != 0) {
		 	//Display a limited number of photos
		 	if (data.photoset.photo.length < displayNum) { displayNum = data.photoset.photo.length };
			var i=0;
			for (i=0;i<=displayNum-1;i++) {
				var photo = data.photoset.photo[i];
				$.fn.fancyPhotoset.addPhotos(photo, obj, opts);
			}
		 } else {
		 	//Display all photos
			 $.each(data.photoset.photo, function(index, photo) {
			 	$.fn.fancyPhotoset.addPhotos(photo, obj, opts);
		   });
		 }

       // Hide all except the first image
       if (opts.firstOnly) {
         $(obj).find('li').not(':first').hide();
       }
     });
   });
 };

 // generate static image url
 $.fn.fancyPhotoset.urlFor = function(photo, options) {
   var opts = $.extend({size: 'square'}, options);
   var urls = {
     'square'    : photo.url_sq,
     'thumbnail' : photo.url_t,
     'small'     : photo.url_s,
     'medium'    : photo.url_m,
     'large'     : photo.url_l,
     'original'  : photo.url_o
   };
   return urls[opts.size];
 };
 
 // add photos
 $.fn.fancyPhotoset.addPhotos = function(photo, obj, opts) {
 
	 $(obj).find('ul').append(
	   $('<li/>').html(
		 $('<a/>').attr({'href': $.fn.fancyPhotoset.urlFor(photo, {size: opts.large}), 'rel': 'flickr-' + opts.photosetId, 'title': photo.title}).html(
		   $('<img/>').attr({'src': $.fn.fancyPhotoset.urlFor(photo, {size: opts.small}), 'title': photo.title, 'alt': photo.title}).after(
			 (opts.captions ? $('<span/>').addClass('caption').text(photo.title) : '')
		   )
		 ).fancybox(opts.fancybox)
	   )
	 );
	
   return;
 };

 // default options
 $.fn.fancyPhotoset.defaults = {
   apiKey     : '',
   photosetId : '',
   small      : 'square',
   large      : 'large',
   captions   : false,
   firstOnly  : false,
   fancybox   : {}
 };
})(jQuery);
