
Slideshow = 
{
	
	slides: new Array(),
	current_index: 0,
	
	
	initialize: function()
	{
		var col = $$( 'col2' );
		
		if ( !col ) return;
		
		// Let's loop through all the slides.
		for ( var i = 0; i < 9; i++ )
		{
			
			// Create a div for this slide.
			Slideshow.slides[i] = $( col.create( 'div', null, true, '<img src="' + SITE_URL + '/img/slideshow/slide' + i + '.jpg" />' ) );
			
			// Set the slide to transparent.
			Slideshow.slides[i].setStyle( 'opacity', 0.01 );
			
		}
		
		// Let's set up the first slide.
		Slideshow.setupSlide( 0 );
		
	},
	
	
	setupSlide: function( slide_id )
	{
		
		// Get how many times we should loop.
		var loop = ((Slideshow.slides.length - slide_id) >= 3) ? 3 : (Slideshow.slides.length - slide_id);
		
		// Loop through the slides we should show.
		for ( var i = 0; i < loop; i++ )
		{
			
			// Get the variables we need.
			var slide = Slideshow.slides[slide_id + i];
			var right_columnY = $$( 'col2' ).position()[1];
			
			// Set the new Y value.
			slide.position( null, right_columnY - 135 );
			
			// Now let's set this slide up for animation.
			if ( i < loop - 1 )
				Slideshow.setupAnimation( slide_id + i, i, false );
			else
				Slideshow.setupAnimation( slide_id + i, i, true );
			
		}  // Next slide.
		
	},
	
	
	setupAnimation: function( slide_id, stage, last )
	{
		
		var slide = Slideshow.slides[slide_id];
		
		// Create the animation sequence object.
		var anim_sequence = new Legato_Animation_Sequence();
		
		// Set up the animation depending upon which stage it's at.
		if ( stage == 0 )
		{
			
			// Create the initial animation.
			var anim = new Legato_Animation( slide, 1000 );
			
			anim.controller.move.by.Y = 160;
			anim.controller.move.ease = Legato_Animation.STRONG_EASE_OUT;
			anim.controller.opacity.to = 1;
			anim.controller.delay = 1000;
			
			anim_sequence.addAnimation( anim );
			
			// Set the final slide out animation.
			var anim = new Legato_Animation( slide, 1000 );
			anim.controller.opacity.to = 0;
			anim.controller.delay = 6000;
			
			anim_sequence.addAnimation( anim );
			
		}
		else if ( stage == 1 )
		{
			
			// Create the initial animation.
			var anim = new Legato_Animation( slide, 1000 );
			
			anim.controller.move.by.Y = 320;
			anim.controller.move.ease = Legato_Animation.STRONG_EASE_OUT;
			anim.controller.opacity.to = 1;
			anim.controller.delay = 500;
			
			anim_sequence.addAnimation( anim );
			
			// Set the final slide out animation.
			var anim = new Legato_Animation( slide, 1000 );
			anim.controller.opacity.to = 0;
			anim.controller.delay = 6500;
			
			anim_sequence.addAnimation( anim );
			
		}
		else if ( stage == 2 )
		{
			
			// Create the initial animation.
			var anim = new Legato_Animation( slide, 1000 );
			
			anim.controller.move.by.Y = 480;
			anim.controller.move.ease = Legato_Animation.STRONG_EASE_OUT;
			anim.controller.opacity.to = 1;
			
			anim_sequence.addAnimation( anim );
			
			// Set the final slide out animation.
			var anim = new Legato_Animation( slide, 1000 );
			anim.controller.opacity.to = 0;
			anim.controller.delay = 7000;
			
			anim_sequence.addAnimation( anim );
			
		}
		
		// If this is the last slide in the set of animations, set the onFinish event. 
		if ( last ) anim_sequence.onFinish = function(){ Slideshow.nextSlide(); };
		
		// Start the sequence.
		anim_sequence.start();
		
	},
	
	
	nextSlide: function()
	{
		
		// Get the next slide's ID.
		var next_slide = Slideshow.current_index + 3;
		
		// If this is the last slide in the slideshow, start over again.
		if ( next_slide >= Slideshow.slides.length )
			next_slide = 0;
		
		// Set the current index to the next slide's ID.
		Slideshow.current_index = next_slide;
			
		// Now set up the slide to slide in.
		Slideshow.setupSlide( next_slide );
		
	}
	
}

Legato_Events_Handler.DOMReady( Slideshow.initialize );