var itemsContainer;
var itemCount;
var totalPageCount;
var paginatorContainer;
var pageInput;
var nextBtn;
var previousBtn;
var viewAllBtn;
var itemsPerPage;
var currentPageNum = 1;
var currentItemRangeDsp;
var pageURL;

function initPagination(itemsContainerId, paginatorContainerId, options){
	itemsContainer = $("#" + itemsContainerId);
	paginatorContainer = $("#" + paginatorContainerId);
	itemCount = itemsContainer.children('[pageItem="true"]').length;
	
	if(options !== null){
		if(options["pageInput"] != null){
			pageInput = paginatorContainer.children('.'+options.pageInput+'');
			pageInput.bind('keyup', function(event){pageInput_keyup(event);});
		}
		if(options["nextBtn"] != null)
		{
			nextBtn = paginatorContainer.children('.'+options.nextBtn+'');
			nextBtn.click(nextBtn_click);
		}
		if(options["previousBtn"] != null)
		{
			previousBtn = paginatorContainer.children('.'+options.previousBtn+'');
			previousBtn.click(previousBtn_click);
		}
		if(options["firstBtn"] != null)
		{
		
			previousBtn.click(firstBtn_click);
		}
		if(options["lastBtn"] != null)
		{
			previousBtn.click(lastBtn_click);
		}
		if(options["itemsPerPage"] != null)
		{
			itemsPerPage = options.itemsPerPage;
			totalPageCount = Math.ceil(itemCount / itemsPerPage);
			paginatorContainer.children('.text').children('.pageCountTotal').html(totalPageCount);
		}else{
			itemsPerPage = 5;	
		}
		
		if(options["viewAllBtn"] != null)
		{
			viewAllBtn = paginatorContainer.children('.viewAll').children('.'+options.viewAllBtn+'');
			viewAllBtn.click(viewAllBtn_click);
		}
		if(options["currentItemRangeDisplay"] != null)
		{
			currentItemRangeDsp = $("#" + options.currentItemRangeDisplay);
			currentItemRangeDsp.children('.total').html(itemCount);
		}
		else{
			currentItemRangeDsp = null;	
		}
		if(options["pageURL"] != null)
		{
			pageURL = false;	
		}
		else if(options["pageURL"] == true){
			pageURL = true;	
		}
		
	}
	else
	{
		itemsContainer, pageInput, nextBtn, previousBtn, itemsPerPage = null;
	}
	
	if(pageURL){
		//if there is no page in the hash, just go to page 1
		if(window.location.hash == '' || window.location.hash == '#')
			setPage(1);
		else{
			var hash = window.location.hash;
			hash = hash.substring(1);
			hash = parseInt(hash.substring(4));
			if(hash == null || isNaN(hash))
				setPage(1);
			else
				setPage(hash);
		}
	}
	else{
		setPage(1);	
	}
	
	
/*
 * EVENT HANDLERS
 */
}
function pageInput_keyup(e){
	//DO CHECKS FOR INT FIRST
	var input = parseInt($(e.currentTarget).val());
	if(isNaN(input))
		alert('Please enter numbers only');	
	else
		setPage($(e.currentTarget).val());	
}
function nextBtn_click(){
	setPage(currentPageNum + 1);	
}
function previousBtn_click(){
	setPage(currentPageNum - 1);	
}
function firstBtn_click(){
	setPage(1);	
}
function lastBtn_click(){
	setPage(totalPageCount);	
}
function viewAllBtn_click(){
	if(itemsContainer.children('tr[pageItem="true"]').length >0)
		itemsContainer.children('tr[pageItem="true"]').removeClass('hide');
	else
		itemsContainer.children('[pageItem="true"]').addClass('visible');
	
	pageInput.val(1)
	if(currentItemRangeDsp !== null){
		updateItemRangeDisplay(1, itemCount);
	}
}


/*
 * FUNCTIONS
 */
function setPage(inputNum){
	currentPageNum = inputNum;
	//alert(totalPageCount);
	//stay on page 1 if you try to decrement page lower than 1
	if(currentPageNum < 1){
		currentPageNum = 1;
		pageInput.val(1);
	}
	//stay on last page if you try to increment page lower than the total
	else if(currentPageNum >= totalPageCount){
		currentPageNum = totalPageCount;
		pageInput.val(totalPageCount);
	}
	else{
		pageInput.val(currentPageNum);	
	}
	if(pageURL){
		window.location.hash = 'page'+currentPageNum;
	}
	
	goToPage(currentPageNum);
}

//display the items for the page
function goToPage(pageNum){
	startingItemIndex = (pageNum * itemsPerPage) - itemsPerPage;
	
	//check if it's a table row...to apply a ie7 fix
	if(itemsContainer.children('tr[pageItem="true"]').length >0){
		itemsContainer.children('tr[pageItem="true"]').addClass('hide');
	}
	else
		itemsContainer.children('[pageItem="true"]').removeClass('visible');
	for(var i=0; i<itemsPerPage; i++){
		//check if it's a table row...to apply a ie7 fix
		if(itemsContainer.children('tr[pageItem="true"]').length >0){
			itemsContainer.children('tr[pageItem="true"]:eq('+ (startingItemIndex  + i) +')').removeClass('hide');
		}
		else
			itemsContainer.children('[pageItem="true"]:eq('+ (startingItemIndex  + i) +')').addClass('visible');
	}
	
	//if the item display
	if(currentItemRangeDsp !== null){
		updateItemRangeDisplay(startingItemIndex+1, startingItemIndex+itemsPerPage);	
	}
}

//we update the 3 - 6 range here
function updateItemRangeDisplay(startNum, endNum){
	//currentItemRangeDsp.hide();
	currentItemRangeDsp.children('.first').html(startNum);
	if(endNum > itemCount){
		currentItemRangeDsp.children('.last').html(itemCount);
	}
	else if(endNum < itemsPerPage){
		currentItemRangeDsp.children('.last').html(itemCount);	
	}
	else{
		currentItemRangeDsp.children('.last').html(endNum);	
	}
}