/*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/

var Gallery = 
{
	pause: true,
	activePhoto: 1,
	imagesLoaded: 1,
	showPhoto : function( photo )
	{
		// photo: one of the photos loaded. Default: next
		var $active = $('#content div.gallery img.active');
		
		if ( $active.length == 0 ) $active = $('#content div.gallery img:last');
	
		if( photo==null ){
			// use this to pull the images in the order they appear in the markup
			var $next = $active.next("img").length;
			if( $next ) { $next = $active.next("img"); Gallery.activePhoto ++; } else
			{
				$next = $("#content div.gallery img").eq( 0 );
				Gallery.activePhoto = 1;
			}
			
		} else if( photo == -1 ) {
			// select previous photo
			var $next = $active.prev("img").length;
			if( $next ) { $next = $active.prev("img"); Gallery.activePhoto --; } else
			{
				$next = $('#content div.gallery img:last');
				Gallery.activePhoto = $('#content div.gallery img').length;
			}
		} else {
			// select 'photo'
			var $next = $("#content div.gallery img").eq( photo-1 );
			Gallery.activePhoto = photo;
		}
		$active.removeClass('active last-active');
		$next.addClass('active');
		window.location.hash = Gallery.activePhoto;

		$active.addClass('last-active');
		$("#gallery-nav")
			.css( "paddingTop", $("#content div.gallery IMG.active").height() + "px" )
			.css( "width", $("#content div.gallery IMG.active").width() + "px" );

			
		$("#gallery").css( "width", $("#gallery .active").width() + "px" );
		$("#gallery").css( "height", $("#gallery .active").height() + "px" );
	
	},
	next : function()
	{
		Gallery.showPhoto();
	},
	prev : function()
	{
		Gallery.showPhoto( -1 );
	},
	pausePlay: function()
	{
		if( Gallery.pause )
		{
			Gallery.pause = false;
			$("#gallery-pauseplay").html( "slideshow on" );
		} 
		else
		{
			Gallery.pause = true;
			$("#gallery-pauseplay").html( "slideshow off" );
		}
	},
	count: function()
	{
		return $('#content div.gallery img').length;
	}
}

function photoSwitch() 
{
	if( !Gallery.pause ) Gallery.next();
}

$(document).ready( function()
{
	// Keyboard shortcuts
	$(document).keydown( function(e)
	{
		if( e.keyCode == 36 ){ 	Gallery.showPhoto( 1 ); return false }					// home
		if( e.keyCode == 35 ){ 	Gallery.showPhoto( Gallery.count() ); return false }	// end
		if( e.keyCode == 39 ){ 	Gallery.next(); }										// right
		if( e.keyCode == 37 ){ 	Gallery.prev(); }										// left
	});
	
	$("#loading").html( "<p>Please wait. Loading image 1 of " + $("#content div.gallery img").length + "...</p>" );
	$("#content div.gallery img").load( function()
	{
		Gallery.imagesLoaded++;
		$("#loading").html( "<p>Please wait. Loading image " + Gallery.imagesLoaded + " of " + $("#content div.gallery img").length + "...</p>" );
	});
});

$(window).load( function()
{
	// show .gallery which is by default hidden, until all images load
	$("#loading").hide();
	$("#gallery-nav").show();
	$(".gallery").show();
	
	// assign active photo, based on hash or default (1)
	if( window.location.hash )
	{
		Gallery.showPhoto( window.location.hash.slice(1) );
		//$("div.gallery img").eq( window.location.hash.slice(1) - 1 ).addClass('active');
		//Gallery.activePhoto = window.location.hash.slice(1);
	} else { $("div.gallery img:first").addClass('active'); }
	
	// re-arrange gallery navigation
	$("#gallery-nav")
		.css( "paddingTop", $("#content div.gallery img.active").height() + "px" )
		.css( "width", $("#content div.gallery img.active").width() + "px" );
	
	// turn the navigation on
	$("#gallery-prev").click( function() { Gallery.prev(); return false });
	$("#gallery-next").click( function() { Gallery.next(); return false });
	$("#gallery-pauseplay").click( function() { Gallery.pausePlay(); });
	
	// turn on the slideshow
	setInterval( "photoSwitch()", 4000 );
});
