var jcookie = {
  set: function(name, content) {
    document.cookie = name + "=" + content + ";";
	return true;
  },
  get: function( name ) {
  	var nameE = name + "=";
	var cookies = document.cookie.split(";");
	for(var i = 0, Cookie; Cookie = cookies[i]; i++) {
	  while(Cookie.charAt(0) == " ") {
	  	Cookie = Cookie.substring(1,Cookie.length);
	  }
	  if(Cookie.indexOf(nameE) == 0) {
	  	return Cookie.substring(nameE.length,Cookie.length);
	  }
	}
	return false;
  },
  unset: function( name ) {
  	this.set(name, "", -1);
  	return true;
  }
}

var pagination = {

	    //Total de ancoras na paginacao
	    anchors: document.getElementById("boxPaginacao").getElementsByTagName("a"),

	    //Div pagination
	    divPaginationLeft: document.getElementById("boxPaginacaoEsq"),
	    divPaginationRight: document.getElementById("boxPaginacaoDir"),

	    totalPage: 0,
	    totalBlock: 0,
	    currentPageNumber: 0,
		
	    init: function(totalBlock,currentPN){
			this.currentPageNumber = parseInt(currentPN);
			if(isNaN(this.currentPageNumber)) {
				this.currentPageNumber = "0";
				jcookie.set("paginacaoLateral",0);
			}
			this.totalBlock = totalBlock;
			this.totalPage = (this.anchors.length < totalBlock)?this.anchors.length:totalBlock;
			this.pagesForBlock();
			this.nextPage();
			this.previousPage();	
			if(this.currentPageNumber == 0){
				//this.anchors[0].href="javascript:void(0)";
				//this.anchors[0].innerHTML = " <b>"+ 1 +"</b>";
			}else{
				//this.anchors[parseInt(this.currentPageNumber)].href="javascript:void(0)";
				//this.anchors[parseInt(this.currentPageNumber)].innerHTML = " <b>"+(parseInt(this.currentPageNumber))+"</b>";
			}
			
	    },
	    pagesForBlock: function(){
			var init = 0;
			if(this.anchors.length < 11){
				init = 0;			
			} else {
				if(this.currentPageNumber < 7) {
					init = 0;
				} else if(this.currentPageNumber > (this.anchors.length - 4)) {
					init = this.anchors.length - 10;
				} else {
					init = this.currentPageNumber - 6;
				}
			}
			
			
			
			
			for(var i=0; i<init; i++){
				this.anchors[i].style.display="none";
			}
			
			for(var i=init; i<this.anchors.length; i++){
				if(i >= this.totalPage+init) {
					this.anchors[i].style.display="none";
				}
			}
	    },

	    nextPage: function(){
			this.currentPageNumber = jcookie.get("paginacaoLateral");
			if(parseInt(this.totalBlock) < this.anchors.length && parseInt(this.currentPageNumber) < this.anchors.length-1){
				   if(jcookie.get("paginaGeral") == ""){
					 var objeto1 = '<a href="'+this.anchors[parseInt(this.currentPageNumber)+1].href+'" onclick="' ;
				     var objeto2 = 'jcookie.set('+"'paginaGeral'"+','+(parseInt(this.currentPageNumber)+2)+');jcookie.set('+"'paginacaoLateral'"+','+(parseInt(this.currentPageNumber)+2)+');';
				   } else {
					 var objeto1 = '<a href="'+this.anchors[parseInt(this.currentPageNumber)].href+'" onclick="' ;
				     var objeto2 = 'jcookie.set('+"'paginaGeral'"+','+(parseInt(this.currentPageNumber)+1)+');jcookie.set('+"'paginacaoLateral'"+','+(parseInt(this.currentPageNumber)+1)+');';
				   }
				   var objeto3 = '">>></a>';
				   var todos = objeto1+objeto2+objeto3;
				   
				   var nextPage = "";
				   var next =  todos;
					
				   var d = this.divPaginationRight;
				   d.innerHTML = d.innerHTML + next;
			}
	    },
	    previousPage: function(){
			this.currentPageNumber = jcookie.get("paginacaoLateral");
			if(parseInt(this.totalBlock) < this.anchors.length && parseInt(this.currentPageNumber) > 0){
				    var objeto1 = '<a href="'+this.anchors[parseInt(this.currentPageNumber)-2].href+'" onclick="' ;
				    var objeto2 = 'jcookie.set('+"'paginaGeral'"+','+(parseInt(this.currentPageNumber)-1)+');jcookie.set('+"'paginacaoLateral'"+','+(parseInt(this.currentPageNumber)-1)+');';
				    var objeto3 = '"><<</a>';
				    var todos = objeto1+objeto2+objeto3;
					
					var previous = todos;
					
					var d = this.divPaginationLeft;
				    d.innerHTML = previous + d.innerHTML;

			}
		}
	 }
