// JavaScript code for image slideshow with one row of thumbnails in sidebar below or beside main
// image div (actual format and image size depends on css file and matching html code block used)
// Please note that most of this code is completely original and is copyright (c) Ian Graham and Lane Realty
// The slide transition code is derived from http://brainerror.net/scripts/javascript/blendtrans/
// We obviously cannot prevent you using this code and no warranties are given or implied. However,
// we would appreciate an acknowledgment on any copy or derivative of this code.
// Enquiries to grahamis(AT)onthenet.com.au
var current_image = -1;
var n_visible = 6;
var n_current_start = 0;
var playing = 0;
var save = 0; 
var my_timer = 0;
var image_preload = [];
var fade_time = 1000;
// Fade-out, fade-in code derived from http://brainerror.net/scripts/javascript/blendtrans/ & elsewhere
//	change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.images[id].style;
	object.filter="progid:DXImageTransform.Microsoft.Alpha(Opacity=" + opacity + ')'; //IE 8
    object.filter = "alpha(opacity=" + opacity + ")"; // IE lt 8
    object.opacity = (opacity / 100);  // Standard: FF gt 1.5, Opera, Safari
    object.MozOpacity = (opacity / 100);  // FF lt 1.5, Netscape
    object.KhtmlOpacity = (opacity / 100);  // Safari 1.x 
}
function blendimage(imageid, bgndid, imagefile, millisec) { 
    var speed = Math.round(millisec / 100); 
    var timer = 0;      
    //set the current image as background
	document.getElementById(bgndid).style.backgroundImage = "url(" + document.images[imageid].src + ")"; 
    //make image transparent 
    changeOpac(0, imageid);      
    //make new image 
    document.images[imageid].src = imagefile; 
    //fade in image 
    for(i = 0; i <= 100; i++) { 
        setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed)); 
        timer++; 
    } 
}
//
function slideUp() { moveLeft(); }
function moveLeft() {
		turnOffHighlight();
		for (i = 0; i < n_visible; i++ ) {
			document.images['tn'+i].src = images[(n_current_start+i+1) % n_images];
		}
		n_current_start = (n_current_start + 1) % n_images;
		turnOnHighlight();
		return true;
}
function slideDn() { moveRight(); }
function moveRight() {
		turnOffHighlight();
		for (i = 0; i < n_visible; i++ ) {
			var j = n_current_start+i-1;
			if (j < 0 ) { j += n_images; }
			j = j % n_images;
			document.images['tn'+i].src = images[j];
		}
		if (--n_current_start < 0 ) { n_current_start += n_images; }
		turnOnHighlight();
		return true;
}
function first() {
	save = playing;
	if (playing) { playing = 0; }
	setMainImage(0);
	if (save) restartPlay();
	playing = save;
}
function last() {
	save = playing;
	if (playing) { playing = 0; }
	setMainImage(n_images-1);
	if (save) restartPlay();
	playing = save;
}	
function previous() {
	save = playing;
	if (playing) {
		playing = 0;
		save = -1;
	}
	if (current_image <= 0) {
		setMainImage(n_images-1);
	} else {
		setMainImage(current_image-1);
	}
	if (save) restartPlay();
	playing = save;
}	
function next() {
	save = playing;
	if (playing) {
		playing = 0;
		save = 1;
	}
	setMainImage((current_image+1) % n_images);
	if (save) restartPlay();
	playing = save;
}
function play() {
	if (playing) {
		clearTimeout(my_timer);
		document.images["play_btn"].src = "images/arrow_play_grn.gif";
		document.images["play_btn"].title = "Play slideshow";
		document.images["prev_btn"].title = "Go to previous image";
		document.images["next_btn"].title = "Go to next image";
		playing = 0;
	} else {
		document.images["play_btn"].src = "images/arrow_pause_grn.gif";
		document.images["play_btn"].title = "Pause slideshow";
		document.images["prev_btn"].title = "Play sideshow in reverse";
		document.images["next_btn"].title = "Play sideshow forwards";
		playing = 1;
		restartPlay();
	}
}
function restartPlay() {
	clearTimeout(my_timer);
	my_timer = setTimeout('continuePlay()', my_speed);
}
function continuePlay() {
	clearTimeout(my_timer);
	if (playing) {
    	if (playing > 0) {
			next();
		} else {
			previous();
		}
	}
}
function turnOffHighlight() {
	if (current_image >= 0 && inRange(current_image)) {
		document.images['tn'+((current_image-n_current_start+n_images) % n_images)].style.borderColor = "#006600";
	}
}
function turnOnHighlight() {
	if (current_image >= 0 && inRange(current_image)) {
		document.images['tn'+((current_image-n_current_start+n_images) % n_images)].style.borderColor = "#660000";
	}
}
function inRange(image_n) {
	for (i = 0; i < n_visible; ++i) {
		if (image_n == (n_current_start + i) % n_images) { return true; }
	}
	return false;
}	
function setMainImageToTn(tn_n) {
	setMainImage((n_current_start + tn_n) % n_images);
	if (playing) {
		restartPlay();
	}
}
function setMainImage(image_n) {
	turnOffHighlight();
	while (!inRange(image_n)) {
		if (save > 0) { moveLeft(); }
		else { moveRight(); }
		turnOffHighlight();
	}
//	document.images['img_main'].src = images[image_n];
	blendimage('img_main', 'img_main_container', images[image_n], fade_time); 
	document.images['img_main'].title = titles[image_n];
	document.getElementById("main_title").innerHTML = "<p class='img_title_line'>"+titles[image_n]+"</p>";
	current_image = image_n;
	turnOnHighlight();
}	
function initialiseSS() {
	if (n_images <= n_visible) {
		if (document.images['arrow_up']) {
			document.images['arrow_up'].style.display = "none";
			document.images['arrow_dn'].style.display = "none";
		} else if (document.images['scrolll_btn']) {
			document.images['scrolll_btn'].style.display = "none";
			document.images['scrollr_btn'].style.display = "none";
		}
	}
	if (n_images < n_visible) {
		for (i = n_images; i < n_visible; i++ ) {
			document.images['tn'+i].style.display = "none";
		}
		n_visible = n_images;
	}
	for (i = 0; i < n_visible; i++ ) {
		document.images['tn'+i].src = images[i];
		document.images['tn'+i].title = titles[i];
	}
	if (n_images >= n_visible) {
		for (i = n_visible; i < n_images; i++ ) {
			var pic = new Image();
			pic.src = images[i];
			image_preload.push(pic);
		}
	}
	setMainImage(0);
	if (autoplay) { play(); }
}
// copyright (c) Ian Graham and Lane Realty P/L, 2009