/**
 * Entry function for the banner animation.
 *
 * @param countBanners The amount of banners we have to loop through.
 * @param withAnimation Decides whether we show an animation or not (usually used when there is no prototype available on given page). In that case only first banner image is shown wihtout any effect.
 */
function bannerAnimationEntry(countBanners,withAnimation) {
	//get init banner
	var initBanner = Math.floor(( Math.random() * (countBanners)));
	
	//set correct z-indexes
	var curBanner = initBanner;
	for (var i = 1; i <= countBanners; i++) {
		document.getElementById('bannerImg'+curBanner).style.zIndex = i;
		curBanner = (curBanner+1) % countBanners;
	}
	
	//init banner
	if (withAnimation) {
		//we want to show the banner as animation
		//show first image as animation
		setTimeout ('Effect.Appear(\'bannerImg'+initBanner+'\', {duration: 0.2})', 0);
			
		//start animation loop
		bannerAnimation(initBanner, countBanners);
	}
	else {
		//we do not want to show banner as animation
		//show first image without animation
		document.getElementById('bannerImg'+initBanner).style.display = 'block';
	}
}

/**
 * The banner animation as endless loop (achieved by calling itself again at the end).
 *
 * @param initBanner The index of the banner we began animation from
 * @param countBanners The total amount of banners
 */
function bannerAnimation(initBanner, countBanners) {
	//set animation params
	var animationInterval = 10000;
	var animationDuration = 3; //should be smaller than interval
	
	//start animation
	for (var i = 1; i < countBanners; i++) {
		curImg = (initBanner + i) % countBanners;
		
		appearInterval=i*animationInterval;
		if (i == (countBanners-1)) {
			fadeInterval=(i+1)*animationInterval;	
		}
		else {
			fadeInterval=(i+1)*animationInterval+animationInterval/2;
		}
		
		setTimeout ('Effect.Appear(\'bannerImg'+curImg+'\', {duration: '+animationDuration+'})', appearInterval);
		setTimeout ('Effect.Fade(\'bannerImg'+curImg+'\', {duration: '+animationDuration+'})', fadeInterval);
	}
	
	//preserve endless loop
	setTimeout ('bannerAnimation('+initBanner+','+countBanners+')', i*animationInterval);
}