﻿var image_location = "slideshow/"; //leave blank if same location as page;
var images_array = [];
var captions = [];
var image_timeout = 5000; // in miliseconds
var image_index = 0; //do not alter
var image_visible = 0; //do not alter



function start_slideshow(img_arr,location,timeout,initial){
    image_location = location;
    image_timeout = timeout;
      

    //set-up images and captions
    
    if(img_arr.length) //images with no captions
        images_array = img_arr;
    else
        for(k in img_arr){
            images_array.push(k);
            captions.push(img_arr[k]);
        }
    
    //pre-load images
    var temp_image_array = [];
    for(var i=0; i<images_array.length; i++){
        var img = new Image();
        img.src = image_location + images_array[i];	
        temp_image_array[i] = img;
    }
    
    
    //start slideshow
	jQuery(document).ready(function () { 
	    if(captions.length){
	        document.getElementById('slideshow_captions').style.display = "block";
	        document.getElementById('slideshow_captions').innerHTML = captions[0];
	    }
        document.getElementById('slideshow').getElementsByTagName("img")[image_visible].src = image_location + images_array[image_index];	
        setTimeout("scroll_slideshow()",initial);	    
    });
}


function scroll_slideshow(){
	var img_hide = document.getElementById('slideshow').getElementsByTagName("img")[(image_visible)];
	image_visible = image_visible == 0 ? 1 : 0;
	var img_show = document.getElementById('slideshow').getElementsByTagName("img")[(image_visible)];
	
	image_index = image_index + 1 < images_array.length ? image_index + 1 : 0;
	
	img_hide.style.zIndex = 1;
	img_show.style.zIndex = 0;
	
	img_show.src = image_location + images_array[image_index];
	img_show.style.display = "block";
	
	if(captions.length)
	    document.getElementById('slideshow_captions').innerHTML = captions[image_index];
	    
	//$(img_show).fadeIn("slow");   //for a slightly better effect --> may cause lagging on slower computers
	$(img_hide).fadeOut("slow",function(){setTimeout('scroll_slideshow()',image_timeout)});
}

