/** Our function that initializes when the page
    is finished loading. */
var pagerType = "favJourneys";
function initializePagers()
{
   	// initialize the DHTML History framework
   	dhtmlHistory.initialize();

   	// add ourselves as a DHTML History listener
   	dhtmlHistory.addListener(handleHistoryChange);

	// if this is the first time we have
	// loaded the page...
	if (dhtmlHistory.isFirstLoad()) {
		//  Nothing to do in this situation...
	}
	
   	// Determine what our initial location is by retrieving it from the browser's
   	// location (after the # in the URL)...may have been passed via a stored bookmark
   	var currentLocation = 
   		dhtmlHistory.getCurrentLocation();
      
   	// if there is no location then display the default
   	if (currentLocation == "")
   		currentLocation = "pgOffset:0;pagerType:favJourneys";   
   		
   	processLocationUpdate(currentLocation);
}

// 	Handles history (back/forward) events from dhtmlHistory for Ajax 
function handleHistoryChange(newLocation, historyData) 
{
   	if (newLocation == "")
   		newLocation = "pgOffset:0;pagerType:favJourneys";   
	processLocationUpdate(newLocation); 
}

// Updates this page's Ajax settings.  newLocation argument must be of the format: "pgOffset:0;pagerType:favJourneys"
function processLocationUpdate(newLocation) 
{
   	// extract the offsets to display from
   	var startIndex = newLocation.indexOf(";");
   	pgOffset   = newLocation.substring(9, startIndex);

	if (pgOffset != parseInt(pgOffset)) // ensure we have an integer
		pgOffset = 0;

	var startIndex = newLocation.indexOf("pagerType");
	if ( startIndex >= 0) {
		var endIndex = newLocation.length;
		pagerType = newLocation.substring(startIndex+10, endIndex);
	}
	if ( pagerType != "favJourneys" && pagerType != "submittedReviews")
		pagerType = "pgOffset";
	
   // display this initial location
   updatePager();
} 

function showPagerType(newPagerType) {
	pagerType = newPagerType;
	showPagerOffset(0);
}

//  This function is called when the user clicks to update the Pager.
var pgOffset = 0;
function showPagerOffset(offset,isScrollToTop) {
	// Register a history event so this action gets recorded in the browser; Internet
	// Explorer has a bug that prevents us from setting a location value if there
	// are ANY HTML elements in the document that have the same ID already. For example,
	// if we tell dhtmlHistory to add the location 'mainPanel' to our browser's history, and there
	// is an element in the document with the ID 'mainPanel', then Internet Explorer will 
	// misbehave. We must accomodate this problem in IE by changing the location so it does
	// not match a pre-existing HTML id
	pgOffset = offset; 
   	var modifiedLocation = "pgOffset:" + pgOffset + ";pagerType:" + pagerType;
   	var historyData = new Object(); // currently store nothing in history
	dhtmlHistory.add(modifiedLocation, historyData);
  	updatePager();
  	
	// And lastly, scroll the webpage to the top if requested.
	// Typically requested in case the user clicked the bottom pager navigation buttons
	if ( isScrollToTop == "true") {
		window.scroll(0,0);
	}
}

// 	Loads Favorite Journeys selection from the server
//  Offset will be used by pg:pager to display the correct page.  Assumed to be zero offset if not present
function updatePager() 
{
	//  First validate that we are dealing with a real number
	if (pgOffset != parseInt(pgOffset))
		pgOffset = 0;

	var action = "ajaxGetFavoriteJourneysPage.do";
	if ( pagerType == "submittedReviews") {
		action = "ajaxGetReviewedJourneysPage.do";
	}

	var params  = "pagerOffset=" + pgOffset; // The pg:pager offset

	displayRelativeAjaxWarning("memberHomePager", "Loading Tours");

	new Ajax.Updater(
		"memberHomePager", 
		action, 
		{
			parameters : params,
			method : "post",
	 		onFailure : function() {
				alert("An has error occurred while processing your request.\n\nPlease refresh your page and try again.");
				}
		}
	);
	
	// And lastly, scroll the webpage to the top in case the user clicked the bottom pager navigation buttons
	window.scroll(0,0);
} 

function updateSelectedTab(newSelTab)
{
	var favsTab    = $("favJourneysTab");
	var reviewsTab = $("submittedReviewsTab");

	var newPagerType;
	if ( newSelTab == "favJourneysTab") {
		favsTab.className="selectedTab";
		reviewsTab.className="";
		newPagerType = "favJourneys";
	} else { //if ( newSelTab == "submittedReviewsTab")
		favsTab.className="";
		reviewsTab.className="selectedTab";	
		newPagerType = "submittedReviews";
	}
	
	showPagerType(newPagerType);
}
