/**
 * @author Hans Pots
 * in opdracht van progressoMedia
 */

var toDisplay = 0
var stepDirection = 1
var speed = 10
var frameSize = 410
var isInterval = false
var isIE = false
var carouselLength = 0;

function carouselJump(toGo) {
	if (!isInterval) {
		isInterval = true
		if (toGo < toDisplay) {
			stepDirection = 1
			toDisplay = toGo			
			moveToPositionID = setInterval("moveToPositionInterval()", 10)
		}
		if (toGo > toDisplay) {
			stepDirection = -1
			toDisplay = toGo
			moveToPositionID = setInterval("moveToPositionInterval()", 10)
		}
	}
}

function moveToPositionInterval () {
	selectDiv = document.getElementById(toDisplay)
	currentXpos = parseInt(selectDiv.style.left)
	stepSize = Math.abs(Math.ceil(currentXpos/speed))
	
	if (stepSize == 0) stepSize = stepDirection
	if (stepSize > frameSize/speed) stepSize = frameSize/speed

	for (i=0; i<carouselLength; i++) {
		selectDiv = document.getElementById(i)
		
		currentXpos = parseInt(selectDiv.style.left)
		
		if (currentXpos == -frameSize && stepDirection == -1) {
			currentXpos = (carouselLength-1) * frameSize
		}  
		if (currentXpos == (carouselLength-1) * frameSize && stepDirection == 1) {
			currentXpos = -frameSize
		}
		
		currentXpos = currentXpos + (stepDirection*stepSize)
		selectDiv.style.left = currentXpos+"px"
	}
	
	selectDiv = document.getElementById(toDisplay)

	if (parseFloat(selectDiv.style.left) == 0) {
		clearInterval(moveToPositionID)
		isInterval = false
	}
}

function carouselTo(toGo) {
	if (!isInterval) {
		isInterval = true
		selectDiv = document.getElementsByName("carousel")
		
		if (toGo == "L") {
			toDisplay++
			if (toDisplay == carouselLength)  toDisplay = 0
			stepDirection = -1
		} else {
			toDisplay--
			if (toDisplay < 0) toDisplay = carouselLength-1
			stepDirection = 1
		}		
		moveToPositionID = setInterval("moveToPositionInterval()",10)
	}
}

function carouselInnit (count) {
	var appName = navigator.appName
	carouselLength = count

	if (appName == "Microsoft Internet Explorer") {
		isIE = true
		selectDiv = document.getElementsByTagName("div")
		var count = 0 
		for (i=0; i < selectDiv.length; i++) {
			if (selectDiv.item(i).name == "carousel" ){
				xPos = count*frameSize
				count++
				selectDiv.item(i).style.left = xPos+"px"
			}												
		}
	} else {
		isIE = false;
		for (i=0; i < carouselLength; i++) {
			selectDiv = document.getElementById(i)
			xPos = i*frameSize
			selectDiv.style.left = xPos+"px"		
		}
	}
	
}



