// JavaScript EnterActiv 2010

var dirWeb = "http://www.inmodoval.es/";

var paginas = 0;
var numDestacados = 0;
var tiempo;
var estadoCarga = true;	
var progreso = 0;
var seccionWeb = "";
var filtrado = "";
var pasoFiltro = true;
var pasoAni = true;
var paso = 0;
var g_IDidioma = "";

var autoPlay = false;
var cargando = false;
var aux = 0;
var temp;
var tiempoHome;
var velRep = 4000;
var auxAuto = 0;

var scroller  = null;
var scrollbar = null;

var aAncho;
var aAlto;

function init(sec, IDidioma, refe)
{
	seccionWeb = sec;
	g_IDidioma = IDidioma;
	
	$("#menuGeneral #"+sec).addClass("selected");
	if($("#buscadorGeneral").attr("value") != "Ejemplo: 5412 o Pisos en Vigo o  alquiler en porriño...")	$("#buscadorGeneral").css("color", "#333333");
	
	if(sec == "home")
	{
		forzarBola("inicio");
		//pasarPagina('', 1);
	}
	
	if(sec == "venta" || sec == "alquiler" || sec == "avanzado" || sec == "general")
	{
		var url = window.location.hash.substring(2);
		variables = url.split("/");
		
		if(variables[0] == "" || variables[0] <= 0) variables[0] = 1;
		pasarPaginaLista('', IDidioma, sec, (variables[0]-1), "");
	}
	
	if(sec == "ficha")
	{
		$("#visor img").fadeOut("fast");
		prepararOvers();
		cargarGaleria($("#visita"), refe, "visita");
		
		var flashvars = {referencia:refe, idioma:"castellano"};
		var params = {menu:"false", allowFullScreen:"true"};
		var attributes = {id:"visorFlash", name:"visorFlash", allowFullScreen:"true"};
 
		swfobject.embedSWF(dirWeb+"/flash/visor.swf", "contenedorFlash", "150", "30", "10.0.0", dirWeb+"/flash/expressInstall.swf", flashvars, params, attributes);
 
 		ponerMapaInmueble($("#mapa").html(), "mapa");
	}
	
	if(sec == "empresa")
	{
		cargarGaleriaEmpresa("#fotos", "porrino");
	}
	
	if(sec == "contacto")
	{
		ponerMapa("42.16258032760227;-8.621494174003601", 15, "mapa");
		ponerMapa("42.236332159299195;-8.724214732646942", 15, "mapa2");
	}
	
	if ($.browser.msie && $.browser.version.substr(0,1) < 7) 	navegar("html/update.php");
	else														$("body").fadeIn("fast");
	
	addEventos();
}

function addEventos()
{
	$("#areaBuscador").click(function(e) 
	{ 
		cerrarPaneles();
		plegar();
	});
}

function capturarEventos()
{
	$("#areaBuscador").css("width", "100%");
	$("#areaBuscador").css("height", "100%");
}

/*function scrollbarEvent (o, type) 
{
	if (type == "mousedown") {
		if (o.className == "Scrollbar-Track") o.style.backgroundColor = "#E3E3E3";
		else o.style.backgroundColor = "#BBB";
	} else {
		if (o.className == "Scrollbar-Track") o.style.backgroundColor = "#EEE";
		else o.style.backgroundColor = "#CCC";
	}
}*/

function cargarImagenes(contenedor, ancho, alto, forzar)
{
	aAncho = ancho;
	aAlto = alto;
	
	$(contenedor+'.image').load(function()
	{
		if($(this).width() > $(this).height())
		{
			if($(this).width() > aAncho)
			{
				$(this).css("height", (alto+20)+"px");
				var left = (aAncho - $(this).width()) / 2;
				$(this).css("margin-left", left+"px");
			}
		}
		else
		{
			if($(this).width() > aAncho)			$(this).css("width", aAncho+"px");
			var left = (aAncho - $(this).width()) / 2;
			$(this).css("margin-left", left+"px");
		}
		
		if($(this).height() < aAlto)
		{
			var top = (aAlto - $(this).height()) / 2;
			$(this).css("margin-left", left+"px");
		}
		
		$(this).fadeIn("slow");		
	});
}

function prepararOvers()
{
	if(seccionWeb == "home")
	{
		$(".destacado").bind("mouseenter", function()	{	$(".fondo", this).animate({opacity:1}, 200);	});
		$(".destacado").bind("mouseleave", function()	{	$(".fondo", this).animate({opacity:.5}, 200);	});	
	}
	
	if(seccionWeb == "ficha")
	{
		$("#thums img").bind("mouseenter", function()	{	$(this).css("border-color", "#666");	});
		$("#thums img").bind("mouseleave", function()	{	$(this).css("border-color", "#e7e6e6");	});	
	}
}

function cargar()	//Controla el progreso automatico del paginizado de los destacados
{
	if(estadoCarga)
	{
		$('.barraCarga').stop();
		$('.barraCarga').animate({width:"100%"}, 8000, "linear", function()
		{
			restablecerTiempo();
			pasarPagina('avanzar', g_IDidioma);		
		});
	}
}

function restablecerTiempo()
{
	$('.barraCarga').animate({width:"0px"}, 500, "easeInOutQuart");
}

function cambiarEstadorCarga()
{
	if(estadoCarga)
	{
		estadoCarga = false;
		$('.barraCarga').stop();
		restablecerTiempo();
		$('#stop').html("ON");
	}
	else
	{
		estadoCarga = true;
		cargar();
		$('#stop').html("OFF");
	}
}


