var midstop=200; // Width of the "stopping" area in the center of the gallery
var maxspeed=2; // Top scroll speed in pixels. Script auto creates a range from 0 to top speed.
var scrollspeed=0; // Start off NOT scrolling
var movediv="stop"; // Start off NOT moving
var actualwidth=''; // This is a variable because it will change depending on the number of projects
var leftwait, rightwait;

function getOdivOffset(odiv, offsettype) {
	var totalOffset=odiv.offsetLeft; // get initial offset
	var parentEl=odiv.offsetParent;
	while (parentEl!=null) { // recurse through parent elements to find total offset
		totalOffset = totalOffset + parentEl.offsetLeft;
		parentEl = parentEl.offsetParent;
	}
	return totalOffset;
}

function shiftright(){
	movediv="right";
	if (parseInt(document.getElementById("fs_innerdiv").style.left) < 0){
		document.getElementById("fs_innerdiv").style.left = parseInt(document.getElementById("fs_innerdiv").style.left) + scrollspeed + "px";
		document.getElementById("fs_projname").style.left = parseInt(document.getElementById("fs_projname").style.left) + scrollspeed + "px";
	}
	rightwait = setTimeout("shiftright()",10); // keep running shiftright until stopscroll is called
}
function shiftleft(){
	movediv="left";
	if (parseInt(document.getElementById("fs_innerdiv").style.left) > (outerwidth-actualwidth)){
		document.getElementById("fs_innerdiv").style.left = parseInt(document.getElementById("fs_innerdiv").style.left) - scrollspeed + "px";
		document.getElementById("fs_projname").style.left = parseInt(document.getElementById("fs_projname").style.left) - scrollspeed + "px";
	}
	leftwait = setTimeout("shiftleft()",10); // keep running shiftleft until stopscroll is called
}

function startscroll(e){
	var curpos = window.event ? event.clientX : e.clientX ? e.clientX : ""; // get cursor position from event relative to browser window
	curpos = curpos - getOdivOffset(document.getElementById("fs_outerdiv"), "left"); // get cursor position relative to inside the filmstrip
	var leftbound=(outerwidth-midstop)/2; // calculate lefthand scroll area
	var rightbound=(outerwidth+midstop)/2; // calculate righthand scroll area
	if (curpos>rightbound) { // cursor is in the righthand side
		scrollspeed = (curpos-rightbound)/((outerwidth-midstop)/2) * maxspeed; // determine scroll speed depending on how far right cursor is
		clearTimeout(rightwait);
		if (movediv!="left") shiftleft(); // on the right side, we shift the div left
	} else if (curpos<leftbound){ // cursor is in the lefthand side
		scrollspeed=(leftbound-curpos)/((outerwidth-midstop)/2) * maxspeed; // determine scroll speed depending on how far left cursor is
		clearTimeout(leftwait);
		if (movediv!="right") shiftright(); // on the left side, we shift the div right
	} else { // cursor is in the "stopped" area
		scrollspeed=0;
	}
}

function stopscroll(e){
	clearTimeout(leftwait);
	clearTimeout(rightwait);
	movediv="stop";
}

function initfs() {
	outerwidth = document.getElementById("fs_outerdiv").offsetWidth; // get the width of the container div
	actualwidth = document.getElementById("filmstrip_w").offsetWidth; // get the width of the actual filmstrip
	
	// set event listeners
	document.getElementById("fs_outerdiv").onmousemove=function(e){
		startscroll(e);
	}
	document.getElementById("fs_outerdiv").onmouseover=function(e){
		startscroll(e);
	}
	document.getElementById("fs_outerdiv").onmouseout=function(e){
		stopscroll(e);
	}
	/* document.getElementById("ar").onmousemove=function(e){
		startscroll(e);
	}
	document.getElementById("ar").onmouseover=function(e){
		startscroll(e);
	}
	document.getElementById("ar").onmouseout=function(e){
		stopscroll(e);
	} */
}
window.onload=initfs;
