var previousImageIndex = -1;
var timerEnabled = true;
var timeout;

$(document).ready( function() {
	if ( jQuery.support.boxModel ) {
		$('#gallery a').lightBox({
			fixedNavigation: true,
			imageBtnPrev: '/images/lightbox/lightbox-btn-prev.gif',
			imageBtnNext: '/images/lightbox/lightbox-btn-next.gif',
			imageBlank: '/images/lightbox/lightbox-blank.gif',
			imageLoading: '/images/lightbox/lightbox-ico-loading.gif',
			imageBtnClose: '/images/lightbox/lightbox-btn-close.gif'
		});

		$('#tabs').tabs({
			fxFade: true,
			fxSpeed: 'fast'
		});
	}
});

// handleOnLoad
// The onLoad Event Handler
function handleOnLoad() {
	changeToPNG();	// for supporting browsers, change foreground to PNG
	randomBackgroundImage();	// randomize the background image
}

// changeToPNG
// This function assumes that all browsers, except older versions of
// Internet Explorer, fully support PNG (i.e. the alpha transparency
// channel), and so it replaces the GIF image with a PNG one.
function changeToPNG() {
	if (document.getElementById) {	// make sure browser understands HTML DOM
		var browser = navigator.appName;
		var version = navigator.appVersion;
		var idForegroundImage = "ovalpicture";
		var objForeground = document.getElementById(idForegroundImage);
		var supportsPNG = false;

		if (objForeground) {	// the object exists
			// browser testing
			if (browser != "Microsoft Internet Explorer") {
				supportsPNG = true;	// well, not really
			} else {	// MSIE, so check the version
				var offset = version.indexOf("MSIE");
				if (offset != -1 && (parseFloat(version.substring(offset+5))) >= 7.0 ) {
					supportsPNG = true;
				}
			}

			if (supportsPNG)
				objForeground.src = "/images/hollowoval.png";
		}
	}
}

// handleOnClickRandom
// This event handler uses a click event to randomly change the
// background image.  It also disables the randomize timer.  The end
// result is that the user puts the randomization on manual control.
function handleOnClickRandom() {
	timerEnabled = false;
	clearTimeout(timeout);

	randomBackgroundImage();
}

// randomBackgroundImage
// This function works either on a timer or under manual control.  It puts
// a random picture as the background image of a transparent image,
// creating a merged layered effect.  The possible background images are
// also provided with offsets so that a feature in the image can be made
// prominent.
function randomBackgroundImage() {
	if (document.getElementById) {	// make sure browser understands HTML DOM
		// the foreground object
		var idForegroundImage = "ovalpicture";
		var objForeground = document.getElementById(idForegroundImage);

		// set up manual control
		objForeground.onclick = handleOnClickRandom;

		// the random images we will use as a background
		var i;	// will be set to a random index within our array
		var randomImage = new Array();
		randomImage[0] = "eveningpeace-sm.jpg,0,-120";
		randomImage[1] = "whitechurch-sm.jpg,0,0";
		randomImage[2] = "countryhome-sm.jpg,0,0";
		randomImage[3] = "stilllife-sm.jpg,0,0";
		randomImage[4] = "timeexpired-sm.jpg,0,0";
		randomImage[5] = "railroadbridge-sm.jpg,0,0";
		randomImage[6] = "londonhall-sm.jpg,0,0";
		randomImage[7] = "fallenrebel-sm.jpg,0,0";
		randomImage[8] = "laurellake-sm.jpg,0,0";
		randomImage[9] = "oldcountry-sm.jpg,0,0";
		randomImage[10] = "mtndelivery-sm.jpg,0,0";
		randomImage[11] = "crossingcumberland-sm.jpg,0,0";

		var isPreloaded = new Array();
		for (i = 0; i < randomImage.length; i++) {
			isPreloaded = false;
		}

		if (previousImageIndex == -1) {	// if first run
			i = Math.floor(Math.random() * randomImage.length);
		} else {						// otherwise
			do {	// loop to make sure we pick a new one
				i = Math.floor(Math.random() * randomImage.length);
			} while (previousImageIndex == i);
		}
		previousImageIndex = i;	// save our new pick for later testing

		// split the array element into its components
		var imagename = randomImage[i].split(",")[0];
		var xpos = randomImage[i].split(",")[1];
		var ypos = randomImage[i].split(",")[2];

		// Preload image because some browsers have weird behavior
		// when a background image hasn't been loaded yet
		if (isPreloaded[i] == false) {
			var objImagePreload;	// temp object to preload the new image
			objImagePreload = new Image();
			objImagePreload.src = "/images/" + imagename;
			isPreloaded[i] = true;
		}

		// change the style
		objForeground.style.backgroundImage = "url(/images/" + imagename + ")";
		objForeground.style.backgroundPosition = xpos + "px " + ypos + "px";

		if (timerEnabled) {	// set a new timeout if still in automatic control
			timeout = setTimeout("randomBackgroundImage()",15000);
		}
	}
}

// set event handler
window.onload = handleOnLoad;
