// JavaScript Document

//carousel / gallery setups

var carouselInfos = new Array();

//itemDiv should be something like "#carouselItems"
//coltrolList should be something like "#carouselcontrols" (should point to a <ul>)
//autoplayChangeDelay - delay before change.  To not autoplay, send in 0 or -1
function registerCarousel(itemDiv, controlList, prevButton, nextButton, autoplayChangeDelay, slideWidth) {
	var infoObj = new Object();
	infoObj.itemDiv = itemDiv;
	infoObj.controlList = controlList;
	infoObj.prevButton = prevButton;
	infoObj.nextButton = nextButton;
	infoObj.index = -1;
	infoObj.numSlides = $(itemDiv).children().size();
	infoObj.timer = null;
	infoObj.slideDelay = 2000;
	infoObj.slideWidth = slideWidth;
	carouselInfos.push(infoObj);
	
	for (var i = 0; i < infoObj.numSlides; i++) {
		var navDotStr = '<li><a href="#">';
		if (isIE6 != true && typeof document.body.style.maxHeight != "undefined") {
			navDotStr = navDotStr + '<div id="featureTooltip"><div id="featureTooltipContent">' +  $(itemDiv).children().slice(i,i+1).find("input").val() + '</div></div>';
		}
		navDotStr = navDotStr +  '</a></li>';
		$(controlList).append(navDotStr);
	}
	
	$(controlList +  " li a").click(function ()  {
		var newIndex = $(this).parent().prevAll().size();
		var carouselIndex = -1;
		
		for (var i = 0; i < carouselInfos.length; i++) {
			if ( $(this).parents(carouselInfos[i].controlList).length > 0) {
				carouselIndex = i;
				
				break;
			}
			
		}
		if (carouselIndex != -1) gotoCarouselSlide(carouselIndex, newIndex);
		return false;
	});
	
	if (nextButton) {
		$(nextButton).click(function () {
			var carouselIndex = -1;
			for (var i = 0; i < carouselInfos.length; i++) {
				if ( $(this) == $(carouselInfos[i].nextButton)) {
					carouselIndex = i;
					break;
				}
			} 
			if (carouselIndex != -1) gotoNext(carouselIndex);
		});
	}
	
	if (prevButton) {
		$(prevButton).click(function () {
			var carouselIndex = -1;
			for (var i = 0; i < carouselInfos.length; i++) {
				if ( $(this) == $(carouselInfos[i].prevButton)) {
					carouselIndex = i;
					break;
				}
			}
			if (carouselIndex != -1) gotoPrev(carouselIndex);
		});
	}
	
	if (autoplayChangeDelay > 0) {
		infoObj.slideDelay = autoplayChangeDelay;
		var timeoutStr = "gotoNext("+ (carouselInfos.length - 1) +");";
		infoObj.timer = setTimeout(timeoutStr,infoObj.slideDelay);
	}
	
	gotoCarouselSlide(carouselInfos.length - 1, 0);
	
	return infoObj;
}



function gotoNext(infoObjIndex) {
	var infoObj = carouselInfos[infoObjIndex];
	if (infoObj.index < infoObj.numSlides-1) {
		gotoCarouselSlide(infoObjIndex, infoObj.index + 1);
	} else {
		gotoCarouselSlide(infoObjIndex, 0);
	}
}

function gotoPrev(infoObjIndex) {
	var infoObj = carouselInfos[infoObjIndex];
	if (infoObj.index > 0) {
		gotoCarouselSlide(infoObjIndex, infoObj.index - 1);
	} else {
		gotoCarouselSlide(infoObjIndex, infoObj.numSlides-1);
	}
}


function gotoCarouselSlide(infoObjIndex, index) {
	var infoObj = carouselInfos[infoObjIndex];
	if (infoObj.index != index) {
		$(infoObj.controlList + " li a.selected").removeClass("selected");
		$(infoObj.controlList + " li a").slice(index,index + 1).addClass("selected");
		$(infoObj.itemDiv).animate({marginLeft:(-infoObj.slideWidth * index)}, 500);
		//TODO: deactive old "currentItem"
		infoObj.index = index;
		
		if (infoObj.timer) {
			clearTimeout(infoObj.timer);
			var timeoutStr = "gotoNext("+ infoObjIndex +");";
			infoObj.timer = setTimeout(timeoutStr,infoObj.slideDelay);
		}
	}
}
