// JavaScript Document

var posX = 0;
var posY = 0;
var posXInicial = 0;
var posYInicial = 0;
var posXDivInicial = 0;
var posYDivInicial = 0;
var arrastando = false;
var tamanhoElementos;
var totalEstados;
var totalCidades;
var intRolaCima;
var intRolaBaixo;

function getMouse(e) {
	if (!e) {
		e = window.event;
	}  
	
	if ((e.pageX) && (e.pageY)) {
		posX = e.pageX;
		posY = e.pageY;
		scrollX = 0;
		scrollY = 0;
	} else {
		posX = e.clientX;
		posY = e.clientY;
		scrollX = f_scrollLeft();
		scrollY = f_scrollTop();
	}
	
	if (arrastando == true) {
		selecao(document.body, false);
		selecao(document.documentElement, false);
		difX = posX - posXInicial;
		difY = posY - posYInicial;
		posXFinal = posXDivInicial - 0 + difX;
		posYFinal = posYDivInicial - 0 + difY;
		document.getElementById("filtrosArrastando").style.left = posXFinal+"px";
		document.getElementById("filtrosArrastando").style.top = posYFinal+"px";
		if (posXFinal > 500) {
			document.getElementById("filtrosGeral").style.styleFloat = "right";
			document.getElementById("filtrosGeral").style.cssFloat = "right";
			document.getElementById("filtrosGeral").style.marginTop = "-53px";
		//	document.getElementById("filtroAba").style.styleFloat = "left";
		//	document.getElementById("filtroAba").style.cssFloat = "left";
			document.getElementById("posicaoDoFiltro").value = "right";
		}
		if (posXFinal < 250) {
			document.getElementById("filtrosGeral").style.styleFloat = "left";
			document.getElementById("filtrosGeral").style.cssFloat = "left";
			document.getElementById("filtrosGeral").style.marginTop = "0px";
		//	document.getElementById("filtroAba").style.styleFloat = "right";
		//	document.getElementById("filtroAba").style.cssFloat = "right";
			document.getElementById("posicaoDoFiltro").value = "left";
		}
	}else{
		selecao(document.body, true);
		selecao(document.documentElement, true);
	}
	document.getElementById("searching").style.lineHeight = document.getElementById("handlerv").offsetHeight+"px";
	
//	tamanhoInicial();
}

function selecao(target, act){
	if (!act) {
		if (typeof target.onselectstart != "undefined") //IE route
			target.onselectstart = function() { return false; }
		else if (typeof target.style.MozUserSelect != "undefined") //Firefox route
			target.style.MozUserSelect = "none";
		else //All other route (ie: Opera)
			target.onmousedown = function() { return false; }
	} else {
		if (typeof target.onselectstart != "undefined") //IE route
			target.onselectstart = function() { return true; }
		else if (typeof target.style.MozUserSelect != "undefined") //Firefox route
			target.style.MozUserSelect = "";
		else //All other route (ie: Opera)
			target.onmousedown = function() { return true; }
	}
}

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

//document.onmousemove = getMouse;
if (typeof window.onload == "function") {
	fnc = window.onload;
} else {
	fnc = function() {
		//	
	}	
}

window.onload = function() {
	fnc();
}

function arrasta() {
	arrastando = true;
	document.getElementById("filtrosArrastando").style.display = "";
	document.getElementById("filtrosGeral").style.visibility = "hidden";
	posXInicial = posX;
	posYInicial = posY;
	posXDivInicial = posX - 100;
	posYDivInicial = posY - 10;
	document.getElementById("filtrosArrastando").style.left = posXDivInicial+"px";
	document.getElementById("filtrosArrastando").style.top = posYDivInicial+"px";
}

function stop_arrasta() {
	if (document.getElementById("filtrosArrastando").style.display != "none") {
		posXInicial = 0;
		posYInicial = 0;
		posXDivInicial = 0;
		posYDivInicial = 0;
		arrastando = false;
		document.getElementById("filtrosArrastando").style.display = "none";
		document.getElementById("filtrosGeral").style.visibility = "visible";
	}
}

function tamanhoInicial() {
	tamanhoTela = document.getElementById("handler").offsetWidth;
	tamanhoElementos = document.getElementById("filtrosGeral").offsetWidth;
	tamanhoResultados = tamanhoTela - tamanhoElementos - 10;
//	if (tamanhoResultados > 800) { tamanhoResultados = 800; }
//	document.getElementById("resultados").style.width = tamanhoResultados+"px";
//	document.getElementById("filtroAba").style.height = document.getElementById("filtrosGeral").offsetHeight+"px";
}

function funcaoAba() {
	if (document.getElementById("innerFiltro").style.display == "none") {
		document.getElementById("innerFiltro").style.display = "";
		document.getElementById("filtrosGeral").style.borderBottom = "2px solid #9CBB20";
		document.getElementById("botaoAbaImg").src = "img/ico_fechar_verde.gif";
		document.getElementById("statusDoFiltro").value = "aberto";
		document.getElementById("resultados").style.width = "720px";
	}else{
		document.getElementById("innerFiltro").style.display = "none";
		document.getElementById("filtrosGeral").style.borderBottom = "none";
		document.getElementById("botaoAbaImg").src = "img/ico_abrir_verde.gif";
		document.getElementById("statusDoFiltro").value = "fechado";
		document.getElementById("resultados").style.width = "900px";
	}
}

