


var updateImage = function(image,title,summary)
{
   var myImage=document.getElementById('galleryImage');
   var myTitle=document.getElementById('photoTitle');
   var myDescription=document.getElementById('photoDescription');
   
   myImage.src=image;
//      Reflection.remove(myImage);
   //crossfade(myImage,image,1,title);

   myTitle.innerHTML=title;
   myDescription.innerHTML=summary;
//   window.setTimeout("",'100');
}


/**
 * 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]; 
	
	// fetch twice the number for caching. images are create once.
	
	makeRequest(this, 'getUsers.php','', start, (last-start+1) * 2);
	// Illustrates setting the size during the load. Set silent parameter to true
	
	
	//this.setProperty("size",ourSize, true);
};

/**
 * 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, 'getUsers.php','', start, (last-start+1) * 2);
	}

};

/**
 * 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, 'getUsers.php','',  start, (last-start+1) * 2);
	}
};

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

var handleNextButtonState = function(type, args) {
	var enabling = args[0];
	var rightImage = args[1];
	
	if(enabling) {
		rightImage.src = "images/Thumbs_RightArrow_OFF.gif";
	} else {
		rightImage.src = "images/Thumbs_RightArrow_OFF.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:        7,
			animationSpeed:    0.4,
			scrollInc:         7,
			loadInitHandler:   loadInitialItems,
			prevElement:       "prev-arrow",
			nextElement:       "next-arrow",
			loadNextHandler:   loadNextItems,
			loadPrevHandler:   loadPrevItems,
			prevButtonStateHandler:   handlePrevButtonState,
			nextButtonStateHandler:   handleNextButtonState
		}
	);

};

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 theTrip = eval( '(' + callbackResponse.responseText + ')' );
		
		for(var i=0; i< theTrip.ResultSet.totalResultsReturned; i++) {
			var result = theTrip.ResultSet.Result[i];
			
			carousel.addItem(start+i, fmtTripInnerHTML(result));
		}
		showButtons();
     }
};

/**
 * Since carousel.addItem uses an HTML string to create the interface
 * for each carousel item, this method takes an individual trip plan
 * result and cobbles together HTML for the innerHTML argument.
 */
var fmtTripInnerHTML = function(result) {

  /*	var tripInnerHTML = 
  		'<a href="' + 
  		result.Url + 
  		'"><img src="' + 
  		result.Image +
		'" width="' +
		75 +
		'" height="' +
		75+
		'"/>' + 
  		trunc(result.Title, 40, 20) + 
  		'<\/a>' + 
  		trunc(result.Summary, 20, 20);
  		*/
  	var tripInnerHTML=
  	'<a href="'+result.Link+'" alt="'+result.Title+'">'+
  		'<img border=0 src="' + 
  		result.Thumb +
		'" width="' +
		75 +
		'" height="' +
		75+
		'"/></a>'+'&nbsp;&nbsp;&nbsp;&nbsp;'
	return tripInnerHTML;
	
};

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)
 * with a URL that matches the Yahoo! developer network Trip Planner
 * APIs (see: http://developer.yahoo.com/travel/tripservice/V1/tripSearch.html)
 *
 * The callback object is the configuration object for the YUI Connection
 * manager. If this is successful, the 'handleSuccess' function is called.
 */
var makeRequest = function(carousel, url, query, start, numResults)
{
	var params = '?id=' + query + 
	                        '&start=' + start + 
	                        '&results=' + numResults; 
	
	var callback =
	{
  		success: handleSuccess,
  		failure: handleFailure,
  		argument: [start, numResults, carousel]
	};
	
	var sUrl = url + params; 
	YAHOO.util.Connect.asyncRequest("GET", sUrl, callback, null);
};

/**
 * Just a utility function for cleaning up the returned HTML response
 * and truncating it.
 */
var trunc = function(str, maxLen, maxWordLen) 
{
	// Strip markup
	if(!str)
	return false;
    str = str.replace("<b>", "");
    str = str.replace("<\/b>", "");
    str = str.replace("<B>", "");
    str = str.replace("<\/B>", "");
    
    // Simple truncation
	if(str.length > maxLen) {
		str = str.substring(0,maxLen) + "...";
	}

	// Truncate for long words
	var start = 0;
	var loopCnt = 0;
	var strSlice = str;
	
	do  {
		var spaceBreak = strSlice.indexOf(' ');
		var lenOfWord = spaceBreak;
		if(lenOfWord == -1)
		{
			lenOfWord = strSlice.length;
		}

		if (lenOfWord > maxWordLen) {
			//debugMsg("Long word found in: " + strSlice);
			str = str.substring(0, maxWordLen);  // TRUNCATE
		}
		start = spaceBreak+1;
		strSlice = strSlice.substring(start);
		spaceBreak = strSlice.indexOf(' ');
	} while(spaceBreak != -1)
	
	
	return str;
};
