
/*!
	@ flickr_badge()
	---------------------------------------------------------------
	Enhance static badge.
	Heavily based on code by AK
	
	Usage:	
	$('#id').flickr_badge();
	_______________________________________________________________
*/		

	(function($) {
		$.fn.flickr_badge = function(options) 
		{			
			// PLUGIN DEFAULTS
			$.fn.flickr_badge.defaults = {
				max_no_images: 9,
				max_per_row: 3,
				img_container_tag: 'dt',
				enlargeable_classname: false
			};				
		
			options = $.extend($.fn.flickr_badge.defaults,options);							
		
			return this.each(function() {
																
				// IF ALREADY ENHANCED, EXIT
				if ( $(this).data('flickr_badged') )
				{
					return;
				}	
				
				// exclude IE6		
				if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7) 
				{ 
					return true; 
				}
								
				// all image containers
				options.image_containers = $(this).find( options.img_container_tag );
				
				// all image containers which contain images that are suitable for enlargement
				if ( options.enlargeable_classname )
				{
					options.image_containers_enlargeable = $(this).find(options.img_container_tag + '.' + options.enlargeable_classname);		
				}
				else
				{
					options.image_containers_enlargeable = $(this).find(options.img_container_tag);			
				}										
				
				// pick one of the images that is suitable for enlargement
				options.random_number = Math.floor( Math.random() * $(options.image_containers_enlargeable).length ); // was options.max_no_images									
				options.image_container_enlarged = $(options.image_containers_enlargeable).eq(options.random_number);								
				options.image_enlarged = $(options.image_container_enlarged).find('img:first');							
													
				// Loop through items to add helper classes
				options.counter = 1;	
				
				$.each( $(options.image_containers), function(i, item) 
				{
					// if in the last column
					if ( options.counter % options.max_per_row === 0) 
					{ 
						$(item).addClass('last-col');
					}
					
					// if in the last row
					if ( options.counter > ( options.max_no_images - options.max_per_row ) ) 
					{ 
						$(item).addClass('last-row');
					}
					
					options.counter++;
				});						
				
				// Enlarge a random image and use larger size URL from flickr api
				options.image_enlarged_src = $(options.image_enlarged).attr('src');				
				
				 // get file extension
				options.image_enlarged_ext = options.image_enlarged_src.substring( options.image_enlarged_src.length - 4, options.image_enlarged_src.length );
				
				// trim url		
				options.image_enlarged_src = options.image_enlarged_src.substring( 0, options.image_enlarged_src.length - 6 );
				
				// preload image (onload only works with this code syntax)
				options.preloaded = new Image();
				options.preloaded.src = ( options.image_enlarged_src + '_m' + options.image_enlarged_ext );
				
				// when the image has loaded, display it
				options.preloaded.onload = function() 
				{
					options.enlargement = options.image_enlarged_src + '_m' + options.image_enlarged_ext;
					
					$(options.image_enlarged).attr({ src : options.enlargement });
					
					// determine image dimensions for CSS cropping
					if (options.preloaded.width == 240) 
					{ 
						// show it as landscape image
						$(options.image_container_enlarged).addClass('landscape'); 
					}
					
					// mark as active
					$(options.image_container_enlarged).addClass('active');
					
					// show it
					$(options.image_enlarged).hide().fadeIn('slow'); 
															
				};				
				
				// MARK AS DONE					
				$(this).data('flickr_badged', true);		
				
			});			
		};
	})(jQuery);	

