

function animateSlider(element, numberOfImages, imageWidth) {
	element = $(element);
	
	fps = 50;
	transitionLength = 1.5;
	pauseLength = 8;
	animationLength = pauseLength + transitionLength;
	
	currentImage = 1;
	currentPosition = 0;
	nextPosition = currentPosition - imageWidth;
	
	firstElement = element.childNodes.item(0);
	elementType = firstElement.tagName;
	loopElement = null;
	
	if (elementType == 'IMG') {
		loopElement = document.createElement('img');
		loopElement.src = firstElement.src;
		loopElement.id = 'loopElement';
		loopElement.alt = firstElement.alt;
	} else if (elementType == 'A' ) {
		loopElement = document.createElement('a');
		loopElement.href = firstElement.href;
		
		firstImg = firstElement.childNodes.item(0);
		loopImg = document.createElement('img');
		loopImg.src = firstImg.src;
		loopImg.id = 'loopImg';
		loopImg.alt = firstImg.alt;
		
		loopElement.appendChild(loopImg);
	}
	
	
	element.style.width = (parseInt(element.getStyle('width')) + imageWidth) + 'px';
	element.appendChild(loopElement);
	numberOfImages += 1;
	
	function animate() {
		if (currentImage == numberOfImages) {
			sendToZero();
		}
		
		slide(element, currentPosition, nextPosition, fps, transitionLength);
		currentImage += 1;
		currentPosition -= imageWidth;
		nextPosition -= imageWidth;
	}
	
	function sendToZero() {
		element.style.left = '0px';
		currentImage = 1;
		currentPosition = 0;
		nextPosition = currentPosition - imageWidth;
	}
	
	function loopAnimation() {
		animate();
		window.setTimeout(loopAnimation, animationLength * 1000)
	}
	
	window.setTimeout(loopAnimation, pauseLength * 1000);
	
	
}


function slide(element, startPosition, endPosition, fps, duration) {
	
	// Set up internal variables
	element = $(element);
	duration = duration * 1000;
	frameLength = 1000 / fps;
	frameCount = parseInt(duration / frameLength);
	
	distance = endPosition - startPosition;
	distancePerFrame = distance / frameCount;
	
	
	// Make an array for the frame values and precalculate the frames
	framesArray = new Array();
	
	thisFramePosition = startPosition + distancePerFrame;
	for (i = 0; i < frameCount; i++) {
		framesArray.push(thisFramePosition);
		thisFramePosition += distancePerFrame;
	}
	
	// Get ready to animate
	thisFrame = null;
	lastFrame = framesArray[framesArray.length - 1];
	animationIndex = 0;
	
	function step() {
		thisFrame = framesArray[animationIndex];
		element.style.left = thisFrame + 'px';
		animationIndex += 1
		if (thisFrame == lastFrame) {
			element.style.left = lastFrame + 'px';
		} else {
			window.setTimeout(step, frameLength);
		}
	}
	
	// Do it.
	step();
	
	
}