/* PRESENTACION ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

function moverPortada(bola, url)
{
	clearInterval(tiempoHome);
	
	$('#bolas a').removeClass("btnBolaSelected");
	$('#bolas a').addClass("btnBola");
	bola.className = "btnBolaSelected";
	
	$('.imagenes img').fadeOut("slow");
	$('.imagenes img[src$="'+url+'"]').fadeIn("slow");
	
	tiempoHome = setInterval("forzarBola('')", 6000);
}

function forzarBola(modo)
{
	if(modo == "inicio")
	{
		var bola = $('#bolas a').eq(0);
		bola.click();
	}
	else
	{
		var ant = $('#bolas a').filter('.btnBolaSelected');
		var sig = $('#bolas a').filter('.btnBolaSelected').next();
		
		if(ant.index() == $('#bolas a:last').index())	sig = $('#bolas a:first');
		sig.click();
	}
}



/* ANIMACION & INTERACCION DE LA LISTA /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

function verInfos()
{
	$(".imagen").bind("mouseenter", function()	
	{
		$(this).css("border-color", "#333");		
		$(this).prev().animate({opacity:1, width:450}, 200, "easeOutQuart");
	});
	
	$(".imagen").bind("mouseleave", function()
	{
		$(this).css("border-color", "#CCC");		
		$(this).prev().animate({opacity:0, width:0}, 200, "easeOutQuart");
	});
	
	$(".imagen").bind("click", function()
	{
		$(this).css("border-color", "#CCC");		
		$(this).prev().animate({opacity:0, width:0}, 200, "easeOutQuart");
	});
}

function cambiarOrden(filtro)
{
	var url = window.location.hash.substring(2);
	variables = url.split("/");
	
	if(filtrado != filtro)	pasoFiltro = true;
	
	if(pasoFiltro == true)
	{
		pasarPaginaLista('', '', "venta", (variables[0]-1), filtro+"A");
		pasoFiltro = false;	
	} 
	else if(pasoFiltro == false)
	{
		pasarPaginaLista('', '', "venta", (variables[0]-1), filtro+"B");
		pasoFiltro = true;	
	}
	
	filtrado = filtro;	
}

function verPaginizado(pag, sec, intervalo)
{
	
	var datos = $("#datos").html();
	
	if(datos == "")		$("#paginizado").css("display", "none");
	else
	{
		var matriz = datos.split(";");
		paginas = (Math.ceil(matriz[0]/intervalo));
		
		//Info para paginas
		$("#paginas").html("P&aacute;gina <b>"+pag+"</b> de "+(Math.ceil(matriz[0]/intervalo))+"");
		
		//Info para el intervalo
		numDestacados = matriz[0];
		final = sec + (intervalo-1);
		if(final > matriz[0])	final = matriz[0];
		$("#intervalo").html("Viendo <b>"+sec+" - "+final+"</b> de "+matriz[0]+" inmuebles");
		
		restablecerTiempo();
		prepararOvers();
		cargarImagenes("", 231, 161, true);
		cargar();
	}
}



/* ANIMACION & INTERACCION DE LA FICHA /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

function cambiarOpcion(padre, opc)
{
	if(!$("#"+opc).hasClass("disable"))
	{
		$("#"+padre+" li").removeClass("selected");
		$("#"+opc).addClass("selected");
	}
}

function verImagen(thum, url, titulo)
{
	$("#thums .contenedor .thumb").css("border-color", "#e7e6e6");
	$(thum).parent().css("border-color", "#333");
	
	//auxAuto = $(thum).index();
	//alert(auxAuto);
	
	cargando = true;
	verInfoGaleria(titulo)
		
	$("#visor img").fadeOut("fast", function()	
	{
		$("#visor").html("<div class='carga'></div><div id='contImagen'> <img class='image' src='"+url+"' title=''> </div>");
		$("#visor img").css("display", "none");
		
		$('.image').load(function()
		{
			var ancho = $(this).width();
			var alto = $(this).height();
			var left = 0;
			var top = 0;
			
			if(alto > $("#visor").height())
			{
				if($(this).height() > $("#visor").height())	$(this).css("height", $("#visor").height()+"px");
				if($(this).width() > $("#visor").width())	
				{
					var anchoNuevo = (ancho * $(this).height()) / alto;
					$(this).css("width", anchoNuevo+"px");
				}
			}
			else
			{
				if($(this).width() > $("#visor").width())	$(this).css("width", $("#visor").width()+"px");
				if($(this).height() > $("#visor").height())	
				{
					var altoNuevo = (alto * $(this).width()) / ancho;
					$(this).css("height", altoNuevo+"px");	
				}
			}
			
			left = ($("#visor").width() - $(this).width()) / 2;
			
			$(this).css("left", left+"px");
			$("#visor .carga").css("display", "none");
			$("#visor").css("padding", "10px 10px 10px 10px");
			
			cargando = false;
			ancho = $(this).width();
			alto = $(this).height();
			
			if(ancho > $("#visor").width())		//Para las visitas
			{
				$("#visor #contImagen").css("width", ((ancho*2) - $("#visor").width())+"px"); 
				
				var medio = ancho - $("#visor").width() - 10;
				
				$("#visor #contImagen").css("left", (-medio)+"px"); 
				$("#visor img").draggable({ axis: "x", containment: "#contImagen", cursor: 'move', scroll: false });
				$(this).css("margin-left", (ancho - $("#visor").width())+"px");
			}
			
			$(this).fadeIn("slow");
		});
	
	});
}

function automatico()
{
	if(autoPlay)
	{
		if(auxAuto == $("#thums .contenedor img").length)	auxAuto = -1;
		
		if(!cargando)
		{
			auxAuto++;
			
			var pos = $("#thums .contenedor").position();
			var posThum = $("#thums .contenedor .thumb").eq(auxAuto).position();
			
			if(posThum.left > Math.abs(pos.left) + 900)	moverGaleria("avanzar");
			$("#thums .contenedor img").eq(auxAuto).trigger('click');
		}
	}
	
	/*if(autoPlay && auxAuto >= $("#thums .contenedor img").length)
	{
		autoPlay = true;
		play();
		auxAuto = 0;
	}*/
}

