/**
 * Custom inital load handler. Called when the scroller loads the initial
 * set of data items. Specified to the scroller 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 scroller loads the next
 * set of data items. Specified to the scroller 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 scroller loads the previous
 * set of data items. Specified to the scroller 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(scroller, start, last) {
    for(var i=start;i<=last;i++) {
        scroller.addItem(i, fmtItem(newsArticleList[i-1]));
    }
}

var getRandom = function(max, last) {
    var randomIndex;
    do {
        randomIndex = Math.floor(Math.random()*max);
    } while(randomIndex == last);

    return randomIndex;
}

/**
 * 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 = "interface/scrollarrow_left.gif";
    } else {
        leftImage.src = "interface/scrollarrow_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) {
        rightImage.src = "interface/scrollarrow_right.gif";
    } else {
        rightImage.src = "interface/scrollarrow_right_disabled.gif";
    }

};

/**
 * Custom button state handler for enabling/disabling button state.
 * Called when the scroller has determined that the previous button
 * state should be changed.
 * Specified to the scroller as the configuration
 * parameter: upButtonStateHandler
 **/
var handleUpButtonState = function(type, args) {

    var enabling = args[0];
    var topImage = args[1];
    if(enabling) {
        topImage.src = "interface/scrollarrow_up.gif";
    } else {
        topImage.src = "interface/scrollarrow_up_disabled.gif";
    }

};

/**
 * Custom button state handler for enabling/disabling button state.
 * Called when the scroller has determined that the next button
 * state should be changed.
 * Specified to the scroller as the configuration
 * parameter: downButtonStateHandler
 **/
var handleDownButtonState = function(type, args) {

    var enabling = args[0];
    var botImage = args[1];

    if(enabling) {
        botImage.src = "interface/scrollarrow_down.gif";
    } else {
        botImage.src = "interface/scrollarrow_down_disabled.gif";
    }

};

var carousel;
var scroller;

/**
 * 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 pageLoad = function()
{
    carousel = new YAHOO.extension.Carousel("mycarousel",
        {
            numVisible:						4,
            animationSpeed:				.15,
            scrollInc:							4,
            navMargin:						0,
			orientation:						"horizontal",
            prevElement:						"prev-arrow",
            nextElement:						"next-arrow",
            size:									8,
            prevButtonStateHandler:		handlePrevButtonState,
            nextButtonStateHandler:		handleNextButtonState
        }
    );
	scroller = new YAHOO.extension.Carousel("myscroller",
        {
            numVisible:						4,
            animationSpeed:				.15,
            scrollInc:							4,
            navMargin:						0,
			orientation:						"vertical",
            prevElement:						"up-arrow",
            nextElement:						"down-arrow",
            size:									6,
			loadInitHandler:					loadInitialItems,
			loadNextHandler:				loadNextItems,
            loadPrevHandler:				loadPrevItems,
            prevButtonStateHandler:		handleUpButtonState,
            nextButtonStateHandler:		handleDownButtonState
        }
    );
}

YAHOO.util.Event.addListener(window, 'load', pageLoad);

/**
 * 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(newsItem) {

      var innerHTML =
          '<a href="' +
          newsItem.url +
          '">' +
          newsItem.title +
          '</a>';

    return innerHTML;

}