var baners;
var ban;
var movent=false;
var accionCV="";
var num1=false;
var num2=false;
var num3=false;
var num4=false;
var pGrid;
var cod=0;
var contban=0;
var banant=0;
var banact=1;

var contbanA=0;
var banantA=0;
var banactA=1;

window.onload=function() {
	
var d=$('gal');
	
	if (d) {
		var protoCarousel=new ProtoCarousel(d,{
					itemWidth: 100,
					itemHeight: 80,
					buttonLeft: 'http://www.pisosmallorca.com/public/img/flecha_izq.png',
					buttonRight: 'http://www.pisosmallorca.com/public/img/flecha_drcha.png',
					buttonLeftWidth: 25,
					buttonRightWidth: 25,
					itemsDesp: 1
		});
	}
	
	
	
	
	if($('cort')){
		cort();
	}
	
	if($('oferta')){
		protoFlash=new ProtoFlash('oferta');
	}
	if($('map')){
		map();
	}
	if($('telefonos')){
		protoFlash=new ProtoFlash('telefonos', {
			interval:0.1,
			width: 271,
			height: 37,
			animSpeed: 1
		});
	}
	var rap=$('rap');
	if(rap){
		if(rap.value=='ok'){
			buscar();
		}
	}else{
		if($('grid')){
			//pintaLlista();
			
			//pintaLlistaMatada();
			buscar();
		}
	}
	
	if($('fotos')){
		animaFotos();
	}
	
	
	
}

function buida(nom){
	var el=$(nom);
    if(el.childNodes){
        for(var i=el.childNodes.length-1;i>=0;i--){
            var childNode=el.childNodes[i];
            el.removeChild(childNode);
        }
    }
}


function cammun(cod) {
	var url="xml/carregaZon.php";
	var pars="muncod="+$('Opcslmun').value;
	
	var ajax=new Ajax.Request ( url , {
		method:'get',
		parameters: pars,
		onComplete: carregaZon
	});
	
	
}

function carregaZon(reply) {
	var nodeSub=reply.responseXML.selectNodes("//zonas/zona");
	var x=0;
	buida('Opcslzon');
	var cbzon=$('Opcslzon');
	while(x<nodeSub.length){
		var cod=nodeSub[x].childNodes[0].childNodes[0].nodeValue;
		var desc=nodeSub[x].childNodes[1].childNodes[0].nodeValue;
		var option=document.createElement('option');
		option.setAttribute('value', cod);
		option.update(desc);
		cbzon.appendChild(option);
		x++;
	}
}


function cort(){
	setTimeout("indice()",1500);

}
function animaFotos(){
	protoFlash=new ProtoFlash('fotos', {
		interval:2.5,
		width: 450,
		height: 250,
		animSpeed: 1.5
	});
}


function indice(){
	new Effect.Opacity('cort', {duration:1.0, from:1.0, to:0.0, afterFinish: function(){
		window.location="http://www.pisosmallorca.com/public/ES";
	 }});
	
}

function cambiarban(){
	var cont=$('contV').value;
	if(cont>banact){
		$('banB'+banant).style.display="none";
		$('banB'+banact).style.display="block";
		banant++;
		banact++;
	}else{
		banant=0;
		$('banB'+parseInt(banact-1)).style.display="none";
		$('banB'+banant).style.display="block";
		banact=1;
		
	}
}
function cambiarbanA(){
	var contA=$('contA').value;
	if(contA>banactA){
		$('banA'+banantA).style.display="none";
		$('banA'+banactA).style.display="block";
		banantA++;
		banactA++;
	}else{
		banantA=0;
		$('banA'+parseInt(banactA-1)).style.display="none";
		$('banA'+banantA).style.display="block";
		banactA=1;
		
	}
}
	


