$(document).ready(function() {

	var A_section = [ 'testimonial', 'television', 'press', 'photograph' ];
	var sectionIndex = 0;
	var sliderPauseSwitch = false;
	var sliderSpeed = 8000;
	/* Power the browsing of slides within a section*/

	function slideshowBoardBrowser( boardObject )
	{
		/* Make active tab visible*/
		$('#slideshow .board .tabs .tab').removeClass('active');
 		boardObject.addClass('active');	

		/* Find what slide the user has chosen and display it */ 		
		var slideId = boardObject.attr('id');

		$('#slideshow .board .view .slide, slideshow .board .view .slide *').hide();
		
		/* Beautify the transition using a loader overlay */ 		
		$('#slideshow .loader').show();
		$('#slideshow .board .view .slide#'+slideId+'').fadeIn(500);
		$('#slideshow .loader').fadeOut(250);
	}

	$('#slideshow .board .tabs .tab').click(function(){
		$('#slideshow .board .television .media .video .cover').show();
		$('#slideshow .board .television .media .video .cover').parent().children('.stream').hide();
		slideshowBoardBrowser( $(this) );
	})

	$('#slideshow .board .television .media .video .cover').click(function(){
		$(this).hide();
		$(this).parent().children('.stream').show();
		
		sliderPauseSwitch = false;
		slideshowPause();		
	})

	/* Power the browsing of sections */

	function slideshowSectionBrowser( sectionObject )
	{
		/* Make active section visible*/
		$('#slideshow .board .tabs .tab').removeClass('active');
		$('#slideshow .board .tabs .tab#first').addClass('active');
		$('#slideshow .sections .tabs .tab').removeClass('active');
		
		/* Find what slide the user has chosen and display it */ 				
		sectionObject.addClass('active');		
		var sectionId = sectionObject.attr('id');		

		/*
			Style the slideshow accordingly to the section to display
				Note : this could probably be handled by CSS alone
		*/
		
		switch(sectionId) {
		 case 'television':
			$("#slideshow").addClass('black');
		 break;
		 case 'photograph':
			$("#slideshow").addClass('black');
		 break;
		 default:
			$("#slideshow").removeClass('black');
		 break;
		}

		//Beautify the transition using a loader overlay 		
		$('#slideshow .loader').show();
		
		//Show chosen section
		$('#slideshow .board .view').hide();
		$('#slideshow .board .view .slide').hide();
		$('#slideshow .board .view .slide#first').show();
		$('#slideshow .board .view.' + sectionId ).show();
		$('#slideshow .loader').fadeOut(250);
	}
	
	$('#slideshow .sections .tabs .tab').click(function(){
	    
		$('#slideshow .board .television .media .video .cover').show();
		$('#slideshow .board .television .media .video .cover').parent().children('.stream').hide();

	    var currentSection = $(this);
	    
	    $.each( A_section, function( index, value ) {

	    	if ( value == currentSection.attr( 'id' ) )
	    	{
				sectionIndex = index;
				slideshowSectionBrowser( currentSection );
	    	}
	    });
		
	})

	/* AUTO SLIDER */
	
	if ( ! sliderPauseSwitch )
	{
	    slideshowAutoSlider();
	}
	
	function slideshowAutoSlider()
	{
		$(document).delay( sliderSpeed, function(){
		
			if ( ! sliderPauseSwitch )
			{
				var nextBoardObject = new Object();
				var nextSectionObject = new Object();
				
				$('#slideshow .board .' + A_section[ sectionIndex ] + ' .tabs .tab').each(function(){
					
					if ( $(this).is( '.active' ) )
					{
						nextBoardObject = $(this).next();
					}
				})
				
				if ( nextBoardObject.attr( 'id' ) == undefined || nextBoardObject.attr( 'id' ) == '' )
				{
					if ( sectionIndex < A_section.length - 1 )
					{
						sectionIndex++;
					}
					else
					{
						sectionIndex = 0;
					}
									
					$('#slideshow .sections .tabs .tab').each(function(){
						
						if ( $(this).is( '.active' ) )
						{
							if ( sectionIndex > 0 )
							{
								nextSectionObject = $(this).next();
							}
							else
							{
								nextSectionObject = $('#slideshow .sections .tabs .tab#' + A_section[ sectionIndex ]);
							}
						}
						
					})
					
					slideshowSectionBrowser( nextSectionObject );
				}
				else
				{
					slideshowBoardBrowser( nextBoardObject );
				}
				
				if ( ! sliderPauseSwitch )
				{
					slideshowAutoSlider();
				}
			}
		});
	}	
	
	
	/* PAUSE */
	
	$('#slideshow .board .tabs .pause input').click(function(){
		slideshowPause();
	})

	function slideshowPause()
	{
		if ( sliderPauseSwitch )
		{
			sliderPauseSwitch = false;
			$('#slideshow .board .tabs .pause input').parent().removeClass('on');
			$('#slideshow .board .tabs .pause input').parent().addClass('off');
			$('#slideshow .board .television .media .video .cover').show();
			$('#slideshow .board .television .media .video .cover').parent().children('.stream').hide();
			slideshowAutoSlider();
		}
		else
		{
			$('#slideshow .board .tabs .pause input').parent().removeClass('off');
			$('#slideshow .board .tabs .pause input').parent().addClass('on');
			sliderPauseSwitch = true;
		}
	}
	
});

