var Utils = {}

Utils.Pagination = function(total,offset) {
	this.total = total;
	this.offset = offset;
};

Utils.Pagination.prototype = {
	show: function(page) {
		var texto = 'Resultados <b>'+page+'</b> de <b>'+Math.ceil(this.total/this.offset)+'</b><br />';
		document.getElementById('pagination_total').innerHTML = texto;
		texto = 'Outras páginas:&nbsp;&nbsp; ';
		for(i=0;i<this.total/this.offset;i++){
			atual = '';
			if((i+1)==page){
				atual = 'class="paginaatual"';
			}
			if(i==0){
				texto += '<a href="javascript:pag_display.show('+(i+1)+','+this.total+');void(0)" '+atual+' >'+(i+1)+'</a>';
			} else {
				texto += ' | <a href="javascript:pag_display.show('+(i+1)+','+this.total+');void(0)" '+atual+' >'+(i+1)+'</a>';
			}
			document.getElementById('pag_'+(i+1)).style.display = 'none';
		}
		document.getElementById('pagination_pages').innerHTML = texto;
		document.getElementById('pag_'+page).style.display = 'block';
	}
}

var requisicao = { getParameter: new Function("param", "url", "return Request(param, url)") };

function Request(querystring, url)
  {
   var querystr = [];
   urlStr = (url) ? url : window.location.href;
   locationStr = urlStr.substr(urlStr.indexOf("?") + 1).split("&");

   for (q = 0; q < locationStr.length; q++)
     {
      var query = locationStr[q].split("=");
      querystr[query[0]] = query[1];
     }

   if (!querystr[querystring])
     { return (null); }

   return (unescape(querystr[querystring]));
  } 


function disableLink(objItemForm){
	objItemForm.onclick=function(){return false;}
}

function getThumbImage(strArquivoImagem, intTipo){

	var strArquivo = "";
	var strCaminho = "";

	strCaminho = strArquivoImagem.substr(0,strArquivoImagem.lastIndexOf("/"));
	strArquivo = strArquivoImagem.substr(strArquivoImagem.lastIndexOf("/") + 1);

	return strCaminho+"/thumb/"+intTipo+"_"+strArquivo;

}

/**
 * Limita a quantidade de caracteres do texto.
 * @param quantidade máxima de caracteres.
 * @param texto a ser tratado.
 * @return texto tratado.
 */
function tratarTexto(max, texto){
	
	var intContador;
	var tamanho = texto.length;

	if(tamanho < max){
		return texto;
	}
	
	subTexto = texto.substr(0, max - 3);
	
	var last = subTexto.lastIndexOf(" ");
	if( last > max/2 ){
		return subTexto.substring(0, last ) + '...' ;
	} else {
		return subTexto + '...'
	}

}

/**
 * Verifica se o caractere é do tipo numérico
 * @param valor para verificação
 * @return true caso seja numérico, false caso contrário
 */
function isInteger(strValor){
	
	var intContador;
    
    for (intContador = 0; intContador < strValor.length; intContador++){   
		
		//Verifica se o caractere é número.
		var c = strValor.charAt(intContador);
		if ((c < "0") || (c > "9")){
			return false;
		}
    
    }
    //Todos os caracteres são numéricos.
    return true;
}

/**
* Verifica se o campo passado tem o limite de tamanho especificado, se tiver, corta o conteudo do
* campo para o tamanho limite. Além disso, mostra a contagem de caracteres restantes até o limite,
* no elemento dado pelo nome 'restantes'.
*
* @param field
*    Campo do formulario a ser limitado pelo limite. 
*
* @param limite
*    O limite de caracteres para o campo.
*
* @param restantes
*    O id do elemento onde a contagem deve ser mostrada.
*/
function contador(field, limite, restantes){

	var texto = field.value;
	
	if( field.value.length > limite ) {
		field.value = field.value.substring( 0, limite);
	}

	$(restantes).innerHTML = "<span class='caracteres' >Caracteres restantes: " + (limite - field.value.length) + "</span>";

}


function checkchars(form) {

	var max = 15;

	if(form.chars.value.length > max){

		alert('O máximo de caracteres é ' + max + '. Por favor, entre de novo com o texto.');
		return false;

	}else{
		return true;
	}

}



/**
* Método que abre um pop-up para reportar um conteúdo considerado abusivo.
* 
* @idConteudo
*		Identificador do Conteúdo a ser considerado abusivo.<b> 
*
* @idConteudoPai
*		Identificador do Conteúdo Pai, caso o conteúdo filho dependa de um conteudo superior para ser exibido.<b>
*
* @path 
*		Caminho onde é possível exibir o conteúdo a ser considerado abusivo. Este caminho será o link 
*	que aparecerá no e-mail do editor.<b> 
*
* @tipoAbuso
*		Constante que indica o tipo de conteúdo a ser cosiderado abusivo. Atualmente são:
*		- PERGUNTA, RESPOSTA, COMENTÁRIO, CONTEUDO COLABORATIVO.<b>
*/
function reportaAbuso(idConteudo, idConteudoPai , path, tipoAbuso){

	var link = '/pcasite/abuso.jsf?idConteudo=' + idConteudo+'&idConteudoPai='+idConteudoPai+'&tipo='+tipoAbuso+'&path='+path;

	noscroll(link,300,450,'envie');
}


/**
* Método que ativa o bookmark no navegador do cliente.<b> 
*/
function favorito() {
	 var title = document.title; 
	 url = window.location;
	 
	if( document.all ) { // IE Favorite
		window.external.AddFavorite( url, title);
	} else if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if(window.opera && window.print) { // Opera Hotlist
	        var mbm = document.createElement('a');
      	    mbm.setAttribute('rel','sidebar');
	        mbm.setAttribute('href',url);
	        mbm.setAttribute('title',title);
	        mbm.click();    
	}
}