var elements;
var hud_nav;
var currentIndex=0;
var ed=0;
var dly=0;
var hudInterval;
var width=0;
var busy=false;
var nextSlideTo=null;
function initCarrousel(container, effectDelay, delay, hudnav) {
	elements = $(container).children();
	hud_nav = $(hudnav).children();
	dly=delay;
	ed = effectDelay;
	width=$(container).css("width");
	slidePlay();
}

function slideNext() {
	var toIndex = (currentIndex+1) %elements.length;
	slideTo(toIndex);
}

function slidePause() {
	clearInterval(hudInterval);
	hudInterval = null;
}

function navTo(index) {
	slidePause();
	slideTo(index);
	slidePlay();
}

function slidePlay() {
	if(hudInterval == null) {
		hudInterval = setInterval("slideNext()", dly);
	}
}

function slideTo(toIndex) {
	if (!busy) {
		busy = true;
		if(currentIndex != toIndex) {
			$(elements[toIndex]).css('left', width);
			$(elements[toIndex]).animate({ left: '-='+width},ed);
			$(hud_nav[currentIndex]).removeClass('hud_selected').addClass('hud_unselected');
			$(hud_nav[toIndex]).removeClass('hud_unselected').addClass('hud_selected');
			$(elements[currentIndex]).animate({ left: '-='+width},ed,function() {
				currentIndex = toIndex;
				busy=false;
				if (nextSlideTo!=null) {
					tempToIndex = nextSlideTo;
					nextSlideTo = null;
					slideTo(tempToIndex);
				}
			});
		} 
	} else {
		nextSlideTo=toIndex;
	}
}
