
var isZoomed = false;
var tripwire = true;
var isLoading = false;
var loadedPhotos = [true];
var loadedZooms = [];
var panPosY = -250;
var imgHeight;

function showPhotoBox () {
	if (!isLoading) {
		$('zoomcontainer').className = " offLeft";
		$('photo').className = " backOn";
		$('caption').style.width = imgWidth + 'px';
		if (imgHeight < 500) {
			//$('title').innerHTML = imgWidth;
			$('caption').style.marginTop = (((imgHeight - 500) / 2) + 2) + 'px';
		}
		$('zoomstatus').style.display = 'none';
		$('dimensions').style.display = 'inline';
		$('zoomphotobox').style.backgroundImage='none';
		isZoomed = false;
	}
}

function showCaption () {
	$('caption').style.marginTop = '2px';
	imgWidth = $('realPhoto').width;
	imgHeight = $('realPhoto').height;
	if (imgWidth < 301) {
		$('caption').style.width = '300px';
	}else{
		$('caption').style.width = imgWidth + "px";
	}
	if (imgHeight < 500) {
		$('caption').style.marginTop = (((imgHeight - 500) / 2) + 2) + 'px';
	}
	if (theCaptions[curPhoto - 1][0] !== undefined) {
		$('title').innerHTML = theCaptions[curPhoto - 1][0];
		$('dimensions').innerHTML = theCaptions[curPhoto - 1][1];
	}else{
		$('title').innerHTML = "";
		$('dimensions').innerHTML = "";
	}
}

function showPhoto () {
	$('realPhoto').display = 'none';
	$('realPhoto').display = 'block';
	$('realPhoto').className = '';
	$('loading').style.display = 'none';
	if (isZoomed) {
		showPhotoBox();
	}
	showCaption();
	loadedPhotos[curPhoto] = true;
}

function hideLoading () {
	zoomWidth = parseInt(window['zoomVar'+curPhoto].width,10);
	$('zoomphotobox').style.width = zoomWidth + "px";
	panPosX = Math.round((((zoomWidth - 643) / 2) * -1));
	$('zoomphotobox').style.top = panPosY + "px";
	$('zoomphotobox').style.left = panPosX + "px";
	$('zoomedphoto').src = '';
	$('zoomedphoto').style.display = 'none';
	$('zoomloading').style.display = 'none';
	$('zoomphotobox').style.backgroundImage='url(images/zoomed/zoomed'+curPhoto+'.jpg)';
	$('zoomphotobox').style.backgroundRepeat='no-repeat';
	loadedZooms[curPhoto] = true;
	isZoomed = true;
	isLoading = false;
}

function showZoomBox () {
	isLoading = true;
	$('photo').className = " offLeft";
	$('zoomedphoto').style.display = 'block';
	$('zoomedphoto').src = $('realPhoto').src;
	$('zoomedphoto').width = imgWidth * 2;
	$('zoomedphoto').height = 1000;
	$('zoomedphoto').style.left = ((((imgWidth * 2) - 643) / 2) * -1) + "px";
	$('zoomedphoto').style.top = "-250px";
	$('caption').style.width = "643px";
	$('caption').style.marginTop = "2px";
	$('dimensions').style.display = 'none';
	$('zoomloading').style.display = 'block';
	$('zoomstatus').style.display = 'inline';
	$('zoomcontainer').className = " backOn";
	if (window['zoomVar'+curPhoto] == undefined) {
		window['zoomVar'+curPhoto] = new Image();
		window['zoomVar'+curPhoto].src = 'images/zoomed/zoomed'+curPhoto+'.jpg';
		zoomTrip = false;
		window['zoomVar'+curPhoto].onload = hideLoading;
	}
	
	if (loadedZooms[curPhoto]) {
		hideLoading();
	}
}

function initZoom () {
	if (!isZoomed) {
		showZoomBox();
	}
}


function moveKnob () {
	newScroll = Math.round((windowWidth - knobWidth) * (newX / (scrollWidth - windowWidth))) * -1;
	$('knob').style.left = newScroll + 'px';
}


function scrollerJump () {
	if (scrollWidth > windowWidth) {
		// move scrollbar + thumbnail array
		newX = moves[curPhoto]; // newX is how far to move thumbnails, gotten from 'moves' Array
		if (newX < the_end) {newX = the_end;}
		pb.style.left = newX + 'px';
		moveKnob();
	}
}

function addToLoadedArray() {
	loadedPhotos[theNextPhoto] = true;
	//$('title').innerHTML = loadedPhotos;
}


function preloadPhoto () {
	if (loadedPhotos[theNextPhoto] !== true) {
		window['img'+theNextPhoto] = new Image();
		window['img'+theNextPhoto].src = imagePath + theNextPhoto + '.jpg';
		window['img'+theNextPhoto].onload = addToLoadedArray;
	}
}

function renderCurPhoto () {
	if (isZoomed) {
		$('zoomphotobox').style.background = 'none';
	}
	hires = imagePath + curPhoto + '.jpg';
	// Safari 2.04 image swap distortion bug
	if (isSafari2) {
		$('realPhoto').style.display = 'none';
		$('realPhoto').src = hires;
		$('realPhoto').style.display = 'block';
	}else{
		$('realPhoto').src = hires;
	}
	//$('title').innerHTML = loadedPhotos;
	if (loadedPhotos[curPhoto] === true) {
		showCaption();
		showPhotoBox();	
	}
	if (loadedPhotos[curPhoto] !== true) {
		$('loading').style.display = 'block';
		window['img'+curPhoto] = new Image();
		window['img'+curPhoto].src = hires;
		$('realPhoto').onload = showPhoto;
	}
}

function nextPhoto () {
	if (!isLoading) {
		if (curPhoto < totalPhotos) {
			curPhoto++;
			renderCurPhoto ();
			if (curPhoto < totalPhotos) {
				theNextPhoto = curPhoto + 1;
				preloadPhoto();
			}
			if (safariJunk) {
				safariJunk = false;
			} else {
				scrollerJump ();
			}
		}
		tripwire = false;
	}
}

function prevPhoto () {
	if (!isLoading) {
		if (curPhoto > 1) {
			curPhoto--;
			renderCurPhoto ();
			if (curPhoto > 2) {
				theNextPhoto = curPhoto - 1;
				preloadPhoto ();
			}
			scrollerJump ();
		}
	}
}

function jumpToPhoto (newPhoto) {
	if (!isLoading) {
		if (newPhoto == 1 && isZoomed) {showPhotoBox();}
		if (newPhoto == 1 && tripwire === true) {return false;}
		tripwire = false;
		if (newPhoto == curPhoto && isZoomed) {
			curPhoto = newPhoto;
			showPhotoBox();
		}else{
			curPhoto = newPhoto;
			renderCurPhoto ();
		}
	}
}

