// JavaScript Document

var images = [];
var stack = [];
var loadIndex = 3;
var soundLoaded = false;
var slideShowStarted = false;

images[1] = new Image(900,600);
images[1].src = 'images/gallery/gallery.jpg';

images[2] = new Image(900,600);
images[2].src = 'images/gallery/gallery2.jpg';

// preload images into an array
function loadImg() {
	images[loadIndex] = new Image(900,600);
	images[loadIndex].src = 'images/gallery/gallery' + loadIndex + '.jpg';
	window.status = "Loading image " + loadIndex + "...";
	if (images[loadIndex].complete) { // this check is for IE6
		doneLoading();
	} else {
		images[loadIndex].onload = function() { doneLoading(); }
	}
}

function doneLoading() {
	checkStart();
	stack.push(images[loadIndex]);
	if (loadIndex < 80) {
		loadIndex++;
		loadImg();
	} else {
		window.status = "Gallery Loading Complete"
	}
}

function checkStart() {
	if (!slideShowStarted) {
		if (images[1].complete && images[2].complete) {
			startSlideShow();
			slideShowStarted = true;
		}
	}
}

function startSlideShow() {
	// start slideshow 
	$('#slideshow').cycle({
		prev:   '#prevlink',
		next:   '#nextlink',
		speed: 0,
		timeout: 2300,
		before:   onBefore,
		after: function(curr, next, opts) {
			$('#count').html((opts.currSlide + 1) + " / " + opts.slideCount);
		}
	});
	
}

// add images to slideshow 
function onBefore(curr, next, opts) { 
    if (opts.addSlide) // <-- important! 
        while(stack.length) 
            opts.addSlide(stack.shift());  
};


