
nudgeID = 0;
nudgeMaxSpeed = 10;
fadeSpeed = 2;
opacityInUse = 40;
opacityActive = 30;
opacityInactive = 0;


function fade(sID,sDir)
	{
	// repeating fade function needs the object ID,
	// the control direction we're changing,
	// the target opacity

	var currentOpacity;
	var targetOpacity;
	var fadeIntervalID;
	var obj;

	if (sDir == -1)
		{
		currentOpacity = window[sID + 'NudgeLeftOpacity'];
		targetOpacity = window[sID + 'NudgeLeftOpacityTarget'];
		fadeIntervalID = window[sID + 'FadeLID'];
		obj = document.getElementById(sID + "ScrollL");
		}
	else
		{
		currentOpacity = window[sID + 'NudgeRightOpacity'];
		targetOpacity = window[sID + 'NudgeRightOpacityTarget'];
		fadeIntervalID = window[sID + 'FadeRID'];
		obj = document.getElementById(sID + "ScrollR");
		}

	if (currentOpacity < targetOpacity)
		{
		currentOpacity+=fadeSpeed;
		}
	else if (currentOpacity > targetOpacity)
		{
		currentOpacity-=fadeSpeed;
		}

	if (sDir == -1)
		{
		window[sID + 'NudgeLeftOpacity'] = currentOpacity;
		}
	else
		{
		window[sID + 'NudgeRightOpacity'] = currentOpacity;
		}

	obj.style.opacity = formOpacity(0,currentOpacity);

	if (currentOpacity == 0)
		{
		obj.style.display = "none";
		}
	else
		{
		obj.style.display = "block";
		}
	}
function formOpacity(x,opac)
	{
	if(x==0)
		{
		return(opac/100);
		}
	}

function setupScrolling(sID)
	{
	if(!sID || window[sID + 'Operating'])
		{
			return;
		}
	// So first we get all the array indexes and values set up for this object.
	// This must be run first in JS on the page or in the head
	window[sID + 'NudgeAmount'] = 0;
	window[sID + 'NudgeSpeed'] = 0;
	window[sID + 'NudgeToggle'] = true;
	window[sID + 'NudgeRightOpacity'] = 0;
	window[sID + 'NudgeLeftOpacity'] = opacityInactive;
	window[sID + 'NudgeRightOpacityTarget'] = opacityActive;
	window[sID + 'NudgeLeftOpacityTarget'] = 0;
	window[sID + 'MouseOnLeft'] = false;
	window[sID + 'MouseOnRight'] = false;
	window[sID + 'NudgeID'] = 0;
	window[sID + 'FadeLID'] = 0;
	window[sID + 'FadeRID'] = 0;
	window[sID + 'Operating'] = 1;

	document.getElementById(sID).style.overflow = "hidden";
	document.getElementById(sID + "ScrollL").style.display = "block";
	document.getElementById(sID + "ScrollR").style.display = "block";

	setNudgeControlVisibility(sID);

	// The javascript checking opacity for the controls is running all the time
	// this is currently the best way I can think of to ensure multiple setintervals
	// don't run at the same time.  All we now need to do is set how visible we
	// want the controls to be in our functions and don't worry about doing anything else
	window[sID + 'FadeLID'] = setInterval("fade('"+sID+"',-1);",30);
	window[sID + 'FadeRID'] = setInterval("fade('"+sID+"',1);",30);

	window.onresize = function()
		{
		setNudgeControlVisibility(sID);
		}
	}