function aplicaFiltro(id_filtro,destino) {
	valorFiltro = document.getElementById(id_filtro).value;
	if (document.getElementById(id_filtro).checked == false) {
		valorAtual = document.getElementById(destino).value;
		valorNovo = valorAtual;
		while (valorNovo.indexOf("@"+valorFiltro+"@") != -1) {
			valorNovo = valorNovo.replace("@"+valorFiltro+"@","");
		}
		document.getElementById(destino).value = valorNovo;
	}else{
	/*	if (destino == "filtroEstado") {
			document.getElementById("filtroCidade").value = "";
			document.getElementById("filtroBairro").value = "";
		}
		if (destino == "filtroCidade") {
			document.getElementById("filtroBairro").value = "";
			document.getElementById("filtroEstado").value = "";
		}
		if (destino == "filtroBairro") {
			document.getElementById("filtroCidade").value = "";
			document.getElementById("filtroEstado").value = "";
		}*/
		document.getElementById(destino).value += "@"+valorFiltro+"@";
	}
	document.getElementById('geralTotal').style.display='none';
	document.getElementById('filtrosGeral').style.display='none';
	document.getElementById('searching').style.display='';
	document.getElementById("form2").submit();
}

function apagaFiltroCidade(id_filtro,destino) {
	vlFiltro = document.getElementById(id_filtro).value;
	vlAtual = document.getElementById('filtroCidade').value;
	vlNovo = vlAtual;
	while (vlNovo.indexOf("@"+vlFiltro+"@") != -1) {
		vlNovo = vlNovo.replace("@"+vlFiltro+"@","");
	}
	document.getElementById('filtroCidade').value = vlNovo;
	num2 = document.getElementById('totalBairros'+vlFiltro).value;
	j=0;
	while(j<num2){
		j++;
		if (document.getElementById("bairro"+vlFiltro+"["+j+"]").checked == true) {
			vlFiltro2 = document.getElementById("bairro"+vlFiltro+"["+j+"]").value;
			vlAtual2 = document.getElementById('filtroBairro').value;
			vlNovo2 = vlAtual2;
			while (vlNovo2.indexOf("@"+vlFiltro2+"@") != -1) {
				vlNovo2 = vlNovo2.replace("@"+vlFiltro2+"@","");
			}
			document.getElementById('filtroBairro').value = vlNovo;
		}
	}
	document.getElementById('geralTotal').style.display='none';
	document.getElementById('filtrosGeral').style.display='none';
	document.getElementById('searching').style.display='';
	document.getElementById("form2").submit();
}

function apagaFiltroEstado(id_filtro,destino) {
	valorFiltro = document.getElementById(id_filtro).value;
	valorAtual = document.getElementById(destino).value;
	valorNovo = valorAtual;
	while (valorNovo.indexOf("@"+valorFiltro+"@") != -1) {
		valorNovo = valorNovo.replace("@"+valorFiltro+"@","");
	}
	document.getElementById(destino).value = valorNovo;
	num = document.getElementById('totalCidades'+valorFiltro).value;
	i=0;
	while(i<num){
		i++;
		if (document.getElementById("cidade"+valorFiltro+"["+i+"]").checked == true) {
			vlFiltro = document.getElementById("cidade"+valorFiltro+"["+i+"]").value;
			vlAtual = document.getElementById('filtroCidade').value;
			vlNovo = vlAtual;
			while (vlNovo.indexOf("@"+vlFiltro+"@") != -1) {
				vlNovo = vlNovo.replace("@"+vlFiltro+"@","");
			}
			document.getElementById('filtroCidade').value = vlNovo;
			num2 = document.getElementById('totalBairros'+vlFiltro).value;
			j=0;
			while(j<num2){
				j++;
				if (document.getElementById("bairro"+vlFiltro+"["+j+"]").checked == true) {
					vlFiltro2 = document.getElementById("bairro"+vlFiltro+"["+j+"]").value;
					vlAtual2 = document.getElementById('filtroBairro').value;
					vlNovo2 = vlAtual2;
					while (vlNovo2.indexOf("@"+vlFiltro2+"@") != -1) {
						vlNovo2 = vlNovo2.replace("@"+vlFiltro2+"@","");
					}
					document.getElementById('filtroBairro').value = vlNovo2;
				}
			}
		}
	}
	document.getElementById('geralTotal').style.display='none';
	document.getElementById('filtrosGeral').style.display='none';
	document.getElementById('searching').style.display='';
	document.getElementById("form2").submit();
}

function apagaFiltro(destino) {
	document.getElementById('geralTotal').style.visibility='hidden';
	document.getElementById('filtrosGeral').style.visibility='hidden';
	document.getElementById(destino).value = "";
	
	document.getElementById("form2").submit();
}

