
/************************************
	Slide Show V
	version 1.0
	last revision: 01.25.2005
	steve@slayeroffice.com

	should you modify or improve upon
	this code, let me know so that I can
	update the version hosted at slayeroffice

	PLEASE LEAVE THIS NOTICE IN TACT!

************************************/

// shorcut reference var to the document object	
var d = document;			
var zInterval = null;		// timer interval
var swapped = false;		// boolean to tell us if we've swapped the old image for the new one yet

var mImages = new Array();	// reference array to the image elements in the container element
var preload = new Array();

var mImagesWidth = 513;

var MASK_SPEED = 4;

var activeTransition = false;		// are we currently transitioning?

function slide_init() {
	if(!d.getElementById) return;					// older browser, do nothing.
	mObj = d.getElementById("mContainer");			// reference to the container element

	mImages[0] = 'skins/projixi/images/homeslide1.jpg'; 
	mImages[1] = 'skins/projixi/images/homeslide4.jpg'; 
	mImages[2] = 'skins/projixi/images/homeslide3.jpg'; 

	for (var i = 0 ; i < mImages.length ; i++ ) {
		preload[i] = new Image();
		preload[i].index = i;
		preload[i].loading = 0;
		preload[i].onload = function() {
			
			preload[this.index].loading = 2;
		}

		preload[i].src = mImages[i];
		preload[i].loading = 1;
	}

	
	// securitée : considérer les images comme chargées apres 3s
	setTimeout( function() { for(var i in preload) preload[i].loading = 2; } , 3000 );
	
	
	loadImageAndInit(0,0);
}


function loadImageAndInit(index, delay) {
	
	if (2 == preload[index].loading) {
		setTimeout( function () { initSlideMask(index); }, delay);
	} else {
		setTimeout( function () { loadImageAndInit(index, delay); } ,50);
	}
}




function initSlideMask(nxtImage) {
	// initialize the transition
	// already transitioning, bail out.
	if(zInterval != null) return;

	d.getElementById("mContainerScroll").style.backgroundPosition = 513+'px';
	d.getElementById("mContainerScroll").style.filter = 'alpha(opacity=-20)';

	src = mImages[nxtImage];
	d.getElementById("mContainerScroll").style.backgroundImage = "url(" + src + ")";

	i = 1;
	
	// define the interval function
	fn = function() { i += 1; m = i % 2; slideMask(nxtImage, m); }			
	// start the interval
	zInterval = setInterval(fn,50);
}

function slideMask(nxtImage, m) {					// the function we iterate over to do the transitions
	x = parseInt(d.getElementById("mContainerScroll").style.backgroundPosition);
	o = '';
	if (d.getElementById("mContainerScroll").style.filter)
	{
		d.getElementById("mContainerScroll").style.filter.match(/alpha\(opacity=(.*)\)/);
		o = parseInt(RegExp.$1);
	}
	
	if(!activeTransition) {
		// prevents the user from changing the transition type mid-transition	
		activeTransition = true;
	}

	x-=MASK_SPEED;
	if (o < 100 && m==0 ) {
		o+=2;
		d.getElementById("mContainerScroll").style.filter = 'alpha(opacity='+o+')';
	}
	

	// put the mask div where it needs to be
	d.getElementById("mContainerScroll").style.backgroundPosition = x+'px top';
	clr = false;

	if(x <= 1) clr = true;
	
	if(clr) { 
		// transition has completed. clear out the interval and reset "swapped" to false
		clearInterval(zInterval);
		zInterval = null;
		swapped = false;
		activeTransition = false;

		d.getElementById("mContainer").style.backgroundImage = d.getElementById("mContainerScroll").style.backgroundImage;
		d.getElementById("mContainerScroll").style.backgroundImage = 'none';

		if (typeof mImages[nxtImage+1] != 'undefined' ) {
			loadImageAndInit( nxtImage+1 , 1000);
		} 	
	}
}