function play()
{
	if(!autoPlay)
	{
		autoPlay = true;	
		$(".auto").html("<img src='"+dirWeb+"css/img/interface/icons/16/pause.png' />PARAR PRESENTACI&Oacute;N");
		automatico();
		temp = setInterval("automatico()", velRep);
	}
	else
	{
		autoPlay = false;	
		$(".auto").html("<img src='"+dirWeb+"css/img/interface/icons/16/play.png' />INICIAR PRESENTACI&Oacute;N");
		clearInterval(temp);
	}
}

function moverGaleria(tipo)
{
	var pos = $("#thums .contenedor").position();
	var final = 0;
	var ancho = $("#thums .contenedor img").length * 133;
	
	if(tipo == "avanzar" && pasoAni == true)
	{
		final = pos.left - 810;
		if(final > -ancho)	
		{
			 pasoAni = false;
			 $('#thums .contenedor').animate({"left":final+"px"}, 400, "easeOutQuart", function()	{	pasoAni = true;	});
		}
	}
	
	if(tipo == "retroceder" && pasoAni == true)
	{
		final = pos.left + 810;
		if(final < 820)
		{
			 pasoAni = false;
			 $('#thums .contenedor').animate({"left":final+"px"}, 400, "easeOutQuart", function()	{	pasoAni = true;	});
		}
	}
	
	if(tipo == "reiniciar" && pasoAni == true)
	{
		final = 10;
		pasoAni = false;
		$('#thums .contenedor').animate({"left":final+"px"}, 400, "easeOutQuart", function()	{	pasoAni = true;	});
	}
}

function verInfoGaleria(titulo)
{
	var total = $("#thums .contenedor img").length;
	$("#paginas").html("Fotograf&iacute;a "+(aux+1)+" de "+total);
	//$("#intervalo").html(titulo);
}

function moverPaneles(panel)
{
	var pos = $("#"+panel).index() * 980;
	$("#escenarios").animate({"left": "-"+pos+"px"}, 400, "easeOutQuart");
}

function moverPaso(dir)
{
	var pos = 0;
	
	if(dir == "der" && paso < $("#pasos .campoGrande").length - 1)
	{
		if(validarFormulario(document.forms.fHipoteca, paso))	paso++;
	}
	
	if(dir == "izq" && paso > 0)	
	{
		$("#Lhipoteca .divB").fadeOut("fast");
		paso--;
	}
	
	if(paso == $("#pasos .campoGrande").length - 1)	calcularHipoteca();
	
	pos = paso * 900;
	$("#pasos").animate({"left": "-"+pos+"px"}, 200, "easeOutQuart", function()
	{
		if(paso == $("#pasos .campoGrande").length - 1)	$("#Lhipoteca .btnMedio").animate({opacity:1}, 100);
		else											$("#Lhipoteca .btnMedio").animate({opacity:0.5}, 100);
				
	});
}

function validarFormulario(form, tope, legal)
{
	var paso = true;
	var sms = "";
	
	for(var i=0;i<form.elements.length;i++)
	{
		if($(form.elements[i]).hasClass("requerido")) if(form.elements[i].value == "")			
		{
			paso = false;
			sms = "Campo obligatorio";
			break;
		}
		
		if($(form.elements[i]).hasClass("numero")) if(form.elements[i].value != "" && !validarNumero(parseInt(form.elements[i].value)))	
		{
			paso = false;
			sms = "Este datos debe ser un n&uacute;mero";
			break;
		}
		
		if($(form.elements[i]).hasClass("mayor")) if(parseInt(form.elements[i].value) <= 0)	
		{
			paso = false;
			sms = "Este dato debe ser un n&uacute;mero mayor de 0";
			break;
		}
		
		if($(form.elements[i]).hasClass("mayor")) if(parseInt(form.elements[i].value) > parseInt(form.elements[i].alt))	
		{
			paso = false;
			sms = "Este dato debe ser un n&uacute;mero menor que "+parseInt(form.elements[i].alt);
			break;
		}
		
		if($(form.elements[i]).hasClass("mail")) if(!validarEmail(form.elements[i].value))	
		{
			paso = false;
			sms = "Este e-mail tiene un formato incorrecto";
			break;
		}
		
		if(i == form.elements.length-1 && legal != "undefined" && legal != "") 
		{
			if(!$("#"+legal).hasClass("chkPanelSelected"))	
			{
				paso = false;
				sms = "Debe aceptar las condiciones de uso";
				break;
			}
		}
		
		var cajon = $(form.elements[i]).parent();	
		
		$(cajon).css({backgroundColor: '#fff', border: 'none'});
		$(".error", cajon).html("");
		
		if(tope != "undefined" && i == tope)	break;
	}
	
	if(!paso)
	{
		var cajon;
		
		if(i == form.elements.length-1 && legal != "undefined" && legal != "") 
		{
			cajon = $("#"+legal).parent();	
			$(".error", cajon).css({color: '#d6523f'});
			$(".error", cajon).html(sms);
		}
		else
		{
			form.elements[i].focus();
			cajon = $(form.elements[i]).parent();	
			$(cajon).css({backgroundColor: '#dd7a6b', border: '1px solid #d6523f'});
			$(".error", cajon).html(sms);
		}
		
		return false;	
	}
	
	return true;
}



