var navegar = {
	ctr:{V:1,L:2,E:3},
	td:{V:"tabelaProVnd",L:"tabelaProLoc",E:"tabelaProEmp"},
	intervalo:{V:null,L:null,E:null},
	tempoRefresh:60000,
	pedirIdentificacao:0,
	resumo:null,
	pagina:0,
	ordem:"referencia",
	direcao:1,
	aguarde: {
		modal:null,
		option:{width:150,height:150,resizable:false,modal:true,title:"Aguarde..."},
		show: function() {
			this.modal=$('#divAguarde').dialog(navegar.aguarde.option);
			this.modal.bind('dialogclose', function(event, ui) {modal.dialog('destroy');});
		},
		hide: function() {
			this.modal.dialog("close");
		}
	},
	ordena: function(ordem, direcao) {
		this.carrega(0,ordem,direcao);
	},
	paginar: function(pagina) {
		this.carrega(pagina,this.ordem,this.direcao);
	},
	exibeResumo: function(posX ,posY, ref) {
		if ( this.resumo && this.resumo != null) this.escondeResumo();
		var option={width:230,height:330,resizable:false,position:[posX+50,posY-150]};
		var my=this;
		this.resumo=$('#divResumoImovel' + ref).dialog(option);
		this.resumo.bind('dialogclose', function(event, ui) {my.resumo.dialog('destroy');});
	},
	escondeResumo: function() {
		this.resumo.dialog('close');
	},
	carrega: function(pagina,ordem,direcao) {
		this.pagina=pagina;
		this.ordem=ordem;
		this.direcao=direcao;
		this.aguarde.show();
		var fx=$('#faixaValor :selected');
		ordem = parseInt(direcao) < 0 ? "-" + ordem : ordem;
		var queryString={ctr:$("#ctr").val(),tipo:$("#tipo").val(),cidade:$("#cidade").val(),regiao:$("#regiao").val(),regiao:$("#regiao").val(),faixaValor:fx.val(),valorminimo:fx.attr("valorminimo"),valormaximo:fx.attr("valormaximo"),garagens:$("#garagens").val(),suites:$("#suites").val(),dormitorios:$("#dormitorios").val(),pagina:pagina,ordem:ordem,direcao:direcao};
		$.ajax({
			url: "pesquisa.asp",
			type: 'POST',
			contentType: 'application/x-www-form-urlencoded; charset=ISO-8859-1',
			async: true,
			data: queryString,
			success: function(request) {
				$("#divResultado").html(request);
				navegar.aguarde.hide();
				$(".tabelaImoveisTh").tooltip();
				$(".tabelaImoveisNavegacaoFim").tooltip();
				$(".tabelaImoveisNavegacaoPg").tooltip();
			},
			error: function(request,statusText,errorThrown) {
				alert(statusText + errorThrown);
			}
		});
	},
	seleciona: {
		referencia:null,
		tipoVis:null,
		modal:null,
		options:{resizable:false,modal:true},
		exibe: function(referencia, tipoVis, key, tipo) {
			this.referencia=referencia;
			this.tipoVis=tipoVis;
			navegar.identificacao.modal.dialog('close');
			var my=this;
			var queryString = {codigo: key, tipo: tipo};
			$.ajax({
				url: "carregaCliente.asp",
				type: 'POST',
				contentType: 'application/x-www-form-urlencoded; charset=ISO-8859-1',
				async: true,
				data: queryString,
				success: function(request) {
					my.modal=$(request).dialog(my.options);
					my.modal.bind('dialogclose', function(event, ui) {
						var title=my.modal.dialog("option","title");
						if (title=="Cadastro Realizado") {navegar.pedirIdentificacao=99999;}
						my.modal.dialog('destroy');
					});
				},
				error: function(request,statusText,errorThrown) {
					alert(statusText + errorThrown);
				}
			});
		}
	},
	identificacao: {
		referencia:null,
		tipoVis:null,
		modal:null,
		nome:null,
		tel:null,
		email:null,
		options:{resizable:false,modal:true},
		envia: function(referencia, tipoVis, nome, tel, email, ignoraPesquisa) {
			this.referencia=referencia;
			this.tipoVis=tipoVis;
			this.nome=nome;
			this.tel=tel;
			this.email=email;
			var queryString = {nome:nome,tel:tel,email:email,tipoVis:tipoVis};
			var my=this;
			$.ajax({
				url: "identificaCliente.asp",
				type: 'POST',
				contentType: 'application/x-www-form-urlencoded; charset=ISO-8859-1',
				async: true,
				data: queryString,
				success: function(request) {
					my.modal=$(request).dialog(my.options);
					my.modal.bind('dialogclose', function(event, ui) {
						var title=my.modal.dialog("option","title");
						if (title=="Cadastro Realizado") {navegar.pedirIdentificacao=99999;}
						navegar.exibeImovel(my.referencia, my.tipoVis, 0);
						my.modal.dialog('destroy');
					});
				},
				error: function(request,statusText,errorThrown) {
					alert(statusText + errorThrown);
				}
			});
		},
		nenhum: function() {
			this.modal.dialog("close");
			this.envia(this.referencia, this.tipoVis, this.nome, this.tel, this.email, 1);
		}
	},
	identificar: {
		ident:null,
		campos:null,
		referencia:null,
		tipoVis:null,
		timeouterro:null,
		options:{autoOpen:false,modal:true,resizable:false,width:500,height:220},
		exibe: function(referencia, tipoVis) {
			this.referencia=referencia;
			this.tipoVis=tipoVis;
			if ( navegar.resumo && navegar.resumo != null) navegar.escondeResumo();
			this.ident=$('#divIdent').dialog(this.options);
			this.ident.bind('dialogclose',function(event,ui) {
				navegar.identificar.ident.dialog('destroy');
				navegar.exibeImovel(referencia, tipoVis, 0);
			});
			$('#optIndentS').unbind();
			$('#optIndentN').unbind();
			$('#optIndentS').get(0).checked=false;	
			$('#optIndentN').get(0).checked=false;	
			if (window.ActiveXObject){
				$('#optIndentS').click(function() {
					navegar.identificar.ident.unbind();
					navegar.identificar.ident.dialog('close');
					navegar.identificar.exibeCampos();
				});
				$('#optIndentN').click(function(){$('#divIdent').dialog('close')});
			} else {
				$('#optIndentS').change(function() {
					navegar.identificar.ident.unbind();
					navegar.identificar.ident.dialog('close');
					navegar.identificar.exibeCampos();
				});
				$('#optIndentN').change(function(){$('#divIdent').dialog('close')});
			}
			this.ident.dialog('open');
		},
		exibeCampos: function() {
			var opc=navegar.identificar.options;
			opc.width=550;
			opc.height=250;
			$('#txtTelefone').mask('(99) 9999-9999');
			/*$("#txtNome").val("Eduardo");
			$("#txtEmail").val("a@b.com");
			$('#txtTelefone').val("11 4457-5028");*/
			this.campos=$('#divIdentCampos').dialog(opc);
			this.campos.bind('dialogclose',function(event,ui) {
				navegar.identificar.campos.dialog('destroy');
				navegar.exibeImovel(navegar.identificar.referencia, navegar.identificar.tipoVis, 0);
			});
			this.campos.dialog('open');
		},
		exibeErro: function(msg,obj) {
			$('#'+obj).html(msg);
			if (msg !='') {
				this.timeouterro = setTimeout('navegar.identificar.exibeErro("","'+obj+'")',5000);
			} else {
				if (this.timeouterro) clearTimeout(this.timeouterro);
			}
		},
		checkMail: function(mail) {
			var er=/^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
			if(typeof(mail)=='string') {
				if(er.test(mail)) return true; 
			} else if(typeof(mail)=='object') {
				if(er.test(mail.value)) return true; 
			}
			return false;
		},
		getTelefone: function() {
			var tel='';
			tel=$('#txtTelefone').val();
			return tel;
		},
		valida: function() {
			if (this.timeouterro) {
				this.exibeErro('','tdMens');
			}
			if ($('#txtNome').val() == '') {
				this.exibeErro('Voc&ecirc; deve informar seu Nome','tdMens');
				return false;
			}
			if ($('#txtEmail').val() == '') {
				this.exibeErro('Voc&ecirc; deve informar seu E-mail','tdMens');
				return false;
			} else if (!this.checkMail($('#txtEmail').get(0))) {
				this.exibeErro('Seu E-mail n&atilde;o parece v&aacute;lido','tdMens');
				return false;
			}
			return true;
		},
		identificar: function() {
			if (this.valida()==true) {
				$('#divIdentCampos').unbind();
				$('#divIdentCampos').dialog('close');
				//this.resposta=true;
				this.enviaIdentificacao(false);
			}
		},
		enviaIdentificacao: function(ignoraPesquisa) {
			var nome=$('#txtNome').val();
			var tel=this.getTelefone();
			var email=$('#txtEmail').val();
			$('#divIdentCampos').dialog('close');
			navegar.identificacao.envia(navegar.identificar.referencia, navegar.identificar.tipoVis, nome, tel, email, 0);
		}
	},
	exibeImovel: function(referencia, tipoVis, exibeIndentificar) {
		$("#referencia").val(referencia);
		$("#tipoVis").val(tipoVis);
		if (exibeIndentificar && (parseInt(exibeIndentificar) == 1) && (parseInt(navegar.pedirIdentificacao) < parseInt($("#maximoPedirIdentificacao").val()))) {
			this.identificar.exibe(referencia, tipoVis);
		} else {
			if (parseInt(tipoVis) == 4) {
				document.frmResultadoPesquisa.method="POST";
				document.frmResultadoPesquisa.action="imovel.asp";
				document.frmResultadoPesquisa.submit();
			} else if (parseInt(tipoVis) == 5) {
				document.frmInicial.method="POST";
				document.frmInicial.action="imovel.asp";
				document.frmInicial.submit();
			} else if (parseInt(tipoVis) == 6) {
				//Nao faz nada esta no proprio imovel
			}
		}
	},
	paginarPromo: function(pagina, tipoPromocao) {
		var queryString={pagina:pagina,ctr:this.ctr[tipoPromocao],offset:8};
		var my=this;
		$.ajax({
			url: "promocao.asp",
			type: 'POST',
			contentType: 'application/x-www-form-urlencoded; charset=ISO-8859-1',
			async: true,
			data: queryString,
			success: function(request) {
				$("#" + my.td[tipoPromocao]).html(request);
				$(".divThumb").tooltip();
				if (my.intervalo[tipoPromocao]) {
					clearInterval(my.intervalo[tipoPromocao]);
				}
				my.intervalo[tipoPromocao]=setInterval(function() {
					navegar.pagina(parseInt(pagina)+1,tipoPromocao);
				},my.tempoRefresh);
			},
			error: function(request,statusText,errorThrown) {
				alert(statusText + errorThrown);
			}
		});
	}
};


