/**
This script contains the slider

Used framework : Prototype
*/

var currentPicture = 0;
var pictureCollection = new Array();
var started = 0;
var loadPicture = 0;
var oldPicture = 0;
// Delay time
var Delay = 5;
var Duration = 2;
var Direction = 0;
var shallIFade = 1;
var mouseOverBox = 0;
var currentEffect = null;
var timeOutId = null;

function initSlider(event) {
	
	//pictureCollection = $$('div.topContentBoxHome');
	if($$('div.topContentBoxHome')) {
		$$('div.topContentBoxHome').each(function(s, index) {
				s.style.visibility = 'visible';
				pictureCollection.push($$('div.topContentBoxHome')[index].up());
		});
		
		// Comment this out if you want always the same picture at the beginning
		currentPicture = 0;
		oldPicture = currentPicture;

		if (pictureCollection.length > 0) {
			pictureCollection.each(function(ctElement, index) {
				ctElement.style.position="absolute";
			});
			pictureCollection[currentPicture].style.zIndex=100;
			hideOthers();
			timeOutId = setTimeout("fade()", Delay*1000);
		}
	}
}

function switchFade(delay, direction) {
	Direction = direction;
	shallIFade = 0;
	if (timeOutId) {
		clearTimeout(timeOutId);
	}
	if (currentEffect) {
		currentEffect.cancel();
	}
	fadeFast();
	returnToNormal();
}

function returnToNormal() {
	Direction = 0;
	if (shallIFade == 0) {
		timeOutId = setTimeout('fade()', Delay*1000);
	}
	shallIFade = 1;
}

function fadeFast() {
	loadPicture = currentPicture + Direction;
	if(loadPicture < 0) {
		loadPicture = pictureCollection.length - 1;
	} else if (loadPicture >= pictureCollection.length) {
		loadPicture = 0;
	}

	pictureCollection[currentPicture].style.zIndex = 99;
	pictureCollection[loadPicture].setOpacity(1);
	pictureCollection[loadPicture].style.display='inline-block';
	pictureCollection[loadPicture].style.zIndex=100;

	oldPicture = currentPicture;
	currentPicture = loadPicture;

	hideOthers();
}

function fade() {
	if (shallIFade == 1) {
		// do not hide the pic in the first pass
		pictureCollection[loadPicture].style.display='inline-block';
		if(started != 0) {
			$(pictureCollection[oldPicture].id).style.display = 'none';
		}
		started = 1;
		
		loadPicture = getRandom(1,pictureCollection.length) - 1;
		//loadPicture = currentPicture + 1;
		
		// is this a possible picture number?
		if(loadPicture == currentPicture) {
			if(loadPicture + 1 == pictureCollection.length) {
				loadPicture = loadPicture - 1;
			} else {
				loadPicture = loadPicture + 1;
			}
		}

		pictureCollection[currentPicture].style.zIndex = 99;
		pictureCollection[loadPicture].setOpacity(0.01);
		pictureCollection[loadPicture].style.display='inline-block';
		pictureCollection[loadPicture].style.zIndex=100;

		oldPicture = currentPicture;
		currentPicture = loadPicture;

		currentEffect = new Effect.Appear(
			pictureCollection[loadPicture].id,
			{
				from: 0.01, 
				to: 1, 
				duration: Duration, 
				delay: 0,
				queue :{ 
					scope: 'banner'
				},
				afterFinish: callbackFade
			}
		);
	}
}

function callbackFade(effect) {
	if (shallIFade == 1) {
		hideOthers();
		timeOutId = setTimeout("fade()", Delay*1000);
	}
}

function hideOthers() {
	pictureCollection.each(function(ctElement, index) {
		if(index != currentPicture) {
			ctElement.style.display="none";
			ctElement.style.zIndex=98;
			ctElement.setOpacity(0.01);
		}
		
	});
}

function getRandom( min, max ) {
	if( min > max ) {
		return( -1 );
	}
	if( min == max ) {
		return( min );
	}
	return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}

Event.observe(window, 'load', initSlider);

