/* Slideshow for the gallery */

var timer; 
var pics_loaded = 0;
var isPlaying = 0;
var direction = 1;
var timeout_value;
var images = new Array;
var picture_prefix = "";
var picture_ext = ".jpg";

var loop = 1;

var photo_captions = new Array;
var photo_url = new Array;
var photo_name = new Array;
var photo_count = 0;
var current_location = 1;
var next_location = 1;

var transitionNames = new Array;
var transitions = new Array;
var current_transition = 0;
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;

// Minor browser capabilities detection to check for browser support of transitions
var agt=navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie5_5up = false;
if (is_ie && (agt.indexOf("msie 5.0")==-1) && (agt.indexOf("msie 4")==-1) && (parseInt(navigator.appVersion)>=4))
{
	is_ie5_5up = true;
}
// IE5.5 and up can do the blending transition.
var browserCanBlend = (is_ie5_5up);

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

function toggleLoop(el) {
	if (el.checked) {
	loop = 1;
    } else {
	loop = 0;
    }
}

function toggleDirection(el) {
	if (el.checked) {
	direction = -1; // Reverse
    } else {
	direction = 1;
    }

    preload_next_photo();	
}

function changeElementText(id, newText) {
    element = document.getElementById(id);
    element.innerHTML = newText;
}

function stop() {
	document.getElementById("stopBtn").style.display = "none";
	document.getElementById("playBtn").style.display = "";	

    isPlaying = 0;
    clearTimeout(timer);
}

function play() {
	document.getElementById("playBtn").style.display = "none";
	document.getElementById("stopBtn").style.display = "";

    isPlaying = 1;
    // status = "Slide show is running...";
    go_to_next_photo();
}

function changeDirection() {
    if (direction == 1) {
	direction = -1;
	changeElementText("changeDirText", "forward direction");
    } else {
	direction = 1;
	changeElementText("changeDirText", "reverse direction");
    }
    preload_next_photo();

}

function change_transition() {
    current_transition = document.TopForm.transitionType.selectedIndex;
}

function preload_complete() {
}

function reset_timer() {
    clearTimeout(timer);
    if (isPlaying) {
	timeout_value = document.TopForm.time.options[document.TopForm.time.selectedIndex].value * 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.
	 */
		clearTimeout(timer);
		timer = setTimeout('wait_for_current_photo()', 500);
		return 0;
    } else {
		preload_next_photo();
		reset_timer();
    }
}

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;
    }

	/*
	 * Important for the current stylesheet
	 */    
	if (document.getElementById("content"))
	{
		if (images[current_location].height > images[current_location].width) {
			// This is a vertical image
			document.getElementById("content").className = "v";
		} else {
			document.getElementById("content").className = "";
		}
	}
	
    /* transistion effects */
    if (browserCanBlend){
	var do_transition;
	if (current_transition == (transition_count)) {
		// Last item means random... pick one 
	    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;
    setCaption(photo_captions[current_location],photo_name[current_location]);

    if (browserCanBlend) {
	document.images.slide.filters[0].Play();
    }

    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 = photo_url[index];
	    pics_loaded++;
	}
    } 
}

function zeroPad(num) {
	if (num < 10)
		return "0"+num;
	else
		return num;
}

function setCaption(text,picturename) {
    changeElementText("counter", "Picture " + current_location + " of " + photo_count);
	if (picturename != "")
		changeElementText("picturename", picturename);
	else	
		changeElementText("picturename", "Picture " + current_location);
		
	if (text != "")
		changeElementText("caption", text);
	else
		changeElementText("caption","<em>No description</em>");
}

