/**
 * 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);	
	var status = " " + start + " / " + this.getProperty("size");
	YAHOO.util.Dom.get("indicator").innerHTML = status;
};

var animComplete = function(type, args) {
	var direction = args[0]; // string either: 'next' or 'prev'
	
	var status = " " + this.getFirstVisible() + " / " + this.getProperty("size");
	YAHOO.util.Dom.get("indicator").innerHTML = status + " ";

};

/**
 * 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) {
 

last = titleList.length;

  var url = "http://webstaging.zu.ac.ae/cascade";
  var virdir = "/cascade";
  var fileext = ".aspx";
	if (location.href.indexOf("www.zu.ac.ae") > -1){
		url = "http://www.zu.ac.ae/main";
		virdir = "/main";
	}
	else if (location.href.indexOf("localhost") > -1){
		url = "http://localhost/zu3/";
		virdir = "/zu3";
	}
	
	
	if (location.href.indexOf("/ar/") > -1){
		fileext = ".asp";
	}
	
	for(var i=start; i<= last+1; i++) {
		
	 	
		var value = "<div class=\"img\"><a href=\"" + virdir + urlList[i-1] + fileext + "\" class=\"image\"><img src=\"" + url + pictureList[i-1] + "\" border=\"0\" alt=\"\" name=\"\"  />    </a> </div><div class=\"bottom\"><h3>" +  titleList[i-1] +"</h3><a href=\"" +  virdir +  urlList[i-1] + fileext + "\">" + captionList[i-1] +"</a></div>	"
 
		carousel.addItem(i, value);
	}
}

/**
 * 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";
	}
	
};

/**
 * Custom button state handler for enabling/disabling button state. 
 * Called when the carousel has determined that the next button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: nextButtonStateHandler
 **/
var handleNextButtonState = function(type, args) {

	var enabling = args[0];
	var rightImage = args[1];
	if(enabling) {
		rightImage.src = "images/right-enabled.gif";
	} else {
		rightImage.src = "images/right-disabled.gif";
	}
	
};

/**
 * 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 carousel; // for ease of debugging; globals generally not a good idea

var pageLoad = function() 
{
	carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
		{
			numVisible:        1,
			animationSpeed:   0.35,
			scrollInc:         1,
			//animationMethod:   YAHOO.util.Easing.backOut,
			navMargin:         0,
			size:         	 titleList.length,
			prevElement:     "left-arrow",
			nextElement:     "right-arrow",
			loadInitHandler:   loadInitialItems,
			loadNextHandler:   loadNextItems,
			loadPrevHandler:   loadPrevItems,
			animationCompleteHandler: animComplete,
			prevButtonStateHandler:   handlePrevButtonState,
			nextButtonStateHandler:   handleNextButtonState
		}
	);

};

/**
 * 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) {
	var innerHTML = imgUrl;
  	return innerHTML;
};

YAHOO.util.Event.addListener(window, 'load', pageLoad);

 