/**
 * Image src URLs
 */
var imageList = [
				"http://images6.fotki.com/v74/photos/3/379444/3867489/IMG_3379-th.jpg",
				"http://images14.fotki.com/v20/photos/3/379444/3867489/IMG_3380-th.jpg",
				"http://images18.fotki.com/v25/photos/3/379444/3867489/IMG_3381-th.jpg",
				"http://images18.fotki.com/v339/photos/3/379444/3867489/IMG_3382-th.jpg",
				"http://images18.fotki.com/v340/photos/3/379444/3867489/IMG_3383-th.jpg",
				"http://images14.fotki.com/v20/photos/3/379444/3867489/IMG_3386-th.jpg",
				"http://images14.fotki.com/v21/photos/3/379444/3867489/IMG_3388-th.jpg",
				"http://images14.fotki.com/v20/photos/3/379444/3867489/IMG_3390-th.jpg",
				"http://images18.fotki.com/v24/photos/3/379444/3867489/IMG_3391-th.jpg",
				"http://images7.fotki.com/v126/photos/3/379444/3867489/IMG_3392-th.jpg",
				"http://images9.fotki.com/v173/photos/3/379444/3867489/IMG_3393-th.jpg",
				"http://images18.fotki.com/v25/photos/3/379444/3867489/IMG_3396-th.jpg",
				"http://images18.fotki.com/v339/photos/3/379444/3867489/IMG_3404-th.jpg",
				"http://images18.fotki.com/v330/photos/3/379444/3867489/IMG_3407-th.jpg",
				"http://images12.fotki.com/v196/photos/3/379444/3867489/IMG_3411-th.jpg",
				"http://images14.fotki.com/v21/photos/3/379444/3867489/IMG_3418-th.jpg",
				"http://images14.fotki.com/v21/photos/3/379444/3867489/IMG_3421-th.jpg",
				"http://images14.fotki.com/v21/photos/3/379444/3867489/IMG_3422-th.jpg",
				"http://images18.fotki.com/v24/photos/3/379444/3867489/IMG_3425-th.jpg"
				 ];

var lastRan = -1;

var fmtItem = function(imgUrl, url, title) {

  	var innerHTML =
  		'<img src="' +
  		imgUrl +
		'" width="' +
		120 +
		'" height="' +
		90+
		'"/>';

	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 leftImage = args[1];
	if(enabling) {
		leftImage.src = "images/left-enabled.gif";
	} else {
		leftImage.src = "images/left-disabled.gif";
	}

};

var carousel;

/**
 * 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 pageLoad = function()
{
	carousel = new YAHOO.extension.Carousel("dhtml-carousel",
		{
			numVisible:        1,
			animationSpeed:   .10,
			scrollInc:         1,
			navMargin:         10,
			prevElementID:     "prev-arrow",
			nextElementID:     "next-arrow",
			loadInitHandler:   loadInitialItems,
			loadNextHandler:   loadNextItems,
			loadPrevHandler:   loadPrevItems,
			prevButtonStateHandler:   handlePrevButtonState,
			autoPlay: 5000,
			size:11,
			wrap:true
		}
	);
};

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