/***********************************************************************/
/*spinner.js                                                           */
/*a handy utility for offering a rotating library of items, articles   */
/*version 1: not entirely robust yet                                   */
/*Authored in its entirety by Sam Kent: sam@htmelegance.com            */
/*Free for public use on the condition that this header remains intact */
/*business use requires author's approval                              */
/***********************************************************************/

//this is how wide your items are and needs to correspond to the width in the .item style class
var itemWidth = 134;
var showItems = 4;	//this doesn't have to be an integer but needs to be positive

//object for which way to adjust when scrolling
scrollDirections = {"left":1,"right":-1};
scrollMax = {"left":-itemWidth,"right":-1};
scrollNext  = {"left":-1,"right":1};

//for an interval
var scrollInterval;

//resets the scroll interval when a user clicks a link
function resetScrollInterval() {
	window.clearInterval(scrollInterval);
}

//seconds between scrolling.  Should be an integer, and positive
//set to 0 if you do not want to scroll
var secondsBetweenInterval = 8;

//sets the interval for auto-scrolling
function  setScrollInterval() {
		scrollInterval = window.setInterval("shiftItemListLeft();",secondsBetweenInterval*1000);
}

//initializes the scroller by counting the items
//and adjusting widths of container elements
function initScroller() {
	//how wide are all items together?
	var itemCount = document.getElementById("greaterContainer").childNodes.length;
	var allItemWidth =  itemCount * itemWidth;
	document.getElementById("greaterContainer").style.width = allItemWidth + "px";
	document.getElementById("itemContainer").style.width = (itemWidth * showItems) + "px";

		//safari doesn't scroll
		if(navigator.appVersion.indexOf("Safari") >= 0) {
			document.getElementById("greaterContainer").style.width  = ((itemWidth * showItems) + itemWidth) + "px"
		}


	//handle for not having enough items
	if(itemCount <= showItems) {
		shiftItemList = noAction;
		document.getElementById("shiftLinkRight").style.display = "none";
		document.getElementById("shiftLinkLeft").style.display = "none";
	}

	//and if you want, rotate the items periodically
	setScrollInterval();
}

function noAction() {
}

var shifting = new Boolean(false);
var move;

//function for shifting greaterContainer left and right
function shift(direction, moveTo) {
	shifting = new Boolean(true);

	//capture first time here
	if(!moveTo) {
		//first time here
		if(direction == "left") {
			move = 0;
		} else {
			move = itemWidth * scrollDirections[direction];
		}
	}

	//change the position
	document.getElementById("greaterContainer").style.marginLeft = move + "px";

	//speed this up for FF on Mac
	var moveMultiplier = (navigator.appVersion.indexOf("Macintosh") >= 0) ? 3 : 1;


	//figure out how much to move to next time
	if(direction == "left") {
		if(move < -(itemWidth*0.8)) {
			move -= 1;
		} else if (move < -(itemWidth*0.6)) {
			move -= 3 * moveMultiplier;
		} else {
			move -= 5 * moveMultiplier;
		}
	} else {
		if(move < -(itemWidth*0.2)) {
			move += 5 * moveMultiplier;
		} else if (move < -(itemWidth*0.4)) {
			move += 3 * moveMultiplier;
		} else {
			move += 1;
		}
	}

	document.getElementById("greaterContainer").style.marginLeft = move + "px";

	//alert(move);

	if(move != scrollMax[direction]) {
	setTimeout("shift('" + direction + "'," + move + ");",1);
	} else {
		shifting = new Boolean(false);
		killElement(direction);
	}
}

function safariShift(direction) {
		killElement(direction);
}

//change shift method for Safari
if (navigator.appVersion.indexOf("Safari") >= 0) {
	shift = safariShift;
}

//function to kill the added element
function killElement(direction) {
	if(direction == "right") {
		//kill last item
		document.getElementById("greaterContainer").removeChild(itemDivs.item(itemDivs.length-1));
	} else {
		//kill first item
		document.getElementById("greaterContainer").removeChild(itemDivs.item(0));
	}

	//rename temp item's id with killed item's id
	document.getElementById("tempID").id = exchangeID;

	//reset the margin to account for killed item
	document.getElementById("greaterContainer").style.marginLeft = "0px";

}

//this will store the items in the rotater as pointer elements
var itemDivs;

//holder for the id parameter of the element to later be destroyed
var exchangeID;

//method for shifting the scroller one direction or another
function shiftItemList(direction) {

	//not more than one shift at a time
	if(shifting == false) {

		//have to specify a direction
		if(!direction) {
			return;
		}

		//direction has to be left or right
		if(direction != "right" && direction != "left") {
			return;
		}

		itemDivs = document.getElementById("greaterContainer").getElementsByTagName("div");

		var newDiv = document.createElement("div");
		newDiv.setAttribute("id","tempID");
		newDiv.setAttribute("class","item");
		newDiv.setAttribute("className","item"); //for IE

		newDiv.innerHTML = (direction == "left") ? itemDivs[0].innerHTML : itemDivs[itemDivs.length-1].innerHTML;

		if (direction == "right") {
			exchangeID = itemDivs.item(itemDivs.length-1).id;
		} else {
			exchangeID = itemDivs.item(0).id;
		}

		//insert into the queue of items
		if(direction == "right") {
			//insert
			document.getElementById("greaterContainer").insertBefore(newDiv,itemDivs[0]);

			//shift
			shift("right");

		} else {
			//insert at end
			document.getElementById("greaterContainer").insertBefore(newDiv,itemDivs[itemDivs.length-1].nextSibling);

			//shift
			shift("left");

		}
	}
}

function shiftItemListLeft() {
	shiftItemList("left");
}


function left() {
	shiftItemListLeft();
	resetScrollInterval();
}

function shiftItemListRight() {
	shiftItemList("right");
}

function right() {
	shiftItemListRight();
	resetScrollInterval();
}