// Code ripped from the Gallery project at gallery.menalto.com, for more info: mjavisserREMOVETHIS@hotmail.com

//globals
var timer;
var current_location = 1;
var next_location = 1;
var pics_loaded = 0;
var images = new Array;
is_ie=top.window.is_ie;
checkItemID=top.window.checkItemID;
lang=top.window.lang;
welcomeStatus=top.window.welcomeStatus;
is_ie5_5up=top.window.is_ie5_5up;
translate=top.window.frames[0].window.translate;
menuItemStatus=top.window.frames[0].window.menuItemStatus;
var browserCanBlend = (is_ie5_5up); // IE5.5 and up can do the blending transition.

function stopOrStart() {
	if (onoff) {
		stop();
	} else {
		play();
	}
}

function changeElementText(id, newText) {
	var element;
	if(!is_ie) element = eval("document.getElementById('"+id+"')");
	else element = eval(id);
	element.innerHTML = newText;
}

function stop() {
	if(translate) changeElementText("stopOrStartText", "<img src=\"../pic/play.bmp\" alt=\"" + translate('slideshowPlay') + "\" width=\"32\" border=\"0\">");
	onoff = 0;
	if(menuItemStatus) menuItemStatus("slideshowStop");
	clearTimeout(timer);
}

function play() {
	if(translate) changeElementText("stopOrStartText", "<img src=\"../pic/stop.bmp\" alt=\"" + translate('slideshowStop') + "\" width=\"32\" border=\"0\">");
	onoff = 1;
	welcomeStatus(lang);
	go_to_next_photo();
}

function preload_complete() {
}

function reset_timer() {
	clearTimeout(timer);
	if (onoff) {
		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.
		*/
		if(menuItemStatus) menuItemStatus("slideshowWait");
		clearTimeout(timer);
		timer = setTimeout('wait_for_current_photo()', 500);
		return 0;
	} else {
		welcomeStatus(lang);
		preload_next_photo();
		reset_timer();
	}
}

function go_to_photo(index) {
	if(index > photo_count) {
		next_location = 1;
	} else {
		next_location = index;
	}
	go_to_next_photo();
}

function go_to_next_photo() {
	/* Go to the next location */
	current_location = next_location;

	/* 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 */
	next_location = (parseInt(current_location) + parseInt(direction));
	if (next_location > photo_count) {
		next_location = 1;
		if (!loop) {
			stop();
		}
	}
	if (next_location == 0) {
		next_location = photo_count;
		if (!loop) {
			stop();
		}
	}

	/* Preload the next photo */
	preload_photo(next_location);
}

function show_current_photo() {

	/*
	 * If the current photo is not completely loaded don't display it.
	 */
	if (!images[current_location]  || !images[current_location].complete) {
		preload_photo(current_location);
		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();
	}
	document.slide.src = images[current_location].src;
	if(photos[current_location].caption) setCaption(photos[current_location].caption);

	if (browserCanBlend) {
		document.images.slide.filters[0].Play();
	}	
	
	updatePhotoIndex(current_location);
	if(photos[current_location].itemID != "") {
		if(!checkItemID(photos[current_location].itemID)) return false;
		top.window.frames[0].window.menuItemIn(photos[current_location].itemID,colorFlash);
		top.window.frames[0].window.setTimeout("top.window.frames[0].window.menuItemOut(\"" + photos[current_location].itemID + "\",\"" + colorDefault + "\");",timeFlash);
	}

	return 1;
}

function preload_photo(index) {

	/* Load the next picture */
	if (pics_loaded < photo_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 = photos[index].url;
			pics_loaded++;
		}
	}
}

function setCaption(text) {
	if(text==="") text = "<br>";
	changeElementText("caption", "<br><br>" + text);
}

function updatePhotoIndex(toIndex) {
	for (var cell=1; cell <= photo_count; cell++) {
		var currentCell;
		if(!is_ie) currentCell = eval('document.getElementById("cell'+cell+'")');
		else currentCell = eval('cell' + cell);
		var range = Math.round(photo_count/2);
		var startCell = toIndex - range + cell;
		if(startCell < 1) { //we're over the top
			currentCell.innerHTML = '<a href="#" style="text-decoration:none;color:#000000;" onclick="go_to_photo(' + (startCell + photo_count) + ')">' + (startCell + photo_count) + '</a>';
		} else if(startCell > photo_count) {
			currentCell.innerHTML = '<a href="#" style="text-decoration:none;color:#000000;" onclick="go_to_photo(' + (startCell - photo_count) + ')">' + (startCell - photo_count) + '</a>';
		} else {
			currentCell.innerHTML = '<a href="#" style="text-decoration:none;color:#000000;" onclick="go_to_photo(' + startCell + ')">' + (startCell) + '</a>';
		}
	}
}

