﻿var divArray = new Array();
var divNumber=0;
var currentImage=0;

var waitInterval=5000;  //this is the interval between each fade
var fadeSpeed=2000;    //this is the speed of the fade action


var timer=-1;

$(function(){
getAllDivs();
setFader();

if(divNumber>0){
	timer = window.setInterval("fade()", waitInterval);
}

});

/**
 *	Gets all the divs that have to be shown in the slider and fills them in an array.
 */
function getAllDivs(){
	//fill the divs in an array
	$(".imageHolder").each(function(i){
		divArray[i]=$(this);
		divNumber++;
	});
}

/**
 *	Makes all the images invisible.
 */
function setFader(){
	for(var i=1; i<divNumber; i++){
		divArray[i].find("img").css({display:"none"});
	}
}

/**
 *	The whole fading is performed here.
 */
function fade(){
	if(divNumber>0){
		$img=divArray[currentImage].find("img");
		$img.fadeOut(fadeSpeed);
		if(currentImage<divNumber-1){
			$img=divArray[currentImage+1].find("img");
			currentImage++;
		}else{
			$img=divArray[0].find("img");
			currentImage=0;
		}
			
		$img.fadeIn(fadeSpeed);
	}
}
