var slideshows = [];
var slides = [];
var slideshow_count = 0;
var current_slideshow = 0;
var current_slide = [];
var interval = -1;
var initial_timeout = 3500;
var timeout = 3500;
var transition = 3.0;

function init_slideshow() {
	slideshows = $$('.ol_slideshow');
	slideshow_count = slideshows.size();

	for (var i = 0; i < slideshow_count; i++) {
		slides[i] = [];
		slideshows[i].select('img').each(function (item, j) {
			var styles = {
				position : 'absolute',
				top : '0px',
				left : '0px',
				width : '100%',
				height : '100%',
				zIndex : '1'
			}

			item.setStyle(styles);
			slides[i][j] = item;
		});
		current_slide[i] = 0;
	}

	generate_timeout();

	interval = setTimeout('rotate()', initial_timeout);
}

function rotate() {
	var now = slides[current_slideshow][current_slide[current_slideshow]];

	current_slide[current_slideshow] = get_next_slide();

	var next = slides[current_slideshow][current_slide[current_slideshow]];

	//bring the current image to the front
	now.setStyle({'zIndex' : parseInt(now.getStyle('zIndex')) - 1});
	next.setStyle({'zIndex' : parseInt(next.getStyle('zIndex')) + 1});

	new Effect.Fade(now, {duration: transition});
	new Effect.Appear(next, {duration: transition});

	set_next_slideshow();

	interval = setTimeout('rotate()', timeout);
}

function get_next_slide() {
	current_slide[current_slideshow]++;

	if (slides[current_slideshow][current_slide[current_slideshow]] == undefined) {
		return 0;
	}

	return current_slide[current_slideshow];
}

function set_next_slideshow() {
	if (slideshow_count > 1) {
		var temp = current_slideshow;

		while (temp == current_slideshow) {
			current_slideshow = Math.floor(Math.random() * slideshow_count);
		}
	}
}

function generate_timeout() {
	timeout += (Math.random() * 1000);
}

