var current;
var total;
var topLeft;
var topRight;
var bottomLeft;
var bottomRight;
var position = new Array();
position[0] = 'topLeft';
position[1] = 'topRight';
position[2] = 'bottomRight';
position[3] = 'bottomLeft';
var preload = new Array();


function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function setPictures() {
	current = 0;
	total = totalPictures;
	topLeft = document.getElementById('topLeft');
	topRight = document.getElementById('topRight');
	bottomLeft = document.getElementById('bottomLeft');
	bottomRight = document.getElementById('bottomRight');

	topLeft.src = 'images/rotate/'+images[current++];
	topRight.src = 'images/rotate/'+images[current++];
	bottomRight.src = 'images/rotate/'+images[current++];
	bottomLeft.src = 'images/rotate/'+images[current++];

	var len = preload.length;
	if(len < total) {
		preload[len] = new Image();
		preload[len].src = 'images/rotate/'+images[current];
	}

	startRotation(0);

}

function startRotation(pos) {

	var which = new Array();
	var temp = pos;
	for(var i=0;i<4;i++) {
		rotate[position[temp]] = setInterval("rotate('"+position[temp]+"');", (3000+(3000*i)))
		which[i] = temp++;
		if(temp > 3) {
			temp = 0;
		}
	}

}

function clickRotate(which) {
	clearInterval(rotate['topLeft']);
	clearInterval(rotate['topRight']);
	clearInterval(rotate['bottomRight']);
	clearInterval(rotate['bottomLeft']);

	if(which.filters) {
		which.filters(0).Apply();
		which.src = 'images/rotate/'+images[current++];
		which.filters(0).Play();
	}
	else {
		which.src = 'images/rotate/'+images[current++];
	}

	if(current == total) {
		current = 0;
	}

	//Pre-load the images
	var len = preload.length;
	if(len < total) {
		preload[len] = new Image();
		preload[len].src = 'images/rotate/'+images[current];
	}

	var pos = which.id == 'topLeft' ? 1 : which.id == 'topRight' ? 2 : which.id == 'bottomRight' ? 3 : 0;

	startRotation(pos);
}

function rotate(which) {
	clearInterval(rotate[which]);
	rotate[which] = setInterval("rotate('"+which+"');", 12000);

	if(eval(which).filters) {
		eval(which).filters(0).Apply();
		eval(which).src = 'images/rotate/'+images[current++];
		eval(which).filters(0).Play();
	}
	else {
		eval(which).src = 'images/rotate/'+images[current++];
	}

	if(current == total) {
		current = 0;
	}

	//Pre-load the images
	var len = preload.length;
	if(len < total) {
		preload[len] = new Image();
		preload[len].src = 'images/rotate/'+images[current];
	}

}

addLoadEvent(setPictures);

