var kSwapTimeout = 3000; /* 4*1000 = 4 seconds */
var kSwapRetry = 1000;
var kBlankImage = new Image();
var swap1Data = new Array();
var swap2Data = new Array();
var swap3Data = new Array();
var current1Swap = 0;
var current2Swap = 0;
var current3Swap = 0;
var swap1Count = 0;
var swap2Count = 0;
var swap3Count = 0;
var in1Animation = false;
var in2Animation = false;
var in3Animation = false;

document.getNextSwap = function(which) {
	var nextSwap;
	if(which == 1) {
		if(current1Swap + 1 >= swap1Data.length) {
			nextSwap = 0;
		} else {
			nextSwap = current1Swap + 1;
		}
	} else if(which == 2) {
		if(current2Swap + 1 >= swap2Data.length) {
			nextSwap = 0;
		} else {
			nextSwap = current2Swap + 1;
		}
	} else {
		if(current3Swap + 1 >= swap3Data.length) {
			nextSwap = 0;
		} else {
			nextSwap = current3Swap + 1;
		}
	}
	
	return nextSwap;
}

document.loadNextImage = function(nextSwap, which) {
	if(which == 1) {
		if(!swap1Data[nextSwap]['image']) {
			swap1Data[nextSwap]['image'] = new Image();
			swap1Data[nextSwap]['image'].src = swap1Data[nextSwap]['path'];
		}
	} else if(which == 2) {
		if(!swap2Data[nextSwap]['image']) {
			swap2Data[nextSwap]['image'] = new Image();
			swap2Data[nextSwap]['image'].src = swap2Data[nextSwap]['path'];
		}
	} else {
		if(!swap3Data[nextSwap]['image']) {
			swap3Data[nextSwap]['image'] = new Image();
			swap3Data[nextSwap]['image'].src = swap3Data[nextSwap]['path'];
		}
	}
}

document.swapImage = function(which) {
	if(which == 1) {
		if(in1Animation || swap1Data.length < 2) {
			return;
		}
	
		var nextSwap = document.getNextSwap(1);
		
		if(!swap1Data[nextSwap]['image']) {
			setTimeout('document.swapImage(1);', kSwapRetry);
			return;
		}
	
		var nextImg = document.getElementById('swap1Img'+((swap1Count+1) % 2));
		nextImg.src = swap1Data[nextSwap]['path'];
		
		if(swap1Data[nextSwap]['animation']) {
			swap1Data[nextSwap]['animation'].stop();
		}
		
		if(document.swap1Animation) {
			document.swap1Animation();
		}
		
		in1Animation = true;
	} else if(which == 2) {
		if(in2Animation || swap2Data.length < 2) {
			return;
		}
	
		var nextSwap = document.getNextSwap(2);
		
		if(!swap2Data[nextSwap]['image']) {
			setTimeout('document.swapImage(2);', kSwapRetry);
			return;
		}
	
		var nextImg = document.getElementById('swap2Img'+((swap2Count+1) % 2));
		nextImg.src = swap2Data[nextSwap]['path'];
		
		if(swap2Data[nextSwap]['animation']) {
			swap2Data[nextSwap]['animation'].stop();
		}
		
		if(document.swap2Animation) {
			document.swap2Animation();
		}
		
		in2Animation = true;
	} else {
		if(in3Animation || swap3Data.length < 2) {
			return;
		}
	
		var nextSwap = document.getNextSwap(3);
		
		if(!swap3Data[nextSwap]['image']) {
			setTimeout('document.swapImage(3);', kSwapRetry);
			return;
		}
	
		var nextImg = document.getElementById('swap3Img'+((swap3Count+1) % 2));
		
		nextImg.src = swap3Data[nextSwap]['path'];
		
		if(swap3Data[nextSwap]['animation']) {
			swap3Data[nextSwap]['animation'].stop();
		}
		
		if(document.swap3Animation) {
			document.swap3Animation();
		}
		
		in3Animation = true;
	}
}

document.swapImageFinish = function(which) {
	if(which == 1) {
		var nextSwap = document.getNextSwap(1);
		var currentDiv = document.getElementById('swap1Div'+(swap1Count % 2));
		var nextDiv = document.getElementById('swap1Div'+((swap1Count+1) % 2));
		var tempIndex;
		
		tempIndex = currentDiv.style.zIndex;
		currentDiv.style.zIndex = nextDiv.style.zIndex;
		nextDiv.style.zIndex = tempIndex;
		
		current1Swap = nextSwap;
		swap1Count += 1;
		
		nextSwap = document.getNextSwap(1);
		document.loadNextImage(nextSwap, 1);
		
		in1Animation = false;
	} else if(which == 2) {
		var nextSwap = document.getNextSwap(2);
		var currentDiv = document.getElementById('swap2Div'+(swap2Count % 2));
		var nextDiv = document.getElementById('swap2Div'+((swap2Count+1) % 2));
		var tempIndex;
		
		tempIndex = currentDiv.style.zIndex;
		currentDiv.style.zIndex = nextDiv.style.zIndex;
		nextDiv.style.zIndex = tempIndex;
		
		current2Swap = nextSwap;
		swap2Count += 1;
		
		nextSwap = document.getNextSwap(2);
		document.loadNextImage(nextSwap, 2);
		
		in2Animation = false;
	} else {
		var nextSwap = document.getNextSwap(3);
		var currentDiv = document.getElementById('swap3Div'+(swap3Count % 2));
		var nextDiv = document.getElementById('swap3Div'+((swap3Count+1) % 2));
		var tempIndex;
		
		tempIndex = currentDiv.style.zIndex;
		currentDiv.style.zIndex = nextDiv.style.zIndex;
		nextDiv.style.zIndex = tempIndex;
		
		current3Swap = nextSwap;
		swap3Count += 1;
		
		nextSwap = document.getNextSwap(3);
		document.loadNextImage(nextSwap, 3);
		
		in3Animation = false;
	}
}

if(document.initSwap) {
	document.initSwap();
	
	var nextSwap = document.getNextSwap(1);
	document.loadNextImage(nextSwap, 1);
	
	nextSwap = document.getNextSwap(2);
	document.loadNextImage(nextSwap, 2);
	
	nextSwap = document.getNextSwap(3);
	document.loadNextImage(nextSwap, 3);
}