/* BUSCADORES ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

function desplegar()
{
	var pos = $("#contenido").position().top;
	
	$('#desplegado').css('height', 0);
	$('#desplegado').css('display', 'block');
	$('#desplegado').animate({height:1000}, 500, "easeOutQuart");
	if(pos > 100)	$('html, body').animate({scrollTop: pos}, 300, "easeOutQuart");
	capturarEventos();
}

function plegar()
{
	$('#desplegado').animate({height:0}, "fast", "easeOutQuart", function()
	{
		$('#desplegado').css('display', 'none');
		$("#desplegado").css("width", "320px");	
		$("#areaBuscador").css("width", "0px");
		$("#areaBuscador").css("height", "0px");
	});
}

function verDatos(cont, combo)
{
	var txt = "";
	var total = $("#"+cont+" .fila a").length;
	
	//Comprobar si estan todos chekeados
	if($("#"+cont+" a").hasClass("chkPanel"))
	{
		$("#"+cont+" .chkTodos").addClass("chkPanel");
		$("#"+cont+" .chkTodos").removeClass("chkPanelSelected");
	}
	
	for(i=0;i<total;i++)
	{	
		if($("#"+cont+" .fila a").eq(i).hasClass("chkPanelSelected") && $("#"+cont+" .fila a").eq(i).attr("id") != "todos" && $("#"+cont+" .fila").eq(i).css("display") == "block")		txt += $("#"+cont+" .fila a").eq(i).attr("id")+", ";	
	}
	
	txt = txt.replace("<br />", "- ");
	txt = txt.substr(0, txt.length-2);
	txt = recortarCadena(txt, 21);
	$("#"+combo).html(ucWords(txt));
	
	if(txt != "")	$("#"+combo).removeClass("cmbDisabled");
	else			$("#"+combo).hasClass("cmbDisabled");
}

function elegirDatos(obj, cont, combo)
{
	if($(obj).hasClass("chkTodos"))
	{
		if($(obj).hasClass("chkPanel"))
		{
			$("#"+cont+" .chkPanel").filter(function(index) {  return $(this).css("display") == "block";  }).addClass("chkPanelSelected");
			$("#"+cont+" .chkPanel").filter(function(index) {  return $(this).css("display") == "block";  }).removeClass("chkPanel");	
			
		}
		else if($(obj).hasClass("chkPanelSelected"))
		{
			$("#"+cont+" .chkPanelSelected").filter(function(index) {  return $(this).css("display") == "block";  }).addClass("chkPanel");
			$("#"+cont+" .chkPanelSelected").filter(function(index) {  return $(this).css("display") == "block";  }).removeClass("chkPanelSelected");	
		}
	}
	else
	{
		if($(obj).hasClass("chkPanel"))
		{
			$(obj).removeClass("chkPanel");
			$(obj).addClass("chkPanelSelected");
		}
		else if($(obj).hasClass("chkPanelSelected"))
		{
			$(obj).removeClass("chkPanelSelected");
			$(obj).addClass("chkPanel");	
		}
	}
	
	verDatos(cont, combo);
}

function elegirDato(obj, tipo)
{
	var combo = document.getElementById(tipo);
	
	if(tipo == "precio")
	{
		if(obj.id == "mas")	combo.innerHTML = "superior a 1.500.000 &euro;";
		else				combo.innerHTML = "hasta "+obj.id+" &euro;";
	}
	else
	{
		combo.innerHTML = obj.id+"";
	}
}

function filtrarLista(idPanel, combo, idPanelVinculado, lista)
{
	var total = $("#"+idPanel+" .fila a").length;
	var totalVinculados = $("#"+idPanelVinculado+" .fila a").length;
	var filtros = Array();
	var aux = 0;
	
	//Reviso los check que estan punteados
	for(i=0;i<total;i++)	
	{	
		if($("#"+idPanel+" .fila a").eq(i).hasClass("chkPanelSelected") && $("#"+idPanel+" .fila").eq(i).css("display") == "block")	filtros.push($("#"+idPanel+" .fila a").eq(i).html());
	}
	
	//Oculto todos los check del panel que depende del anterior
	//Recorro todos los filtros y en cada uno compruebo todos los check dentro del panel vinculado. Los que tengan una clase = al filtro los activo
	$("#"+idPanelVinculado+" .fila").css("display", "none");
	$("#"+idPanelVinculado+" .fila a").removeClass("chkPanelSelected");
	$("#"+idPanelVinculado+" .fila a").addClass("chkPanel");
		
	for(i=0;i<filtros.length;i++)
	{
		for(j=0;j<totalVinculados;j++)
		{	
			if($("#"+idPanelVinculado+" .fila").eq(j).hasClass(filtros[i].toUpperCase()) && $("#"+idPanelVinculado+" .fila a").eq(j).html() != filtros[i].toUpperCase())		
			{
				$("#"+idPanelVinculado+" .fila").eq(j).css("display", "block");
				aux++;
			}
		}
	}
	
	//Si no hay ningun resultado desactivo el combo vinculado
	if(aux == 0)
	{
		$("#"+combo).addClass("cmbDisabled");
		$("#"+combo).html("");
	}
	else
	{
		$("#"+idPanelVinculado+" .chkTodos").parent().css("display", "block");
	}
}

function filtrarListaDefecto(idPanel, idPanelVinculado, comboVinculado)
{
	var total = $("#"+idPanel+" .fila a").length;
	var totalVinculados = $("#"+idPanelVinculado+" .fila a").length;
	var filtros = Array();
	var aux = 0;
	
	//Reviso los check que estan punteados
	for(i=0;i<total;i++)	
	{	
		if($("#"+idPanel+" .fila a").eq(i).hasClass("chkPanelSelected"))	filtros.push($("#"+idPanel+" .fila a").eq(i).html());
	}
	
	//Oculto todos los check del panel que depende del anterior
	//Recorro todos los filtros y en cada uno compruebo todos los check dentro del panel vinculado. Los que tengan una clase = al filtro los activo
	$("#"+idPanelVinculado+" .fila").css("display", "none");
		
	for(i=0;i<filtros.length;i++)
	{
		for(j=0;j<totalVinculados;j++)
		{	
			if($("#"+idPanelVinculado+" .fila").eq(j).hasClass(filtros[i].toUpperCase()) && $("#"+idPanelVinculado+" .fila a").eq(j).html() != filtros[i].toUpperCase())		
			{
				$("#"+idPanelVinculado+" .fila").eq(j).css("display", "block");
				aux++;
			}
		}
	}
	
	if(aux > 0)	$("#"+comboVinculado).removeClass("cmbDisabled");
}

function vaciarLista(idPanel)
{
	var total = $("#"+idPanel+" .fila a").length;
	var aux = 0;
	
	//Reviso los check que estan punteados
	for(i=0;i<total;i++)	
	{	
		if($("#"+idPanel+" .fila a").eq(i).hasClass("chkPanelSelected"))	
		{
			$("#"+idPanel+" .fila a").removeClass("chkPanelSelected");
			$("#"+idPanel+" .fila a").addClass("chkPanel");
		}
		$("#"+idPanel+" .fila a").css("display", "block");
	}
}

function abrirPanel(tipo, idPanel, idVinculado, idPanelVinculado)
{
	var panel = document.getElementById(idPanel);
	cerrarPaneles(idPanel);
	
	if(panel.style.display == 'none' && !$("#"+tipo).hasClass("cmbDisabled"))
	{
		var pos = $("#"+tipo).position();
		panel.style.left = (pos.left + $("#"+tipo).width() - 8) + 'px';
		panel.style.top = (pos.top-9) + 'px';
		panel.style.display = 'block';
		
		if(!$("#"+idPanel+" .middle").hasClass("usado"))
		{
			$("#"+idPanel+" .middle .fila a").click(function(e) 
			{ 
				elegirDatos(this, idPanel, tipo);
				
				if(tipo == "regimen")	verPrecios(tipo)
				
				var vinculados = idVinculado.split(",");
				
				if($("#"+tipo).html() != "")
				{
					$("#"+vinculados[0]).removeClass("cmbDisabled");
				}
				else	
				{
					for(i=0;i<vinculados.length;i++)
					{
						$("#"+vinculados[i]).addClass("cmbDisabled");
						borrar(vinculados[i]);																//Borra el contenido del cmb
					}	
				}
				
				filtrarLista(idPanel, vinculados[0], idPanelVinculado, $("#"+tipo).html());
			});
			$("#"+idPanel+" .middle").addClass("usado");
		}
	}
	else
	{
		panel.style.display = 'none';
	}
}

function cerrarPaneles(idPanel)
{
	if(idPanel != "")	$("#paneles .panel").not("#"+idPanel+"").css("display", "none");
	else				$("#paneles .panel").css("display", "none");
}

function chekear(obj)
{
	if($(obj).hasClass("chkPanel"))
	{
		$(obj).removeClass("chkPanel");
		$(obj).addClass("chkPanelSelected");
	}
	else if($(obj).hasClass("chkPanelSelected"))
	{
		$(obj).removeClass("chkPanelSelected");
		$(obj).addClass("chkPanel");	
	}
}

function validarChek(id, value)
{	
	if(value == "true")	
	{
		$("#"+id).removeClass("chkPanel");
		$("#"+id).addClass("chkPanelSelected");
	}
}

function areaFocusIn(obj, texto)
{
	if(obj.value == texto)
	{
		obj.value = "";	
		obj.style.color = "#333";
		if(obj.name == "clave")		obj.type = "password";	
	}
}

function areaFocusOut(obj, texto)
{
	if(obj.value == "")
	{
		obj.style.color = "#999";
		obj.value = texto;	
		if(obj.name == "clave")		obj.type = "text";	
	}
}

function limpiar()
{
	$("#regimen").html("");
	vaciarLista("cRegimenes");
	
	$("#tipo").html("");
	vaciarLista("cTipos");
	
	$("#pais").html("");
	vaciarLista("cPaises");
	
	$("#provincia").html("");
	$("#provincia").addClass("cmbDisabled");
	vaciarLista("cProvincias");
	
	$("#poblacion").html("");
	$("#poblacion").addClass("cmbDisabled");
	vaciarLista("cPoblaciones");
	
	$("#zona").html("");
	$("#zona").addClass("cmbDisabled");
	vaciarLista("cZonas");
	
	$("#distrito").html("");
	$("#distrito").addClass("cmbDisabled");
	vaciarLista("cDistritos");
	
	$("#precio").html("");
	
	$("#metro").html("");
	$("#dormitorio").html("");
	$("#bano").html("");
	
	if(getChekeo("garaje"))		chekear($("#garaje"));
	if(getChekeo("ascensor"))	chekear($("#ascensor"));
	if(getChekeo("terraza"))	chekear($("#terraza"));
	if(getChekeo("chimenea"))	chekear($("#chimenea"));
	if(getChekeo("piscina"))	chekear($("#piscina"));
}

function borrar(id)
{
	$("#"+id).html("");											//Borra el contenido del cmb
	$("#"+id+" .chkPanel").removeClass("chkPanelSelected");		//Deseleccionarlos todos
}

function buscarCotenido(idPanel, tipo)
{
	var res = "";
	
	if(tipo == "valor")
	{
		res = $("#"+idPanel).html();
	}
	else
	{
		var total = $("#"+idPanel+" .fila a").length;
		
		//Reviso los check que estan punteados
		for(i=0;i<total;i++)	
		{	
			if($("#"+idPanel+" .fila a").eq(i).hasClass("chkPanelSelected"))	
			{
				res += $("#"+idPanel+" .fila a").eq(i).html();
				res += ", ";
			}
		}
	}
	
	return res;
}

function getChekeo(idChek)
{
	if($("#"+idChek).hasClass("chkPanelSelected")) 	return true;
	else											return false;
}

function checkKey(tipo, buscador)
{
	if(window.event.keyCode == 13) 
	{
		buscar(tipo, buscador);
	}
}

function ponerScroll(div, area)
{
	scroller  = new jsScroller(document.getElementById(div), 400, 200);
 	scrollbar = new jsScrollbar (document.getElementById(area), scroller, false);
}

function verPrecios(combo)
{
	var filtro = $("#"+combo).html().toLowerCase();
	
	if(filtro == "" || filtro.search("venta") >= 0 || filtro.search("traspaso") >= 0)
	{
		$(".pVenta").css("display", "block");
		$(".pAlquiler").css("display", "none");
	}
	else
	{
		$(".pAlquiler").css("display", "block");
		$(".pVenta").css("display", "none");
	}
}


/* SERVICIOS ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

function moverBody(div)
{
	var pos = 0;
	if(div != "0")	pos = $("#"+div+"").position().top + 150;
	$('html, body').animate({scrollTop: pos}, 300, "easeOutQuart");
}


/* EMPRESA ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

/*function cargarGaleriaEmpresa(tipo)
{
	var galeria = "";
	
	if(tipo == "porrino")
	{
		galeria += "<img class='image' onclick='verImagen(this, \""+dirWeb+"images/empresa/porrino/img_01.jpg\", \"\")' src='"+dirWeb+"images/empresa/porrino/img_01.jpg' title='' />";
		galeria += "<img class='image' onclick='verImagen(this, \""+dirWeb+"images/empresa/porrino/img_02.jpg\", \"\")' src='"+dirWeb+"images/empresa/porrino/img_02.jpg' title='' />";
		galeria += "<img class='image' onclick='verImagen(this, \""+dirWeb+"images/empresa/porrino/img_03.jpg\", \"\")' src='"+dirWeb+"images/empresa/porrino/img_03.jpg' title='' />";
		galeria += "<img class='image' onclick='verImagen(this, \""+dirWeb+"images/empresa/porrino/img_04.jpg\", \"\")' src='"+dirWeb+"images/empresa/porrino/img_04.jpg' title='' />";
		galeria += "<img class='image' onclick='verImagen(this, \""+dirWeb+"images/empresa/porrino/img_05.jpg\", \"\")' src='"+dirWeb+"images/empresa/porrino/img_05.jpg' title='' />";
		galeria += "<img class='image' onclick='verImagen(this, \""+dirWeb+"images/empresa/porrino/img_06.jpg\", \"\")' src='"+dirWeb+"images/empresa/porrino/img_06.jpg' title='' />";
		galeria += "<img class='image' onclick='verImagen(this, \""+dirWeb+"images/empresa/porrino/img_07.jpg\", \"\")' src='"+dirWeb+"images/empresa/porrino/img_07.jpg' title='' />";
		galeria += "<img class='image' onclick='verImagen(this, \""+dirWeb+"images/empresa/porrino/img_09.jpg\", \"\")' src='"+dirWeb+"images/empresa/porrino/img_09.jpg' title='' />";
	}
	
	if(tipo == "vigo")
	{
		galeria += "<img class='image' onclick='verImagen(this, \""+dirWeb+"images/empresa/vigo/img_03.jpg\", \"\")' src='"+dirWeb+"images/empresa/vigo/img_03.jpg' title='' />";
		galeria += "<img class='image' onclick='verImagen(this, \""+dirWeb+"images/empresa/vigo/img_05.jpg\", \"\")' src='"+dirWeb+"images/empresa/vigo/img_05.jpg' title='' />";
		galeria += "<img class='image' onclick='verImagen(this, \""+dirWeb+"images/empresa/vigo/img_01.jpg\", \"\")' src='"+dirWeb+"images/empresa/vigo/img_01.jpg' title='' />";
		galeria += "<img class='image' onclick='verImagen(this, \""+dirWeb+"images/empresa/vigo/img_02.jpg\", \"\")' src='"+dirWeb+"images/empresa/vigo/img_02.jpg' title='' />";
		galeria += "<img class='image' onclick='verImagen(this, \""+dirWeb+"images/empresa/vigo/img_04.jpg\", \"\")' src='"+dirWeb+"images/empresa/vigo/img_04.jpg' title='' />";
		
	}
	
	$("#thums .contenedor").html(galeria);
	$("#thums .contenedor img").eq(0).trigger('click');
	$("#thums").animate({opacity:1}, 300);
	cargarImagenes("#thums .contenedor ", 110, 90);
	moverGaleria("reiniciar");
}
*/

