var i = 0;

// preloader code from Kyle Neath @ warpspire.com
var preloaderfinished = 0;
var Preloader = {
	callbacks: [],
	images: [],
	loadedImages: [],
	imagesLoaded: 0,

	add: function(image){
		if (typeof image == 'string') this.images.push(image);
		if (typeof image == 'array' || typeof image == 'object'){
			for (var i=0; i< image.length; i++){
				this.images.push(image[i]);
			}
		}
	},
	onFinish: function(func){
		if (typeof func == 'function') this.callbacks.push(func);
		if (typeof func == 'array' || typeof func == 'object'){
			for (var i=0; i< func.length; i++){
				this.callbacks.push(func[i]);
			}
		}
	},
	load: function(){
		for (var i=0; i<this.images.length; i++){
			this.loadedImages[i] = new Image();
			this.loadedImages[i].onload = function() { Preloader.checkFinished.apply(Preloader) }
			this.loadedImages[i].src = this.images[i];
		}
	},

	checkFinished: function(){
		this.imagesLoaded ++;
		if (this.imagesLoaded == this.images.length) this.fireFinish();
	},

	fireFinish: function(){
		for (var i=0; i<this.callbacks.length; i++){
			this.callbacks[i]();
		}
		this.images = [];
		this.loadedImages = [];
		this.imagesLoaded = 0;
		this.callbacks = [];
	}
}

function preloadImages()
{
	var uiImgSrc = new Array ("images/play.gif","images/playOn.gif","images/pause.gif","images/pauseOn.gif",
		"images/rw.gif", "images/ff.gif", "images/stepb.gif","images/stepf.gif");
	var uiImg = new Array();
	for (i = 0; i<uiImgSrc.length; i++)
	{
		uiImg[i] = new Image();
		uiImg[i].src = uiImgSrc[i];	
	}
}

function setOptions(chosen){

	var monthbox = document.data_selection.months;
	var daybox = document.data_selection.days;
	
	if (chosen.length == 9)
	{	
		monthbox.options.length = 0;
		daybox.options.length = 0;
	}else{

	if (chosen.length == 4)
	{	var level = "years";	}

	if (level == "years")
	{			
		monthbox.options.length = 0;
		monthbox.options[monthbox.options.length] = new Option ("...","...");
		daybox.options.length = 0;
	}else{	
		daybox.options.length = 0;
		daybox.options[daybox.options.length] = new Option ("...","...");
	}

	if (level == "years")
	{
		for (i in listMonths[chosen]){
		monthbox.options[monthbox.options.length] = new Option(listMonths[chosen][i],listMonths[chosen][i]);
		}
	}else{
		var chosen_year = document.data_selection.years.options[document.data_selection.years.selectedIndex].value;
		var listDaysIndex = chosen_year+"."+chosen;
		for (i in listDays[listDaysIndex]){
			daybox.options[daybox.options.length] = new Option(listDays[listDaysIndex][i],listDays[listDaysIndex][i]);
		}
	}
	}	
}


function animate(){
	i ++;
	if (i >= leftframearray.length){
		i = 0;
	}
//	var currentFrame = new Image();
//	currentFrame.src = framearray[i];
	document["left_anim_images"].src = leftframearray[i];
	if (rightframearray.length == leftframearray.length)
		document["right_anim_images"].src = rightframearray[i];
	
	document.getElementById('frame').innerHTML = i+1;
	if (document["buttonPlay"].src.search("images/play.gif") > 1)
	{
		document["buttonPlay"].src = "images/playOn.gif";
		document.getElementById('fpm').innerHTML = Math.round(fpm);
	}
}

function start(){
	if (preloaderfinished == 1)
	{
	clearInterval(timerval);
	timerval = setInterval(animate,speed);
	document["buttonPlay"].src = "images/playOn.gif";
	document["buttonPause"].src = "images/pause.gif";
	}
}
function stop(){
	if (preloaderfinished == 1)
	{
	clearInterval(timerval);
	document["buttonPlay"].src = "images/play.gif";
	document["buttonPause"].src = "images/pauseOn.gif";
	}
}
function speeddown(){
	if (fpm <= 250)
	{
		fpm -= 25;
		if (fpm < 25)
		{
			fpm = 10;
		}
	}else if (fpm <= 500)
	{
		fpm -= 50;

	}else{
		fpm -= 250;
	}
	speed = 60000 / fpm; 
	document.getElementById('fpm').innerHTML = Math.round(fpm);

	if (preloaderfinished == 1)
	{
	clearInterval(timerval);
	timerval = setInterval(animate,speed);

	document["buttonPlay"].src = "images/playOn.gif";
	document["buttonPause"].src = "images/pause.gif";
	}
}
function speedup(){

	if (fpm < 250)
	{
		fpm += 25;
		if (fpm == 35) fpm = 25;
	}else if (fpm < 500)
	{
		fpm += 50;
	}else{
		if (fpm < 2000)
			fpm += 250;
	}
	speed = 60000 / fpm; 
	document.getElementById('fpm').innerHTML = Math.round(fpm);

	if (preloaderfinished == 1)
	{
	clearInterval(timerval);
	timerval = setInterval(animate,speed);

	document["buttonPlay"].src = "images/playOn.gif";
	document["buttonPause"].src = "images/pause.gif";
	}
}
function stepb(){
	if (preloaderfinished == 1)
	{

	clearInterval(timerval);

	i --;
	if (i < 0){
		i = leftframearray.length -1;
	}
//	var currentFrame = new Image();
//	currentFrame.src = framearray[i];
//	document["anim_images"].src = currentFrame.src;
	document["left_anim_images"].src = leftframearray[i];
	if (rightframearray.length == leftframearray.length)
		document["right_anim_images"].src = rightframearray[i];
	document["buttonPlay"].src = "images/play.gif";
	document["buttonPause"].src = "images/pauseOn.gif";
	document.getElementById('frame').innerHTML = i +1;
	}
}

function stepf(){
	if (preloaderfinished == 1)
	{
	clearInterval(timerval);

	i ++;
	if (i >= leftframearray.length){
		i = 0;
	}
//	var currentFrame = new Image();
//	currentFrame.src = framearray[i];
//	document["anim_images"].src = currentFrame.src;
	document["left_anim_images"].src = leftframearray[i];
	if (rightframearray.length == leftframearray.length)
		document["right_anim_images"].src = rightframearray[i];
	document["buttonPlay"].src = "images/play.gif";
	document["buttonPause"].src = "images/pauseOn.gif";
	document.getElementById('frame').innerHTML = i +1;
	}
}
