function cursorPosition(e) {
	newPosition = e.clientX;

	if (newPosition < lastPosition) {
		changeToLeft();
	} else if (newPosition > lastPosition) {
		changeToRight();
	}

	lastPosition = newPosition;
}

function keyListener(e) {
    if (!e) {
        e = window.event;
    }

    if (e.keyCode == 37) {
        changeToLeft();
        stopScroll();
    }

    if (e.keyCode == 39) {
        changeToRight();
        stopScroll();
    }
}

function scrollLeft() {
	if (numberLayers > numberVisible) {
        for (i=1; i<=numberLayers; i++) {
			theDiv = document.getElementById("layer" + i);
			
			if (theDiv == null) {
				return;
			}
			
			position = ((i - 1) * 153) - toMove;
			if (position <= minPosition) {
				position = position + (numberLayers * 153);
			}
			
			if (position > maxPosition) {
				position = position - (numberLayers * 153);
			}
			
			if ((position > maxPosition) || (position <= minPosition)) {
				theDiv.style.left = "-153px";
				theDiv.style.visibility = "hidden";
			} else {
				theDiv.style.left = position + "px";
				theDiv.style.visibility = "visible";
				theDiv.style.clip = "rect(0px 156px 156px 0px)";
				if (position < 0) {
					theDiv.style.clip = "rect(0px 156px 156px " + (0 - position) + "px)";
				}
				
				if (position > ((numberVisible - 1) * 153)) {
					theDiv.style.clip = "rect(0px " + (623 - position) + "px 156px 0px)";
				}
			}
		}
	   
		toMove += toMoveIncrement;
		if (toMove < maxToMove) {
			timerID = setTimeout("scrollLeft()", 60);
		} else {
			toMove = 0;
			timerID = setTimeout("scrollLeft()", 60);
		}
	}
}

function scrollRight() {
    if (numberLayers > numberVisible) {
	   for (i = 1; i <= numberLayers; i ++) {
		  theDiv = document.getElementById("layer" + i );

		  if (theDiv == null) {
			break;
		  }

		  position = ((i - 1) * 153) - toMove;
		  if (position > maxPosition) {
			 position = position - (numberLayers * 153);
		  }
		  if (position <= minPosition) {
			 position = position + (numberLayers * 153);
		  }

		  if ((position > maxPosition) || (position <= minPosition)) {
			 //Move it to -153 and hide.
			 theDiv.style.left = "-153px";
			 theDiv.style.visibility = "hidden";
		  } else {
			 //Set position.
			 theDiv.style.left = position + "px";
			 theDiv.style.visibility = "visible";
			 theDiv.style.clip = "rect(0px 156px 156px 0px)";
			 if (position < 0) {
				    theDiv.style.clip = "rect(0px 156px 156px " + (0 - position) + "px)";
			 }
			 if (position > ((numberVisible - 1) * 153)) {
				    theDiv.style.clip = "rect(0px " + (482 - position) + "px 156px 0px)";
			 }
		  }
	   }

	   toMove -= toMoveIncrement;

	   if (toMove > -maxToMove) {
		  timerID = setTimeout("scrollRight()", 60);
	   } else {
		  toMove = 0;
		  timerID = setTimeout("scrollRight()", 60);
	   }
    }
}

function changeToRight() {
	if (numberLayers > numberVisible) {
        direction = "Right";
	    timerID = clearTimeout(timerID);
	    scrollRight();
    }
}

function changeToLeft() {
	if (numberLayers > numberVisible) {
        direction = "Left";
	    timerID = clearTimeout(timerID);
	    scrollLeft();
    }
}

function stopScroll() {
	if (numberLayers > numberVisible) {
        timerID = clearTimeout(timerID);
    }
}

function continueScroll() {
	if (direction == "Right") {
		scrollRight();
	} else {
		scrollLeft();
	}
}

function initialiseScroller() {
	mainDiv = document.getElementById("imageScroller");
	mainDiv.style.overflow = "hidden";
	
    if (numberLayers <= numberVisible) {
       for (i = 1; i <= numberLayers; i ++) {
          layerDiv = document.getElementById("layer" + i);
		  if(layerDiv != null) {
			  layerDiv.style.position = "absolute";
			  layerDiv.style.left = ((i - 1) * 153) + "px";
			  layerDiv.style.top = "0px";
		  }
       }
    } else {
	   for (i = 1; i <= numberLayers; i ++) {
		  layerDiv = document.getElementById("layer" + i);
		  if(layerDiv != null) {
			  layerDiv.style.position = "absolute";
			  layerDiv.style.left = "0px";
			  layerDiv.style.top = "0px";
			  layerDiv.style.visibility = "hidden";
		  }
	   }
    }

    document.onkeydown = keyListener;
}