
/**
 * 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]; 
    var searchStr = "getImage"; 
    // fetch twice the number for caching. images are create once.
    makeRequest(this, 'cPDFViewer.aspx', searchStr, start, (last-start) * 2);    
};

/**
 * 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];
    var searchStr = "getImage"; 
    if(!alreadyCached) {
        makeRequest(this, 'cPDFViewer.aspx', searchStr, 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];
    var searchStr = "getImage"; 
    if(!alreadyCached) {
        makeRequest(this, 'cPDFViewer.aspx', searchStr, start, (last-start+1) * 2);
    }
};
var getFilename = function() {
    
};
/**
 * 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 (leftImage){
	    if(enabling) {
		   leftImage.src = "images/left-enabled.gif";    
	    } else {
		   leftImage.src = "images/left-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;
var searchStr = "";
var pageLoad = function() 
{
    carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
        {
            numVisible:        3,
            animationSpeed:   .15,
            scrollInc:         3,
			navMargin:         55,
			size:              9,
            loadInitHandler:   loadInitialItems,
            prevElementID:     "prev-arrow",
            nextElementID:     "next-arrow",
            loadNextHandler:   loadNextItems,
            loadPrevHandler:   loadPrevItems,
            prevButtonStateHandler:   handlePrevButtonState
        }
    );

};
/**
 * 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="javascript: showImagePreview(\'' + result.Image.Url + '\');"><img src="' + 
          result.Image.Url +
        '" width="' +
        135 +
        '" border=0"/>' + 
          '</a>';
    
    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.
 */

/**
 * Just a utility function for cleaning up the returned HTML response
 * and truncating it.
 */
var trunc = function(str, maxLen, maxWordLen) 
{
    // Strip markup
    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;
};



function toggleDiv(div){
	var doc = document.getElementById(div);
	var display = doc.style.display
	if(display == "none"){
		doc.style.display = "block";
	}
	else {
		doc.style.display = "none";
	}
}
function openDiv(div){
	document.getElementById("subNav1").style.display = "none";
	document.getElementById("subNav2").style.display = "none";
	document.getElementById("subNav3").style.display = "none";
	document.getElementById("subNav4").style.display = "none";
	document.getElementById("subNav5").style.display = "none";
	var doc = document.getElementById(div);
	doc.style.display = "block";
}
function clearDiv(){
	document.getElementById("subNav1").style.display = "none";
	document.getElementById("subNav2").style.display = "none";
	document.getElementById("subNav3").style.display = "none";
	document.getElementById("subNav4").style.display = "none";
	document.getElementById("subNav5").style.display = "none";
}
function closeDiv(div){
	var doc = document.getElementById(div);
	var display = doc.style.display
		doc.style.display = "none";
}
var searchTrips = function(filename) {
 carousel.reload();
}
function returnObjById( id ) 
{ 
    if (document.getElementById) 
        var returnVar = document.getElementById(id); 
    else if (document.all) 
        var returnVar = document.all[id]; 
    else if (document.layers) 
        var returnVar = document.layers[id]; 
    return returnVar; 
} 

function launch(file,name,winwidth,winheight)      
{ 
var string="width="+winwidth+",height="+winheight+"toolbar=no,directories=no,menubar=no,resizable=yes,dependent=yes";
hwnd = window.open(file,name,string) ;                  
          if (navigator.appName == "Netscape") { 
             hwnd.focus(); 
          } 
}
