/*
	Auther S.GH
	86/08/12
*/

/**
	to use paination class just instantiate from GeneralAjaxPagination class and pass to it 
	objName: name of new object which created from this class
	callBackFunc: name of function you desire to execute after click on pagination bar
	container: id of pagination container
	
	also for some styles, users can set some object members like:
	OBJECT.bgColor: backgroud color for deselected case
	OBJECT.overColor: mouse over color
	OBJECT.selectedColor: selected page lable backround color
	
*/

var GeneralAjaxPagination = function(objName,callBackFunc){

	this.objName = objName;
	this.callBackFunc = callBackFunc;
	this.property = '';
	this.bgColor = '#FFFFFF';
	this.overColor = '#f0ff0f';
	this.selectedColor = '#cacaca';
	this.rpp = 10;
	this.pageCount = 1;
	this.selectedPage = 1;
	this.selectedLable = 0;
	this.pageLabels	= [];
	this.maxLabelsInBar = 9;
	
	this.firstTitle = '';
	this.prevTitle = '';
	this.nextTitle = '';
	this.lastTitle = '';
	
	this.isOverOutOn = true;
	this.mirrorNumber = 0;//this member saves mirrors of one certain instance of paginator i.e. top & bottom paginators
}

/**
	this function generate the fibonacci series
	it accepts an index as series index and returns appropiated member
*/
GeneralAjaxPagination.prototype.fibonacci = function(index){

	index = parseInt(index);
	if (index < 2 ) return 1;
	if (index == 2) return 2;

	index -= 2;
	var prePrev = 1;
	var prev 	= 2;
	var tmp 	= 0;
	var i 		= 0;

	while (i < index){
		i++;
		tmp = prev + prePrev;
		prePrev = prev;
		prev	= tmp;
	}
	return tmp;
}
	
GeneralAjaxPagination.prototype.getHtml = function(countAll,rpp){
	
	//reset labels array
	//for(i in this.pageLabels) this.pageLabels.pop();
	var pageLabels = Array();
	
	this.mirrorNumber++;//increase mirrors count
	
	this.rpp = rpp;
	var pageCount = parseInt(countAll/rpp);
	if( parseInt(countAll/rpp) != (countAll/rpp) ) pageCount++;
	this.pageCount = pageCount;
	
	if(pageCount <= this.maxLabelsInBar){
		var ind = 0;
		while(ind < pageCount){
			pageLabels.push(ind+1);
			ind++;
		}
		
	}else{
		
		var center = this.selectedPage;
		if(center != 1) pageLabels.push(center);
		
		var fiboIndex = 1;
		while((center+this.fibonacci(fiboIndex)) < pageCount){
			pageLabels.push(center+this.fibonacci(fiboIndex));
			fiboIndex++;
		}
		var lastMember = pageLabels.pop();
		if(lastMember == pageCount){
			pageLabels.push(lastMember);
		}else{
			pageLabels.push(lastMember);
			pageLabels.push(pageCount);
		}
		
		fiboIndex = 1;
		while((center-this.fibonacci(fiboIndex)) > 1){
			pageLabels.splice(0, 0, center-this.fibonacci(fiboIndex));
			fiboIndex++;
		}
		pageLabels.splice(0, 0,1);
		
	}
	this.pageLabels = pageLabels;

	//set selected index
	for(var z in this.pageLabels) if(this.pageLabels[z] == this.selectedPage) this.selectedLable = z;
	
	var index = 0;
	
	var html = '';

	html += '<span class="ajNavBx" title="Last" style=" '
	if(this.bgColor != '') html += 'background:'+this.bgColor+'; ';
	html += ' " ';
	html += ' onclick="'+this.objName+'.navPage(\'last\')" ';
	if(this.isOverOutOn){
		html += ' onmouseover="this.style.background=\''+this.overColor+'\'" ';
		html += ' onmouseout ="this.style.background=\''+this.bgColor+'\'" ';
	}
	html += ' >&nbsp;&lt;&lt;&nbsp;'+this.lastTitle+'</span>';

	html += '<span class="ajNavBx" title="Next" style="';
	if(this.bgColor != '') html += 'background:'+this.bgColor+'; ';
	html += ' " ';
	html += ' onclick="'+this.objName+'.navPage(\'next\')" ';
	if(this.isOverOutOn){
		html += ' onmouseover="this.style.background=\''+this.overColor+'\'" ';
		html += ' onmouseout ="this.style.background=\''+this.bgColor+'\'" ';
	}
	html += ' >&nbsp;&lt;&nbsp;'+this.nextTitle+'</span>';
	
	for(var i in pageLabels){
		index = pageLabels[i];
		html += '<span class="ajNavBx" href="" id="'+this.objName+'_page_'+index+'_'+this.mirrorNumber+'" ';
		html += ' style = " ';
		if(this.bgColor != ''){
			html += ' background:';
			if(this.selectedPage == pageLabels[i]){
				html += this.selectedColor+';';
			}else{
				html += this.bgColor+';';
			}
		}
		html += ' " ';
		html += ' onclick="'+this.objName+'.handleClick('+parseInt(index)+')" ';
		if(this.isOverOutOn){
			html += ' onmouseover="this.style.background=\''+this.overColor+'\'" ';
			html += ' onmouseout ="'+this.objName+'.cOnMouseOut('+index+','+this.mirrorNumber+')" ';
		}
		html += '><strong>';
		html += index;
		html += '</strong></span>';
	}
	
	html += '<span class="ajNavBx" title="Prev" style="';
	if(this.bgColor != '') html += 'background:'+this.bgColor+'; ';
	html += ' " ';
	html += ' onclick="'+this.objName+'.navPage(\'prev\')" title="Prev" ';
	if(this.isOverOutOn){
		html += ' onmouseover="this.style.background=\''+this.overColor+'\'" ';
		html += ' onmouseout ="this.style.background=\''+this.bgColor+'\'" ';
	}
	html += ' >'+this.prevTitle+'&nbsp;&gt;&nbsp;</span>';
	
	html += '<span class="ajNavBx" title="First" style="';
	if(this.bgColor != '') html += 'background:'+this.bgColor+'; ';
	html += ' " ';
	html += ' onclick="'+this.objName+'.navPage(\'first\')" ';
	if(this.isOverOutOn){
		html += ' onmouseover="this.style.background=\''+this.overColor+'\'" ';
		html += ' onmouseout ="this.style.background=\''+this.bgColor+'\'" ';
	}
	html += ' >'+this.firstTitle+'&nbsp;&gt;&gt;&nbsp;</span>';
	
	return html;
	
}

