/*
  current_image and img_queue are set up here in case 
  init_rotation isn't called to set up the lists.
*/
var current_image = 'img_1';
var img_queue = ['img_2', 'img_3'];
var stopped = false;

function swap_images(image1, image2, duration){
	if($(image1) && $(image2)){
		new Effect.Fade($(image1), {duration: duration});
		new Effect.Appear($(image2), 
				{duration: duration});
		current_image = image2;
	}
}

function switch_image() {
	if(!stopped){
		img_queue.push(current_image);
		swap_images(current_image, current_image = img_queue.shift(), 2);
	}
}

function init_rotation(q){
	if(arguments.length > 0){
		current_image = q.shift();
		img_queue = q;
	}
	new PeriodicalExecuter(switch_image, 5);
}

function popup(image){
	stopped = true;
	swap_images(current_image, image, 1);
	current_image = image;
}

function permute(list){
	var new_array = new Array(list.length);
	for(var i=0;i<list.length;i++){
		new_array[i] = i;
	}
	var temp;
	var index;
	for(var i=0;i<list.length;i++){
		temp = new_array[i];
		swap_index = Math.floor(Math.random() * list.length);
		new_array[i] = new_array[swap_index];
		new_array[swap_index] = temp;
	}
	new_list = new Array(list.length);
	for(var i = 0; i < list.length; i++){
		new_list[i] = list[new_array[i]];
	}
	return new_list;
}
function slidePopup(id){
	slideShow.pause();
	new Effect.Fade(slideShow.currentFrame);
	new Effect.Appear(id);
	slideShow.currentFrame = id;
}

