/*====================================================================*\

  Stormchild.net
  Basic Image Viewer 1.0

  | Jason Sims
  | Symmetriq
  | www.symmetriq.net

  Feel free to steal my code and adapt it for your own purposes.

  Keyboard input code adapted from MozPoint (http://mozpoint.mozdev.org/)

  > Version 1.0 (2006-07-14)
  - Initial release
  - Added timing code to keyboard input to prevent double-fired key events.
    Key events must be at least 100ms apart. This fixes a problem in Safari
    (up to and including the latest WebKit nightly build at this time) in
    which the key event fires twice when an arrow key is pressed.

\*====================================================================*/

// preload image controls
pre_exit = new Image(20,20); pre_exit.src = imagePath + 'button_exit_on.gif';

var scrollOffset = 0;

// viewport dimensions
var viewportX, viewportY;
if (self.innerHeight) {
	// all except Explorer
	viewportX = self.innerWidth;
	viewportY = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) {
	// Explorer 6 Strict Mode
	viewportX = document.documentElement.clientWidth;
	viewportY = document.documentElement.clientHeight;
} else if (document.body) {
	// other Explorers
	viewportX = document.body.clientWidth;
	viewportY = document.body.clientHeight;
}

function show_viewport_size() {
	alert('Viewport size: ' + viewportX + ' x ' + viewportY);
}

// Page Objects
var normalContent = document.getElementById('normal_content');
var imagebox = document.getElementById('imagebox');
var imageTitle = document.getElementById('image_title');
var imageDate = document.getElementById('image_date');
var theImage = document.getElementById('the_image');
var imageInfo = document.getElementById('image_info');

function show_image(imgID) {

	if (self.pageYOffset) {
		// all except Explorer
		scrollOffset = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		// Explorer 6 Strict
		scrollOffset = document.documentElement.scrollTop;
	} else if (document.body) {
		// all other Explorers
		scrollOffset = document.body.scrollTop;
	}

	display_image(imgID);

	normalContent.style['display'] = 'none';
	imagebox.style['display'] = 'block';

	if (viewportY < 900) {
		window.scrollTo(0,170);
	} else {
		window.scrollTo(0,0);
	}

}

function display_image(imgID) {

	// Build <img> tag
	imgTag = '<img src="' + imgPath + imageList[imgID][0] + '" width="' + imageList[imgID][2] + '" height="' + imageList[imgID][3] + '"';

	imgTag += ' />';

	// Swap in image and info
	imageTitle.innerHTML = imageList[imgID][1];
	imageDate.innerHTML = imageList[imgID][4];
	theImage.innerHTML = imgTag;
	// additional info (optional)
	imageInfo.innerHTML = imageList[imgID][5] ? imageList[imgID][5] : '&nbsp;';

}

function hide_image() {
	imagebox.style['display'] = 'none';
	normalContent.style['display'] = 'block';
	window.scrollTo(0,scrollOffset);
}

var timeStamp = prevTimeStamp = 0;

// Keyboard input
function keys(key) {
	if (!key) {
		key = event;
		key.which = key.keyCode;
	}

	curTime = new Date();
	timeStamp = curTime.getTime();

	// at least 100ms must elapse between key events to prevent double-firing
	if (timeStamp - prevTimeStamp > 100) {

		switch (key.which) {
			case 27: // esc
			case 88: // x
				hide_image();
				break;
		}

		prevTimeStamp = timeStamp;
	}

}

function keyStartup() {
	document.onkeyup = keys;
}

addLoadEvent(keyStartup);
