/**
 * Sexy Javascript pagination and ordering for tables using jQuery,
 * the pagination plugin and custom page ordering.
 *
 * Also falls back well to standard HTML for non-JS browsers
 */

// Track our page variables through AJAX reloads...
$.pageVariables = {};
$.pageVariables.totalItems;
$.pageVariables.order;
$.pageVariables.direction;
$.pageVariables.page;
$.pageVariables.initialUrl;

/**
 * Load a page into the AJAX container
 */
function pageOrderCallback(page, order, direction) {
	$.pageVariables.order = order;
	$.pageVariables.direction = direction;
	$.pageVariables.page = page;

	// On the first page load, don't scroll the window
	// On subsequent page loads, scroll the window to the top of the page content
	/*if($.pageVariables.firstPageLoad == true) {
		$.pageVariables.firstPageLoad = false;
	} else {
		window.scrollTo(0, 250);
	}*/
	
	url = $.pageVariables.initialUrl + "?order=" + order + "&direction=" + direction;
	
	// Support Type param (used by Venues)
	if($.pageVariables.type > 0) {
		url = url + "&type=" + $.pageVariables.type;
	}
	
	//alert(url + "&format=html&page=" + page);

	$("#ajaxContent").load(url + "&format=html&page=" + page, function() { buildPagination(url); initTable(); } );
	
	return false;
}
			
/**
 * Callback function called by Pagination plugin
 */
function pageSelectCallback(page, jq){
	// Pagination links start at 0, database fetch starts at 1
	// Accomodate this off-by-one here.
	$.pageVariables.page = page+1;
	
	pageOrderCallback($.pageVariables.page, $.pageVariables.order, $.pageVariables.direction);
					
	return false;
}

function initTable() {
	// Disable clicks on the thead and rely on the onclick event
    // handler instead. This means that old/non-JS browsers will still
    // be able to fall back to simple clicks
	$("thead a").click(function() {
    	return false; 
	})
	
	// Hide featured and page copy unless we're on the landing page
	/*if($.pageVariables.page > 1) {
		if($.pageVariables.doInnerSlide == true) {
			$("#frontContent").slideUp(750);
			$.pageVariables.doInnerSlide = false;
			$.pageVariables.frontContentHidden = true;
		} else {
			$("#frontContent").hide();
			$.pageVariables.frontContentHidden = true;
		}		
		
		$.pageVariables.doFrontSlide = true;
	} else {
		if($.pageVariables.doFrontSlide == true && $.pageVariables.frontContentHidden == true) {
			$("#frontContent").slideDown(750);
			// Scroll window to top so user can easily access navigation links
			// and get out of events section
			window.scrollTo(0, 0);
		} else {
			$("#frontContent").show();
			$.pageVariables.frontContentHidden = false;
		}
		$.pageVariables.doInnerSlide = true;
	}*/
	
}
         
function buildPagination(url) {
	// Pagination starts at 0, database fetching starts at 1
	// so accomodate this off by one issue here.
	page = $.pageVariables.page > 0 ? $.pageVariables.page : 1;

	// Create pagination element
	$("#paginationLinks").pagination($.pageVariables.totalItems, {
    	callback: pageSelectCallback,
        current_page: (page-1),
        link_to: url,
        next_text: "next &raquo;",
        next_show_always: false,
        prev_text: "&laquo; prev",
        prev_show_always: false
	});
}
