// Don't execute any code until the DOM is ready!
jQuery(document).ready(function() {				
						   
	// Our very special jQuery JSON fucntion call to Flickr, gets details of the most recent 20 images			   
	jQuery.getJSON("http://api.flickr.com/services/feeds/groups_pool.gne?id=1415562@N21&lang=en-us&format=json&jsoncallback=?", displayImages);
	
	function displayImages(data) {																																   
		// Randomly choose where to start. A random number between 0 and the number of photos we grabbed (20) minus 8 (we are displaying 8 photos).
		var iStart = Math.floor(Math.random()*(12));	
		
		// Reset our counter to 0
		var iCount = 0;								
		
		// Start putting together the HTML string
		var htmlString = "<ul>";					
		
		// Now start cycling through our array of Flickr photo details
		jQuery.each(data.items, function(i,item){
									
			// Let's only display 8 photos (a 2x4 grid designated by CSS), starting from a random point in the feed					
			if (iCount > iStart && iCount < (iStart + 9)) {
				
				// I only want the square thumbnails
				var sourceSquare = (item.media.m).replace("_m.jpg", "_m.jpg");		
				
				// Here's where we piece together the HTML
				htmlString += '<li><a href="' + item.link + '" target="_blank">';
				htmlString += '<img src="' + sourceSquare + '" alt="' + item.title + '" title="' + item.title + '"/>';
				htmlString += '</a></li>';
			}
			// Increase our counter by 1
			iCount++;
		});		
		
	// Pop our HTML in the #flickr_stream DIV	
	jQuery('#flickr_stream').html(htmlString + "</ul>");
	
	// Close down the JSON function call
	}
	
// The end of our jQuery function	
});
