/********************************
 *
 *  BUSQUEDA
 * 
 **********************************/

function search()
{
	tipo = document.getElementById("search_tipo").value;
	localidad = document.getElementById("search_localidad").value;
	inversores = document.getElementById("search_inversores").value;
	infraestructura = document.getElementById("search_infraestructura").value;
	tabla = document.getElementById("search_tabla").value;
	id = document.getElementById("search_id").value;
	document.getElementById("search_tabla").value = "";
	document.getElementById("search_id").value = "";
	comercializacion = 0;
	for (var i = 0; i < 5; i++) {
		if (document.getElementById("search_comercializacion_"+i).checked) {
			comercializacion = document.getElementById("search_comercializacion_"+i).value;
		}
	}

	//searchTipo = document.getElementById("search_tipo");
	loadXMLList("xml_venta_inmuebles.php?comercializacion=" + comercializacion + "&localidad=" + localidad + "&tipo=" + tipo + "&inversores=" + inversores+ "&infraestructura=" + infraestructura+ "&tabla=" + tabla + "&id=" + id);
}


/********************************
 *
 *  LISTADO
 * 
 **********************************/

var requestList;
var responseList;
/*
function loadList(inv, inf)
{
	inversores = inv;
	loadXMLList("xml_venta_inmuebles.php?inversores=" + inversores + "&infraestructura=" + inf);
}
*/
function loadXMLList(url) 
{
    // opción para el objeto nativo XMLHttpRequest
    if (window.XMLHttpRequest) {
        requestList = new XMLHttpRequest();
        requestList.onreadystatechange = processChangeRequestList;
        requestList.open("GET", url, true);
        requestList.send(null);
    // opción para la versión ActiveX de IE/Windows
    } else if (window.ActiveXObject) {
        requestList = new ActiveXObject("Microsoft.XMLHTTP");
        if (requestList) {
            requestList.onreadystatechange = processChangeRequestList;
            requestList.open("GET", url, true);
            requestList.send();
        }
    }
}