function ponertitulo(tit){
	$('titban1').update(tit);
}
function map(){
	if (GBrowserIsCompatible()) { 
			map = new GMap2(document.getElementById("map")); 
			map.setCenter(new GLatLng(39.57867337816976, 2.628425359725952), 15); 
			map.addControl(new GSmallMapControl()); 
			map.addControl(new GMapTypeControl()); 
			map.setMapType(G_NORMAL_MAP); 
			map.openInfoWindow(map.getCenter(), 
				"<div id='menmapa'><div id='iconomapa' style='float: left; margin-right: 23px;'><img src=../img/logo.jpg style='width:100px;'></div><div id='titulomapa' style='color: black; font-weight: bold; font-size: 15px;'>PisosMallorca<br/></div><div id='dirmapa' style='color:black; font-size:12px;'>Calle de Dragonera, 21 07014 Palma (Mallorca)<br/><br/> Tel 971-222-022 Fax FAX: 971-222-023<br/><b>informacion@pisosmallorca.com</b></div></div>"); 
			 

		/*	GEvent.addListener(map,"click", function(overlay,latlng) {      
		      if (latlng) {    
		        //var myHtml = "The GLatLng value is: " + map.fromLatLngToDivPixel(latlng) + " at zoom level " + map.getZoom(); 
		        var myHtml = "The GLatLng value is: " + latlng + " at zoom level " + map.getZoom(); 
		        map.openInfoWindow(latlng, myHtml); 
		      } 
	    });*/
	    
	}
}

function enviarMail() {
	
	if (($('inmun').value=="") || ($('Opcsltipo').value=="") || ($('selope').value=="") || ($('inprec').value=="") || ($('inhab').value=="") || ($('txtdatos').value=="")|| ($('innom').value=="")|| ($('inape').value=="")|| ($('indir').value=="")|| ($('intel').value=="")|| ($('inmov').value=="")|| ($('inmail').value=="")) {
		alert('tiene que rellenar todos los campos');
		return;
	}
	var url="../xml/mailForm.php";
	var params="municipio="+encodeURI($('inmun').value)+"&tipo="+encodeURI($('Opcsltipo').value)+"&ope="+encodeURI($('selope').value)+"&precio="+encodeURI($('inprec').value)+"&hab="+encodeURI($('inhab').value)+"&datos="+encodeURI($('txtdatos').value)+"&nombre="+encodeURI($('innom').value)+"&ape="+encodeURI($('inape').value)+"&dir="+encodeURI($('indir').value)+"&tel="+encodeURI($('intel').value)+"&mov="+encodeURI($('inmov').value)+"&mail="+encodeURI($('inmail').value);
	var ajax=new Ajax.Request ( url, {
		method: 'post',
		postBody: params,
		onLoading: function() {
					//buida($('msg'));
					//var t=document.createTextNode(unescape(iditextos[1]));
					//$('msg').style.visibility='visible';
					//$('msg').appendChild(t);
				},
		onComplete:function(reply) {
					if (reply.responseText=="Err") {
						var txt="Ha ocurrido un error.";
					} else {
						var txt="Mensaje enviado satisfactoriamente";
					}
					
					alert(txt);
					
					if (reply.responseText=="Ok") {
						
						
						
					}
			}
		});	
}
function enviarMailInfo() {
	
	if (($('txtdatos').value=="")|| ($('innom').value=="")|| ($('inape').value=="")|| ($('indir').value=="")|| ($('intel').value=="")|| ($('inmov').value=="")|| ($('inmail').value=="")) {
		alert('tiene que rellenar todos los campos');
		return;
	}
	var url="../xml/mailinfo.php";
	var params="datos="+encodeURI($('txtdatos').value)+"&nombre="+encodeURI($('innom').value)+"&ape="+encodeURI($('inape').value)+"&dir="+encodeURI($('indir').value)+"&tel="+encodeURI($('intel').value)+"&mov="+encodeURI($('inmov').value)+"&mail="+encodeURI($('inmail').value);
	var ajax=new Ajax.Request ( url, {
		method: 'post',
		postBody: params,
		onLoading: function() {
					//buida($('msg'));
					//var t=document.createTextNode(unescape(iditextos[1]));
					//$('msg').style.visibility='visible';
					//$('msg').appendChild(t);
				},
		onComplete:function(reply) {
					if (reply.responseText=="Err") {
						var txt="Ha ocurrido un error.";
					} else {
						var txt="Mensaje enviado satisfactoriamente";
					}
					
					alert(txt);
					
					if (reply.responseText=="Ok") {
						
						
						
					}
			}
		});	
}


function comalq(){
	if($('rdalq').checked==true){
		window.location="alquilar.php?tipo="+$('Opcsltipo').value+"&zona="+$('Opcslzon').value+"&desdeP="+$('indesdeP').value+"&hastaP="+$('inhastaP').value+"&desdeM="+$('indesdeM').value+"&hastaM="+$('inhastaM').value;
	}else{
		window.location="comprar.php?tipo="+$('Opcsltipo').value+"&zona="+$('Opcslzon').value+"&desdeP="+$('indesdeP').value+"&hastaP="+$('inhastaP').value+"&desdeM="+$('indesdeM').value+"&hastaM="+$('inhastaM').value;
	}
}

