function Navegacao(args){
	var me = this;
	this.index = 0;
	this.lim = (args.limite-1);
	this.nums = args.nums;
	this.repetir = args.repetir;
	this.bts_num = [];
	this.botoes = args.botoes;
	this.callBack = args.callBack;	
	this.initialize = function(){
		this.setIndex(this.index);
	}
	this.incrIndex=function(){
		if(this.repetir){
			this.index = (this.index >= this.lim)?0:this.index+1;
		}else{
			this.index = (this.index >= this.lim)?this.lim:this.index+1;			
		}
		this.setIndex(this.index);
	}
	this.decrIndex=function(){
		if(this.repetir){
			this.index = (this.index <= 0)?this.lim:this.index-1;
		}else{
			this.index = (this.index <= 0)?0:this.index-1;			
		}			
		this.setIndex(this.index);
	}
	this.setIndex = function(num){
		this.index = num;
		if(this.nums && (this.lim > 0)){
			this.setNum()
		}
		if(!this.repetir){
			if(me.index > 0){
				me.botoes.ant.attr('class','nextActive');
				me.botoes.prim.attr('class','nextActive');
			}else{
				me.botoes.ant.attr('class','nextLast');
				me.botoes.prim.attr('class','nextLast');
			}
			if(me.index < me.lim){
				me.botoes.prox.attr('class','prevfirst');
				me.botoes.ult.attr('class','prevfirst');
			}else{
				me.botoes.prox.attr('class','prevInactive');
				me.botoes.ult.attr('class','prevInactive');
			}
		}
		if(me.callBack)
		me.callBack(this.index);
	}
	var clAtual = (this.nums.clAtual)?this.nums.clAtual:'';
	var clNums = (this.nums.clNums)?+this.nums.clNums:'';
	var separador = (this.nums.separador)?this.nums.separador:'';	
	var mtd = Math.floor(this.nums.qntd/2);
	
	this.setNum = function(){
		if(this.nums.classe){				
			var pagAt = this.index;						
			var primNum = (pagAt > mtd )?(pagAt-mtd):0;
			var limN = (primNum+this.nums.qntd)-1;
			var str = '';
			$(this.nums.classe).html('');
			for(var i=primNum;i <= limN;i++){					
				if(i <= this.lim){					
					var elem = document.createElement('a');
					var classe = (i == pagAt)?clAtual:clNums;
					var sepd = separador;
					if(this.nums.sep_pos != 'innerRight'){
						var sepd = (i!=primNum)?separador:'';
					}
					elem.className = classe;
					elem.innerHTML = (i+1);
					elem.setAttribute('id','num_'+i);
					if(this.nums.click){
						elem.setAttribute('href','javascript:'+args.obj.nome+'.nav.setIndex('+i+')');
					}
					if(!this.nums.sep_pos){
						$(this.nums.classe).append(sepd).append(elem);	
					}
					if(this.nums.sep_pos == 'innerRight'){
						elem.innerHTML=elem.innerHTML+sepd;
						$(this.nums.classe).append(elem);
					}
				}										
			}			
		}
	}
	this.initialize();	
}

function PaginacaoBloco(args){
	var me = this;	
	this.nome = args.nome;
	this.conteiner = args.conteiner.split(',');
	this.atuais = [];
	this.blocos = [];
	this.qntBcs = args.qntBcs;	
	this.bt_ant = $(args.bt_anterior);
	this.bt_prox = $(args.bt_proximo);	
	this.bt_ult = $(args.bt_ultimo);
	this.bt_prim = $(args.bt_primeiro);	
	this.limPgs = [];
	this.excessao = args.excessao;
	this.aoPaginar = args.aoPaginar;

	this.isExcessao = function(obj){
		if(me.excessao){
			for(var i in me.excessao){			
				if(obj.attr(i) == me.excessao[i]){
					return true;
				}			
			}
		}
		return false;
	}
	
	for(var j=0;j<this.conteiner.length;j++){
		me.blocos[j] = [];
		me.atuais[j] = [];
		var cont = 0;
		$(me.conteiner[j]).each(function(i){
			if(!me.isExcessao($(this))){
				me.blocos[j][cont] = {};					  
				me.blocos[j][cont].data=$(this).html();
				//alert(me.blocos[j][cont].data)
				me.blocos[j][cont].href=$(this).attr('href');
				if(cont < args.qntBcs){
					me.atuais[j][cont] = $(this);
				}else{
					$(this).remove();
				}
				cont++;
			}
		});
		this.limPgs[j] = me.blocos[j].length/me.qntBcs;
		this.limPgs[j] = (this.limPgs[j]%1)==0?this.limPgs[j]:parseInt(this.limPgs[j]+1);
	}
	
	this.at_blocos = function(ind){
		if(args.fade){	
			for(var j=0;j<me.atuais.length;j++){			
				for(var i=0;i<me.atuais[j].length;i++){								
					me.atuais[j][i].fadeOut(25);				
				}		
				for(i=0;i<me.atuais[j].length;i++){
					var indc = ind*me.qntBcs+i;
					if(indc < me.blocos[j].length){
						//alert(indc+':'+blocos.length)
						me.atuais[j][i].attr('href',me.blocos[j][indc].href);
						me.atuais[j][i].html(me.blocos[j][indc].data);	
						me.atuais[j][i].fadeIn('normal');
					}				
				}
			}	
		}else{
			for(var j=0;j<me.atuais.length;j++){
				for(var i=0;i<me.atuais[j].length;i++){
					var indc = ind*me.qntBcs+i;	
					me.atuais[j][i].css('display','none');
					if(indc < me.blocos[j].length){
						me.atuais[j][i].attr('href',me.blocos[j][indc].href);
						me.atuais[j][i].html(me.blocos[j][indc].data);	
						me.atuais[j][i].css('display','block');
					}
				}
			}		
		}
		if(me.aoPaginar)
			me.aoPaginar();
	}	
	var rept = (args.repetir)?args.repetir:false;
	this.nav = new Navegacao({
		limite:me.limPgs[0],
		repetir:rept,
		nums:args.nums,
		botoes:{
			ant:me.bt_ant,
			prox:me.bt_prox,
			ult:me.bt_ult,
			prim:me.bt_prim
		},
		callBack:me.at_blocos,
		obj:me
	});	

	this.proximo = function(){	
		me.nav.incrIndex();
	}
	this.anterior = function(){
		me.nav.decrIndex();
	}	

	this.bt_ant.attr('href','javascript:'+this.nome+'.anterior()');
	this.bt_prox.attr('href','javascript:'+this.nome+'.proximo()');
	if(this.bt_prim)
	this.bt_prim.attr('href','javascript:'+this.nome+'.nav.setIndex(0)');
	if(this.bt_ult)
	this.bt_ult.attr('href','javascript:'+this.nome+'.nav.setIndex('+(me.limPgs[0]-1)+')');
	
}
