/**
 * Image src URLs
 **/
var imageList = [
                 "images/thumb_1.jpg",
                 "images/thumb_2.jpg",
                 "images/thumb_3.jpg",
                 "images/thumb_4.jpg",
                 "images/thumb_5.jpg",
                 "images/thumb_6.jpg",
                 "images/thumb_7.jpg",
                 "images/thumb_8.jpg",
                 "images/thumb_9.jpg",
                 "images/thumb_10.jpg",
                 "images/thumb_11.jpg",
                 "images/thumb_12.jpg",
                 "images/thumb_13.jpg",
                 "images/thumb_14.jpg",
                 "images/thumb_15.jpg",
                 "images/thumb_16.jpg",
                 "images/thumb_17.jpg",
                 "images/thumb_18.jpg",
				 "images/thumb_19.jpg",
				 "images/thumb_20.jpg",
				 "images/thumb_21.jpg",
				 "images/thumb_22.jpg",
				 "images/thumb_23.jpg",
				 "images/thumb_24.jpg",
				 "images/thumb_25.jpg",
			     "images/thumb_26.jpg",
				 "images/thumb_27.jpg",
				 "images/thumb_28.jpg",																																						                    					                 "images/thumb_29.jpg"
                 ];

var lastRan = -1;

/**
 * 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(imgUrl, url, title) {

      var innerHTML = 
          '<a href="' + 
          url + 
          '"><img src="' + 
          imgUrl +
        '" width="' +
        75 +
        '" height="' +
        75+
        '"/>' + 
          title + 
          '<\/a>';
    return innerHTML;
    
};

/**
 * 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++) {
        var randomIndex = getRandom(18, lastRan);
        lastRan = randomIndex;
        carousel.addItem(i, fmtItem(imageList[randomIndex], "#", "Number " + i));
    }
};

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 upImage = args[1];

    if(enabling) {
        upImage.src = "./data/up-enabled.gif";
    } else {
        upImage.src = "./data/up-disabled.gif";
    }
    
};
var carousel; // for ease of debugging; globals generally not a good idea
var pageLoad = function() 
{
    carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
        {
            numVisible:        5,
            animationSpeed:    0.55,
            scrollInc:         5,
            orientation:       "vertical",
            navMargin:         0,
            loadInitHandler:   loadInitialItems,
            prevElement:     "up-arrow",
            nextElement:     "down-arrow",
            loadNextHandler:   loadNextItems,
            loadPrevHandler:   loadPrevItems,
            prevButtonStateHandler:   handlePrevButtonState
        }
    );
};

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