var currentIndex=0;
var totalElts=0;

function autoMove() {
	var api=$("#coverflow").scrollable({api:true});
	api.next();
	currentIndex = (currentIndex+1)%totalElts;
}

$(document).ready(function() {
	if ($("#coverflow").attr("id")) {
	var cf=$("#coverflow").scrollable({
		size:1,
		items: "#coverflow_items",
		hoverClass: "hover",
		api:true,
		loop:true
	});
	
	$(".coverflow_item").each(function() {
		totalElts++;	
	});


	$(".coverflow_prev").click(function() {
		if (currentIndex==0) {
			cf.seekTo(totalElts-1);
		} else {
			cf.prev();
		}
		clearInterval(autoTimer);
		currentIndex=(totalElts+currentIndex-1)%totalElts;
	});
	
	$(".coverflow_next").click(function() {
		cf.next();
		clearInterval(autoTimer);
		currentIndex=(currentIndex+1)%totalElts;
	});

	/*
	$(".coverflow_direct").click(function() {
		userInterface();
		goingIndex=$(this).attr("rel");
		doTheFade();
	});
	*/

	//$("#coverflow").click(function() {
	//	userInterface();
	//});
	
	//for (var i=1; i<totalElts; i++)
	//	$("#coverflow_text_"+i).hide();

	autoTimer=setInterval("autoMove()",4000);

	//setInterval("putBackBox();",50);

	$(document).keyup(function(a) {
		if (a.keyCode==37) {
			userInterface();
			goingIndex=(totalElts+currentIndex-1)%totalElts;
			doTheFade();
		} 
		if (a.keyCode==39) {
			userInterface();
			goingIndex=(currentIndex+1)%totalElts;
			doTheFade();			
		}
	});
	}
});
