
Slideshow =
{

	slides: [],
	current_index: 0,

	pages:
	{
		'page-services': 'services',
		'page-general-construction': 'services',
		'page-design-build': 'services',
		'page-construction-management': 'services'
	},


	initialize: function()
	{

		// Make sure we should initialize this page.
		var found = false;
		for ( var page_index in Slideshow.pages )
			if ( $$( page_index ) )
				found = Slideshow.pages[page_index];

		if ( !found )
			return;

		// Let's loop through all the slides.
		for ( var i = 0; i < 9; i++ )
		{

			// Create a div for this slide.
			Slideshow.slides[i] = $$( 'right-col' ).create( 'div', {}, true, '<img src="' + SITE_URL + '/img/slides/' + found + '/' + (i + 1) + '.jpg" />' );
			var slide = $( Slideshow.slides[i] );


			slide.setStyle( 'opacity', 0.01 );
			slide.position( null, 0 );

		}

		// 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);

		var right_columnY = $$( 'right-col' ).position()[1];

		// 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] );

			// Set the new Y value.
			slide.position( null, right_columnY - 95 );

			// 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 = 450;
			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.01;
			anim.controller.delay = 7000;

			anim_sequence.addAnimation( anim );

		}
		else if ( stage == 1 )
		{

			// Create the initial animation.
			var anim = new Legato_Animation( slide, 1000 );

			anim.controller.move.by.Y = 310;
			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.01;
			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 = 170;
			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.01;
			anim.controller.delay = 6000;

			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( function(){ Slideshow.initialize(); } );
var Header_Slideshow =
{

	slides: [],
	current_index: 0,

	pages:
	[
		'page-index',
		'projects-index',
		'projects-view',
		'page-construction-management',
		'page-about',
		'page-contact'
	],


	initialize: function()
	{

		// Make sure we should initialize this page.
		var found = false;
		for ( var i in Header_Slideshow.pages )
			if ( $$( Header_Slideshow.pages[i] ) )
				found = true;

		if ( !found )
			return;

		// Let's loop through all the slides.
		for ( var i = 0; i < 9; i++ )
		{

			// Create a div for this slide.
			Header_Slideshow.slides[i] = $$( 'header-pics' ).create( 'div', { id: ('slide' + i), className: 'slide' }, true, '<img src="' + SITE_URL + '/img/slides/header/' + (i + 1) + '.jpg" />' );
			var slide = $( Slideshow.slides[i] );

			slide.setStyle( 'opacity', 0.01 );

		}

		// Let's set up the first slide.
		Header_Slideshow.setupSlide( 0 );

	},


	setupSlide: function( slide_id )
	{

		var slide = Header_Slideshow.slides[slide_id];

		slide.setStyle( 'opacity', 0.01 );
		slide.setStyle( 'z-index', 5 );

		// Set the final slide out animation.
		var anim = new Legato_Animation( slide, 1000 );
		anim.controller.opacity.to = 1;
		anim.controller.delay = 4000;
		anim.onFinish = function(){ Header_Slideshow.nextSlide(); };

		// Start the sequence.
		anim.start();

	},


	nextSlide: function()
	{

		var hidden_index = Header_Slideshow.current_index - 3;

		if ( hidden_index < 0 )
			hidden_index = Header_Slideshow.slides.length + hidden_index;

		Header_Slideshow.slides[hidden_index].setStyle( 'z-index', 1 );

		var slide = Header_Slideshow.slides[Header_Slideshow.current_index];
		slide.setStyle( 'z-index', 4 );

		// Get the next slide's ID.
		var next_slide = Header_Slideshow.current_index + 1;

		// If this is the last slide in the slideshow, start over again.
		if ( next_slide >= Header_Slideshow.slides.length )
			next_slide = 0;

		// Set the current index to the next slide's ID.
		Header_Slideshow.current_index = next_slide;

		// Now set up the slide to slide in.
		Header_Slideshow.setupSlide( next_slide );

	}

};

Legato_Events_Handler.DOMReady( function(){ Header_Slideshow.initialize(); } );
