var timer;
var location_current = 0;
var location_next = 1;
var image_loaded = 0;
var onoff = 0;
var fullsized = 0;
var direction = 1;
var time_slide = 3;
var timeout_value;
var images = new Array;
var image_gallery = new Array;

var transitions = new Array;
var current_transition = 0;
var loop = 1;


transitions[0] = "progid:DXImageTransform.Microsoft.Fade(duration=1)";
transitions[1] = "progid:DXImageTransform.Microsoft.Blinds(Duration=1,bands=20)";
transitions[2] = "progid:DXImageTransform.Microsoft.Checkerboard(Duration=1,squaresX=20,squaresY=20)";
transitions[3] = "progid:DXImageTransform.Microsoft.Strips(Duration=1,motion=rightdown)";
transitions[4] = "progid:DXImageTransform.Microsoft.Barn(Duration=1,orientation=vertical)";
transitions[5] = "progid:DXImageTransform.Microsoft.GradientWipe(duration=1)";
transitions[6] = "progid:DXImageTransform.Microsoft.Iris(Duration=1,motion=out)";
transitions[7] = "progid:DXImageTransform.Microsoft.Wheel(Duration=1,spokes=12)";
transitions[8] = "progid:DXImageTransform.Microsoft.Pixelate(maxSquare=10,duration=1)";
transitions[9] = "progid:DXImageTransform.Microsoft.RadialWipe(Duration=1,wipeStyle=clock)";
transitions[10] = "progid:DXImageTransform.Microsoft.RandomBars(Duration=1,orientation=vertical)";
transitions[11] = "progid:DXImageTransform.Microsoft.Slide(Duration=1,slideStyle=push)";
transitions[12] = "progid:DXImageTransform.Microsoft.RandomDissolve(Duration=1,orientation=vertical)";
transitions[13] = "progid:DXImageTransform.Microsoft.Spiral(Duration=1,gridSizeX=40,gridSizeY=40)";
transitions[14] = "progid:DXImageTransform.Microsoft.Stretch(Duration=1,stretchStyle=push)";
transitions[15] = "special case";
var transition_count = 15;

// - IE5.5 and up can do the blending transition.
var browserCanBlend = (is_ie5_5up);

function stopOrStart() {
	if (onoff) {
		StopSlice();
	} else {
		direction = 1; // Set forword direction
		preload_next_photo();
		PlaySlice();
	}
}

function changeElementText(id, newText) {
	element = document.getElementById(id);
	element.innerHTML = newText;
}

function StopSlice() {
	changeElementText("stopOrStartText", 'Auto Show - ');

	onoff = 0;
	status = "The slide show is stopped, Click  Auto Show.. to play slide show.";
	clearTimeout(timer);

}

function PlaySlice() {
	changeElementText("stopOrStartText", 'Stop - ');
	onoff = 1;
	status = "Slide show is running...";
	go_to_next_photo();
}

function moveDirection(mode) {
	clearTimeout(timer);
	StopSlice();

	if (mode == 'first') {
		direction = -1;
		location_current=2;

	} else if (mode == 'prev') {
		direction = -1;
		//location_current = parseInt(location_current) + parseInt(direction);

	} else if (mode == 'next') {
		direction = 1;
		//location_current = parseInt(location_current) + parseInt(direction);

	} else if (mode == 'last') {
		direction = 1;
		location_current = images_count-1;
	}

	preload_next_photo();
	go_to_next_photo();
}


function change_transition() {
	//current_transition = document.TopForm.transitionType.selectedIndex;
}

function preload_complete() {
}

function reset_timer() {
	clearTimeout(timer);
	if (onoff) {
		timeout_value = parseInt(time_slide) * 1000;
		timer = setTimeout('go_to_next_photo()', timeout_value);
	}
}

function wait_for_current_photo() {

/* Show the current photo */
	if (!show_current_photo()) {

		/*
		** The current photo isn't loaded yet.  Set a short timer just to wait
		** until the current photo is loaded.
		*/
		status = "Picture is loading...(" + location_current + " of " + images_count + "). " + " Please Wait..." ;
		clearTimeout(timer);
		timer = setTimeout('wait_for_current_photo()', 500);
		return 0;
	} else {
		status = "Slide show is running..." ;
		preload_next_photo();
		reset_timer();
	}
}

function go_to_next_photo() {
	//alert(location_current+','+location_next)
	/* Go to the next location */
	location_current = location_next;

	/* Show the current photo */
	if (!show_current_photo()) {
		wait_for_current_photo();
		return 0;
	}
	
	preload_next_photo();
	reset_timer();
}

function preload_next_photo() {
	/* Calculate the new next location */
	location_next = (parseInt(location_current) + parseInt(direction));
	if (location_next > images_count) {
		location_next = 1;
		if (!loop) {
			StopSlice();
		}
	}
	if (location_next == 0) {
		location_next = images_count;
		if (!loop) {
			StopSlice();
		}
	}

	/* Preload the next photo */
	preload_image(location_next);
}

function show_current_photo() {
	/*
	 * If the current photo is not completely loaded don't display it.
	 */
	if (!images[location_current] || !images[location_current].complete) {
		preload_image(location_current);
		return 0;
	}

	/* transistion effects */
	if (browserCanBlend){
		var do_transition;
		if (current_transition == (transition_count)) {
			do_transition = Math.floor(Math.random() * transition_count);
		} else {
			do_transition = current_transition;
		}
		document.images.slide.style.filter=transitions[do_transition];
		document.images.slide.filters[0].Apply();
	}
	//alert(location_current)
	document.slide.src = images[location_current].src;
	setCaption();

	if (browserCanBlend) {
		document.images.slide.filters[0].Play();
	}

	return 1;
}

function preload_image(index) {
	/* Load the next picture */
	if (image_loaded < images_count) {

		/* not all the pics are loaded.  Is the next one loaded? */
		if (!images[index]) {
			images[index] = new Image;
			images[index].onLoad = preload_complete();
			images[index].src = image_gallery[index];
			image_loaded++;
		}
	}
}

function setCaption() {
	changeElementText("caption", location_current + " Of " + images_count);
}