INTERVAL = 5000;
MOVE_SPEED = 500;
ITEM_HEIGHT = 60;

moving = false;
topIndex = 0;		// top selector item for rounded corner
currentIndex = 0; 	// used for auto scroll
intervalID = 0;		// to stop auto scroll on click

function showDiv(pick, click){
	// stop auto-scroll
	if(click) clearInterval(intervalID);
	
	currentIndex = pick;
	$(".highlight-text").hide();
	$("#item" + pick).show();
	$(".select").removeClass("selected");
	$("#select" + pick).addClass("selected");

	$(".highlight-arrow").hide();
	$("#arrow" + pick).show();
}
function slideDown(numItems, click){
	// stop auto-scroll
	if(click) clearInterval(intervalID);
	
	// if(-(((number of items - visible items) * height of each)) < top)
	if(!moving){
		if(-(((numItems - 5) * ITEM_HEIGHT)) < parseInt($("#items").css("top"))){
			moving = true;
			topIndex++;
			
			$("#items").animate({
				top: "-=" + ITEM_HEIGHT}, MOVE_SPEED, function(){
					moving=false;
					$(".select").removeClass("selector-top");
					$(".select:eq(" + topIndex + ")").addClass("selector-top");
					});
		}
	}
}
function slideUp(click){
	// stop auto-scroll
	if(click) clearInterval(intervalID);
	
	if(!moving){
		if(parseInt($("#items").css("top")) <= -ITEM_HEIGHT){
			moving = true;
			topIndex--;
			$("#items").animate({
				top: "+=" + ITEM_HEIGHT}, MOVE_SPEED, function(){
					moving=false;
					});
			$(".select").removeClass("selector-top");
			$(".select:eq(" + topIndex + ")").addClass("selector-top");
		}
	}
}

function autoScroll(numItems){
	if (currentIndex < numItems - 1) {
		showDiv(++currentIndex, false);
		slideDown(numItems, false);
	}
	else{
		// reset to the top
		topIndex = 0;
		$(".select:eq(" + topIndex + ")").addClass("selector-top");
		$("#items").css("top", "0");
		showDiv(0, false);
	}
}

function initScroll(numItems){
	intervalID = setInterval("autoScroll(" + numItems + ")", INTERVAL);
}

