


/**
 * 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]; 

	//makeRequest(this, '/ajax/get_component.asp', component_type, start, 1);	
	if (numPages > 1) {	    
	    //makeRequest(this, '/ajax/get_component.asp', component_type, start, numPages);	
	}

	this.setProperty("size", numPages, true);
	showButtons();
};

/**
 * 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) {
		//makeRequest(this, '/ajax/get_component.asp', component_type, start, 1);
	}

};

/**
 * 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) {
		//makeRequest(this, '/ajax/get_component.asp', component_type, start, 1);
	}
};

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

var handleNextButtonState = function(type, args) {
	var enabling = args[0];
	var rightImage = args[1];
	
	if(enabling) {
		rightImage.src = "/images/buttons/right-enabled.gif";
	} else {
		rightImage.src = "/images/buttons/right-disabled.gif";
	}
	
};

var showButtons = function(type, args) {
	YAHOO.util.Dom.setStyle("next-arrow", "visibility", "visible");
	YAHOO.util.Dom.setStyle("prev-arrow", "visibility", "visible");
};


/**
 * 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.15,
			scrollInc:         1,
			loadInitHandler:   loadInitialItems,
			prevElement:       "prev-arrow",
			nextElement:       "next-arrow",
			loadNextHandler:   loadNextItems,
			loadPrevHandler:   loadPrevItems,			
			prevButtonStateHandler:   handlePrevButtonState,
			nextButtonStateHandler:   handleNextButtonState
		}
	);

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

/**
 * Called via the YUI Connection manager (see makeRequest).
 */
var handleSuccess = function(callbackResponse)
{
	var start = callbackResponse.argument[0];
	var numResults = callbackResponse.argument[1];
	var carousel = callbackResponse.argument[2];
		
  	if(callbackResponse.responseText !== undefined) {
  	    var results = callbackResponse.responseText.split("@@");
  	    for (var i=0; i < numResults; i++)
  	    {  	  	               
  	        carousel.addItem(start + i, results[i]);            
  	    }  	    
		showButtons();
     }
};



var handleFailure = function(o)
{
     var result = o.status + " " + o.statusText;
     alert("Transaction failed.  The error is: " + result);
};
  
/**
 * A utility function for invoking the YUI connection manager (Ajax)
 */
var makeRequest = function(carousel, url, query, start, numResults)
{
	var params = '?query=' + query + '&context=' + component_context +
	             '&start=' + start + 
	             '&results=' + numResults; 
	
	var callback =
	{
  		success: handleSuccess,
  		failure: handleFailure,
  		argument: [start, numResults, carousel]
	};
	
	var sUrl = url + params; 
	//alert(sUrl);
	YAHOO.util.Connect.asyncRequest("GET", sUrl, callback, null);
};