/**
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 = 4;
var Duration = 2;
var Direction = 0;
var shallIFade = 1;
var mouseOverBox = 0;
var currentEffect = null;
var timeOutId = null;

function initHeaderRotation() {
	if (!animationImages || animationImages.length < 2) {
		return;
	}

	imageContentBox = '';
	animationImages.each(function(s, index) {
			imageContentBox += '<div class="headerImageContentBox" style="background: url(' + s + ') no-repeat scroll center top #FFFFFF;">&nbsp;</div>';
	});
	imageContentBox = '<div id="headerImageContainer">' + imageContentBox + '</div>';
	lastBodyItem = $$('body').last();
	lastBodyItem.innerHTML += imageContentBox;
	lastBodyItem.style.background = 'none';

	//pictureCollection = $$('div.headerImageContentBox');
	if($$('div.headerImageContentBox')) {
		$$('div.headerImageContentBox').each(function(s, index) {
				pictureCollection.push($$('div.headerImageContentBox')[index]);
		});
		
		// Comment this out if you want always the same picture at the beginning
		currentPicture = 0;
		oldPicture = currentPicture;

		if (pictureCollection.length > 0) {
			pictureCollection[currentPicture].style.zIndex=500;
			hideOthers();
			timeOutId = setTimeout("fade()", Delay*1000);
		}
	}
}

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]).style.display = 'none';
		}
		started = 1;
		
		//loadPicture = getRandom(1,pictureCollection.length) - 1;
		loadPicture = currentPicture + 1;
		
		if(!pictureCollection[loadPicture]) {
			loadPicture = 0;
		}

		pictureCollection[currentPicture].style.zIndex = 49;
		pictureCollection[loadPicture].setOpacity(0.01);
		pictureCollection[loadPicture].style.display='inline-block';
		pictureCollection[loadPicture].style.zIndex=50;

		oldPicture = currentPicture;
		currentPicture = loadPicture;

		currentEffect = new Effect.Appear(
			pictureCollection[loadPicture],
			{
				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=48;
			ctElement.setOpacity(0.01);
		}
		
	});
}


