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) {

};


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 = "http://billwscott.com/carousel/images/down-enabled.gif";
	} else {
		upImage.src = "http://billwscott.com/carousel/images/down-disabled.gif";
	}
	
};

var handleNextButtonState = function(type, args) {

	var enabling = args[0];
	var downImage = args[1];

	if(enabling) {
		downImage.src = "http://billwscott.com/carousel/images/up-enabled.gif";
	} else {
		downImage.src = "http://billwscott.com/carousel/images/up-disabled.gif";
	}
	
};
var carousel;
var pageLoad = function() 
{
	var ulObj = document.getElementById("myCarouselList");
	
	if(ulObj != null) {
		var liArr = ulObj.getElementsByTagName("li");

		carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
			{
				numVisible:        3,
				animationSpeed:    1.5,
				scrollInc:         1,
				orientation:       "vertical",
				navMargin:         0,
				size:				liArr.length,
				loadInitHandler:   loadInitialItems,
				prevElement:     "up-arrow",
				nextElement:     "down-arrow",
				prevButtonStateHandler:   handlePrevButtonState,
				nextButtonStateHandler:   handleNextButtonState,
				autoPlay: 5000,
				wrap: true
			}
		);
	}
};

YAHOO.util.Event.addListener(window, 'load', pageLoad);