var lastRan = -1;

var fmtItem = function(imgUrl, url, title, desc, index) {
  	var innerHTML = '<div class="rotate_news"><div class="img_rotate"><img id="dhtml-carousel-img-' + index + '" src="' + imgUrl +'" width="' + 262 + '" height="' + 103+'"/></div>';
	innerHTML = innerHTML + '<div class="text_rotate"><div class="title"><a id="dhtml-carousel-a-'+index+'" href="' + url + '">' + title + '</a></div><div class="sumary">'+desc+'</div></div>';
	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 liItem = carousel.addItem(i, fmtItem(imageList[(i-1)], linkList[(i-1)], titleList[(i-1)], descList[(i-1)] , i));
	}
}

/**
 * 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 = "/web/theme/common/images/left-enabled.png";	
	} else {
		leftImage.src = "/web/theme/common/images/left-disabled.png";
	}
	
};

var carousel; // for ease of debugging; globals generally not a good idea

/**
 * 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() {
	if (__slide__size__) {
		if (__slide__size__ > 0) {
		carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
			{
				numVisible:			1,
				animationSpeed:		0.7,
				scrollInc:			1,
				navMargin:			10,
				prevElement:		"prev-arrow",
				nextElement:		"next-arrow",
				loadInitHandler:	loadInitialItems,
				loadNextHandler:	loadNextItems,
				loadPrevHandler:	loadPrevItems,
				prevButtonStateHandler:   handlePrevButtonState,
				autoPlay: 3000,
				size:__slide__size__,
				wrap:true
			}
		);
	}
	}
};



/**
 * Illustrates stop autoplay
 */
var stopAutoPlay = function(e) {
	YAHOO.util.Dom.get("status").innerHTML = "Auto Play Stopped!";
	carousel.stopAutoPlay();
};

/**
 * Illustrates start autoplay
 */
var startAutoPlay = function(e) {
	YAHOO.util.Dom.get("status").innerHTML = "Auto Play Started!";
	carousel.startAutoPlay(4000);
};

YAHOO.util.Event.addListener(window, 'load', pageLoad);

Event.observe(window,'load',function(){
	if ($('dhtml-carousel')) {
		
	}
	if ($('prev-arrow')) {
		$('prev-arrow').setStyle({'z-index':'1'});
	}
	if ($('next-arrow')) {
		$('next-arrow').setStyle({'z-index':'1'});
	}
	if ($('dhtml-carousel')) {
		$('dhtml-carousel').observe('mouseover', function(){
			carousel.stopAutoPlay();
		});
		$('dhtml-carousel').observe('mouseout', function(){
			carousel.startAutoPlay(4000);
		});
	}
});
