// The maximum speed we want the window to scroll
var maxSpeed = 50;

// The position we want the window to be in
var targetPos = null;

// The current link position we are on
var currentLink = 1;

// A handle to a currently open popup window
var popup = null;


// Gets the value of a style attributes (internal or computed via a stylesheet)
function getStyleValue(node, styleAttribute){
	if (!node) return; // check the node is valid
	var value = node.style[styleAttribute];
	if(!value)
		if (document.defaultView) // mozilla
			value = document.defaultView.getComputedStyle(node, "").getPropertyValue(styleAttribute);
		else if (node.currentStyle) // IE
			value = node.currentStyle[styleAttribute];
	return value;
}


// Jump to the position of a target element
function jump( target ) {

	// if the jump target exists...
	if( document.getElementById( "film" + target ) ) {
	
		// get the parent DIV's left style
		//targetPos = parseInt( document.getElementById( "film" + target ).style.left );
		targetPos = parseInt( getStyleValue( document.getElementById( "film" + target ) , "left" ) );
		
		// if the target is right of where we are then call the helper to go right, otherwise go left
		if( targetPos > document.getElementById("scroller").scrollLeft ) {
			jumpRightHelper();
		} else {
			jumpLeftHelper();
		}
	
		//change the style of the links
		document.getElementById( "link"+currentLink ).blur();
		document.getElementById( "link"+currentLink ).className = "LinkOff";
		currentLink = target;
		document.getElementById( "link"+currentLink ).className = "LinkOn";
	}
}


// iterative helper to move left
function jumpRightHelper() {

	// decide how far to scroll this iteration (for inertia)
	scrollDistance = ( targetPos - document.getElementById("scroller").scrollLeft ) / 2;
	
	// but not faster than our maximim speed...	
	if( scrollDistance > maxSpeed ) { scrollDistance == maxSpeed }
	
	// if there is more than 2px to go and the last one didn't take too long, do it, otherwise just put us there and stop
	if( scrollDistance > 2 ) {
	
		// make a note of the last position
		lastPos = document.getElementById("scroller").scrollLeft;

		// try to move us
		document.getElementById("scroller").scrollLeft = document.getElementById("scroller").scrollLeft + scrollDistance;

		// go again
		setTimeout( "jumpRightHelper()" , 20 );
		
	} else {
		document.getElementById("scroller").scrollLeft = targetPos;
	}

}


// iterative helper to move right
function jumpLeftHelper() {
	scrollDistance = ( document.getElementById("scroller").scrollLeft - targetPos ) / 2;
	if( scrollDistance > maxSpeed ) { scrollDistance == maxSpeed }
	if( scrollDistance > 2 ) {
		lastPos = document.getElementById("scroller").scrollLeft;
		document.getElementById("scroller").scrollLeft = document.getElementById("scroller").scrollLeft - scrollDistance;
		setTimeout( "jumpLeftHelper()" , 20 );
	} else {
		document.getElementById("scroller").scrollLeft = targetPos;
	}
}


// Capture key events and react to them
//document.documentElement.onkeypress = function (event) {
document.onkeydown = function (event) {

	// variable to track if the keystroke is handled
	var validKey = true;
	
	// handle IE/Moz.	IE uses .keyCode, Moz uses .which
	event = event || window.event;
	var key = event.which || event.keyCode;

	// handle keypresses (right, left, 1-9)
	switch( key ) {
		case 39 : jump( currentLink + 1 ); break;
		case 63235 : jump( currentLink + 1 ); break;
		case 37 : jump( currentLink - 1 ); break;
		case 63234 : jump( currentLink - 1 ); break;
		case 49 : jump( 1 ); break;
		case 50 : jump( 2 ); break;
		case 51 : jump( 3 ); break;
		case 52 : jump( 4 ); break;
		case 53 : jump( 5 ); break;
		case 54 : jump( 6 ); break;
		case 55 : jump( 7 ); break;
		case 56 : jump( 8 ); break;
		case 57 : jump( 9 ); break;
		default : validKey = false;
	}

	// handle event bubbling if it was a handled keystroke
	if( validKey ) {
		if( event.preventDefault ) { event.preventDefault(); } else { event.returnValue = false; }
	}
}



// Functions for popup windows for zoomed photos etc

function openWeb( file ) {
	if( popup ) { popup.close(); }
	popup = window.open( file , "photo" , "width=800,height=500,MENUBAR=no,scrollbars=no" ); popup.focus();
}

function openFilm( file ) {
	if( popup ) { popup.close(); }
	popup = window.open( file , "photo" , "width=800,height=500,MENUBAR=no,scrollbars=no" ); popup.focus();
}

function openPhoto( file ) {
	if( popup ) { popup.close(); }
	popup = window.open( file , "photo" , "width=800,height=500,MENUBAR=no,scrollbars=no" ); popup.focus();
}

function openPhotoTall( file ) {
	if( popup ) { popup.close(); }
	popup = window.open( file , "phototall" , "width=450,height=600,MENUBAR=no,scrollbars=no" ); popup.focus();
}

function closeWindow() {
	window.close();
}


// getStyleById: given an element ID and style property
// return the current setting for that property, or null.
// args:
//  i - element id
//  p - property
function getStyleById(i, p) {
	var n = document.getElementById(i);
	var s = eval("n.style." + p);

	// try inline
	if((s != "") && (s != null)) {
		return s;
	}

	// try currentStyle
	if(n.currentStyle) {
		var s = eval("n.currentStyle." + p);
		if((s != "") && (s != null)) {
			return s;
		}
	}

	// try styleSheets
	var sheets = document.styleSheets;
	if(sheets.length > 0) {
		// loop over each sheet
		for(var x = 0; x < sheets.length; x++) {
			// grab stylesheet rules
			var rules = sheets[x].cssRules;
			if(rules.length > 0) {
				// check each rule
				for(var y = 0; y < rules.length; y++) {
					var z = rules[y].style;
					// selectorText broken in NS 6/Mozilla: see
					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
					ugly_selectorText_workaround();
					if(allStyleRules) {
						if(allStyleRules[y] == i) {
							return z[p];
						}
					} else {
						// use the native selectorText and style stuff
						if(((z[p] != "") && (z[p] != null)) ||
						   (rules[y].selectorText == i)) {
							return z[p];
						}
					}
				}
			}
		}
	}
	return null;
}
