var timeout = 3500;
var play = true;
var current = 0;
var myfadeout, chg = null;
var wait = true;
var count = current;

function changeimg() {	
	$('fgimg').setProperties({
		src: imgs[current],
		alt: alts[current]
	});
}

function changebgimg() {
	$('bgimg').setProperties({
		src: imgs[current],
		alt: alts[current]
	});
	$('bgimg').fade('in');
}

function newpicture(next) {
	
	if(myfadeout != null) {
		myfadeout.cancel();
	}
	
	temp = next;
	if($('diashow')){
		window.setTimeout('changebgimg()', 300);

		myfadeout = new Fx.Tween($('fgimg'), {
			property: 'opacity',
			duration: 1200, 
			transition: Fx.Transitions.Quad.easeInOut
		});
		
		myfadeout.start(1,0);
		if(temp == next) {
			if(chg != null) {
				window.clearTimeout(chg);
				changeimg();
			}
			
			chg = window.setTimeout('changeimg()', 3000);
			
			myfadeout.start(0,1);
		}
	}
}

function nextimg() {
	if(play) {
		window.clearInterval(show);
		play = false;
		$('pausebutton').addClass('noshow');
	}
	
	next = current+1;
	if(next>= imgs.length) {
		next = 0;
	}

	changeimg();
	
	newpicture(next);
	current++;
	
	if(current >= imgs.length) {
		current = 0;
	}
}

function gotoimg(count) {
	if(play) {
		window.clearInterval(show);
		play = false;
		$('pausebutton').addClass('noshow');
	}
	
	newpicture(count);
	current = count;
}

function startshow() {
	next = current+1;
	if(next>= imgs.length) {
		next = 0;
	}

	changeimg();
	
	newpicture(next);
	current++;
	
	if(current >= imgs.length) {
		current = 0;
	}
}

function previousimg() {
	if(play) {
		window.clearInterval(show);
		play = false;
		$('pausebutton').addClass('noshow');
	}
		
	before = current-1;
	if(before < 0){
		before = imgs.length-1;
	}
	
	newpicture(before);
	current--;
	
	if(current < 0){
		current = imgs.length-1;
	}
}

function stopshow(){
	if(play){
		window.clearInterval(show);
		play = false;
		$('pausebutton').addClass('noshow');
	} else {
		play = true;
		show = window.setInterval("startshow()", timeout);
		$('pausebutton').removeClass('noshow');
	}
}

window.addEvent('domready', function(){
	if($('diashow')){
		$('pausebutton').removeClass('noshow');
		show = window.setInterval("startshow()", timeout);
	}
});
