var lastRan = -1;

/**
 * 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);		
	// Effacement du picto loadin après chargement
	Dom.setStyle('loading', 'display', 'none');
};

/**
 * 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++) {		
		carousel.addItem(i, fmtItem(newsList[i-1],i));
	}	
}

var getRandom = function(max, last) {
	var randomIndex;
	do {
		randomIndex = Math.floor(Math.random()*max);
	} while(randomIndex == last);
	
	return randomIndex;
}

var stopAutoSroll = function () {	
	carousel.stopAutoPlay();
	
}

var setAutoSroll = function () {	
	carousel.startAutoPlay(5000);
}

/**
 * You must create the carousel after the page is loaded since it is
 * dependent on an HTML element (in this case 'defilant'.) See the
 * HTML code below.
 **/
var carousel; 
var carouselLoad = function() 
{
	if (defilantSize != 0 ) {		
		var firstItem = nbItemsForOneSerie * 4 + 1;							
		carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
			{
				numVisible:        2,
				size : defilantSize,
				firstVisible : 1,				
				animationSpeed:    0.35,
				scrollInc:         2,				
				autoPlay:         5000,
				wrap:             true,
				orientation:     "vertical",
				prevElement:     "up-arrow",
				nextElement:     "down-arrow",			
				loadInitHandler:   loadInitialItems,
				loadNextHandler:   loadNextItems,
				loadPrevHandler:   loadPrevItems				
			}
		);					
				
		// Comportements de blocage au survol de la souris
		var defilant = document.getElementById('defilant');		
		YAHOO.util.Event.addListener(defilant, 'mouseover', stopAutoSroll);
		YAHOO.util.Event.addListener(defilant, 'mouseout', setAutoSroll);	
		
	}
}

/**
 * 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(newsItem, i) {
	try {
  		var innerHTML = '';
  		if (newsItem.displayType == 'urgent') {
  	 		innerHTML += '<span class="urgent">';
  		}
  	
  		// Surtitre
  		innerHTML += '<strong>' + newsItem.surtitre + '</strong>' + ' ';    	
  		if (newsItem.libelle != '' && newsItem.libelle != undefined) {	  			
  			if (newsItem.url != '' && newsItem.url != undefined) { 
  				// Libelle lien
  				innerHTML +=  '<a href="' + Remplace(newsItem.url, '"',"'") + '" title="' + newsItem.libelle + '">' + newsItem.libelle + '</a>'; 
  			}
  			else { 
  				// Libelle simple
  				innerHTML +=  '<a class="nodecoration">' + newsItem.libelle + '</a>';
  			}	
  		}
  		if (newsItem.displayType == 'urgent') {
  		innerHTML += '</span>';
  		}
  		
		return innerHTML;
	}
	catch (e) {
		// Levée de l'erreur
		//throw(e);
		return '';
	}
}
