//Requires Prototype.js
var speed = 5 //How fast the slider will slide.
var offset = 0 //Takes into account the padding/border width etc. Tweak this if the boxes aren't lining up right after a shift.
var tempDiv = document.createElement('LI')
tempDiv.id = 'tempDiv'
shifter = function(listElement) {
	if (typeof listElement == 'undefined') { this.check = false; return; }
	else { this.check = true; }
	this.list = listElement;
	this.nodes = this.list.getElementsByTagName('li');
	this.list.style.marginLeft = -this.nodes[0].offsetWidth+10+"px"; //Offsets the list UL element by the width of the first LI element in order to hide the squish box 
	//moves the last LI element and tacks it on the front so there is a box to squish when animating.
	var lastNode = this.nodes[this.nodes.length-1];
	this.list.removeChild(lastNode);
	this.list.insertBefore(lastNode,this.nodes[0]);
	playState = true;
}
shifter.prototype.move = function(direction) {
	if (direction != "left" && direction != "right") {
		throw new this.exception("Direction argument must be \"left\" or \"right\".");
	}
	if (direction == "left") {
		var node = this.nodes[0];
		this.list.removeChild(node);
		this.list.appendChild(node);
	}	
	else {
		var node = this.nodes[this.nodes.length-2];
		this.list.removeChild(node);
		this.list.insertBefore(node, this.nodes[0]);
	}
	return true;
}
shifter.prototype.exception = function(message) {
   this.message=message;
   this.name="Exception in Carousel";
}
shifter.prototype.right = function() { //Used to shrink a list item in order to simulate animated effect
	if (this.intervalID) {	// check to see is animation is already happening. If so, discard user request
		return;				
	}
	this.origWidth = this.nodes[0].offsetWidth;
	this.index = this.origWidth;
	oInstance = this; // the use of a "closure" is necessary to preserve assoc. w/ object instance in setInterval
	var copy = this.nodes[0].innerHTML
	this.nodes[0].innerHTML = "&nbsp;"
	this.list.appendChild(tempDiv);
	$('tempDiv').innerHTML = copy
	this.intervalID = window.setInterval(oInstance.rightCallback,1); //note: setInterval is evaluated in the global scope
}
shifter.prototype.rightCallback = function() {
			//alert(oInstance.nodes[0].style.width)
			var tempDiv = $('tempDiv')
			var width = oInstance.easeInOut(oInstance.index, 1);
			oInstance.nodes[0].style.width = width+"px";
			oInstance.index = oInstance.index - speed;
			if (oInstance.index <= 0 ) {
				window.clearInterval(oInstance.intervalID);
				oInstance.intervalID = null;
				oInstance.nodes[0].style.width = oInstance.origWidth-offset+"px";
				oInstance.nodes[0].innerHTML = tempDiv.innerHTML
				tempDiv.parentNode.removeChild(tempDiv);
				oInstance.move('left');
			}
};
shifter.prototype.left = function() { //Used to expand a list item in order to simulate animated effect
if (this.intervalID) {	// check to see is animation is already happening. If so, discard user request
		return;				
	}
	this.list = $('slide-list');
	this.nodes = this.list.getElementsByTagName('li');
	this.origWidth = this.nodes[0].offsetWidth;
	this.index = this.origWidth;
	var copy = this.nodes[this.nodes.length-1].innerHTML
	this.nodes[this.nodes.length-1].innerHTML = "&nbsp;"
	this.list.appendChild(tempDiv);
	$('tempDiv').innerHTML = copy
	this.nodes[this.nodes.length-2].style.width = 0+"px";	 
	this.move('right');
	oInstance = this; // the use of a "closure" is necessary to preserve assoc. w/ object instance in setInterval
	oInstance.index = 0;
	this.intervalID = window.setInterval(oInstance.leftCallback,1); //note: setInterval is evaluated in the global scope
}
shifter.prototype.leftCallback = function() {			
			var tempDiv = $('tempDiv')
			var width = oInstance.easeInOut(oInstance.index, 1);
			//alert(oInstance.nodes[0].innerHTML + " width: " + oInstance.nodes[0].style.width)
			oInstance.nodes[0].style.width = width+"px";
			oInstance.index = oInstance.index + speed;
			if (oInstance.index >= oInstance.origWidth) {
				window.clearInterval(oInstance.intervalID);
				oInstance.intervalID = null;
				oInstance.nodes[0].innerHTML = tempDiv.innerHTML
				oInstance.nodes[0].style.width = oInstance.origWidth-offset+"px";
				tempDiv.parentNode.removeChild(tempDiv);
			}
};
shifter.prototype.easeInOut = function(actualStep,powr) { 
    var minValue = 0;
	var totalSteps = this.nodes[0].offsetWidth;
	if (totalSteps == 0) {
		totalSteps++
		}
	var delta = totalSteps - 0; 
    var stepp = Math.pow(((1 / totalSteps) * actualStep), powr) * delta;
	return Math.ceil(stepp)
    }
shifter.prototype.advance = function() {
	
	if (playState)	{
		this.right();
	}
}
shifter.prototype.toggle = function() {
	playState = (playState)?false:true;
	if (playState) {
		$('pausebutton').className = 'pause_on'
		}
	if (!playState){
		$('pausebutton').className = 'play'
		}
}
shifter.prototype.leftClick = function() {
	playState = false;
	$('pausebutton').className = 'play'
	this.left()
}
shifter.prototype.rightClick = function() {
	playState = false;
	$('pausebutton').className = 'play'
	this.right()
}

