/****************************************************************
*	Criado em:			03/07/2007			Por: ANDRÉ LUIZ
****************************************************************/

/*_________________________________ FUNÇÕES DO AJAX _________________________________*/

/*# carrega o conteúdo do portfólio */
function loadContent(id, vc_tipo, it_pag, vc_termo) {
	var vc_tipo		= new String(vc_tipo);
	var it_pag		= (it_pag) ? parseInt(it_pag) : 0;
	var vc_url		= new String();
	var vc_termo	= (vc_termo) ? new String(vc_termo) : new String();
	
	if (vc_tipo.length > 0) {
		// instancia objeto AJAX
		var ob_ajax	= new ajax('GET', 'codigos/cinema_pesquisa.asp?' + vc_tipo + '=' + id + '&pagIT=' + it_pag + '&termoVC=' + vc_termo, true, '', 'showContent');
		document.location.hash	= vc_tipo + '=' + id + '&pagIT=' + it_pag + '&termoVC=' + vc_termo;	// armazena parâms. atuais no hash da URL (conteúdo após o [ # ])
		ob_ajax.ajaxResponse();																		// envia request ao servidor
		loading();																					// ativa o loading
	} else {
		alert('Tipo de pesquisa não informado.');
		return false;
	}
}

/*# exibe "loading" enquanto o conteúdo é carregado */
function loading() {
	$('dv_cinema_pesquisa').innerHTML	= '<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><img src="imagens/layout/loading.gif" id="loading" style="display:block; margin:auto;" />';
}

/*# exibe conteúdo retornado pelo ajax */
function showContent(tx_html) {
	var tx_html		= new String(decodeURI(tx_html));	// arruma problema com acentuação (URI)
	$('dv_cinema_pesquisa').innerHTML	= tx_html;		// escreve conteúdo retornado
}

/*# resume a função [ getElementById ] */
function $(vc_id) {
	return document.getElementById(vc_id);
}

/*# carrega filtro e conteúdo AJAX ao voltar p/ a página [IE] */
window.onload = function() {
	if ($('dv_cinema_pesquisa')) {										// verifica se o conteiner p/ o retorno do conteúdo existe
		var vc_hash			= new String(document.location.hash);		// recupera hash
		
		if (vc_hash.length > 0) {										// verifica se há algo no hash
			vc_hash			= vc_hash.replace('#', '');					// remove tralha
			var ar_hash		= vc_hash.split('&');						// cria array com o nome e o valor dos parametros
			var id			= ar_hash[0].split('=')[1];					// ID
			var vc_tipo		= ar_hash[0].split('=')[0];					// tipo
			var it_pag		= ar_hash[1].split('=')[1];					// pagina
			var vc_termo	= ar_hash[2].split('=')[1];					// termo de pesquisa
			loadContent(id, vc_tipo, it_pag, vc_termo);					// chama conteúdo via AJAX
			
			var ob_combo	= document.getElementsByName(vc_tipo)[0];	// pega combo a ser reposicionado
			for (var i = 0; i < ob_combo.options.length; i++) {			// laço p/ percorrer as opções do combo
				if (ob_combo.options[i].value == id) {
					ob_combo.options[i].selected	= true;				// seleciona opção referente ao conteúdo carregado
					break;
				}
			}
		}
	}
}
