/**
 * Image src URLs
 */

var PromotionBannerItemList = [
 							{Image: 'images/banner/c_gclub.png', Title: 'Gclub Casino', Link: "gclub.html"}, 
							{Image: 'images/banner/c_royalruby888.png', Title: 'Royal Ruby888 Casino', Link: "ruby888.html"}, 
							{Image: 'images/banner/c_royal1688.png', Title: 'Royal1688 คาสิโน', Link: "royal1688.html"}, 
							{Image: 'images/banner/c_holidaypalace.png', Title: 'Holiday Palace', Link: "holiday_palace.html"}, 
							{Image: 'images/banner/c_gentingcrown.png', Title: 'Genting Crown', Link: "genting_club.html"}, 
							{Image: 'images/banner/c_reddragon.png', Title: 'Reddragon88 คาสิโน', Link: "reddragon88.html"}, 
							{Image: 'images/banner/s_sbobet.png', Title: 'Sbobet', Link: "sbobet.html"}, 
							{Image: 'images/banner/s_ibcbet.png', Title: 'Ibcbet', Link: "ibcbet.html"}, 
							{Image: 'images/banner/s_3m.png', Title: '3M', Link: "3m.html"}
						]; 

var lastRan = -1;

var fmtItem = function(imgItem, index) {

  	var innerHTML = 
  		'<a id="dhtml-carousel-a-'+index+'" href="' + 
  		imgItem.Link + 
  		'"><img id="dhtml-carousel-img-' + index + '" src="' + 
  		imgItem.Image +
		'" width="' +
		166 +
		'" height="' +
		112+
		'" title="'+imgItem.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 liItem = carousel.addItem(i, fmtItem(PromotionBannerItemList[i], i));
/*
		// If you want to add handlers for mouse over, mouse out or other events,
		// here is the pattern
		YAHOO.util.Event.addListener(liItem, "mouseover", function(e) {
		//console.log("mouseover>>"+this.id);
		});
		YAHOO.util.Event.addListener(liItem, "mouseout", function(e) {
			//console.log("mouseout<<"+this.id);
		});
*/
	}
}

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

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() 
{
	carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
		{
			numVisible:        4,
			animationSpeed:   0.7,
			scrollInc:         3,
			navMargin:         10,
			prevElement:     "prev-arrow",
			nextElement:     "next-arrow",
			loadInitHandler:   loadInitialItems,
			loadNextHandler:   loadNextItems,
			loadPrevHandler:   loadPrevItems,
			prevButtonStateHandler:   handlePrevButtonState,
			autoPlay: 3000,
			size:8,
			wrap:true
		}
	);
	

};

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