/**
 * 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) {
		leftImage.src = "images/carousel/controls/bck.png";	
	} else {
		leftImage.src = "images/carousel/controls/bck2.png";	
	}
	
};

/**
 * 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) {
		rightImage.src = "images/carousel/controls/fwd.png";
	} else {
		rightImage.src = "images/carousel/controls/fwd2.png";
	}
	
};


            
/**
 * 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[2];
    
    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(tabPages[i-1]));
    }
};

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

/**
 * 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("mycarousel", 
        {
			numVisible:        1, //how many divs visible at a time
			animationSpeed:    1.2,
			animationMethod:    YAHOO.util.Easing.backBoth,
			scrollInc:         1, //how many the scroll increases by
			navMargin:         0, //margin on content inside list
			prevElement:     "prev-arrow",
			nextElement:     "next-arrow",
			size:              3, //increase for more divs
			revealAmount:		0,//in pixels
			orientation:		"horizontal",
	//		autoPlay:			8000, // auto play speed in milliseconds
			wrap:				false, //for if the content keeps on rolling
			prevButtonStateHandler:   handlePrevButtonState,
			nextButtonStateHandler:   handleNextButtonState,
            loadInitHandler:   loadInitialItems,
            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]);
};

/**
 * Since carousel.addItem uses an HTML string to create the interface
 * for each carousel item, this method formats the HTML for an LI.
 **/
var fmtItem = function(tabPage) {

      var innerHTML = 
          '<img src="' + 
          tabPage + 
          '"/>';
  
    return innerHTML;
    
};


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