 /** AJAX FUNCTIONS **/
       var loadInitialItems = function(type, args) {
    var start = args[0];
    var last = args[1]; 
    // fetch twice the number for caching. images are create once.
    makeRequest(this, 'http://poynterlandscape.com/galleries/gallery-ajax/', galleryCategory, start, 1);
 	makeRequest(this, 'http://poynterlandscape.com/galleries/gallery-ajax/', galleryCategory, start + 1, 1);
    makeRequest(this, 'http://poynterlandscape.com/galleries/gallery-ajax/', galleryCategory, start + 2, 1);
};
 

/*** make the AJAX Request   **/
var makeRequest = function(carousel, url, category, start, numResults)
{         
	//gallery frame DS page takes the category
    var params =  category + 
                            '/' + start; 
    var callback =
    {
          success: handleSuccess,
          failure: handleFailure,
          argument: [start, numResults, carousel]
    };
    var sUrl = url + params; 
   //alert('requesting: ' + sUrl )
    YAHOO.util.Connect.asyncRequest("GET", sUrl, callback, null);
};
 		


//On a successful request
  var handleSuccess = function(callbackResponse)
{
    var start = callbackResponse.argument[0];
    var numResults = callbackResponse.argument[1];
    var carousel = callbackResponse.argument[2];

      if(callbackResponse.responseText !== undefined) {
        //var theFrame = eval( '(' + callbackResponse.responseText + ')' );
		var theFrame =  callbackResponse.responseText  
       // for(var i=0; i< theFrame.ResultSet.totalResultsReturned; i++) {
       //            var result = theFrame.ResultSet.Result[i];  // may have to construct this differently
       // 			carousel.addItem(start+i, callbackResponse.responseText);
       //        }  
    carousel.addItem(start, callbackResponse.responseText);
	   
		Shadowbox.setup();
        //showButtons();
     }
};


// On a failed request   
 var handleFailure = function(o)
{
     var result = o.status + " " + o.statusText;
  //alert("Transaction failed.  The error is: " + result);
};

// load next frame 
/**
 * 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];
    makeRequest(this, 'http://poynterlandscape.com/galleries/gallery-ajax/', galleryCategory, start + 2, 1);
  

};  

 // load previous frame
/**
 * 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, 'http://poynterlandscape.com/galleries/gallery-ajax/', galleryCategory, start + 2, 1);
    }
};
	  
/*/    END AJAX FUNCTIONS    



 * 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];
	var staticPrev = leftImage[0];
	if(enabling) {
		staticPrev.innerHTML = "previous";	
	} else {
		staticPrev.innerHTML = "";	
	}

};

/**
 * 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];
    var staticNext = rightImage[0];

	if(enabling) {
		staticNext.innerHTML = "next";
	} else {
		staticNext.innerHTML = "";
	}

};


/**
 * You must create the carousel after the page is loaded since it is
 * dependent on an HTML element (in this case 'mycarousel'.) 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("mycarousel", 
		{   wrap:			   false,
			numVisible:        1,
			animationSpeed:    0.5,
			scrollInc:         1,
			navMargin:         1, 
			loadInitHandler:   loadInitialItems,
		    loadNextHandler:   loadNextItems,
		    loadPrevHandler:   loadPrevItems,
			prevElement:    ["prev-arrow-static", "prev-arrow1", "prev-arrow2", "prev-arrow3", "prev-arrow4", "prev-arrow5", "prev-arrow6", "prev-arrow7"],
			nextElement:    ["next-arrow-static", "next-arrow1", "next-arrow2", "next-arrow3", "next-arrow4", "next-arrow5", "next-arrow6", "next-arrow7"],
			size:              frameCount,
			prevButtonStateHandler:   handlePrevButtonState,
			nextButtonStateHandler:   handleNextButtonState
		}
	);
   	 

};

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