var highlightDot;

/**
 * Custom button state handler for enabling/disabling button state. 
 * Called when the carousel has determined that the previous button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: prevButtonStateHandler
 **/
var handlePrevButtonState = function(type, args) {
	var enabling = args[0];
	var leftImage = args[1];
	if(enabling) {
		highlightPagerDot();
		leftImage.src = "app_themes/default/images/left-enabled.gif";    
	} else {
		leftImage.src = "app_themes/default/images/left-disabled.gif";    
	}        
};

/**
 * Custom button state handler for enabling/disabling button state. 
 * Called when the carousel has determined that the next button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: nextButtonStateHandler
 **/
var handleNextButtonState = function(type, args) {
	var enabling = args[0];
	var rightImage = args[1];        
	if(enabling) {
		highlightPagerDot();
		rightImage.src = "app_themes/default/images/right-enabled.gif";
	} else {
		rightImage.src = "app_themes/default/images/right-disabled.gif";
	}        
};


/**
 * You must create the carousel after the page is loaded since it is
 * dependent on an HTML element (in this case 'mycarousel'.) See the
 * HTML code below.
 **/
var carousel; // for ease of debugging; globals generally not a good idea
var carouselTimer;
var carouselActive = true;
var direction;
var timer;
var pageLoad = function() 
{
	carousel = new YAHOO.extension.Carousel("mycarousel", 
		{
			numVisible:         3,
			animationSpeed:     0.5,
			scrollInc:          3,
			navMargin:          0,
			prevElement:        "prev-arrow",
			nextElement:        "next-arrow",
			size:               6,
			prevButtonStateHandler: handlePrevButtonState,
			nextButtonStateHandler: handleNextButtonState
		}
	);
	autoCarousel();
};

YAHOO.util.Event.addListener(window, 'load', pageLoad);

// FUNCTION TO HIGHLIGHT CORRECT PAGER DOT
function highlightPagerDot()
{
	highlightDot = (carousel.getProperty("firstVisible") - 1) / carousel.getProperty("numVisible");
	for (var i = 0; i < carousel.getProperty("size") / carousel.getProperty("numVisible"); i++)
	{
		document.getElementById("pagerDot" + i).src = "app_themes/default/images/carousel_navbar_inactive.gif";
	}
	document.getElementById("pagerDot" + highlightDot).src = "app_themes/default/images/carousel_navbar_active.gif";
}

// FUNCTION TO AUTOMATE THE CAROUSEL
function autoCarousel()
{
	if (carouselActive)
	{
		if (direction)
		{
			carousel._scrollPrevInc(carousel.cfg.getProperty("scrollInc"),true);
			direction = 0;
		}
		else
		{
			carousel._scrollNextInc(carousel.cfg.getProperty("scrollInc"),true);
			direction = 1;
		}
		
		carouselTimer = setTimeout(autoCarousel, 8000);
		return;
	}
}

// FUNCTION TO PAUSE THE CAROUSEL
function pauseCarousel()
{
	carouselActive = 0;
	if (carouselTimer)
	{
		clearTimeout(carouselTimer);
	}
	if (timer)
	{
		clearTimeout(timer);
	}
	timer = setTimeout(resumeCarousel, 8000);
}

// FUNCTION TO RESUME THE CAROUSEL
function resumeCarousel()
{
	carouselActive = 1;
	autoCarousel();
}	
