
var timer;
var current_location = 0;
var next_location = 0;
var previous_location = 0;
var pics_loaded = 0;
var onoff = 0;
var direction = 1;
var timeout_value = 5000;
var images = new Array;
var photo_urls = new Array;
var photo_captions = new Array;
var loop = 0;
var photo_count = Pictures.length;
var CurrentImageTag = 'image1';
var NextImageTag = 'image2';
var NavigationContainerTag = 'SlideshowNavigation';
var Current;
var Next;
var ResizeImageContainer = false;
var ChangeCaption = false;
var NavigationDisplayed = true;
var ChangeNavigation = false;
var navigationCode;
var PreviousTag = '';
var ChangeLink = false;
var ChangeAlts = false;


function play() {

        onoff = 1;
        go_to_next_photo();
}

function preload_complete() {
}

function reset_timer() {
        clearTimeout(timer);
        if (onoff) {
                timer = setTimeout('go_to_next_photo()', timeout_value);
        }
}

function pause(){
        if (onoff == 1){
                onoff = 0;
                clearTimeout(timer);
                document.getElementById('PlayPause').innerHTML = 'Play';
        }
        else{
                onoff = 1;
                document.getElementById('PlayPause').innerHTML = 'Pause';
                go_to_next_photo();
        }
}

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 go_to_previous(){
        direction = -1;
        if (!images[current_location].complete) {
                // The Slideshow has progessed to the next photo, but
                // it's not done loading yet.  So, subtract 1 from the 
                // current_location so the slideshow will actually go
                // back 2 photos.  It's a bit of a kloodge, but it works
                current_location = current_location - 1;
        }
        preload_next_photo();
        direction = 1;
        go_to_next_photo();

}

function go_to_next(){
        go_to_next_photo();
}

function preload_next_photo() {
        
        /* Calculate the new next location */
        next_location = (parseInt(current_location) + parseInt(direction));
        previous_location = current_location;
        photo_count = Pictures.length;
        if (next_location >= photo_count) {
                next_location = 0;
        }
        if (next_location < 0) {
                next_location = photo_count - 1;
        }
        
        /* 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) {
                if (ChangeNavigation && NavigationDisplayed && NavigationContainerTag != ''){
                        navigationCode = document.getElementById(NavigationContainerTag).innerHTML;
                        document.getElementById(NavigationContainerTag).innerHTML = 'Loading picture...' + PreviousTag;
                        PreviousTag = "&nbsp;&nbsp;(<a href='javascript:go_to_previous();'>Go To Previous Picture</a>)";
                        NavigationDisplayed = false;
                }
                        
                preload_photo(current_location);
                return 0;
        }
        
        if (ChangeNavigation && !NavigationDisplayed && NavigationContainerTag != '') {
                document.getElementById(NavigationContainerTag).innerHTML = navigationCode;
                NavigationDisplayed = true;
        }

        Current = document.getElementById(CurrentImageTag);
        Next = document.getElementById(NextImageTag);
        Next.src = images[current_location].src;
        Next.xOpacity = 0;
        if (ChangeAlts){
                Next.alt = Alts[current_location];
        }
        
        delete images[previous_location];
        
        so_xfade();
        
        tmpTag = NextImageTag;
        NextImageTag = CurrentImageTag;
        CurrentImageTag = tmpTag;
        
        if (ResizeImageContainer && ImageContainerTag != ''){
                document.getElementById(ImageContainerTag).style.width=Widths[current_location];
                document.getElementById(ImageContainerTag).style.height=Heights[current_location];
        }
        if (ChangeCaption && CaptionContainerTag != ''){
                document.getElementById(CaptionContainerTag).innerHTML=Captions[current_location];
        }
        if (ChangeLink && LinkTag != ''){
                document.getElementById(LinkTag).href=Links[current_location];
                document.getElementById(LinkTag).title=Alts[current_location];
        }
        return 1;
}

function preload_photo(index) {

        if (!images[index]) {
                images[index] = new Image;
                images[index].onLoad = preload_complete();
                images[index].src = BaseURL + Pictures[index];
        }
}

function preload_photo_old(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 = BaseURL + Pictures[index];
                        pics_loaded++;
                }
        }
}

function speed_up(){
        if (timeout_value >= 1000){
                timeout_value = timeout_value - 500;
                reset_timer();
        }
}

function slow_down(){
        timeout_value = timeout_value + 500;
}

function so_xfade() {
	cOpacity = Current.xOpacity;
	nOpacity = Next.xOpacity;
	
	cOpacity-=.05; 
	nOpacity+=.05;
	
	Next.style.display = "block";
	Current.xOpacity = cOpacity;
	Next.xOpacity = nOpacity;
	
	setOpacity(Current); 
	setOpacity(Next);
	
	if(cOpacity<=0) {
		Current.style.display = "none";
                Next.xOpacity = 0.99;
	        setOpacity(Next);
                return; 
	} else {
		setTimeout("so_xfade()",30);
	}
	
	function setOpacity(obj) {
		if(obj.xOpacity>.99) {
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
	
}