/* CONTACTO ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

function ampliarMapa(btn, div)
{
	if($(btn).hasClass("btnAmpliar"))	
	{
		$(btn).removeClass("btnAmpliar");
		$(btn).addClass("btnReducir");
	}
	else if($(btn).hasClass("btnReducir"))	
	{
		$(btn).removeClass("btnReducir");
		$(btn).addClass("btnAmpliar");
	}
	
	if($("#"+div).height() < 300)	$("#"+div).animate({height: 450}, 300, "easeOutQuart");
	else							$("#"+div).animate({height: 215}, 300, "easeOutQuart");
	
	
}


/* HIPOTECA ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

function calcularHipoteca()
{
	var form = document.forms.fHipoteca;
	var euribor = 1.92;	//Variable
	var capital = parseFloat(unFormat(form.valor.value));
	var precio = parseFloat(unFormat(form.precio.value));
	var interes = (euribor + 1)/100;	 
	var plazo = parseInt(form.plazo.value);
	var aportacion = parseFloat(unFormat(form.aportacion.value));
	var liquidez = parseFloat(unFormat(form.liquidez.value));
	
	if(String(aportacion) == "NaN")	aportacion = 0;
	if(String(liquidez) == "NaN")	liquidez = 0;
	
	var valor = capital + (capital * 0.13) - aportacion + liquidez;
	var evel = Math.pow((1+interes), -plazo);
	var cuota = valor/((1-evel)/interes)/12;
	
	//cuota = formatearPrecio(MoneyFormat(cuota));
	
	$("#resultado").html("<p>Valor del Prestamo: "+formatearPrecio(valor)+" &euro; que constituye el "+calPorcentaje(valor, precio)+"&permil; del valor del inmueble<br /><br />" +
                         "Cuota Mensual: "+formatearPrecio(MoneyFormat(cuota))+" &euro;</p>");
	
	if(paso == 4)	$("#Lhipoteca .divB").fadeIn("fast");
}

function calPorcentaje(valor, precio)
{
	var por = (valor / precio) * 100;
	return formatearPrecio(MoneyFormat(por));
}


/*
function calNotarioHipoteca(valor)
{
	var notaria = 0;
	
	if((valor * 1.8 - 30050.61) < 0)			notaria = ((90 + ((valor * 1.8) - 6010.13) * 0.0045) * 0.75) * 0.75 + 275;		
	else if((((valor * 1.8) - 60101.22) < 0))	notaria = (90 + (24040.49 * 0.0045 + ((valor * 1.8) - 30050.61) * 0.0015) * 0.75) * 0.75 + 275;
	else if((((valor * 1.8) - 150253.03) < 0))	notaria = (90 + ((24040.49 * 0.0045) + (30050.61 * 0.0015) + ((valor *1.8) - 60101.22) * 0.001) * 0.75) * 0.75 + 275;
	else if((((valor * 1.8) - 601012.1) < 0))	notaria = (90 + ((24040.49 * 0.0045) + (30050.61 * 0.0015) + (90151.82 * 0.001) + ((valor * 1.8) - 150253.03) * 0.0005)) * 0.75 * 0.75 + 275;
	else										notaria = (90 + ((24040.49 * 0.0045) + (30050.61 * 0.0015) + (90151.82 * 0.001) + (450759.07 * 0.0005) + ((valor * 1.8) - 601012.1) * 0.0003) * 0.75) * 0.75 + 275;	
	
	//document.getElementById("notaria_hipoteca").innerHTML = formatearPrecio(MoneyFormat(notaria));
}

function calRegistroHipoteca(valor)
{
	var registro = 0;
	
	if((valor * 1.8 - 30050.61) < 0)			registro = ((24.04 + ((valor * 1.8) - 6010.13) * 0.00175)) * 0.75 * 0.95 + 6.01;		
	else if((((valor * 1.8) - 60101.22) < 0))	registro = (24.04 + (24040.49 * 0.00175 + ((valor * 1.8) - 30050.61) * 0.00125)) * 0.75 * 0.95 + 6.01;
	else if((((valor * 1.8) - 150253.03) < 0))	registro = (24.04 + ((24040.49 * 0.00175) + (30050.61 * 0.00125) + ((valor *1.8) - 60101.22) * 0.00075)) * 0.75 * 0.95 + 6.01;
	else if((((valor * 1.8) - 601012.1) < 0))	registro = (24.04 + ((24040.49 * 0.00175) + (30050.61 * 0.00125) + (90151.82 * 0.00075) + ((valor * 1.8) - 150253.03) * 0.0003)) * 0.75 * 0.95 + 6.01;
	else										registro = (24.04 + ((24040.49 * 0.00175) + (30050.61 * 0.00125) + (90151.82 * 0.00075) + (450759.07 * 0.0003) + ((valor * 1.8) - 601012.1) * 0.0002)) * 0.75 * 0.95 + 6.01;	
	
	//document.getElementById("registro_hipoteca").innerHTML = formatearPrecio(MoneyFormat(registro));
}

function calGestoriaHipoteca(valor)
{
	var gestoria = 0;
	
	if(valor <= 30000)						gestoria = 132;	
	if(valor > 30000 && valor <= 60000)		gestoria = 162;	
	if(valor > 60000 && valor <= 90000)		gestoria = 207;	
	if(valor > 90000 && valor <= 120000)	gestoria = 237;	
	if(valor > 120000 && valor <= 300000)	gestoria = 298;	
	if(valor > 300000 && valor <= 390000)	gestoria = 298;	
	if(valor > 390000)						gestoria = 388;	
	
	//document.getElementById("gestoria_hipoteca").innerHTML = gestoria;
}

function calIajHipoteca(valor)
{
	var iaj = 0;
	iaj = (valor * 0.15) * 0.1;
	//document.getElementById("iaj_hipoteca").innerHTML = formatearPrecio(iaj);
}

function calIvaHipoteca(valor, tipo, prime)
{
	var iva = 0;
	iva = valor * 0.07;
	//document.getElementById("iva_hipoteca").innerHTML = formatearPrecio(MoneyFormat(iva));
}

function calNotarioCompra(valor)
{
	var notaria = 0;
	
	if(((valor - 30050.61)) < 0)			notaria = (90 + (valor - 6010.13) * 0.0045) * 0.75 + 126;		
	else if((((valor - 60101.22)) < 0))		notaria = (90 + (24040.49 * 0.0045 + (valor - 30050.61) * 0.0015)) * 0.75 + 126;
	else if((((valor - 150253.03)) < 0))	notaria = (90 + ((24040.49 * 0.0045) + (30050.61 * 0.0015) + (valor - 60101.22) * 0.001)) * 0.75 + 126;
	else if((((valor - 601012.1)) < 0))		notaria = (90 + ((24040.49 * 0.0045) + (30050.61 * 0.0015) + (90151.82 * 0.001) + (valor - 150253.03) * 0.0005)) * 0.75 + 126;
	else 									notaria = (90 + ((24040.49 * 0.0045) + (30050.61 * 0.0015) + (90151.82 * 0.001) + (450759.07 * 0.0005) + (valor - 601012.1) * 0.0003)) * 0.75 + 126;	
	
	//document.getElementById("notaria_compra").innerHTML = formatearPrecio(MoneyFormat(notaria));
}

function calRegistroCompra(valor)
{
	var registro = 0;
	
	if(((valor - 30050.61)) < 0)			registro = ((24.04 + (valor - 6010.13) * 0.00175) * 0.75) * 0.95 + 6.01;		
	else if((((valor - 60101.22)) < 0))		registro = ((24.04 + (24040.49 * 0.00175) + (valor - 30050.61) * 0.00125) * 0.75) * 0.95 + 6.21;
	else if((((valor - 150253.03)) < 0))	registro = ((24.04 + (24040.49 * 0.00175) + (30050.61 * 0.0015) + (valor - 60101.22) * 0.00075) * 0.75) * 0.95 + 6.01;
	else if((((valor - 601012.1)) < 0))		registro = ((24.04 + (24040.49 * 0.00175) + (30050.61 * 0.00125) + (90151.82 * 0.00075) + (valor - 150253.03) * 0.0003) * 0.75) * 0.95 + 6.01;
	else									registro = ((24.04 + (24040.49 * 0.00175) + (30050.61 * 0.00125) + (90151.82 * 0.00075) + (450759.07 * 0.0003) + (valor - 601012.1) * 0.0002) * 0.75) * 0.95 + 6.01;	
	
	//document.getElementById("registro_compra").innerHTML = formatearPrecio(MoneyFormat(registro));
}

function calGestoriaCompra(valor)
{
	var gestoria = 0;
	
	if(valor <= 30000)						gestoria = 105;	
	if(valor > 30000 && valor <= 60000)		gestoria = 135;	
	if(valor > 60000 && valor <= 90000)		gestoria = 180;	
	if(valor > 90000 && valor <= 120000)	gestoria = 210;	
	if(valor > 120000 && valor <= 300000)	gestoria = 100;	
	if(valor > 300000 && valor <= 390000)	gestoria = 270;	
	if(valor > 390000)						gestoria = 301;	
	
	//document.getElementById("gestoria_compra").innerHTML = gestoria;
}

function calIajCompra(valor, prime)
{
	var iaj = 0;

	if(prime.checked == true)	iaj = (valor * 0.1) * 0.075;
	else						iaj = (valor * 0.1) * 0.1;
	
	//document.getElementById("iaj_compra").innerHTML = formatearPrecio(iaj);
}

function calIvaCompra(precio, tipo, nuevo)
{
	var iva = 0;

	if(nuevo.checked == true)
	{
		if(tipo == "Garaje" || tipo == "Local Comercial" || tipo == "Nave Comercial" || tipo == "Nave Industrial" || tipo == "Oficinas" || tipo == "Parcela")	iva = precio * 0.16;
		else																																					iva = precio * 0.07;
	}
	else
	{
		iva = precio * 0.07;
	}
	
	//document.getElementById("iva_compra").innerHTML = formatearPrecio(iva);
}

function calItpCompra(precio)
{
	var iva = 0;
	iva = precio * 0.07;
	//document.getElementById("itp_compra").innerHTML = formatearPrecio(MoneyFormat(iva));
}
*/