function enviaRadios() {
	document.getElementById('geralTotal').style.display='none';
	document.getElementById('filtrosGeral').style.display='none';
	document.getElementById('searching').style.display='';
	
	document.getElementById("form2").submit();
}

function pegaFiltrosSelecionados() {
	var filtrosSelecionados = "";
	i = 1;
	for(i=1;i<20;i++){
		if (document.getElementById('tipo_'+i).checked == true) {
			filtrosSelecionados += ", "+document.getElementById('tipostr_'+i).innerHTML;
		}
	}
	for(i=1;i<=totalEstados;i++){
		if (document.getElementById('estado_'+i).checked == true) {
			filtrosSelecionados += ", "+document.getElementById('estadostr_'+i).innerHTML;
		}
	}
	for(i=1;i<=totalCidades;i++){
		if (document.getElementById('cidade_'+i).checked == true) {
			filtrosSelecionados += ", "+document.getElementById('cidadestr_'+i).title;
		}
	}
	for(i=1;i<=4;i++){
		if (document.getElementById('dorm_'+i).checked == true) {
			if(i==4){filtrosSelecionados += ", "+i+" ou mais dorm.";}else{filtrosSelecionados += ", "+i+" dorm.";}
		}
	}
	for(i=1;i<=4;i++){
		if (document.getElementById('suites_'+i).checked == true) {
			if(i==4){filtrosSelecionados += ", "+i+" ou mais suíte(s)";}else{filtrosSelecionados += ", "+i+" suíte(s)";}
		}
	}
	for(i=1;i<=4;i++){
		if (document.getElementById('banheiros_'+i).checked == true) {
			if(i==4){filtrosSelecionados += ", "+i+" ou mais banheiro(s)";}else{filtrosSelecionados += ", "+i+" banheiro(s)";}
		}
	}
	
	document.getElementById('filtrosSelecionadosStr').innerHTML = filtrosSelecionados.substr(2);
}

function abreFechaFiltro(id_filtro,identifier) {
	if (document.getElementById(id_filtro).style.display == "none") {
		document.getElementById(id_filtro).style.display = "";
		identifier.style.backgroundImage = "url(img/ico_seta_baixo.jpg)";
	}else{
		document.getElementById(id_filtro).style.display = "none";
		identifier.style.backgroundImage = "url(img/ico_seta_direita.jpg)";
	}
}

function trocaNegocio(novo_negocio) {
//	document.formNegocio.negocio.value = novo_negocio;
//	document.formNegocio.busca.value = document.form1.busca.value;
//	document.getElementById('formNegocio').submit();
	document.getElementById('geralTotal').style.visibility='hidden';
	document.getElementById('filtrosGeral').style.visibility='hidden';
	document.getElementById("form2").submit();
}

function mostraDivOrdem() {
//	document.getElementById("divOrdenamento").style.left = (posX-50)+"px";
//	document.getElementById("divOrdenamento").style.top = (posY+10)+"px";
	if (document.getElementById("divOrdenamento").style.display == "none") {
		document.getElementById("divOrdenamento").style.left = getPosicaoElemento("btnOrdenamento").left+"px";
		document.getElementById("divOrdenamento").style.top = getPosicaoElemento("btnOrdenamento").top+document.getElementById("btnOrdenamento").offsetHeight-2+"px";
		if (document.all) {
			document.getElementById("divOrdenamento").style.width = document.getElementById("btnOrdenamento").offsetWidth+"px";
		}else{
			document.getElementById("divOrdenamento").style.width = document.getElementById("btnOrdenamento").offsetWidth-2+"px";
		}
		setTimeout('document.getElementById("divOrdenamento").style.display = ""',250);
	}else{
		escondeDivOrdem();
	}
}

function escondeDivOrdem() {
	setTimeout('document.getElementById("divOrdenamento").style.display = "none"',200);
}

function ordenaResultado(campo,ordem) {
	document.getElementById("ord").value = campo+"@"+ordem;
	document.getElementById("divOrdenamento").style.display = "none";
	document.getElementById("form2").submit();
}

function mostrarTodosBairros(aumenta,some) {
	if (document.getElementById(aumenta).style.height != "auto") {
		document.getElementById(aumenta).style.height = "auto";
		some.innerHTML = 'Recolher bairros...';
	}else{
		document.getElementById("filtroLocalidadeCont").scrollTop = 0;
		document.getElementById(aumenta).style.height = "115px";
		some.innerHTML = 'Ver mais bairros...';
	}
}

function rolaCima(quemRola) {
	intRolaCima = setInterval("document.getElementById('"+quemRola+"').scrollTop-=2;",10);
}

function rolaCimaStop() {
	clearInterval(intRolaCima);
}

function rolaBaixo(quemRola) {
	intRolaBaixo = setInterval("document.getElementById('"+quemRola+"').scrollTop+=2;",10);
}

function rolaBaixoStop() {
	clearInterval(intRolaBaixo);
}

function getPosicaoElemento(elemID){
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
	var counter = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
		counter++;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

