// init scrolling pagination
function init_scrolling_pagination(page) {
	// which directory?
	if(body_class == 'directory church') {
		page_height  = 510;
	}
	
	// make sure page is an integer
	page = parseInt(page);
	
	// get total pages
	if ($("#total_pages")[0]) {
		var total_pages = $("#total_pages")[0].firstChild.nodeValue;
		var prev_page = page - 1;
		var next_page = page + 1;
		
		// keep it within the total number of pages
		if (prev_page < 1) prev_page = 1;
		if (next_page >= total_pages) next_page = total_pages;
		
		
		$("#prev").unbind("click");
		$("#prev").bind("click", function(e){
			scrollTableToPage(prev_page);
			return false;
		});
		$("#next").unbind("click");
		$("#next").bind("click", function(e){
			scrollTableToPage(next_page);
			return false;
		});
	}
}

// scroll table to page
var page_height  = 720; // px, default for council directory
var current_page = 1;
function scrollTableToPage(page) {
	var distance = (current_page - page) * page_height;
	var to = (distance < 0) ? "-=" + -distance + "px" : "+=" + distance + "px";
	
	// scroll it!
	$("#scrolling_table").animate({
			"top" : to
		},
		1000,
		"easeInOutCirc"
	);
	
	// update current link
	$("#p" + current_page).removeClass('current');
	$("#p" + page).addClass('current');
	
	// update prev and next links
	init_scrolling_pagination(page);
	
	// update current page
	current_page = page;
	
	return false;
}
 
// on load
var body_class       = '';
var body_class_regex = '';
$(document).ready(function(){
	body_class       = $("body").attr('class');
	body_class_regex = '/' + body_class + '/';
	init_scrolling_pagination(1);
});
