var scrollPhotoTime = new Date();
var numPhotoScrolls = 0;
var isIE = true;

if (document.addEventListener) {
	isIE = false;
	document.addEventListener("keydown", keyCapt, false); //code for Mozilla 
} else {
	document.attachEvent("onkeydown",keyCapt); //code for IE 
}

function checkNextPhotoTime() { 
	// prevent users from holding down a key to continuously scroll through the photos,
	//		which can cause server to have problems from too many requests
	var rtn = true;
	var now = new Date();
	numPhotoScrolls++;
	// prevent user from scrolling through more than 10 photos in under 2 seconds
	if (numPhotoScrolls > 10) {
		if (now.getTime() - scrollPhotoTime.getTime() < 2000) {
			alert('Slow down.  Enjoy the scenery.');
			rtn = false;
		}
		numPhotoScrolls = 0;
		scrollPhotoTime = new Date();
	}
	return rtn;
}

function keyCapt(e) { 
	//alert('key = ' + e.keyCode);

	var nav = top.frames.nav;
	if (top != null && nav != null) {
		if (nav != null 
				&& top != null 
				&& top.pages != null 
				&& top.pages[top.currentPageIndex] != null) {
			
			// space, enter, arrow right, tab, n
			if (e.keyCode == 13 || e.keyCode == 32 || e.keyCode == 39 || e.keyCode == 9 || e.keyCode == 78) { 
				// next photo
				if (top.currentPageIndex != top.pages.length - 1 
						|| top.currentPhotoIndex != top.pages[top.currentPageIndex].photos.length - 1) {
					// not the last photo on the last page
					if (checkNextPhotoTime()) {
						top.moveToThumbnailIndex(top.currentPhotoIndex + 1); 
					}
				} 
			} else if (e.keyCode == 8 || e.keyCode == 37 || e.keyCode == 80) { // backspace, arrow left, p
				// prev photo
				if (top.currentPhotoIndex != 0 || top.currentPageIndex != 0) {
					// not first photo on first page
					if (checkNextPhotoTime()) {
						top.moveToThumbnailIndex(top.currentPhotoIndex - 1); 
					}
				} 				
			} else if (e.keyCode == 33 || e.keyCode == 38) { // page up, arrow up
				// prev page
				if (top.currentPageIndex != 0) {
					top.currentPhotoIndex = 0;
					top.displayThumbnails(top.currentPageIndex - 1);
					top.openFolder(top.currentPageIndex - 1);
				}
				
			} else if (e.keyCode == 34 || e.keyCode == 40) { // page down, arrow down
				// next page
				if (top.currentPageIndex < top.pages.length - 1) {
					top.currentPhotoIndex = 0;
					top.displayThumbnails(top.currentPageIndex + 1);
					top.openFolder(top.currentPageIndex + 1);
				}
				
			} else if (e.keyCode == 36) { // home
				// first photo on page
				top.moveToThumbnailIndex(0);			
				
			} else if (e.keyCode == 35) { // end
				// last photo on page
				top.moveToThumbnailIndex(top.pages[top.currentPageIndex].photos.length - 1);			
				
			} else if (e.keyCode == 77) { // m
				// show map
				if (nav.document.getElementById("mapLink").style.visibility == 'visible') {
					nav.showMap();	
				}		
			}
		}
	}
	if (!isIE) {
		e.preventDefault();
		e.stopPropagation();
	}
	return false;
} 
	