GeneralAjaxPagination.prototype.cOnMouseOut = function(index,mirrorNum){
	if(index == this.selectedPage){
		fObj(this.objName+'_page_'+index+'_'+mirrorNum).style.background= this.selectedColor;
	}else{
		fObj(this.objName+'_page_'+index+'_'+mirrorNum).style.background= this.bgColor;
	}
}		

GeneralAjaxPagination.prototype.navPage = function(sw){
	switch (sw){
		
		case('first'):
			this.selectedLable = 0;
			break;
			
		case('prev'):
			this.selectedLable--;
			if(parseInt(this.selectedLable) < 0) this.selectedLable = 0;
			break;
			
		case('next'):
			this.selectedLable++;
			if(parseInt(this.selectedLable) > this.pageLabels.length-1) this.selectedLable = this.pageLabels.length-1;
			break;
			
		case('last'):
			this.selectedLable = this.pageLabels.length-1;
			break;
			
		default:
			//user invokes to show certain page and sw is a page number
			this.handleClick(parseInt(sw));
	}
	this.handleClick(this.pageLabels[this.selectedLable]);
	
}

GeneralAjaxPagination.prototype.turnOnLable = function(){
	
	//turn off all pages in all mirrors
	//_'+this.mirrorNumber+'
	var mirror = 0;
	while(mirror < this.mirrorNumber){
		mirror++;
		for(var i in this.pageLabels){
			var obj = fObj(this.objName+'_page_'+this.pageLabels[i]+'_'+mirror);
			if(obj) obj.style.background = this.bgColor;
		}
		
		var obj = fObj(this.objName+'_page_'+this.selectedPage+'_'+mirror);
		if(obj){
			//this page nuber exists in labels
			obj.style.background = this.selectedColor;
		}else{
			//should turn on nearest label
			var t = 0;
			while(this.pageLabels[t] < this.selectedPage) t++;
			//t--;
			obj = fObj(this.objName+'_page_'+this.pageLabels[t]+'_'+mirror);
			if(obj) obj.style.background = this.selectedColor;
		}
	} 
}

GeneralAjaxPagination.prototype.handleClick = function(pageNumber){
	
	pageNumber = parseInt(pageNumber);
	if (pageNumber < 1) pageNumber = 1;
	if (pageNumber > this.pageCount) pageNumber = this.pageCount;
	this.selectedPage = pageNumber;
	
	index = parseInt(this.selectedPage-1);
	//console.log(this.callBackFunc+"(index*this.rpp,this.rpp)");
	eval(this.callBackFunc+"(index*this.rpp,this.rpp)");
	
	//turn on selected label
	this.turnOnLable();
}