function processChangeRequestList()
{
	// Sólo se procesa si la solicitud esta completada
	if (requestList.readyState == 4) {
		// Sólo si se ha procesado correctamente
		if (requestList.status == 200) {
			// Obtenemos el documento XML que devuelve script php
			responseList = requestList.responseXML.documentElement;
			
			if (responseList) {
							
				var content = document.getElementById("content");
				
				var inmueblesNodes = responseList.getElementsByTagName("inmuebles");
				var inmueblesNode = inmueblesNodes[0];
				
				paginas = inmueblesNode.getAttribute("paginas");
				pagina = inmueblesNode.getAttribute("pagina");
				filas = inmueblesNode.getAttribute("filas");
				filasPorPagina = inmueblesNode.getAttribute("filas-por-pagina");
				
				var out = "<div class=\"title\">";
				
				out += "Se encontraron " + filas + " inmuebles en la búsqueda";
				
				out += "</div>";
				
				var inmuebles = responseList.getElementsByTagName("inmueble");
				
				out += "<ul class=\"items\">";
				
				for (i = 0; i < inmuebles.length; i++) {
					
					id = inmuebles[i].getAttribute("id");
					//name = inmuebles[i].getAttribute("nombre") + " de " + parseInt(inmuebles[i].getAttribute("superficie"))
					//		+ " m<sup>2</sup> en " + inmuebles[i].getAttribute("nombresubzona");
					
					out += "<li class=\"item\">";
					out += "  <div class=\"box\">";
					out += "    <h2>" + inmuebles[i].firstChild.nodeValue + "</h2>";
					out += "      <div class=\"description\">";
					out += "        <div style=\"float:left; padding-right:20px;\"><img src=\"";
					if (inmuebles[i].getAttribute("imagen") == "") {
						out += "img/img_no.png";
					} else {
						out += inmuebles[i].getAttribute("imagen");
					}
					out += "\" /></div>";
					out += "        <div class=\"prices\">";
					
					tipo_comercializacion = inmuebles[i].getAttribute("tipocomercializacion_id");
					
					if (inmuebles[i].getAttribute("precio-tarifas") == "yes") {
						//out += "<div class=\"web\"><a target=\"_blank\" href=\"http://www.zonafrancacadiz.com/doc/file/tarifas/CZFC-RIZF-TARIFAS%20MINIMAS%202010.pdf\">Compruebe nuestras tarifas para este inmueble pulsando aquí</a></div>";
						out += "<div class=\"web\"><a target=\"_blank\" href=\"http://www.zonafrancacadiz.com/doc/file/tarifas/TARIFAS%20OFICIALES-Recinto%20Interior%202011.pdf\">Compruebe nuestras tarifas para este inmueble pulsando aquí</a></div>";
					} else {
						if (inmuebles[i].getAttribute("revisado-nuevaweb") == "yes") {
							if (inmuebles[i].getAttribute("precio_venta")!="0") {
								out += "<div class=\"type\">Venta</div>";
								out += "<div class=\"price\">" + inmuebles[i].getAttribute("precio_venta") + " &euro;</div>";
							}
							if (inmuebles[i].getAttribute("precio_alquiler")!="0") {
								out += "<div class=\"type\">Alquiler</div>";
								out += "<div class=\"price\">" + inmuebles[i].getAttribute("precio_alquiler") + " &euro;/mes</div>";
							}
							if (inmuebles[i].getAttribute("precio_alquiler_opcioncompra")!="0") {
								out += "<div class=\"type\">Alquiler (opción compra)</div>";
								out += "<div class=\"price\">" + inmuebles[i].getAttribute("precio_alquiler_opcioncompra") + " &euro;/mes</div>";
							}
						} else {
							if (tipo_comercializacion == 1) {
								out += "<div class=\"type\">Alquiler</div>";
								out += "<div class=\"price\">" + inmuebles[i].getAttribute("precio_alquiler") + " &euro;/mes</div>";
							}
							else 
								if (tipo_comercializacion == 2) {
									out += "<div class=\"type\">Venta</div>";
									out += "<div class=\"price\">" + inmuebles[i].getAttribute("precio_venta") + " &euro;</div>";
								}
								else 
									if (tipo_comercializacion == 3) {
										out += "<div class=\"type\">Alquiler</div>";
										out += "<div class=\"price\">" + inmuebles[i].getAttribute("precio_alquiler") + " &euro;/mes</div>";
										out += "<div class=\"type\">Venta</div>";
										out += "<div class=\"price\">" + inmuebles[i].getAttribute("precio_venta") + " &euro;</div>";
									}
									else 
										if (tipo_comercializacion == 4) {
											out += "<div class=\"type\">Alquiler (opción compra)</div>";
											out += "<div class=\"price\">" + inmuebles[i].getAttribute("precio_alquiler") + " &euro;/mes</div>";
											out += "<div class=\"type\">Venta</div>";
											out += "<div class=\"price\">" + inmuebles[i].getAttribute("precio_venta") + " &euro;</div>";
										}
						}
					}
					out += "        </div>";
					out += "			<b>Superficie:</b> " + inmuebles[i].getAttribute("superficie") + " m<sup>2</sup><br/>";
					out += "		    <b>Infraestructura:</b> " + inmuebles[i].getAttribute("nombreinfraestructura") + "<br/>";
					out += "            <b>Subzona:</b> " + inmuebles[i].getAttribute("nombresubzona") + "";
					out += "      </div>";
					out += "    <div class=\"links\">";
					if (inmuebles[i].getAttribute("mas-informacion") == "yes") {
						out += "      <a href=\"#\" class=\"off\" id=\"link_info_" + id + "\"><img src=\"img/icon_info.png\" border=\"0\" align=\"absmiddle\" width=\"12\" height=\"12\" /> Más datos</a>";
					}
					out += "      <a href=\"#\" class=\"off\" id=\"link_doc_" + id + "\"><img src=\"img/icon_doc.png\" border=\"0\" align=\"absmiddle\" width=\"12\" height=\"12\" /> Documentación</a>";
					if (inmuebles[i].getAttribute("infraestructura_id") != "0") {
						out += "      <a href=\"#\" class=\"off\" id=\"link_build_" + id + "\"><img src=\"img/icon_envir.png\" border=\"0\" align=\"absmiddle\" width=\"12\" height=\"12\" /> Entorno</a>";
					}
					out += "      <a href=\"#\" class=\"off\" id=\"link_map_" + id + "\"><img src=\"img/icon_map.png\" border=\"0\" align=\"absmiddle\" width=\"12\" height=\"12\" /> Mapa</a>";
					out += "      <a href=\"#\" class=\"off\" id=\"link_contact_" + id + "\"><img src=\"img/icon_inter.png\" border=\"0\" align=\"absmiddle\" width=\"12\" height=\"12\" /> Me interesa</a>";
					out += "      <a href=\"#\" class=\"off\" id=\"link_share_" + id + "\"><img src=\"img/icon_inter.png\" border=\"0\" align=\"absmiddle\" width=\"12\" height=\"12\" /> Compartir</a>";
					out += "    </div>";
					out += "    <div class=\"more\" id=\"box_" + id + "\">";
					out += "    </div>";
					out += "  </div>";
					out += "</li>";
				}
				
				out += "<ul>";
				
				// paginacion
				
				out += "<div class=\"pagination\">";
				
				for (i=0; i<paginas; i++) {
					if (i != pagina) {
						var url = "xml_venta_inmuebles.php?inversores=" + inversores + "&page=" + i;
						if (inmueblesNode.getAttribute("comercializacion")!="") url += "&comercializacion="+inmueblesNode.getAttribute("comercializacion");
						if (inmueblesNode.getAttribute("localidad")!="") url += "&localidad="+inmueblesNode.getAttribute("localidad");
						if (inmueblesNode.getAttribute("tipo")!="") url += "&tipo="+inmueblesNode.getAttribute("tipo");
						out += "<a href=\"#\" onclick=\"loadXMLList('" + url + "');\">" + (i+1) + "</a> ";
					} else {
						out += "<span class=\"page_active\">" + (i+1) + "</span> ";
					}
				}
				
				out += "</div>";
				
				content.innerHTML = out;
				
				for (i = 0; i < inmuebles.length; i++) {
					id = inmuebles[i].getAttribute("id");
					createEvents(""+id, inmuebles[i].getAttribute("grupoinmueble_id"));
				}
			
			} else {
				alert("No existe 'responseList'");
			}

		} else {
			alert("There was a problem retrieving the XML data:\n" + responseList.statusText);
		}
	}
}

