//object to handle the picture fading
var slideshow = {

	speed: 0.4,
	duration: 2,
	photos: new Array(),
	total: 0,
	
	init: function() {

		//total images in the slideshow
		slideshow.total = slideshow.order.length;

		//preload images and set photo order
		for( var i = 0; i < slideshow.total; i++ ) {
			slideshow.photos[i] = new Image();
			slideshow.photos[i].src = slideshow.path + slideshow.order[i] + '.jpg';
		}

		//initialise rotation
		//setTimeout( function() { slideshow.advance() }, slideshow.duration * 1000 );

		//set handler
		
		slideshow.notfirst1 = true;
		slideshow.loadImg = function() {
		
			if( slideshow.notfirst1 ) {
				slideshow.notfirst1 = false;
				return;
			}
			slideshow.animate();
			$('loading').hide();
		
		}

		Event.observe( $('photo2i'), 'load', slideshow.loadImg );

		//slideshow.update_numbers();

	},
	
	update_numbers: function() {
		
		$('currentnumber').update( slideshow.current == 0 ? slideshow.total : slideshow.current ); // i did not write this
		$('totalnumber').update( slideshow.total )
		
	},

	//work out next image to show
	slideshowUp: function() {
		
		slideshow.current = slideshow.current == slideshow.total ? 1 : slideshow.current + 1

		//set the src of photo 2 and update current pointer
		$('photo2i').setOpacity( 0 );
		$('photo2i').src = slideshow.photos[ slideshow.current - 1 ].src;		
		$('loading').show();

		slideshow.update_numbers();
		
		// IE HACK
		slideshow.tryAndLoadImg = function() {
			if( $('photo2i').complete ) slideshow.loadImg();
			else setTimeout( "slideshow.tryAndLoadImg()", 100 )
		}
		if( navigator.userAgent.match( /MSIE/ ) )slideshow.tryAndLoadImg();

	},

	//work out next image to show
	slideshowBack: function() {
		
		slideshow.current = slideshow.current == 1 ? slideshow.total : slideshow.current - 1

		//set the src of photo 2 and update current pointer
		$('photo2i').setOpacity( 0 );
		$('photo2i').src = slideshow.photos[ slideshow.current - 1 ].src;
		$('loading').show();

		slideshow.update_numbers();

		// IE HACK
		slideshow.tryAndLoadImg = function() {
			if( $('photo2i').complete ) slideshow.loadImg();
			else setTimeout( "slideshow.tryAndLoadImg()", 100 )
		}
		if( navigator.userAgent.match( /MSIE/ ) )slideshow.tryAndLoadImg();

	},
	
	//when it's loaded...
	animate: function() {

		//show it and set the opacity to 0
		$('photo2i').show();
		$('photo2i').setOpacity( 0 );
		
		//fade the photos into each other
		new Effect.Parallel( [
			new Effect.Opacity( 'photo1i', { from: 1, to: 0, sync: true } ),
			new Effect.Opacity( 'photo2i', { from: 0, to: 1, sync: true } )
		], {
			duration: slideshow.speed,
			afterFinish: function() {
			
				//swap the src of the 2 and hide photo 2
				$('photo1i').src = $('photo2i').src;
				$('photo2i').setStyle( { opacity: 0 } ).hide();
				$('photo1i').setStyle( { opacity: 1 } );
		
			//recurse
			//slideshow.advance.delay( slideshow.duration + 0.1 );
			
			}
		} );
	
	}
	
}