function pintaLlistaMatada(){
	
	var accion=$('accion').value;
	if(accion=='c'){
		var url="xml/listaInmueble.php?oper=2";	
		accionCV="C";
	}else{
		var url="xml/listaInmueble.php?oper=1";	
		accionCV="A";
	}
	
	
	var ajax=new Ajax.Request( url, {
		method: 'post',
		onComplete: resultatpinta
	});
}
function buscar(){
	
	if($('datos')){
		$('datos').remove();
	}
	var accion=$('accion').value;
	if(accion=='c'){
		accionCV="C";
		var oper=2;
	}else{
		accionCV="A";
		var oper=1;
	}
	
	var url="xml/listaInmueble.php?tipcod="+$('Opcsltipo').value+"&muncod="+$('Opcslmun').value+"&zoncod="+$('Opcslzon').value+"&oper="+oper+"&desdeP="+$('inpdesdeprec').value+"&hastaP="+$('inphastaprec').value+"&desdeM="+$('inpdesdemetros').value+"&hastaM="+$('inphastametros').value+"&desdeH="+$('inpdesdehab').value+"&hastaH="+$('inphastahab').value+"&desdeB="+$('inpdesdeban').value+"&hastaB="+$('inphastaban').value+"&anyo="+$('inpcontr').value;
	var params="tipcod="+$('Opcsltipo').value;
	
	
	var ajax=new Ajax.Request( url, {
		method: 'post',
		onComplete: resultatpinta
	});
}
function resultatpinta(reply){
	var grid=$('grid');
	var color="amarillo";
	var datos=new Element("div", {'style':'overflow: auto; float: left; width: 681px; height: 139px;', 'id':'datos' }).update(r);
	var nodeInm=reply.responseXML.selectNodes("//rows/row");
	for (var k=0;k<nodeInm.length;k++) {
		cod=nodeInm[k].lastChild.firstChild.nodeValue;
		var incod=new Element("input", {'id':'in'+k , 'type':'hidden', 'value':cod});
		if(color=="amarillo"){
			color="nada";
			var lin=new Element("div", {'style':'cursor:pointer; font-size:12px; height:20px; float:left; background-color:#F1F1F1'});
		}else{
			color="amarillo";
			var lin=new Element("div", {'style':'cursor:pointer; font-size:12px; height:20px; float:left; background-color:white;'});
		}
		lin.onmouseover=function(elem){
			//alert(this.parentNode);
			var lupa=new Element("img", {'src':'../img/b_search.png', 'style':'border:none; position:absolute; margin-left:-67px;'});
			this.childNodes[1].firstChild.appendChild(lupa);
		};
		lin.onmouseout=function(elem){
			this.childNodes[1].firstChild.childNodes[1].remove();
		}
	
		
		lin.appendChild(incod);
		var accion=$('accion').value;
		if(accion=='c'){
			var oper=2;
		}else{
			var oper=1;
		}
		
		
		var link1=new Element("a", {'href':'detalles.php?cod='+cod+"&oper="+oper , 'class':'linkdet', 'target':'_new1'+cod});
		var link2=new Element("a", {'href':'detalles.php?cod='+cod+"&oper="+oper , 'class':'linkdet', 'target':'_new2'+cod});
		var link3=new Element("a", {'href':'detalles.php?cod='+cod+"&oper="+oper , 'class':'linkdet', 'target':'_new3'+cod});
		var link4=new Element("a", {'href':'detalles.php?cod='+cod+"&oper="+oper , 'class':'linkdet', 'target':'_new4'+cod});
		var link5=new Element("a", {'href':'detalles.php?cod='+cod+"&oper="+oper , 'class':'linkdet', 'target':'_new5'+cod});
		var link6=new Element("a", {'href':'detalles.php?cod='+cod+"&oper="+oper , 'class':'linkdet', 'target':'_new6'+cod});
		var link7=new Element("a", {'href':'detalles.php?cod='+cod+"&oper="+oper , 'class':'linkdet', 'target':'_new7'+cod});
		
		
		if(nodeInm[k].firstChild.firstChild){
		var r=nodeInm[k].firstChild.firstChild.nodeValue;
		var ref=new Element("div", {'style':'width:95px; float:left; cursor:pointer' }).update(r);
		link1.appendChild(ref);
		var t=nodeInm[k].childNodes[1].firstChild.nodeValue;
		var tipo=new Element("div", {'style':'width:95px; float:left; font-size:10px;'}).update(t);
		link2.appendChild(tipo);
		var z=nodeInm[k].childNodes[2].firstChild.nodeValue;
		var zona=new Element("div", {'style':'width:95px; float:left; font-size:10px;'}).update(z);
		link3.appendChild(zona);
		var m=nodeInm[k].childNodes[3].firstChild.nodeValue;
		var metros=new Element("div", {'style':'width:95px; float:left;'}).update(m);
		link4.appendChild(metros);
		var n=nodeInm[k].childNodes[4].firstChild.nodeValue;
		var numhab=new Element("div", {'style':'width:95px; float:left;'}).update(n);
		link5.appendChild(numhab);
		if(accionCV=="A"){
			var pa=nodeInm[k].childNodes[5].firstChild.nodeValue;
			var precalq=new Element("div", {'style':'width:95px; float:left; text-align:right;'}).update(pa);
			link6.appendChild(precalq);
		}else{
			var pv=nodeInm[k].childNodes[6].firstChild.nodeValue;
			var precvent=new Element("div", {'style':'width:95px; float:left; text-align:right;'}).update(pv);
			link7.appendChild(precvent);
		}
		
		
		
		
		
		lin.appendChild(link1);
		lin.appendChild(link2);
		lin.appendChild(link3);
		lin.appendChild(link4);
		lin.appendChild(link5);
		if(accionCV=="A"){
			lin.appendChild(link6);
		}else{
			lin.appendChild(link7);
		}
		
		var clear=new Element("div", {'style':'clear: both;'});
		
		
		datos.appendChild(lin);
		datos.appendChild(clear);
		}
	}
	grid.appendChild(datos);
}

