function validEmailAddress(str) {

	var validEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

	if (!validEmail.test(str)) return false;
	else return true;

}

function windowInnerWidth() {

	if (self.innerWidth) return self.innerWidth; 
	else if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth;
	else if (document.body) return document.body.clientWidth;

}

function windowInnerHeight() {

	if (self.innerHeight) return self.innerHeight; 
	else if (document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
	else if (document.body) return document.body.clientHeight;

}








function photo(thePhoto) {

	backdrop();
	
	theImage = new Image();
	theImage.src = thePhoto;
	
	if (!document.getElementById('photo')) document.body.innerHTML += '<div id="photo" onmouseover="closebox()" onmouseout="closebox()"><\/div>';
	document.getElementById('photo').innerHTML += '<div id="close"><a href="javascript:void(0)" onclick="off()" title="close photo"><img src="/images/close.png" /></a><\/div>';
	document.getElementById('photo').innerHTML += '<img src="'+thePhoto+'" onload="photoLoaded()" id="thePhoto" \/>';

}

function photoLoaded() {

	var posLeft = (windowInnerWidth() - document.getElementById('thePhoto').width)/2;
	var posTop = (windowInnerHeight() - document.getElementById('thePhoto').height)/2;

	document.getElementById('photo').style.left = posLeft + 'px';
	document.getElementById('photo').style.top = posTop + 'px';
	document.getElementById('photo').style.visibility = 'visible';

}

function closebox() {

	if (document.getElementById('close').style.visibility == "visible") document.getElementById('close').style.visibility = "hidden";
	else document.getElementById('close').style.visibility = "visible";

}

function backdrop() {

	if (!document.getElementById('backdrop')) document.body.innerHTML += '<div id="backdrop" onclick="off()"><\/div>';
	document.getElementById('backdrop').style.visibility = 'visible';

}

function off() {

	document.getElementById('backdrop').style.visibility = 'hidden';

	document.getElementById('photo').innerHTML = "";
	document.getElementById('photo').style.visibility = 'hidden';

}




var theStart = 0;

function slideshow(start) {

	// SET INITIAL PHOTO

	if (!isNaN(start)) theStart = start;

	// PRELOAD THE PHOTOS
	
	for (x in thePhotos) {
	
		thePreloads = new Image();
		thePreloads.src = thePhotos[x];
		
	}
	
	// INITIATE SHOW

	slideshowOn();
	slideshowControls();
	
	// SHOW INITIAL PHOTO

	document.getElementById('slideshowCanvas').style.backgroundImage = 'url('+thePhotos[parseInt(theStart)]+')';
	
}

function slideshowOn() {

	// SHOW BACKDROP

	if (!document.getElementById('slideshowBackdrop')) document.body.innerHTML += '<div id="slideshowBackdrop"></div>';
	document.getElementById('slideshowBackdrop').style.visibility = 'visible';
		
	// SHOW PHOTO CANVAS
	
	if (!document.getElementById('slideshowCanvas')) document.body.innerHTML += '<div id="slideshowCanvas"></div>';
	document.getElementById('slideshowCanvas').style.visibility = 'visible';
	
	// SHOW CONTROLS
	
	if (!document.getElementById('slideshowControls')) document.body.innerHTML += '<div id="slideshowControls"><img src="/images/arrow-previous.png" id="slideshowPrevious" title="previous" onclick="slideshowMove(-1)" width="40" height="40"> <img src="/images/close.png" title="close slideshow" onclick="slideshowOff()" width="40" height="40"> <img src="/images/arrow-next.png" id="slideshowNext" title="next photo" onclick="slideshowMove(1)" width="40" height="40"></div>';
	document.getElementById('slideshowControls').style.visibility = 'visible';

}

function slideshowMove(theDirection) {

	theStart = parseInt(theStart) + parseInt(theDirection);
		
	if (theStart < 0) {
	
		theStart = 0;
		return false;
	
	} else if (theStart >= thePhotos.length) {
	
		theStart = thePhotos.length - 1;
		return false;
	
	} else {
	
		slideshowControls();
		document.getElementById('slideshowCanvas').style.backgroundImage = 'url('+thePhotos[parseInt(theStart)]+')';

	}

}

function slideshowControls() {

	if (theStart==0) document.getElementById('slideshowPrevious').src = '/images/arrow-previous-end.png';
	else document.getElementById('slideshowPrevious').src = '/images/arrow-previous.png';

	if (theStart==thePhotos.length - 1) document.getElementById('slideshowNext').src = '/images/arrow-next-end.png';
	else document.getElementById('slideshowNext').src = '/images/arrow-next.png';

}

function slideshowOff() {

	document.getElementById('slideshowBackdrop').style.visibility = 'hidden';
	document.getElementById('slideshowCanvas').style.visibility = 'hidden';
	document.getElementById('slideshowControls').style.visibility = 'hidden';

}