/********************************
 *
 *  LLAMADAS EN PESTAÑAS
 * 
 **********************************/

var request;
var response;
var ulrFile;
var name;
var nameDiv;

function loadXMLData(url, nameBox) 
{
	ulrFile = url;
	name = nameBox;
	nameDiv = "box_"+nameBox;
    // opción para el objeto nativo XMLHttpRequest
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
        request.onreadystatechange = processChangeRequestData;
        request.open("GET", url, true);
        request.send(null);
    // opción para la versión ActiveX de IE/Windows
    } else if (window.ActiveXObject) {
        request = new ActiveXObject("Microsoft.XMLHTTP");
        if (request) {
            request.onreadystatechange = processChangeRequestData;
            request.open("GET", url, true);
            request.send();
        }
    }
}

function processChangeRequestData()
{
	// Sólo se procesa si la solicitud esta completada
	if (request.readyState == 4) {
		// Sólo si se ha procesado correctamente
		if (request.status == 200) {
			// Obtenemos el documento XML que devuelve script php
			response = request.responseXML.documentElement;

			if (response) {
			
				var nodeData = response.getElementsByTagName("data");
				var content = nodeData[0].firstChild.data;
				content += "<br/><div align=\"center\"><a href=\"javascript:closeSlide('" + nameDiv + "', '" + name + "');\"><img src=\"img/exp_hor.jpg\" border=\"0\" /></a></div>";
				document.getElementById(nameDiv).innerHTML = content;
				milkbox.reloadGalleries();
				if (GBrowserIsCompatible()) {
					var map = new GMap(document.getElementById("map_"+name));
					if (map) {
						map.addControl(new GMapTypeControl());
						map.addControl(new GSmallMapControl());
						lon = document.getElementById("lon_"+name).value;
						lat = document.getElementById("lat_"+name).value;
						var marker = new GMarker(new GPoint(lon, lat));
						GEvent.addListener(marker, "click", function() { 
							marker.openInfoWindowHtml(document.getElementById("name_"+name).value); 
							} );
						map.addOverlay(marker);
						map.centerAndZoom(new GPoint(lon, lat), 2);
					}
				}
			} else {
				alert("No existe 'response'");
			}

		} else {
			alert("There was a problem retrieving the XML data:\n" + request.statusText);
		}
	}
}

function closeSlide(box, name) {
	var box = new Fx.Slide(box);
	box.slideOut();
	if ($('link_info_' + name)) $('link_info_'+name).className = "off";
	if ($('link_doc_' + name)) $('link_doc_'+name).className = "off";
	if ($('link_build_' + name)) $('link_build_'+name).className = "off";
	if ($('link_map_' + name)) $('link_map_' + name).className = "off";
	if ($('link_contact_' + name)) $('link_contact_' + name).className = "off";
	if ($('link_share_' + name)) $('link_share_' + name).className = "off";
}

