/**
* Sets up initial onclick functions for article body pagination
*/
var articlePagingCount = 0;
function initArticleBodyPaging() {
	if ($(".articlepage").length < 2) {
		// no article body pagination
		if ($(".paging").length > 0) {
		    $(".paging")[0].className = "paging singlePage";
	    }
		return;
	}
	
	// get page value
	var pageStr = getURLQueryParameter("page");
	var page = parseInt(pageStr);
	if (!page || page < 1 || page > $(".articlepage").length) {
		// page is NaN or out of range, so set to 1
		page = 1;
	}
	
	gotoArticleBodyPage(page);
}

/**
* Show page and update pagination utility
* @param page
*/
function gotoArticleBodyPage(page) {
	
	var i;

	var articlePages = $(".articlepage");
	numOfPages = articlePages.length;
	// create base url
	var loc = window.location;
	var baseURL = loc.protocol + "//" + loc.hostname + loc.pathname
	var nameValDict = getURLQueryParameters();
	var search = "";
	if(document.getElementById("PageADRefreshValue") != null && document.getElementById("PageADRefreshValue").value != '' && !isNaN(document.getElementById("PageADRefreshValue").value))
	{
		articlePagingCount = document.getElementById("PageADRefreshValue").value;
		
	}
	else
	{
		articlePagingCount = GlobalADRefreshPageCount;
	}
	
	jQuery.each(nameValDict, function(i, val) {
		if (i != "page") {
			search += i + "=" + val + "&"
		}
	});
	baseURL += "?" + search + "page=";
	var nextPage = parseInt(page)+1;
	var prePage  = parseInt(page)-1;
	// create and insert current page text
	if ($(".currPage").length > 0) {
		$(".currPage")[0].innerHTML = "page " + page + " of " + numOfPages;
	}
	
	// create and insert new pagination
	var paginationHTML = "<ul>";
	if (page != 1) {
		if(checkForRefreshPage(prePage))
			paginationHTML += "<li><a href=\"" + baseURL + (prePage) + "\">&lt; Prev</a></li>";
		else
			paginationHTML += "<li><a href=\"" + baseURL + (prePage) + "\" onClick=\"gotoArticleBodyPage('"+prePage+"');return false;\">&lt; Prev</a></li>";
	}
	for (i = 1; i <= numOfPages; i++) {
		if (page != i) {
			if(checkForRefreshPage(i))
				paginationHTML += "<li><a href=\"" + baseURL + i + "\">" + i + "</a></li>";
			else
				paginationHTML += "<li><a href=\"" + baseURL + i + "\" onClick=\"gotoArticleBodyPage('"+i+"');return false;\">" + i + "</a></li>";
		} else {
			paginationHTML += "<li class=\"active\"><span>" + i + "</span></li>";
		}
	}
	
	if (page != numOfPages) {
		if(checkForRefreshPage(nextPage))
			paginationHTML += "<li><a href=\"" + baseURL + (nextPage) + "\">Next &gt;</a></li>";
		else
			paginationHTML += "<li><a href=\"" + baseURL + (nextPage) + "\" onClick=\"gotoArticleBodyPage('"+nextPage+"');return false;\">Next &gt;</a></li>";
	}
	
	paginationHTML += "</ul><br class=\"clear\" />";
	if ($(".paging").length > 0) {
		$(".paging")[0].innerHTML = paginationHTML;
	}
	// show article body page
	jQuery.each(articlePages, function () {
		$(this).addClass("hide");
	});
	$(articlePages[page - 1]).removeClass("hide");
	window.scrollTo(0,0);
	if(!checkForRefreshPage(page))
	{
		 if (this.s) {
			s.pageName = 'Article- no ads reloaded';
			s.events = "event12";
			void (s.t());
		};
	}
}

function checkForRefreshPage(pageNumber)
{

	if(parseInt(articlePagingCount) == 0)
		return false;

	if(parseInt(pageNumber) == 1)
		return true;

	if(((parseInt(pageNumber) -1) % parseInt(articlePagingCount)) == 0)
		return true;
	else
		return false;
}