// Slideshow/image viewer JavaScript - 29/9/09

var image_array = new Array()
var image_names = new Array()
var image_index = 0
var next_image_index = 1
var full_image
var interval_id
var slideshow_running = false

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
}

function changeOpac(opacity, id) {
	//change the opacity for different browsers
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
}

function start_slideshow() {
	interval_id = setInterval("next_slide()", 5000 )
	slideshow_running = true
	next_slide()
}

function stop_slideshow() {
	clearInterval(interval_id)
	slideshow_running = false
}

function next_slide() {

	var fullPath
	var lastSlash
	
    // Increment the array index
    image_index++
    
    // Are we past the last item?
    if (image_index == image_array.length) {
    
        // If so, set it to 0 to start at the beginning
        image_index = 0
    }
    
    // Display the new image
    changeOpac(0,"image_holder")
    if (image_array[image_index].src == "") {
    	// If image is not preloaded - could happen if linking directly to image - then load it now
    	image_array[image_index].src = image_names[image_index]
    }
    document.images["image_holder"].src = image_array[image_index].src
    fullPath = image_array[image_index].src
    lastSlash = fullPath.lastIndexOf("/")
    document.getElementById("imageRefID").innerHTML = fullPath.substring(lastSlash+1, fullPath.length-4)
    
    // Load the next image
	next_image_index=image_index+1
	if (next_image_index == image_names.length) {
		next_image_index = 0
	}
	if (image_array[next_image_index].src == "") {
		// Preload the image if it's not already in the image_array
		image_array[next_image_index].src = image_names[next_image_index]
	}
    
}

function next_image() {
	document.images["start_stop"].src = "../graphics/start.gif";
	stop_slideshow()
	var fullPath
	var lastSlash
    // Increment the array index
    image_index++
    
    // Are we past the last item?
    if (image_index == image_array.length) {
    
        // If so, set it to 0 to start at the beginning
        image_index = 0
    }
    
    // Display the new image
    if (image_array[image_index].src == "") {
    	// If image is not preloaded - could happen if linking directly to image - then load it now
    	image_array[image_index].src = image_names[image_index]
    }
    document.images["image_holder"].src = image_array[image_index].src
    fullPath = image_array[image_index].src
    lastSlash = fullPath.lastIndexOf("/")
    document.getElementById("imageRefID").innerHTML = fullPath.substring(lastSlash+1, fullPath.length-4)
    
    // Load the next image
	next_image_index=image_index+1
	if (next_image_index == image_names.length) {
		next_image_index = 0
	}
	if (image_array[next_image_index].src == "") {
		// Preload the image if it's not already in the image_array
		image_array[next_image_index].src = image_names[next_image_index]
	}
}

function previous_image() {
	document.images["start_stop"].src = "../graphics/start.gif";
	stop_slideshow()
	var fullPath
	var lastSlash
    // Decrement the array index
    image_index--
    
    // Is the index negative?
    if (image_index < 0) {
    
        // If so, set it to the last element in the array
        image_index = image_array.length - 1
    }
    // Display the new image
    if (image_array[image_index].src == "") {
    	// If image is not preloaded - could happen if linking directly to image - then load it now
    	image_array[image_index].src = image_names[image_index]
    }
    document.images["image_holder"].src = image_array[image_index].src
    fullPath = image_array[image_index].src
    lastSlash = fullPath.lastIndexOf("/")
    document.getElementById("imageRefID").innerHTML = fullPath.substring(lastSlash+1, fullPath.length-4)
    
	// Load previous image ready for navigation
	next_image_index=image_index-1
	if (next_image_index<0) {
		next_image_index = image_names.length-1
	}
	if (image_array[next_image_index].src == "") {
		// Preload the image if it's not already in the image_array
		image_array[next_image_index].src = image_names[next_image_index]
	}
}

function show_picref() {
	document.getElementById("imageRefID").innerHTML = (full_image.substring(7, full_image.length -4));
	GetImageIndex()
}

function toggle_button() {
	if (document.images["start_stop"].src.indexOf("start.gif") > 0) {
		document.images["start_stop"].src = "../graphics/stop.gif";
		start_slideshow()
	}
	else {
		document.images["start_stop"].src = "../graphics/start.gif";
		stop_slideshow()
	}
}

function fade_up(id, millisec) {
	// fade image from 0% to 100% visible
	var speed = Math.round(millisec / 100);
	var timer = 0;
	for(i = 0; i <= 100; i++) { 
        setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
        timer++; 
    }
}

function GetImageIndex() {
	// Get index of image chosen from thumbnails page - all navigation starts from this index
	
	for (var n = 0; n < image_names.length; n++) {
		if (image_names[n] == full_image) {
			image_index = n
		}
	}
}

function ImageLoadEvent() {
	if (slideshow_running) {
		fade_up("image_holder",500);
	}
}

// Example of what needs to follow:

// image_array[0] = new Image()
// image_names[0] = 'images/D300_1873.jpg'
// image_array[1] = new Image()
// image_names[1] = 'images/D300_4740.jpg'
// etc ...

image_array[0] = new Image()
image_names[0] = 'images/D40_0227.jpg'
image_array[1] = new Image()
image_names[1] = 'images/D40_0254.jpg'
image_array[2] = new Image()
image_names[2] = 'images/D40_0265.jpg'
image_array[3] = new Image()
image_names[3] = 'images/D40_0275.jpg'
image_array[4] = new Image()
image_names[4] = 'images/D40_0277.jpg'
image_array[5] = new Image()
image_names[5] = 'images/D40_0297.jpg'
image_array[6] = new Image()
image_names[6] = 'images/D40_0310.jpg'
image_array[7] = new Image()
image_names[7] = 'images/D40_0311.jpg'
image_array[8] = new Image()
image_names[8] = 'images/DSC6662.jpg'
image_array[9] = new Image()
image_names[9] = 'images/DSC6690.jpg'
image_array[10] = new Image()
image_names[10] = 'images/DSC6693.jpg'
image_array[11] = new Image()
image_names[11] = 'images/DSC6716.jpg'
image_array[12] = new Image()
image_names[12] = 'images/DSC6728.jpg'

// END


