
// Define the photo entries.
var photoEntries = [
/*	"1-Ben-Forta-@-NYCFUG-The-Centaur-And-Bolt-ColdFusion-User-Group-Tour.jpg",
	"10-Adam-Wayne-Lehman-@-NYCFUG-The-Future-of-ColdFusion-8-And-Beyond.jpg",
	"1-bigimg-NYCFUG.jpg",
	"2-Michael-Smith-@-NYCFUG-Using-Your-Whole-Brain-For-Developers.jpg",
	"3-Ben-Nadel-@-NYCFUG-Ben-Nadel-on-Advanced-ColdFusion-Custom-Tags.jpg",
	"4-Pete-Freitag-@-NYCFUG-Image-Manipulation-in-ColdFusion-8.jpg",
	"5-Jeff-Coughline-at-NYCFUG-Developing-ColdFusion-Applications-With-The-FarCry-Framework.jpg",
	"6-audience-at-NYCFUG-meeting.jpg",
	"7-Gert-Franz-@-NYCFUG-Railo-US-Tour.jpg",
	"8-audience-at-NYCFUG-meeting.jpg",
	"9-Raymond-Camden-@-NYCFUG-How-Parsing-RSS-In-ColdFusion-Saved-Me-From-Going-Bald.jpg"
*/
	"1.jpg",
	"2.jpg",
	"3.jpg",
	"4.jpg",
	"5.jpg",
	"6.jpg",
	"7.jpg",
	"8.jpg",
	"9.jpg",
	"10.jpg",
	"11.jpg",
	"12.jpg",
];


// Select a random photo to use.
var initialPhotoIndex = Math.min(
	Math.floor( Math.random() * photoEntries.length ),
	(photoEntries.length - 1)
);


// Define code for photo rotation.
jQuery(function( $ ){
	
	// The current photo being displayed.
	var currentPhotoIndex = initialPhotoIndex;

	// Get a reference to the photo area.
	var photoArea = $( "div.bigimg-col" );
	
	// Get a reference to the loader.
	var loaderNote = photoArea.find( "div.loading-note" );

	// Get a reference to the pre/next photos.
	var arrows = photoArea.find( "a.navigation-arrow" );
	
	// Get a reference to the photo viewer.
	var photoView = photoArea.find( "div.photo-view-frame" );
	
	// Get a reference to the photo itself.
	var photo = photoView.find( "img" );
	
	// A flag to determine if this photo rotation is in transition 
	// (from one photo to the next).
	var isInTransition = false;
	
	// I am the base SRC path.
	var baseSrc = "images/big-image-assets/slide-images/";
	
	
	// ----------------------------------------------------- //
	// ----------------------------------------------------- //
	
	
	// I make sure the given index is in the appropriate bounds.
	var adjustIndexForBounds = function( nextIndex ){
		if (nextIndex < 0){
		
			// Return last index (loop around).
			return( photoEntries.length - 1 );
		
		} else if (nextIndex >= photoEntries.length){
		
			// Return first index (loop around).
			return( 0 );
		
		} else {
			
			// Return the given index - it is in bounds.
			return( nextIndex );
			
		}
	};
	
	
	// I change to the next photo.
	var changePhoto = function( increment ){
		// Set the photo area to be in transition.
		isInTransition = true;
		
		// Show the loader note.
		loaderNote.stop().fadeTo( 300, 1 );
		
		// Get next current index.
		var nextPhotoIndex = adjustIndexForBounds( currentPhotoIndex + increment );
		
		// Get the current source.
		var currentSrc = photoEntries[ currentPhotoIndex ];
		
		// Get the next source.
		var nextSrc = photoEntries[ nextPhotoIndex ];
		
		// Set the "current" photo index. This will only matter once the transition
		// has ended.
		currentPhotoIndex = nextPhotoIndex;
		
		// Make the current src the background of the view frame.
		photoView.css( "background-image", "url( '" + baseSrc + currentSrc + "' )" );
		
		// Move the photo off-screen.
		photo.css( "left", photo.width() );
		
		// Bind the onload event handler.
		photo.bind(
			"load", 
			{
				prev: (increment === -1),
				next: (increment === 1)
			},
			onLoadHandler 
		);
		
		// Set to next source.
		photo.attr( "src", (baseSrc + nextSrc) );						
	};
					
	
	// I respond to the load event on the next photo.
	var onLoadHandler = function( event ){
		// Check to see which direction we are sliding in from.
		if (event.data.prev){
			
			// Slide in from left. Start on the left.
			photo.css( "left", -photo.width() )
			
		} else {
		
			// Slide in from right. Start on right.
			photo.css( "left", photo.width() )
			
		}
	
		// Slide new photo in.
		photo.animate(
			{
				left: "0px"
			},
			{
				duration: 500,
				complete: function(){
					// Hide the loader note.
					loaderNote.stop().fadeTo( 100, 0 );
		
					// Turn off the transition.
					isInTransition = false;							
				}
			}
		);
		
	};
	
	
	// ----------------------------------------------------- //
	// ----------------------------------------------------- //
	
	
	// Start binding events to the photo areas.
	photoArea.hover(
		function( event ){
			// Show the arrows.
			if (arrows.is( ":animated" )){
				arrows.stop().fadeTo( 150, 1 );
			} else {
				arrows.fadeIn( 150 );
			}
		},
		function( event ){
			// Hide the arrows.
			arrows.stop().fadeOut( 300 );
		}
	);
	
	
	// Bind the prev/next arrows.
	arrows
		.attr( "href", "javascript:void( 0 )" )
		.click(
			function( event ){
				// Prevent default.
				event.preventDefault();
				
				// Check to make sure we are not currently in transiiton.
				if (isInTransition){
					return;
				}
			
				// Change photo.
				changePhoto( $( this ).is( ".next-arrow" ) ? 1 : -1 );
			}
		)
	;
	
	
	// Hide the loader.
	loaderNote.fadeTo( 1, 0 ).show();
				
});			
