//=======================================
//=			jQUERY IMAGE FADER			=
//=======================================

var current = 0; //current image index
var showing1 = 1; //currently visible element #1 (0 = Frame 1; 1 = Frame 2);
var showing2 = 1;
var showing3 = 1;

var showing1b=2;

var last = 2;

//config base array indexes
var images = 0;
var frames = 1;
var timing = 2;

//caching vars
var loaded = false;
var imagesLoading = false;
var cache = new Array();

//other variables
var i;

//start loading the config & image files
var cfg;

jQuery.get("js/jfader-config.csv", function(data) { 
cfg = jQuery.csv()(data);
			
	for (i=0;i<cfg[images].length;i++) { //load the images
		var cImg = document.createElement('img');
		cImg.src = cfg[images][i];
		cache.push(cImg);
	}
				
		//once we're done loading, fire the fade timer
		$(window).load(function() { 
			var $i1 = setInterval(fadeNext1, cfg[timing][1]);
		});
});

//fader function
function fadeNext1() {
		
		switch(last) {
		
			case 3: {
				if (showing1=1) { //fade-toggle both frames, 
					$(cfg[frames][1]+'a').fadeOut(cfg[timing][0], function(){$(cfg[frames][1]).css('background-image', 'url('+cfg[images][current]+')');}); //set bg image to the next one
					showing1 =0;
				}
				
				
			
				
				else {
					$(cfg[frames][1]+'a').fadeIn(cfg[timing][0], function(){$(cfg[frames][0]).css('background-image', 'url('+cfg[images][current]+')');}); //set bg image to the next one
					showing1++;
				}
				break;
			}
			case 1: {
				if (showing2) { //fade-toggle both frames, 
					$(cfg[frames][1]+'b').fadeOut(cfg[timing][0], function(){$(cfg[frames][1]).css('background-image', 'url('+cfg[images][current]+')');}); //set bg image to the next one
					showing2 = 0;
				}
				else {
					$(cfg[frames][1]+'b').fadeIn(cfg[timing][0], function(){$(cfg[frames][0]).css('background-image', 'url('+cfg[images][current]+')');}); //set bg image to the next one
					showing2 = 1;
				}
				break;
			}
			case 2: {
				if (showing3) { //fade-toggle both frames, 
					$(cfg[frames][1]+'c').fadeOut(cfg[timing][0], function(){$(cfg[frames][1]).css('background-image', 'url('+cfg[images][current]+')');}); //set bg image to the next one
					showing3 = 0;
				}
				else {
					$(cfg[frames][1]+'c').fadeIn(cfg[timing][0], function(){$(cfg[frames][0]).css('background-image', 'url('+cfg[images][current]+')');}); //set bg image to the next one
					showing3 = 1;
				}
				break;
			}
			
		}
		
		current++;
		last++;
		
		if (current >= cfg[images].length)
			current = 0;	

		if(last > 5)
			last = 1;
}



