var isSlideshowMaximized     = false;
var isSlideshowButtonEnabled = false;

var slides      = new Array();
var timecodes   = new Array();
var slidesCount = 0;
var loadIndex   = 0;
var visibleBufferIndex = 0;
var visibleSlideIndex = -1;
var areSlidesLoaded = false;

function addSlide( imageName, timecode )
{
	slides[slidesCount]    = imageName;
	timecodes[slidesCount] = timecode;
	slidesCount++;
}

function SlidesPreloader()
{
	var percent_elt = document.getElementById( "loadingPercent" );
	percent_elt.innerHTML = "0&#37;";
	
	document.getElementById( "loadingFrame" ).style.display = "block";
	setChaptersVisible( false );
	
	this.loadedCount = 0;
	this.processedCount = 0;
	this.preloadedImages = new Array;
	
	this.preload( "rsrc/SMA2.001.png" );
}

SlidesPreloader.prototype.preload = function( imageSrc )
{
	var img = new Image;
	this.preloadedImages.push( img );
	
	img.onload = SlidesPreloader.prototype.onload;
	img.preloader = this;
	img.loaded = false;	
	img.src = imageSrc;
}

SlidesPreloader.prototype.continueLoading = function()
{
	for ( var i=0; i<slidesCount; i++ )
	{
		this.preload( "rsrc/" + slides[i] );
	}
}

SlidesPreloader.prototype.onComplete = function()
{
	this.processedCount++;
	
	var percent_elt = document.getElementById( "loadingPercent" );
	var percent = ( 100.0 * parseFloat( this.processedCount ) ) / parseFloat( slidesCount + 1 );
	percent_elt.innerHTML = parseInt( percent ) + "&#37;";
	
	if ( this.processedCount == 1 )
	{
		var loading_img = flowplayer("player").getPlugin("loadingImg");
		loading_img.setHtml(
			"<img src=\"rsrc/SMA2.001.png\" align=\"left\" vspace=\"0\" hspace=\"0\" />"
		);
		loading_img.css( {top:"0px"} );
		loading_img.show();
		this.continueLoading();
	}
	if ( this.processedCount == (slidesCount + 1) )
	{
		areSlidesLoaded = true;
		
		var loading_img = flowplayer("player").getPlugin("loadingImg");
		loading_img.animate(
			{top:"548px"},
			1000,
			function() { flowplayer("player").getPlugin("loadingImg").hide(); }
		);
		
		setChaptersVisible( true );
		flowplayer("player").getPlugin("slideshowSmall_1").show();
		flowplayer("player").getPlugin("slideshowSmall_2").show();
		flowplayer("player").getPlugin("slideshowBig_1").show();
		flowplayer("player").getPlugin("slideshowBig_2").show();
		
		flowplayer("player").getScreen().animate( { left: "0px" }, 1000 );
		showSlide( 0 );
		
		$f().getControls().enable( {all: true} );
		$f(0).play();
	}
}

SlidesPreloader.prototype.onload = function()
{
	this.loaded = true;
	this.preloader.loadedCount++;
	this.preloader.onComplete();
}

function showSlide( index )
{
	if ( index == visibleSlideIndex)
	{
		return;
	}
	visibleSlideIndex = index;
	
	var small_1 = flowplayer("player").getPlugin("slideshowSmall_1");
	var small_2 = flowplayer("player").getPlugin("slideshowSmall_2");
	var big_1 = flowplayer("player").getPlugin("slideshowBig_1");
	var big_2 = flowplayer("player").getPlugin("slideshowBig_2");
	
	if ( visibleBufferIndex != 1 )
	{
		visibleBufferIndex = 1;
		
		small_1.setHtml(
			"<img src=\"rsrc/"+slides[index]+"\" align=\"left\" width=\"380\" height=\"285\" vspace=\"0\" hspace=\"0\" />"
		);
		big_1.setHtml(
			"<img src=\"rsrc/"+slides[index]+"\" align=\"left\" vspace=\"0\" hspace=\"0\" />"
		);
		if ( isSlideshowMaximized )
		{
			big_2.animate( {top:"548px"}, 1000 );
			big_1.animate( {top:"0px"}, 1000 );
		}
		else
		{
			if ( visibleBufferIndex != 0 )
			{
				small_2.animate( {left:"744px"}, 1000 );
			}
			small_1.animate( { left: "353px" }, 1000 );
		}
	}
	else
	{
		visibleBufferIndex = 2;
		
		small_2.setHtml(
			"<img src=\"rsrc/"+slides[index]+"\" align=\"left\" width=\"380\" height=\"285\" vspace=\"0\" hspace=\"0\" />"
		);
		big_2.setHtml(
			"<img src=\"rsrc/"+slides[index]+"\" align=\"left\" vspace=\"0\" hspace=\"0\" />"
		);
		if ( isSlideshowMaximized )
		{
			big_1.animate( {top:"548px"}, 1000 );
			big_2.animate( {top:"0px"}, 1000 );
		}
		else
		{
			small_1.animate( {left:"744px"}, 1000 );
			small_2.animate( { left: "353px" },	1000 );
		}
	}
	
}

