/**
 * Constructor for pagination, which generates the page links for results longer than the page length
 *
 * @param pages total number of pages in a result
 * @param currentPage the current page of results being viewed
*/
function pagination(pages, currentPage){
	//sets the number of page links viewable at one time - 15 fits our template the best for now
	this.PAGE_LIMIT = 15;
	this.createPageMenu(pages, currentPage);
}

pagination.prototype = {
	/**
	 * Creates pagination menu
	 *
	 * @param pages total number of pages in a result
 	 * @param currentPage the current page of results being viewed
	*/
	createPageMenu: function(pages, currentPage){
		var instance = this;
		var pagesList = '';
		
		$('div.pages').show();
		$('div.pages li[class!="label"]').remove();
		
		var pageIndex = 1;
		var pagesShown = this.PAGE_LIMIT;
		
		//if there are multiple pages
		if (pages > 1){
			//if there are more pages than allowed to be viewed at one time
			if (pages > this.PAGE_LIMIT){
				//if the current page is greater than half of the page limit adjust the first and last page numbers accordingly
				if (currentPage > Math.floor(this.PAGE_LIMIT / 2)){
					pageIndex = parseInt(currentPage - Math.floor(this.PAGE_LIMIT / 2));
					pagesShown = (pages > (parseInt(currentPage) + parseInt(Math.floor(this.PAGE_LIMIT) / 2))) ? parseInt(currentPage) + parseInt(Math.floor(this.PAGE_LIMIT) / 2) : pages;
				}
			}
			else{
				pagesShown = pages;
			}
			
			//pagesList += ()
			if (currentPage != 1){
				pagesList += '<li class="arrow"><a href="#" rel="1"><<</a></li>';
				pagesList += '<li class="arrow"><a href="#" rel="' + (parseInt(currentPage) - 1) + '"><</a></li>';
			}
			
			for (var i = pageIndex; i <= pagesShown; i++){
				pagesList += (currentPage == i) ? '<li><a href="#" class="on" rel="' + i + '">' + i + '</a></li>' : '<li><a href="#" rel="' + i + '">' + i + '</a></li>';
			}
			
			if (currentPage != pages){
				pagesList += '<li class="arrow"><a href="#" rel="' + (parseInt(currentPage) + 1) + '">></a></li>';
				pagesList += '<li class="arrow"><a href="#" rel="' + pages + '">>></a></li>';
			}
			
			$('div.pages ul').append(pagesList);
		}
		else{
			$('div.pages').hide();
		}
	}
}