
var fadingOut = 0;
var fadingIn = 0;
var timer;

function switchClass(activeID,photo){
    setPhoto(photo);
}

function setPhoto(photo) {		
    if(currentPhoto != photo){
    	if((fadingOut == 0)&&(fadingIn == 0)){
    		fadingOut = 1;
	    	$("#homeFoto").fadeTo('normal',0, function(){	    		
		        document.getElementById('homeFoto').src="/site_files/imgc/" + photo ;
		        currentPhoto = photo;
		        fadingOut = 0;
		        fadingIn = 1;
		        clearTimeout(timer);
		        $("#homeFoto").fadeTo('normal',1, function(){fadingIn = 0; doAnimation();return true;}); 
	        });
    	}else{
    		$("#homeFoto").stop();    		
    		if((fadingIn == 1)){
    			currentPhoto = photo;
    			document.getElementById('homeFoto').src="/site_files/imgc/" + photo ;
    			clearTimeout(timer);
    			$("#homeFoto").fadeIn('normal', function(){fadingIn = 0; doAnimation(); return true;}); 
    		}
    		if((fadingOut == 1)){
    			$("#homeFoto").fadeTo(0,0, function(){	    		
			        document.getElementById('homeFoto').src="/site_files/imgc/" + photo ;
			        currentPhoto = photo;
			        fadingOut = 0;
			        fadingIn = 1;
			        clearTimeout(timer);
			        $("#homeFoto").fadeTo('normal',1, function(){fadingIn = 0;doAnimation();return true;}); 
		        });
    		}
    	}
    }    
}

function doAnimation(){	
	$("#homeFoto").stop();	
	index = findIndex(images2, currentPhoto);
	index++;		
	if(index ==  images2.length){index = 0;	}
	index2=index + 1 ;
	timer = setTimeout( "switchClass(index2,images2[index])", 5000 );	
}

function findIndex(array,value){
	var ctr = "";
	for (var i=0; i < array.length; i++) {
		// use === to check for Matches. ie., identical (===), ;
		if (array[i] == value) {
			return i;
		}
	}
	return ctr;
};