/**
 * PageNavigation
 *	
 */

function PageNavigation($domElem, options) {
	
	this.$domElem=$domElem;
	this.initDOMElems();

	this.options = options;
	if (typeof this.options == 'undefined') {
		this.options = {};
	}
	if (typeof this.options.showFirstRange == 'undefined') {
		this.options.showFirstRange = PageNavigation.DEFAULT_SHOW_FIRST_RANGE;
	}
	if (typeof this.options.showLastRange == 'undefined') {
		this.options.showLastRange = PageNavigation.DEFAULT_SHOW_LAST_RANGE;
	}
	if (typeof this.options.showAroundSelectionRange == 'undefined') {
		this.options.showAroundSelectionRange = PageNavigation.DEFAULT_SHOW_AROUND_SELECTION_RANGE;
	}

	this.reset();
}

PageNavigation.DEFAULT_SHOW_FIRST_RANGE = 2;
PageNavigation.DEFAULT_SHOW_LAST_RANGE = 2;
PageNavigation.DEFAULT_SHOW_AROUND_SELECTION_RANGE = 2;

/*
var showFirstRange = this.options.showFirstRange; // 2 normal, 1 comments
	var showLastRange = this.options.showLastRange;  // 2 normal, 1 comments
	var showAroundSelectionRange = this.options.showAroundSelectionRange;  // 2 normal
	*/
	
/*

	PUBLIC 
	methods and event handlers

*/



PageNavigation.prototype.reset = function(maxPages, currentPage){
	console.log("PageNavigation.init "+[maxPages, currentPage]);
	this.maxPages=maxPages;
	
	if(currentPage){
		this.currentPage=currentPage;
	} else {
		this.currentPage=1;
	}	
	this.checkButtons();
}

PageNavigation.prototype.goToPage = function(pageNum){
	this.setCurrentPage(pageNum);
	this.onPageChanged(this.currentPage);
}

PageNavigation.prototype.setCurrentPage = function(pageNum){
	if(pageNum<1 || pageNum>this.maxPages){
		console.warn("PageNavigation: invalid page num "+pageNum);
		return;
	}
	this.currentPage=pageNum;
	this.checkButtons();
}

PageNavigation.prototype.setNumPages = function(numPages){
	if(numPages<1){
		console.warn("PageNavigation.setNumPages: invalid argument "+numPages);
		return;
	}
	this.maxPages=numPages;
}

/*

	"OVERWRITEABLES" 

*/

PageNavigation.prototype.onPageChanged = function(pageNum){

}


/*

	CLASS ATTRIBUTES
	for default values
	
*/

/*

	PRIVATE

*/

PageNavigation.prototype.initDOMElems = function(){
	this.$domElem.empty();
	
	this.$domElem.append('<div><a class="button small next noText" style=""><span class="icon">&nbsp;</span></a></div>');
	this.$nextButton=$(".next", this.$domElem);
	
	this.$domElem.append('<div><a class="button small previous noText" style="margin-right:0; margin-left:1em;"><span class="icon">&nbsp;</span></a></div>');
	this.$prevButton=$(".previous", this.$domElem);
		
	var _this=this;
	this.$nextButton.click(function(){
		_this.onNext();
	});
	this.$prevButton.click(function(){
		_this.onPrev();
	});

	this.$domElem.append('<ol class="pageNumberButtons"></ol>');
	this.$pageNumberButtons=$(".pageNumberButtons", this.$domElem);
}

PageNavigation.prototype.onNext = function(){
	console.log("next clicked");
	this.goToPage(this.currentPage+1);
}

PageNavigation.prototype.onPrev = function(){
	console.log("prev clicked");	
	this.goToPage(this.currentPage-1);
}

PageNavigation.prototype.checkButtons = function(){
	this.$pageNumberButtons.empty();
		
	var showFirstRange = this.options.showFirstRange;
	var showLastRange = this.options.showLastRange;
	var showAroundSelectionRange = this.options.showAroundSelectionRange;
	
	// create first few buttons
	var i=1;
	for(; i<=Math.min(showFirstRange, this.maxPages); i++){
		this.createPageButton(i);			
	}
	
	if(this.currentPage>i+showAroundSelectionRange){
		this.createPageOmissionSymbol();
	}

	// create middle part	
	i=Math.max(this.currentPage-showAroundSelectionRange, i);
	for(; i<=Math.min(this.currentPage+showAroundSelectionRange, this.maxPages); i++){
		this.createPageButton(i);
	}
	
	if(i<this.maxPages-showLastRange){
		this.createPageOmissionSymbol();
	}
	
	// create end
	i=Math.max(this.maxPages-showLastRange, i);
	for(; i<=this.maxPages; i++){
		this.createPageButton(i);
	}

	
	if(this.currentPage==1){
		this.$prevButton.addClass("disabled");
	} else {
		this.$prevButton.removeClass("disabled");
	}
	

	$("#page-"+this.currentPage).addClass("selected");
}

PageNavigation.prototype.createPageButton = function(index){
	if(	$("#page-"+index).length || index<1 || index>this.maxPages){
		return;
	}
	this.$pageNumberButtons.append('<li><a id="page-'+index+'" class="button small">'+index+'</a></li>');
	var _this=this;
	$("#page-"+index).click(
		function(){
			_this.goToPage(index);
			return false;
		}
	);
}

PageNavigation.prototype.createPageOmissionSymbol = function(){
	this.$pageNumberButtons.append('<li class="omission">&nbsp;...&nbsp;</li>');
}
