var old_slide = "empty";
var slides;

var slide_1_width = 1;
var slide_1_height = 1;

var slide_2_width = null;
var slide_2_height = null;

var slide_1;
var slide_2;

var s_1_x = null;
var s_2_x = null;
var s_1_y = null;
var s_2_y = null;

var v_1_x = null;
var v_2_x = null;
var v_1_y = null;
var v_2_y = null;

var i_1_x = null;
var i_2_x = null;
var i_1_y = null;
var i_2_y = null;

//var img = new Array();
var img_id = 0;
var i_1_i;
var i_2_i;

var positions_x = new Array('0', '0.5','1.0','1.0','1.0','0.5','0','0');
var positions_y = new Array('0', '0','0','0.5','1.0','1.0','1.0','0.5');

var slide_timeout = 5000;
var place_slide_timeout = 2000;
var slide_timer = 0;
var active_slide;
var slide_changing = 0;
var slide_cnt = 0;
var slide_cnt_2 = -1;
/* this is the interval, lower==faster */
var speed = 30;
var speed_min = 40;
var speed_max = 40;
var speed_1;
var speed_2;

function select_slide_id()
{
	slide_cnt_2++;
	return slide_cnt_2 % img.length;
}

//function start_slide_show(input_slides)
function start_slide_show()
{
	slide_1 = document.getElementById('slide_1');
	slide_2 = document.getElementById('slide_2');

	speed_1 = speed_min-speed_max+1;
	speed_2 = speed_max-1;

	/* Set first slide, and start slide'ing */
	img_id = select_slide_id();
	slide_1.src = img[img_id].src;
	place_slide("slide_1");
	active_slide = "slide_1";

	/* Set the second (currently invisible) slide */
	img_id = select_slide_id();
	slide_2.src = img[img_id].src;
	slide_2.style.opacity = '.00';
	slide_2.style.filter = "alpha(Opacity=0)";
}

function change_slide() {
	slide_changing = 1;
	if (slide_timer) {
		clearTimeout(slide_timer);
		slide_timer = 0;
	}

	if (! (slide_cnt % 2)) {
		current = 'slide_1';
		next = 'slide_2';
	} else {
		current = 'slide_2';
		next = 'slide_1';
	}
	active_slide = next;
	place_slide(next);
	slide_cnt++;
	Effect.Fade(current, { duration: 0.6, from: 1.0, to: 0.0 });
	Effect.Appear(next,  { duration: 1.2, from: 0.0, to: 1.0, afterFinish: function callback(obj) {pic_change(next, current)} });
}

function place_slide(the_slide) {
	id = img_id;
	if (the_slide == 'slide_1') {
		slide_1_width = img[id].width;
		slide_1_height = img[id].height;

		var delta_width = slide_1_width - 308;
		var delta_height = slide_1_height - 466;
	} else {
		slide_2_width = img[id].width;
		slide_2_height = img[id].height;

		var delta_width = slide_2_width - 308;
		var delta_height = slide_2_height - 466;
	}

	var my_rand;
	if (delta_width > 10 && delta_height > 10) {
		/* Randomly select a distance to travel */
		var my_start = Math.random();
		my_start = (my_start * 7);
		my_start = Math.round(my_start);
		var my_stop = Math.random();
		if (my_start & 1) {
		/* Odd */
			my_stop = Math.round((my_stop * 5) - 3);
			my_stop = (my_start + 4 + my_stop) % 8;
		} else {
		/* Even */
			my_stop = Math.round((my_stop * 3) - 2);
			my_stop = (my_start + 4 + my_stop) % 8;
		}
		start_pos_x = Math.round(positions_x[my_start] * delta_width);
		start_pos_y = Math.round(positions_y[my_start] * delta_height);

		stop_pos_x = Math.round(positions_x[my_stop] * delta_width);
		stop_pos_y = Math.round(positions_y[my_stop] * delta_height);

		dist_x = start_pos_x - stop_pos_x;
		dist_y = start_pos_y - stop_pos_y;

		if (dist_x < 0) {
			tmp_x = -1;
			dist_x = 0 - dist_x;
		} else
			tmp_x = 1;

		if (dist_y < 0) {
			tmp_y = -1;
			dist_y = 0 - dist_y;
		} else
			tmp_y = 1;

		if (dist_x == 0) {
			v_y = speed;
			v_x = 400000;
		} else if (dist_y == 0) {
			v_x = speed;
			v_y = 400000;
		} else  {
			var alfa = Math.atan(dist_x/dist_y);
			v_x = Math.cos(alfa);
			v_y = Math.sin(alfa);
			v_x = Math.round(speed / v_x);
			v_y = Math.round(speed / v_y);
		}
	} else {
		start_pos_x = 0;
		start_pos_y = 0;
		tmp_x = -1;
		tmp_y = -1;
		v_x = (speed * 2);
		v_y = (speed * 2);
		if (slide_cnt || (delta_width < 60 || delta_height < 60)) {
			if (slide_timer) {
				clearTimeout(slide_timer);
				slide_timer = 0;
			}
			slide_timer = setTimeout('change_slide()', slide_timeout);
		}
	}

	if (the_slide == 'slide_1') {
		slide_1.style.left = (0 - start_pos_x) + "px";
		slide_1.style.top = (0 - start_pos_y) + "px";

		v_1_x = tmp_x;
		v_1_y = tmp_y;
		s_1_x = v_x;
		s_1_y = v_y;

		i_1_x = setInterval('move_slide_1_x()', s_1_x);
		i_1_y = setInterval('move_slide_1_y()', s_1_y);
	} else {
		slide_2.style.left = (0 - start_pos_x) + "px";
		slide_2.style.top = (0 - start_pos_y) + "px";

		v_2_x = tmp_x;
		v_2_y = tmp_y;
		s_2_x = v_x;
		s_2_y = v_y;

		i_2_x = setInterval('move_slide_2_x()', s_2_x);
		i_2_y = setInterval('move_slide_2_y()', s_2_y);
	}

	if (1 == 2) {
		$('js_err').innerHTML = 'Slide: '+the_slide;
		$('js_err').innerHTML += '<br>ID: '+id;
		$('js_err').innerHTML += '<br>dist_x:'+delta_width+'<br>dist_y:'+delta_height;
		$('js_err').innerHTML += '<br>Start_pos: '+my_start+'<br>Stop_pos: '+my_stop;

		$('js_err').innerHTML += '<br>Speed_x: '+v_x;
		$('js_err').innerHTML += '<br>Speed_y: '+v_y;

		if (slide_timer)
			$('js_err').innerHTML += '<br>Timer: On';
		else
			$('js_err').innerHTML += '<br>Timer: Off';
	}
}