function openSlide(box) {
	if (request.readyState == 4) {
		var box = new Fx.Slide(box);
		box.slideIn();
	} else {
		setTimeout("openSlide('"+box+"')",100); 
	}
}

function hideBox(box) {
	var b = new Fx.Slide(box);
	b.hide();
}

function createEvents(name, grupoinmueble_id) {
	
	hideBox("box_"+name);
	
	window.addEvent('domready', function(){		
		
		if ($('link_info_' + name)) {
			$('link_info_' + name).addEvent('click', function(e){
				type = "info";
				if (grupoinmueble_id==0) type += "_singrupo";
				loadXMLData('xml_generico.php?type=' + type + '&id='+name, name);
				e.stop();
				openSlide("box_" + name);
				if ($('link_info_' + name)) $('link_info_' + name).className = "on";
				if ($('link_doc_' + name)) $('link_doc_' + name).className = "off";
				if ($('link_build_' + name)) $('link_build_' + name).className = "off";
				if ($('link_map_' + name)) $('link_map_' + name).className = "off";
				if ($('link_contact_' + name)) $('link_contact_' + name).className = "off";
				if ($('link_share_' + name)) $('link_share_' + name).className = "off";
			});
		}
		
		if ($('link_doc_' + name)) {
			$('link_doc_' + name).addEvent('click', function(e){
				type = "documentacion";
				if (grupoinmueble_id==0) type += "_singrupo";
				loadXMLData('xml_generico.php?type=' + type + '&id='+name, name);
				e.stop();
				openSlide("box_" + name);
				if ($('link_info_' + name)) $('link_info_' + name).className = "off";
				if ($('link_doc_' + name)) $('link_doc_' + name).className = "on";
				if ($('link_build_' + name)) $('link_build_' + name).className = "off";
				if ($('link_map_' + name)) $('link_map_' + name).className = "off";
				if ($('link_contact_' + name)) $('link_contact_' + name).className = "off";
				if ($('link_share_' + name)) $('link_share_' + name).className = "off";
			});
		}
		
		if ($('link_build_' + name)) {
			$('link_build_' + name).addEvent('click', function(e){
				type = "infraestructura_desdeinmueble";
				if (grupoinmueble_id==0) type += "_singrupo";
				loadXMLData('xml_generico.php?type='+type+'&id='+name, name);
				e.stop();
				openSlide("box_" + name);
				if ($('link_info_' + name)) $('link_info_' + name).className = "off";
				if ($('link_doc_' + name)) $('link_doc_' + name).className = "off";
				if ($('link_build_' + name)) $('link_build_' + name).className = "on";
				if ($('link_map_' + name)) $('link_map_' + name).className = "off";
				if ($('link_contact_' + name)) $('link_contact_' + name).className = "off";
				if ($('link_share_' + name)) $('link_share_' + name).className = "off";
			});
		}
		
		if ($('link_map_' + name)) {
			$('link_map_' + name).addEvent('click', function(e){
				type = "mapa";
				if (grupoinmueble_id==0) type += "_singrupo";
				loadXMLData('xml_generico.php?type=' + type + '&id='+name, name);
				e.stop();
				openSlide("box_" + name);
				if ($('link_info_' + name)) $('link_info_' + name).className = "off";
				if ($('link_doc_' + name)) $('link_doc_' + name).className = "off";
				if ($('link_build_' + name)) $('link_build_' + name).className = "off";
				if ($('link_map_' + name)) $('link_map_' + name).className = "on";
				if ($('link_contact_' + name)) $('link_contact_' + name).className = "off";
				if ($('link_share_' + name)) $('link_share_' + name).className = "off";
			});
		}
		
		if ($('link_contact_' + name)) {
			$('link_contact_' + name).addEvent('click', function(e){
				loadWindow("contact", name, grupoinmueble_id);
				/*loadXMLData('xml_generico.php?type=contacto&id='+name, name);
				e.stop();
				openSlide("box_" + name);
				if ($('link_info_' + name)) $('link_info_' + name).className = "off";
				if ($('link_doc_' + name)) $('link_doc_' + name).className = "off";
				if ($('link_build_' + name)) $('link_build_' + name).className = "off";
				if ($('link_map_' + name)) $('link_map_' + name).className = "off";
				if ($('link_contact_' + name)) $('link_contact_' + name).className = "on";*/
			});
		}
		
		if ($('link_share_' + name)) {
			$('link_share_' + name).addEvent('click', function(e){
				loadWindow("share", name, grupoinmueble_id);
			});
		}
		
	});
	
}