function pintaLlista() {


	pGrid = new ProtoGrid('grid', {
		width: 717,
		height: 186,
		titleBar: false,
		buttonBar1: false,
		buttonBar2: false,
		//parameters: "idicod="+gIdicod+"&admin=-2",
		dataUrl: "xml/listaInmueble.php",
		columnsTitles: ["CODIGO","Tipo","Municipio","Zona","Numhab.","Precio Alq","Precio Vent","ALTA"],
		columnsFields: ["inmcodprop","tipcod","muncod","zoncod","numhab","prealq","precvent","fecalt"],
		columnsAlign: ["right","left","left","left","left","center","center","center","center"],
		columnsVisibility: [true,true,true,true,true,true,true,true],
		columnsWidth: [96,100,100,96,96,74,90,65],
		columnsDraggables: [true,true,true,true,true,true,true,true],
		multiSelect: false,
		searchButton: false
		
	});
}





function linka(url){
	window.location=url;
}

function ticOn(div, num, x){
	div.style.color="gray";
	if(div.childNodes[1]){
		
	}else{
	

		movent=true;
		div=$(div);
		var tic=new Element("div", {'style':'background-image: url("../img/tic.png"); float: left; height: 21px; width: 21px; margin-right: 12px;'});
		div.appendChild(tic);
		
		if(x==3){
			var tapatic=new Element("img", {'src':'../img/tapatic.png' , 'style':'height: 21px; width: 48px; position: absolute; margin-top:87px;'});
		}else{	
			var tapatic=new Element("img", {'src':'../img/tapatic.png' , 'style':'height: 21px; width: 48px;'});
		}
		tic.appendChild(tapatic);
		div.style.color="gray";

		new Effect.Move(tapatic, { mode: 'absolute', x:-45 });
		
	
	}
	
		
}
function ticOff(div){
	div.style.color="#9F9F9F";
} 


function ticOn2(div, num, x){
	div.style.color="gray";
	if(div.childNodes[1]){
		
	}else{
	

		movent=true;
		div=$(div);
		if(x==3){
			var tic=new Element("div", {'style':'background-image: url("../img/tic.png"); float: left; height: 21px; width: 21px; margin-right: 12px; '});
		}else{
			var tic=new Element("div", {'style':'background-image: url("../img/tic.png"); float: left; height: 21px; width: 21px; margin-right: 12px;'});	
		}
		
		div.appendChild(tic);
		var tapatic=new Element("img", {'src':'../img/tapatic2.png' , 'style':'height: 21px; width: 30px;'});
		tic.appendChild(tapatic);
		div.style.color="gray";

		new Effect.Move(tapatic, { mode: 'absolute', x:-20 });
		
	
	}
	
		
}
function ticOff2(div){
	div.style.color="#9F9F9F";
} 


