function pager(max,current,perPage,el,min){
	this.maximum = max;
	this.current = (current)?current:0;
	this.perPage = (perPage)?perPage:1;
	this.minimum = (min)?min:0;
	this.mainElement = el;
	
	this.lastPage = Math.ceil(this.maximum / this.perPage);

	this.firstLink = ''; 
	this.prevLink = ''; 
	this.nextLink = '';
	this.lastLink = '';
	this.emptyLink = '...';
	this.CSSClass = 'pager';
	this.fieldTpl = '';
	this.majorElement = 'ul';
	this.minorElement = 'li';
	
	this.displayLimit = 6;

	this.onclick = null;
    
	this.draw = function( el ) {
		var out = [ (this.majorElement!='')?'<'+this.majorElement+((this.CSSClass)?' class="'+this.CSSClass+'"':'')+'>':''];
		//var tmp = (this.fieldTpl!='')?this.fieldTpl:'{val}';

		if (this.displayLimit!=0){
			var visibleFrom = Math.max(0, Math.min( parseInt(this.current-this.displayLimit), this.lastPage-(this.displayLimit*2) ));
			var visibleTo = Math.min(this.lastPage-1, Math.max( parseInt(this.current+this.displayLimit) , this.displayLimit*2));
		} else {
			var visibleFrom = 0;
			var visibleTo = this.lastPage;			
		}

//		console.log(visibleFrom+' <-> '+visibleTo +' [] '+this.lastPage );
		
		if (this.firstLink) out.push('<'+this.minorElement+' title="'+this.minimum+'" class="pagerFIRST'+ ((this.current==0)?' pagerINACTIVE':'')+'">' + this.firstLink + '</'+this.minorElement+'>');
		if (this.prevLink) out.push('<'+this.minorElement+' title="'+(this.current-1)+'" class="pagerPREV'+ ((this.current==0)?' pagerINACTIVE':'')+'">' + this.prevLink + '</'+this.minorElement+'>');

		if (visibleFrom>0) out.push( this.putListElement(0) );
		if (visibleFrom>1) out.push( this.putListEmptyElement() );
		for (var n=visibleFrom; n<=visibleTo; n++) out.push( this.putListElement(n) );			
		if (visibleTo<this.lastPage-2) out.push( this.putListEmptyElement() );
		if (visibleTo<this.lastPage-1) out.push( this.putListElement(this.lastPage-1) );
		if (this.nextLink) out.push('<'+this.minorElement+' title="'+(this.current+1)+'" class="pagerNEXT'+ ((this.current==this.lastPage-1)?' pagerINACTIVE':'')+'">' + this.nextLink + '</'+this.minorElement+'>');
		if (this.lastLink) out.push('<'+this.minorElement+' title="'+this.maximum+'" class="pagerLAST'+ ((this.current==this.lastPage-1)?' pagerINACTIVE':'')+'">' + this.lastLink + '</'+this.minorElement+'>');
		if (this.majorElement!='') out.push('</'+this.majorElement+'>');

		var e = (el)?el:this.mainElement;
		e.innerHTML=out.join('');
		this.appendHandlers(e,visibleFrom,visibleTo);
	}
	
	this.putListElement = function(n){
		return '<'+this.minorElement+' title="'+n+'"'+((n== parseInt(this.current) )?' class="pagerCURRENT"':'')+'>'+((this.fieldTpl!='')?this.fieldTpl:'{val}').replace( /{val}/ ,n+1).replace( /{all}/, this.lastPage) +'</'+this.minorElement+'>'
	}
	this.putListEmptyElement = function(n){
		return '<'+this.minorElement+' class="pagerEMPTY">'+this.emptyLink+'</'+this.minorElement+'>'
	}
		
	this.appendHandlers = function(el,visibleFrom,visibleTo){
		var li = el.getElementsByTagName(this.minorElement);
		
		for (var n=0; n<li.length; n++){
			if (li[n].title){
				li[n].pagerNumber = li[n].title;
				li[n].title='';
				li[n].pagerObject = this;
				li[n].onclick = function(){
					with (this.pagerObject) {
						current = parseInt(this.pagerNumber); 
						draw();
						onclick( current*perPage );						
					}
				}
			}
		}
	}
/*
	this.setClicked = function(){
		with (this.pagerObject) {
			current = this.pagerNumber; 
			draw();
			onclick( current*perPage );
		}
	}

	this.setFirst = function(){
		with (this.pagerObject) {
			current = minimum;
			draw();
			onclick(current*perPage);
		}
	}
	this.setPrev =function (){
		with (this.pagerObject) {
			current--;
			draw();
			onclick(current*perPage);
		}
	}
	this.setNext =function (){
		with (this.pagerObject) {
			current++;
			draw();
			onclick(current*perPage);
		}
	}
	this.setLast =function (){
		with (this.pagerObject) {
			current = maximum;
			draw();
			onclick(current*perPage);
		}
	}
	
	this.clone = function( destElement ){
		destElement.appendChild( this.mainElement.firstChild.cloneNode(true) );
	}
*/
}	