function pic_change(next, previous) {
	/* Change the invisible picture */
	img_id = select_slide_id();
	if (previous == "slide_1") {
		clearInterval(i_1_x);
		clearInterval(i_1_y);
		slide_1.src = img[img_id].src;
	} else {
		clearInterval(i_2_x);
		clearInterval(i_2_y);
		slide_2.src = img[img_id].src;
	}
	slide_changing = 0;
}

function move_slide_1_x() {
	var tmp;
	if (slide_1.width > 308) {
		var x = parseInt(slide_1.style.left);

		tmp = x + (25 * v_1_x);
		if ((tmp > 0 || tmp < (308 - slide_1.width)) && !slide_changing && active_slide == "slide_1") {
			change_slide();
		}

		tmp = x + v_1_x;
		if (tmp > 0 || tmp < (308 - slide_1.width)) {
			clearInterval(i_1_x);
			clearInterval(i_1_y);
		} else
			slide_1.style.left = tmp + "px";
	}
}

function move_slide_1_y() {
	var tmp;

	if (slide_1.height > 466) {
		var x = parseInt(slide_1.style.top);

		tmp = x + (25 * v_1_y);
		if ((tmp > 0 || tmp < (466 - slide_1.height)) && !slide_changing && active_slide == "slide_1") {
			change_slide();
		}

		tmp = x + v_1_y;
		if (tmp > 0 || tmp < (466 - slide_1.height)) {
			clearInterval(i_1_y);
			clearInterval(i_1_x);
		} else
			slide_1.style.top = tmp + "px";
	}
}

function move_slide_2_x() {
	var tmp;

	if (slide_2.width > 308) {
		var x = parseInt(slide_2.style.left);

		tmp = x + (25 * v_2_x);
		if ((tmp > 0 || tmp < (308 - slide_2.width)) && !slide_changing && active_slide == "slide_2") {
			change_slide();
		}

		tmp = x + v_2_x;
		if (tmp > 0 || tmp < (308 - slide_2.width)) {
			clearInterval(i_2_x);
			clearInterval(i_2_y);
		} else
			slide_2.style.left = tmp + "px";
	}
}

function move_slide_2_y() {
	var tmp;

	if (slide_2.height > 466) {
		var x = parseInt(slide_2.style.top);

		tmp = x + (25 * v_2_y);
		if ((tmp > 0 || tmp < (466 - slide_2.height)) && !slide_changing && active_slide == "slide_2") {
			change_slide();
		}

		tmp = x + v_2_y;
		if (tmp > 0 || tmp < (466 - slide_2.height)) {
			clearInterval(i_2_y);
			clearInterval(i_2_x);
		} else
			slide_2.style.top = tmp + "px";
	}
}

