/***** IMPORTANT *****/
/* The variable carousel_item_count must be declared globally in the calling namespace, 
 * set to the number of items in the carousel
 */

/**
 * Function to style navigation tabs when selected data item changes.
 **/
function styleTabs(start, last) {
	for (i=0; i <= carousel_item_count; i++) {
		if (i < start || last < i) {
			YAHOO.util.Dom.removeClass('tab' + i, 'selected'); 
		} else {
			YAHOO.util.Dom.addClass('tab' + i, 'selected'); 
		}

	}
}


/**
 * Custom load init handler. Called when the carousel loads the first
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadInitHandler
 **/
var loadInitItems = function(type, args) {
	var start = args[0];
	var last = args[1]; 

	styleTabs(start, last);
}

/**
 * Custom load next handler. Called when the carousel loads the next
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadNextHandler
 **/
var loadNextItems = function(type, args) {	

	var start = args[0];
	var last = args[1]; 
	var alreadyCached = args[2];
	
	if(!alreadyCached) {
		load(this, start, last);
	}

	styleTabs(start, last);
};

/**
 * Custom load previous handler. Called when the carousel loads the previous
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadPrevHandler
 **/
var loadPrevItems = function(type, args) {
	var start = args[0];
	var last = args[1]; 
	var alreadyCached = args[2];
	
	if(!alreadyCached) {
		load(this, start, last);
	}

	styleTabs(start, last);
};

var changePage = function(e, args) {
	var carousel = args[0];
	var pageNum = args[1];
	
	carousel.stopAutoPlay();
	carousel.scrollTo(pageNum);
};

/**
 * You must create the carousel after the page is loaded since it is
 * dependent on an HTML element (in this case 'carousel'.) See the
 * HTML code below.
 **/
var carousel; // for ease of debugging; globals generally not a good idea

var pageLoad = function() 
{
	carousel = new YAHOO.extension.Carousel("carousel", 
		{
			numVisible:        1,
			animationSpeed:    0.001,
			autoPlay:          5000,
			animationMethod:   YAHOO.util.Easing.easeOutStrong,
			scrollInc:         1,
			size:              carousel_item_count,
			wrap:              true,
			navMargin:         0,
			loadInitHandler:   loadInitItems,
			loadNextHandler:   loadNextItems,
			loadPrevHandler:   loadPrevItems
		}
	);
	YAHOO.util.Event.addListener(this.carouselNext, "click", this._scrollNext, this);

	YAHOO.util.Event.addListener("tab1", "click", changePage, [carousel, 1]);
	YAHOO.util.Event.addListener("tab2", "click", changePage, [carousel, 2]);
	YAHOO.util.Event.addListener("tab3", "click", changePage, [carousel, 3]);
	YAHOO.util.Event.addListener("tab4", "click", changePage, [carousel, 4]);
	YAHOO.util.Event.addListener("tab5", "click", changePage, [carousel, 5]);
	YAHOO.util.Event.addListener("tab6", "click", changePage, [carousel, 6]);
	YAHOO.util.Event.addListener("tab7", "click", changePage, [carousel, 7]);
	YAHOO.util.Event.addListener("tab8", "click", changePage, [carousel, 8]);
	YAHOO.util.Event.addListener("tab9", "click", changePage, [carousel, 9]);
};

/**
 * Since carousel.addItem uses an HTML string to create the interface
 * for each carousel item, this method formats the HTML for an LI.
 **/

YAHOO.util.Event.addListener(window, 'load', pageLoad);