function setSlideshowButtonEnabled( isEnabled )
{
	var button = document.getElementById( "slideshowButton" );
	
	if ( isEnabled == true )
	{
		button.className = "slideshowButtonEnabled";
		isSlideshowButtonEnabled = true;
	}
	else
	{
		button.className = "slideshowButtonDisabled";
		isSlideshowButtonEnabled = false;
	}
}

function toggleSlideshow()
{
	if ( isSlideshowButtonEnabled == false )
	{
		return;
	}
	var screen = flowplayer("player").getScreen();
	var big;
	var small;
	
	if ( visibleBufferIndex == 1 )
	{
		big   = flowplayer("player").getPlugin("slideshowBig_1");
		small = flowplayer("player").getPlugin("slideshowSmall_1");
	}
	else
	{
		big   = flowplayer("player").getPlugin("slideshowBig_2");
		small = flowplayer("player").getPlugin("slideshowSmall_2");
	}
	if ( isSlideshowMaximized == true )
	{
		isSlideshowMaximized = false;
		setSlideshowButtonEnabled( false );
		document.getElementById( "slideshowButton" ).innerHTML = "Agrandir la présentation";
		
		
		big.animate( {top:" 548px"}, 1000 ); //, function() { big.hide(); } );
		small.animate( { left: "353px" }, 1000 );
		screen.animate( { left: "0px" }, 1000 );	
		setTimeout( "setSlideshowButtonEnabled( true )", 1000 );
	}
	else
	{
		isSlideshowMaximized = true;
		setSlideshowButtonEnabled( false );
		document.getElementById( "slideshowButton" ).innerHTML = "Tout voir";
		
		big.show();
		big.animate( { top:" 0px"},	1000 );
		
		small.animate( { left: "744px" }, 1000 );
		screen.animate( { right: "744px" }, 1000 );	
		setTimeout( "setSlideshowButtonEnabled( true )", 1000 );
	}
}

function setChaptersVisible( isVisible )
{
	var loading_frame = document.getElementById( "loadingFrame" );
	var chapters = document.getElementById( "chapters" );
	var slideshow_btn_frame = document.getElementById("slideshowButtonFrame");
	
	if ( isVisible )
	{
		loading_frame.style.display = "none";
		chapters.style.display = "block";
		slideshow_btn_frame.style.display = "block";
	}
	else
	{
		loading_frame.style.display = "block";
		chapters.style.display = "none";
		slideshow_btn_frame.style.display = "none";
	}
}

function chapterClicked( timecode )
{
	if ( areSlidesLoaded )
	{
		$f().seek( timecode );
	}
}

function showFlashPlayerMsg()
{
	document.getElementById("mainFrame").innerHTML =
		"<div id='failFrame'>" +
		"    <h3>Lecteur Adobe&#174; Flash&#174; nécessaire</h3>" +
		"    <p style='text-align:left'>net-sketch requiert le plugin Adobe&#174; Flash&#174; version 9.0 ou supérieure.</p>"+
		"    <p style='text-align:left'>Vous n'avez pas de plugin Adobe&#174; Flash&#174; installé ou ce plugin est désactivé." +
		"    Cliquez sur le bouton ci-dessous pour installer le plugin.</p>" +
		"    <p><a href='http://get.adobe.com/flashplayer/'>" + 
		"        <img src='./rsrc/160x41_get_flashplayer.gif' />" +
		"</a></p></div>";
}
