/* 	Sidescrolling Functions for Kate Schutt site
	Barking Dog Studios
	David Lachapelle
	Last Modified: February 20th, 2007
*/

/*	Global Variables
*/
var x = 0;
var nX = 0;
var nXo = 0;
var timer = null;
var ps = window;

var inertia = 0.3;
var k = 0.1;
var xp = 0;
var xo = 0;

/*	Scroll Window function
	Sets up the scroller's globals based on current position and final position
	then proceeds to call scrollWindowForward() and moveNavigation()
*/
function scrollWindow(scrollTo) {
	if(isLoading) return;
	var regex = /px/;
	scrolling = true;
	for(i = 0; i < panels.length; i++) {
		if(panels[i][0] == scrollTo) {
			nX = panels[i][1];
			if(parseInt(document.getElementById('overlay').style.left.replace(regex, "")) > panels[i][2])
				nXo = panels[i][2]-8;
			else
				nXo = panels[i][2];
		}
	}

	x = getScrollX();
	clearTimeout(timer);
	scrollWindowForward();
	moveNavigation(scrollTo);
}

/*	Scroll Window Forward
	Called by scrollWindow to actually move the window
	When complete, un-sets the globals
*/
function scrollWindowForward() {
	if(getScrollX() == nX) {
		clearTimeout(timer);
		timer = null;
		nx = 0;
		xp = 0;
		nXo = 0;
		xo = 0;
		scrolling = false;
		return;
	}
	if(document.all && x < ps.document.body.scrollWidth || x < ps.document.width) {
		//calculate how far to scroll the window
		xp = xp * inertia + (nX-getScrollX())*k;
		x = x + xp;
		//do the scrolling
		window.scrollTo(x,0);
		//call moveOverlay to move the navigation overlay
		moveOverlay();
		timer =  setTimeout('scrollWindowForward()',1);
	}
	return;
}

/*	Get current X position
	Determines the current position of the top left corner of the browser for use in the
	scrolling calculations
*/
function getScrollX() {
	var scrOfX = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfX = document.documentElement.scrollLeft;
	}
	return scrOfX;
}

/*	Moves the overlay on the navigation
*/
function moveOverlay() {
	var temp = 0;
	var regex = /px/;
	if(document.getElementById('overlay').style.left)
		var curpos = document.getElementById('overlay').style.left.replace(regex, "");
	else
		var curpos = 0;

	//if the overlay isn't in position, then keep moving it
	if(parseInt(curpos) != nXo) {
		xo = xo * inertia + (nXo - curpos)*0.1;
		temp = parseInt(curpos) + xo;
	
		document.getElementById('overlay').style.position = 'absolute';
		document.getElementById('overlay').style.left = temp+"px";
	}
}

/* Move navigation function
	Highlights the appropriate navigation element by modifying its style
*/
function moveNavigation(scrollTo) {
	for(i = 0; i < panels.length; i++) {
		document.getElementById(panels[i][0]).className = panels[i][0];
	}
	document.getElementById(scrollTo).className = document.getElementById(scrollTo).className+"_selected";
}

/* 	Moves the overlay on the navigation
	This function is called by the main window scrolling callback function to re-position the overlay
	when the user scrolls the window manually
*/
function moveNavOverlay(scrollTo) {
	for(i = 0; i < panels.length; i++) {
		if(panels[i][0] == scrollTo) {
			document.getElementById('overlay').style.position = 'absolute';
			if(panels[i][2] == 0)
				document.getElementById('overlay').style.left = panels[i][2]+"px";
			else
				document.getElementById('overlay').style.left = panels[i][2]-8+"px";
		}
	}
}

