/**
 * Image src URLs
 **/
var imageList = [
                 "http://static.flickr.com/116/268249561_a32393f6ac_s.jpg",
                 "http://static.flickr.com/97/268249177_d0f53c75ae_s.jpg",
                 "http://static.flickr.com/91/268249048_67b9ecb504_s.jpg",
                 "http://static.flickr.com/101/268248936_9cbaffeb08_s.jpg",
                 "http://static.flickr.com/96/268248678_24c0a48e71_s.jpg",
                 "http://static.flickr.com/106/268248583_f781f44205_s.jpg",
                 "http://static.flickr.com/106/268248481_ca3ee38e1e_s.jpg",
                 "http://static.flickr.com/113/268248145_b12fbb7413_s.jpg",
                 "http://static.flickr.com/106/268248063_3aa0e9f801_s.jpg",
                 "http://static.flickr.com/85/268247010_f9e4e0d0a2_s.jpg",
                 "http://static.flickr.com/92/268246360_cd1dc9269e_s.jpg",
                 "http://static.flickr.com/114/268246264_150b9f97a5_s.jpg",
                 "http://static.flickr.com/84/268245733_bdf9efb167_s.jpg",
                 "http://static.flickr.com/86/268245514_0eac3b5e2d_s.jpg",
                 "http://static.flickr.com/96/268245042_76622df01e_s.jpg",
                 "http://static.flickr.com/92/268244677_a45258db84_s.jpg",
                 "http://static.flickr.com/92/268244474_2f3393ee8c_s.jpg",
                 "http://static.flickr.com/105/268244323_e5f0fa4103_s.jpg"
                 ];
var urlList = [
                 "http://static.flickr.com/116/268249561_a32393f6ac_b.jpg",
                 "http://static.flickr.com/97/268249177_d0f53c75ae_b.jpg",
                 "http://static.flickr.com/91/268249048_67b9ecb504_b.jpg",
                 "http://static.flickr.com/101/268248936_9cbaffeb08_b.jpg",
                 "http://static.flickr.com/96/268248678_24c0a48e71_b.jpg",
                 "http://static.flickr.com/106/268248583_f781f44205_b.jpg",
                 "http://static.flickr.com/106/268248481_ca3ee38e1e_b.jpg",
                 "http://static.flickr.com/113/268248145_b12fbb7413_b.jpg",
                 "http://static.flickr.com/106/268248063_3aa0e9f801_b.jpg",
                 "http://static.flickr.com/85/268247010_f9e4e0d0a2_b.jpg",
                 "http://static.flickr.com/92/268246360_cd1dc9269e_b.jpg",
                 "http://static.flickr.com/114/268246264_150b9f97a5_b.jpg",
                 "http://static.flickr.com/84/268245733_bdf9efb167_b.jpg",
                 "http://static.flickr.com/86/268245514_0eac3b5e2d_b.jpg",
                 "http://static.flickr.com/96/268245042_76622df01e_b.jpg",
                 "http://static.flickr.com/92/268244677_a45258db84_b.jpg",
                 "http://static.flickr.com/92/268244474_2f3393ee8c_b.jpg",
                 "http://static.flickr.com/105/268244323_e5f0fa4103_s.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], urlList[randomIndex], "Number " + i));
/*
        // Example of an alternate way to add an item (passing an element instead of html string)
        var p = document.createElement("P");
        var t = document.createTextNode("Item"+i);
        p.appendChild(t);
        carousel.addItem(i, p );
*/
    }
};

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 = "images/left-enabled.gif";    
    } else {
        leftImage.src = "images/left-disabled.gif";
    }
    
};

/**
 * 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:        4,
            animationSpeed:    0.25,
            scrollInc:         3,
            navMargin:         40,
            prevElement:       "prev-arrow",
            nextElement:       "next-arrow",
            loadInitHandler:   loadInitialItems,
            loadNextHandler:   loadNextItems,
            loadPrevHandler:   loadPrevItems,
            prevButtonStateHandler:   handlePrevButtonState
        }
    );
};

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

