var iTotalNumberOfJobs = 0;

var iPreSetPageIndex = $.query.get('preSetPageIndex');
if (iPreSetPageIndex == "") {
	iPreSetPageIndex = 0;
}

function displayJobListings(iCurrentPage, jq) {
	var iResultsPerPage = 10;
	$.ajax({
		type: "POST",
		url: "Default.aspx/GetJobListing",
		data: "{'iCurrentPageIndex':" + iCurrentPage + ", 'iResultsPerPage':" + iResultsPerPage + "}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(msg) {
			// Replace the div's content with the page method's return.
			$("#jobListing").hide();
			$("#jobListing").html(msg.d);
			$("#jobListing").fadeIn("normal");
		}
	});
}

function getTotalNumberOfJobs() {
	$.ajax({
		type: "POST",
		url: "Default.aspx/GetTotalNumberOfJobs",
		data: "{}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: displayPagination

	});
}

function displayPagination(data) {
	//only display pagination if needed
	if (data.d == 0) {
	    $("#pages-title").css("display", "none");	    
	    $("#jobListing").html("<ul><li>No positions available at this time.</li></ul>");
	    
	}
	else if (data.d > 10) {
		$("#Jobs-Pagination").pagination(data.d, {
			items_per_page: 10,
			next_text: "",
			prev_text: "",
			current_page: iPreSetPageIndex,
			callback: displayJobListings
		});
		displayJobListings(iPreSetPageIndex);
	}
	else {
		$("#pages-title").css("display", "none");
		displayJobListings(iPreSetPageIndex);
	}
	
}

$(document).ready(function() {
	getTotalNumberOfJobs();
});