//------------------------------- Rich's Amazing Scroller of DOOM w/ getDivsByClassName & insertAfter --------------------------------------->
		/*	<script type="text/javascript">
				slideLoop(container, arrowLeft, arrowRight, scrollItem, slideNumber, startPosition, numberShown); // start position not required, it will accept '#px' or just the # of the item you want to start on.
				speed = 10;
			</script> */

var animateTime = 30;
var animate = [false, false];
var slideCont;
var slideItems = [];
var itemWidth;
var totalSlide;
var position = 0;
var timerId = [];
var startPos;
var slideNumber;
var itemClass;

function slideLoop(container, arrowLeft, arrowRight, scrollItem, scrollNumber, startPosition, numberShown) { //pass in the sliding container id, the arrow ids, scrolling item classname, the number of elements you want to slide, and where you want it to start (either a different px amount then the button, or it will utilize the button as the offset, and you can specify which item to start on, or leave it blank)
	itemClass = scrollItem;
	slideNumber = scrollNumber;
	createWidth(container, scrollItem, arrowLeft, arrowRight, startPosition, numberShown);
}

function createWidth(container, scrollItem, arrowLeft, arrowRight, startPosition, numberShown){
	var buttonLeft = document.getElementById(arrowLeft);
	var buttonRight = document.getElementById(arrowRight);
	slideCont = document.getElementById(container);
	slideItems = getDivsByClassName(scrollItem);
	itemWidth = slideItems[0].offsetWidth;
	var itemHeight = slideItems[0].offsetHeight;
	var totalWidth = itemWidth * slideItems.length+'px';	
	totalSlide = slideNumber * itemWidth;
	slideCont.style.width = totalWidth;
	slideCont.parentNode.style.width = (buttonLeft.offsetWidth * 2) + itemWidth * numberShown + 'px'; // set wrapper width;
	slideCont.parentNode.style.height = itemHeight + 'px';
	makeButtons(buttonLeft, buttonRight);	// add arrow events
	if(startPosition != null && startPosition != undefined && startPosition != '') {  // you can enter a 'px' amount, otherwise it will start offset of the button, unless you enter a number which will signify which item to start on
		if(startPosition.match && startPosition.match('px')) {
			startPos = startPosition - totalSlide;
		} else {		
			startPos = buttonLeft.offsetWidth - (startPosition-1) * itemWidth;
		}
	} else {
		startPos = buttonLeft.offsetWidth - totalSlide;
	}
	slideCont.style.left = startPos + 'px';
}

function makeButtons(buttonLeft, buttonRight){
	buttonLeft.onclick = function() {
		if(animate[1] != true && animate[0] != true) {
			moveElements('left');
			moveLeft();
			return false;
		}	
	}
	buttonRight.onclick = function() {
		if(animate[1] != true && animate[0] != true) {
			moveElements('right');
			moveRight();
			return false;
		}
	}
}

function moveLeft() {
	clearTimeout(timerId[0]);
		if(position + speed < totalSlide) {
			position += speed;
			slideCont.style.left = startPos + position + 'px';
			timerId[0] = setTimeout('moveLeft()', animateTime); 
			animate[0] = true;
		} else {
			startPos += totalSlide;
			slideCont.style.left = startPos + 'px';
			animate[0] = false;
			position = 0;
			count = 0;
		}
}

function moveRight() {
	clearTimeout(timerId[1]);
		if(Math.abs(position - speed) < totalSlide) {
			position -= speed;
			slideCont.style.left = startPos + position + 'px';
			timerId[1] = setTimeout('moveRight()', animateTime); 
			animate[1] = true;
		} else {
			startPos -= totalSlide;
			slideCont.style.left = startPos + 'px';
			animate[1] = false;
			position = 0;
			count = 0;
		}
}

function moveElements(direction) {
	var childs = getDivsByClassName(itemClass);
	if(direction == 'left') {
		for(i=slideNumber; i>0; i--) {
			slideCont.insertBefore(childs[childs.length-i], childs[0]);
		}
		slideCont.style.left =startPos - totalSlide + 'px'; //itemWidth*count + 'px';
		startPos -= totalSlide;
	} else {
		for(l=slideNumber-1;l>=0;l--) {
			insertAfter(childs[l],childs[childs.length-1]);
		}
		slideCont.style.left =startPos + totalSlide + 'px'; //itemWidth*count + 'px';
		startPos += totalSlide;
	}
}

/*--------- start getDivsByClassName - returns array of div elements with selected class -------- */
function getDivsByClassName(elementClass) {
	var divArray = document.getElementsByTagName('div');
	var elements = [];
	for(i=0;i<divArray.length;i++) {
		var temp = new Array();
		var temp = divArray[i].className.split(' ');
		for(n=0;n<temp.length;n++) {
			if(temp[n].match(elementClass)) {
				elements.push(divArray[i]);
			}
		}
	}
	return elements;
}

/*--------- start insertAfter - appends node after given object -------- */
function insertAfter(newElement, targetElement) {
	var parent = targetElement.parentNode;
	if(parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}
		
//------------------------------- End Rich's Amazing Scroller of DOOM w/ getDivsByClassName & insertAfter --------------------------------------->