function setNudgeControlVisibility(sID)
	{
	// This is run every update to the scroll position
	// It checks to find out if there is a variance between
	// any control opacity and its target opacity and
	// if a setinterval is currently running.
	// If an interval is not running and there is a variance,
	// kick it off.

	// If not at far left, we want the left control to the activeOpacity.
	// If it is not far left and the mouse is over the control, we want opacityInUse
	// If it IS at far left, opacity should be 0.
	// If it is not at far right, we want the right control to the activeOpacity.
	// If it is not far right and the mouse is over the control, we want opacityInUse
	// Otherwise, it should be invisible with opacity 0.

	var maxScrollAmount = findMaxWidth(sID);

	if(document.getElementById(sID).scrollLeft > 1)
		{
		if(window[sID + 'MouseOnLeft'])
			{
			setTargetFade(sID, -1, opacityInUse);
			}
		else
			{
			setTargetFade(sID, -1, opacityActive);
			}
		}
	else
		{
		setTargetFade(sID, -1, opacityInactive);
		}

	if(document.getElementById(sID).scrollLeft < maxScrollAmount)
		{
		if(window[sID + 'MouseOnRight'])
			{
			setTargetFade(sID, 1, opacityInUse);
			}
		else
			{
			setTargetFade(sID, 1, opacityActive);
			}
		}
	else
		{
		setTargetFade(sID, 1, opacityInactive);
		}
	}

function setTargetFade(sID, sDir, targetOpacity)
	{
	// Sets the target fade level in the global variable array
	// Restarts the setInterval process

	if(sDir == -1)
		{
		window[sID + 'NudgeLeftOpacityTarget'] = targetOpacity;
		}
	else
		{
		window[sID + 'NudgeRightOpacityTarget'] = targetOpacity;
		}
	}

function nudgeRight(sID)
	{
	startingScroll(sID);
	window[sID + 'NudgeID'] = setInterval("nudge('" + sID + "',1)",25);
	window[sID + 'MouseOnRight'] = true;
	}
function nudgeLeft(sID)
	{
	startingScroll(sID);
	window[sID + 'NudgeID'] = setInterval("nudge('" + sID + "',-1)",25);
	window[sID + 'MouseOnLeft'] = true;
	}
function stopNudging(sID)
	{
	window[sID + 'NudgeToggle'] = false;
	window[sID + 'MouseOnRight'] = false;
	window[sID + 'MouseOnLeft'] = false;
	}

function startingScroll(sID)
	{
	window[sID + 'NudgeToggle'] = true;
	clearInterval(window[sID + 'NudgeID']);
	window[sID + 'NudgeAmount'] = document.getElementById(sID).scrollLeft;
	}

function findMaxWidth(sID)
	{
	var borders = 0;
	if(document.getElementById(sID).style.borderLeftWidth)
		{
		borders += parseInt(document.getElementById(sID).style.borderLeftWidth);
		}

	if(document.getElementById(sID).style.borderRightWidth)
		{
		borders += parseInt(document.getElementById(sID).style.borderRightWidth);
		}

	contentWidth = document.getElementById(sID).scrollWidth;
	scrollWindowWidth = document.getElementById(sID).offsetWidth;

	return parseInt(contentWidth - scrollWindowWidth + borders);
	}

function nudge(sID,iDir)
	{
	// Start using the nudgeSpeed for this ID.
	// If it's less than the max, increase the rate until it is 10 or more
	var maxScrollAmount = findMaxWidth(sID);
	var nudgeAmount = window[sID + 'NudgeAmount'];
	var nudgeSpeed = window[sID + 'NudgeSpeed'];
	var nudgeToggle = window[sID + 'NudgeToggle'];

	if(nudgeToggle)
		{
		if(nudgeSpeed < nudgeMaxSpeed)
			{
			nudgeSpeed++;
			}
		}
	else
		{
		if(nudgeSpeed <= 1)
			{
			clearInterval(window[sID + 'NudgeID']);
			}
		nudgeSpeed--;
		}

	if(iDir==1)
		{
		nudgeAmount += nudgeSpeed;

		if(nudgeAmount >= maxScrollAmount)
			{
			nudgeSpeed=0;
			nudgeAmount = maxScrollAmount;
			clearInterval(window[sID + 'NudgeID']);
			}
		}
	else
		{
		nudgeAmount -= nudgeSpeed;

		if(nudgeAmount < 0)
			{
			nudgeSpeed=0;
			nudgeAmount=0;
			clearInterval(window[sID + 'NudgeID']);
			}
		}

	document.getElementById(sID).scrollLeft = nudgeAmount;

	setNudgeControlVisibility(sID);

	window[sID + 'NudgeAmount'] = nudgeAmount;
	window[sID + 'NudgeSpeed'] = nudgeSpeed;
	window[sID + 'NudgeToggle'] = nudgeToggle;
	}

