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

	var start = args[0];
	var last = args[1]; 

	load(this, 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[4];
	
	if(!alreadyCached) {
		load(this, 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);
	}
};

var load = function(carousel, start, last) {
	for(var i=start;i<=last;i++) {
		carousel.addItem(i, fmtItem(header_img[i-1], header_h1[i-1], header_text[i-1], header_url[i-1], tab_current[i-1], teaser_border_bottom[i-1]));
	}
};

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

/**
 * You must create the carousel after the page is loaded since it is
 * dependent on an HTML element (in this case 'dhtml-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("dhtml-carousel", 
		{
			numVisible:        1,
			animationSpeed:    0,
			scrollInc:         1,
			size:              4,
			navMargin:         0,
			loadInitHandler:   loadInitialItems,
			loadNextHandler:   loadNextItems,
			loadPrevHandler:   loadPrevItems,
			disableSelection: false,
			autoPlay: 10000,
			wrap:true,
                        firstVisible: 2
		}
	);
	
	YAHOO.util.Event.addListener("tab1_1", "click", changePage, [carousel, 1]);
	YAHOO.util.Event.addListener("tab1_2", "click", changePage, [carousel, 1]);
	YAHOO.util.Event.addListener("tab1_3", "click", changePage, [carousel, 1]);
	YAHOO.util.Event.addListener("tab1_4", "click", changePage, [carousel, 1]);
	YAHOO.util.Event.addListener("tab2_1", "click", changePage, [carousel, 2]);
	YAHOO.util.Event.addListener("tab2_2", "click", changePage, [carousel, 2]);
	YAHOO.util.Event.addListener("tab2_3", "click", changePage, [carousel, 2]);
	YAHOO.util.Event.addListener("tab2_4", "click", changePage, [carousel, 2]);
	YAHOO.util.Event.addListener("tab3_1", "click", changePage, [carousel, 3]);
	YAHOO.util.Event.addListener("tab3_2", "click", changePage, [carousel, 3]);
	YAHOO.util.Event.addListener("tab3_3", "click", changePage, [carousel, 3]);
	YAHOO.util.Event.addListener("tab3_4", "click", changePage, [carousel, 3]);
	YAHOO.util.Event.addListener("tab4_1", "click", changePage, [carousel, 4]);
	YAHOO.util.Event.addListener("tab4_2", "click", changePage, [carousel, 4]);
	YAHOO.util.Event.addListener("tab4_3", "click", changePage, [carousel, 4]);
	YAHOO.util.Event.addListener("tab4_4", "click", changePage, [carousel, 4]);
};

var stopAutoPlay = function(e) {

	carousel.stopAutoPlay();
};

/**
 * Illustrates start autoplay
 */
var startAutoPlay = function(e) {
	YAHOO.util.Dom.get("status").innerHTML = "Auto Play Started!";
	carousel.startAutoPlay(25000);
};


/**
 * 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);
YAHOO.util.Event.addListener("stop-button", 'click', stopAutoPlay);
YAHOO.util.Event.addListener("start-button", 'click', startAutoPlay);
YAHOO.util.Event.addListener("tab1_1", 'click', stopAutoPlay, [carousel, 1]);
YAHOO.util.Event.addListener("tab1_2", 'click', stopAutoPlay, [carousel, 1]);
YAHOO.util.Event.addListener("tab1_3", 'click', stopAutoPlay, [carousel, 1]);
YAHOO.util.Event.addListener("tab1_4", 'click', stopAutoPlay, [carousel, 1]);