// LW

and={};

and.Carousel = (function(){
    var rotationTimer;
    var rotationTimeout = 5000;
	var currentIdx = 0;
	var itemwidth = 83+10;
	var viewportWidth=279;
	var carousel = document.getElementById('carousel');

	var headlinePics = [];
	for (var i = 0; i < 50; i++){
		var el = document.getElementById('carousel-imageholder'+i);
		if (el) headlinePics.push(el);
		else break;
	}
	var uniqueItemCount = headlinePics.length;
	var blockWidth = uniqueItemCount * itemwidth;
	var fillerBlockCount = Math.ceil(viewportWidth/blockWidth);
	
	var thumbHtml = document.getElementById('carousel-thumbs').innerHTML;
	var fillerHtml = '';
	for (var i = 0; i < fillerBlockCount; i++) fillerHtml += thumbHtml;
	document.getElementById('carousel-filler').innerHTML = fillerHtml;

	function hoverInImage(thumbName) {
        stopAutoChange();
        var id = thumbName.replace(/^carousel-thumb/,"");
        displayImage(id);
    }	    
	function hoverOutImage() {
        startAutoChange();
    }
    function setPos(pos) {
    	carousel.style.left = pos+"px";
    }
    function getPos() {
    	return parseInt(carousel.style.left);
    }
    
	function nextItem(){
		var pos = parseInt(carousel.style.left);
	    if(pos == -blockWidth){
			pos = 0;
			carousel.style.left = pos+"px";
	    }
	    startAnim(pos - itemwidth);
		//animation.start('left', newposition);
	}
	function previousItem(){
		var pos = parseInt(carousel.style.left);
	    if(pos == 0){
			pos = -blockWidth;
			carousel.style.left = pos+"px";
	    }
	    startAnim(pos + itemwidth);
		//animation.start('left', newposition);
	}
    function startAutoChange() {
    	rotationTimer = setTimeout('and.Carousel.autoRotateImage()',rotationTimeout);
    }
	function stopAutoChange(){
		clearTimeout(rotationTimer);
		return null;
	};
	function autoRotateImage() {
		displayImage((currentIdx + 1) % uniqueItemCount);
		startAutoChange();
	}
	function displayImage(n) {
		currentIdx = n;
	    for (var i = 0; i < headlinePics.length; i++) {
	        if (i == n) {
				headlinePics[i].style.display='inline';
      		} else {
				headlinePics[i].style.display='none';
	        }
	    }
	}
	
	//========= animation with kinetic effect ===========
	var duration = 400; //animation duration (ms)
    var fps = 35; //frames per sec
    var refresh = 1000/fps;
    var refreshCount = duration/refresh;
    var startPos = 0;
    var distance = 0;
    var animating = false;
    var startTime;
    var animTimer;
    
    function startAnim(pos) {
    	if (!animating) {
	    	animating = true;
	    	startPos = getPos();
	    	distance = pos - startPos;
	    	startTime = getNow();
	    	animTimer = setInterval('and.Carousel.doAnim()',refresh);
    	}
    }
    function doAnim() {
    	var elapsed = getNow() - startTime;
    	var stepCount = Math.floor(elapsed/refresh);
    	var delta = (stepCount >= refreshCount)?distance:Math.ceil((Math.cos(stepCount/refreshCount*Math.PI)/-2+0.5)*distance);
    	setPos(startPos+delta);
    	if (stepCount >= refreshCount || stepCount>200) { //capped in case of endless loop
    		clearInterval(animTimer);
    		animating = false;
    	}
    }
    function getNow() {
    	return (new Date()).getTime();
    }
	//========= animation end ===========
    
    startAutoChange();

	return {
		nextItem : nextItem,
		previousItem : previousItem,
		startAutoChange : startAutoChange,
		hoverInImage : hoverInImage,
		hoverOutImage : hoverOutImage,
		autoRotateImage : autoRotateImage,
		doAnim : doAnim
	}
})();

