// Carga Dinámica Desplegable Localidades BUSCADOR DE OFERTAS -------------------------------

var idioma_global = "es";
function set_idioma(idioma){
	idioma_global = idioma;
}

var myConn_selecloc_ofertas = new XHConn();
var terminado_selecloc_ofertas= function (oXML) { document.getElementById('selec_localidad').innerHTML = oXML.responseText; };
function include_selecloc_ofertas (idprovincia){
	document.getElementById('selec_localidad').innerHTML = "<div style=\"width:178px; float:right;\"><img src=\"http://www.oktomanota.com/buscador_inmo/img/cargando_ok_peq.gif\" width=\"30\" height=\"19\"></div>";
	myConn_selecloc_ofertas.connect("http://www.oktomanota.com/ofertas/selec_localidad_ofertas.php", "GET", "idprovincia="+idprovincia, terminado_selecloc_ofertas);
}

var myConn_colindante_ofertas = new XHConn();
var terminado_colindante_ofertas = function (oXML) { document.getElementById('zona_colindante').innerHTML = oXML.responseText; };
function include_colindante_ofertas (idlocalidad){
	document.getElementById('zona_colindante').innerHTML = "<br><div align=\"center\"><img src=\"http://www.oktomanota.com/buscador_inmo/img/cargando_ok.gif\" width=\"25\" height=\"25\"></div>";
	myConn_colindante_ofertas.connect("http://www.oktomanota.com/ofertas/zona_colindante.php", "GET", "idlocalidad="+idlocalidad+"&idioma="+idioma_global, terminado_colindante_ofertas);
}

var myConn_list_ofertas = new XHConn();
var idlocalidad_selec;
function include_list_ofertas (idlocalidad, pagina){
	var terminado_list_ofertas = function (oXML) { document.getElementById('listado').innerHTML = oXML.responseText; retardo_mapa(idlocalidad_selec);retardo_mapa(idlocalidad);};
	var txt_cargando;
	if (idioma_global == "es"){ txt_cargando = "Cargando Ofertas..."; }
	if (idioma_global == "cat"){ txt_cargando = "Carregant Ofertes..."; }
	if (idioma_global == "ingl"){ txt_cargando = "Loading Offers..."; }
	if (idioma_global == "fr"){ txt_cargando = "Chargeant Offres..."; }
	if (idioma_global == "al"){ txt_cargando = "Die Immobilien werden geladen..."; }
	document.getElementById('listado').innerHTML = "<div id=\"cargando_inmuebles\"><img src=\"http://www.oktomanota.com/buscador_inmo/img/cargando_ok.gif\" width=\"25\" height=\"25\" align=\"absmiddle\"> &nbsp;&nbsp;"+txt_cargando+"</div>";
	idlocalidad_selec = idlocalidad;
	myConn_list_ofertas.connect("http://www.oktomanota.com/ofertas/list_ofertas.php", "GET", "idlocalidad="+idlocalidad+"&pagina="+pagina+"&idioma="+idioma_global, terminado_list_ofertas);
	include_estadistica_impresiones(idlocalidad, pagina);
}

var myConn_ficha = new XHConn();
var include_terminado_ficha = function (oXML) { document.getElementById('ficha').innerHTML = oXML.responseText; };
function include_ficha(idinmueble, pagina){
	if (idioma_global == "es"){ txt_cargando = "Cargando datos..."; }
	if (idioma_global == "cat"){ txt_cargando = "Carregant dades..."; }
	if (idioma_global == "ingl"){ txt_cargando = "Loading information..."; }
	if (idioma_global == "fr"){ txt_cargando = "Chargeant..."; }
	if (idioma_global == "al"){ txt_cargando = "Laden..."; }
	document.getElementById('ficha').innerHTML = "<div id=\"cargando_inmuebles\"><img src=\"http://www.oktomanota.com/buscador_inmo/img/cargando_ok.gif\" width=\"25\" height=\"25\" align=\"absmiddle\"> &nbsp;&nbsp;"+txt_cargando+"</div>";
	myConn_ficha.connect("http://www.oktomanota.com/busquedas/ficha_inmueble.php", "GET", "id="+idinmueble+"&pagina="+pagina+"&idioma="+idioma_global+"&referer=ofertas", include_terminado_ficha);
	include_estadistica_visitas(idinmueble);
}


// GOOGLE MAPS //

function retardo_mapa(idlocalidad){
	document.getElementById('map').style.visibility = 'visible';
	document.getElementById('map').innerHTML = "<br><div align=\"center\" > <img src=\"http://www.oktomanota.com/img/cargando/cargando_selector.gif\" width=\"24\" height=\"24\"> </div><br>";
	window.setTimeout("carga_mapa('"+idlocalidad+"')", 2000);
}

function carga_mapa(idlocalidad) {
	
	var urlstr="http://www.oktomanota.com/busquedas/google_maps/xml_centro_mapa.php?idlocalidad="+idlocalidad;
	var request = GXmlHttp.create();
	request.open("GET", urlstr, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
		var xmlDoc = request.responseXML;
		var markers = xmlDoc.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) {
			longitud_centro = parseFloat(markers[i].getAttribute("lng"));
			latitud_centro = parseFloat(markers[i].getAttribute("lat"));
			inicia_mapa(longitud_centro, latitud_centro, idlocalidad);
       		}
    	 }
	}
	request.send(null);

	
function inicia_mapa(longitud_centro, latitud_centro, idlocalidad){

	if (longitud_centro){
	document.getElementById('map').style.width = '487px';
	document.getElementById('map').style.height = '185px';

	var icon = new GIcon();
	icon.image = "http://www.oktomanota.com/buscador_inmo/iconos/icono_casa.png";
	icon.shadow = "http://www.oktomanota.com/buscador_inmo/iconos/icono_sombra.png";
	icon.iconSize = new GSize(19, 28);
	icon.shadowSize = new GSize(37, 28);
	icon.iconAnchor = new GPoint(4, 28);
	icon.infoWindowAnchor = new GPoint(10, 1);
	
	var icon_selec = new GIcon();
	icon_selec.image = "http://www.oktomanota.com/buscador_inmo/iconos/icono_casa_selec.png";
	icon_selec.shadow = "http://www.oktomanota.com/buscador_inmo/iconos/icono_sombra.png";
	icon_selec.iconSize = new GSize(19, 28);
	icon_selec.shadowSize = new GSize(37, 28);
	icon_selec.iconAnchor = new GPoint(4, 28);
	icon_selec.infoWindowAnchor = new GPoint(10, 1);

	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(latitud_centro, longitud_centro), 11);
	map.setMapType(G_NORMAL_MAP);

	function createInfoMarker(point, address, idlocalidad) {
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "mouseover", function() { crearAlt(address); } );
	GEvent.addListener(marker, "mouseout", function() { hide(); } );
	GEvent.addListener(marker, "click", function() { hide(); ancla_dinamica('list_inmuebles'); include_list_ofertas(idlocalidad, '1');} );
	return marker;
	}
	
	function createInfoMarker_selec(point, address, idlocalidad) {
	var marker = new GMarker(point, icon_selec);
	GEvent.addListener(marker, "mouseover", function() { crearAlt(address); } );
	GEvent.addListener(marker, "mouseout", function() { hide(); } );
	GEvent.addListener(marker, "click", function() { hide(); ancla_dinamica('list_inmuebles'); include_list_ofertas(idlocalidad, '1');} );
	return marker;
	}
	
	var idlocalidad_selec = idlocalidad;
	var urlstr="http://www.oktomanota.com/ofertas/xml_marcas_mapa_ofertas.php?idlocalidad="+idlocalidad+"&idioma="+idioma_global;
	var request = GXmlHttp.create();
	request.open("GET", urlstr, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
		var xmlDoc = request.responseXML;
		var markers = xmlDoc.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) {
			var point = new GPoint(parseFloat(markers[i].getAttribute("lng")), parseFloat(markers[i].getAttribute("lat")));
			var address = '<div id=\"alt_mapa\"><table width=\"182\" height=\"50\" border=\"0\" class=\"borde_tabla_azul\" bgcolor=\"#FFFFCC\"><tr><td height=\"18\" bgcolor=\"#4D6DA8\" class=\"txt1\"><div align=\"center\">'+markers[i].getAttribute("localidad")+'</div></td></tr><tr><td class=\"txt2\"><div align=\"center\">'+markers[i].getAttribute("txt1")+': <span class=\"txt3\">'+markers[i].getAttribute("total_ofertas")+'</span></div></td></tr></table></div>';
			var idlocalidad = markers[i].getAttribute("idlocalidad");
			var tipo = markers[i].getAttribute("tipo");
				if (idlocalidad_selec == idlocalidad){
				var marker = createInfoMarker_selec(point, address, idlocalidad);
				} else {
				var marker = createInfoMarker(point, address, idlocalidad);
				}
			map.addOverlay(marker);
       		}
    	 }
	}
 	 request.send(null);
	 
	} else {
		document.getElementById('map').style.visibility = 'hidden';
	document.getElementById('map').style.width = '0px';
	document.getElementById('map').style.height = '0px';
	document.getElementById('map').innerHTML = '';
	} // fin de if

} // fin funcion inicia mapa

} // fin funcion carga mapa


//----- Anclas dinámicas -----------------------------------------//

var scrollInt;
var scrTime, scrSt, scrDist, scrDur, scrInt;

function scrollPage(){
	scrTime += scrInt;
	if (scrTime < scrDur) {
		window.scrollTo( 0, easeInOut(scrTime,scrSt,scrDist,scrDur) );
	}else{
		window.scrollTo( 0, scrSt+scrDist );
		clearInterval(scrollInt);
	}
}

function ancla_dinamica(aname){
	var anchors, i, ele;

	if (!document.getElementById)
		return;
	
	// get anchor
	anchors = document.getElementsByTagName("a");
	for (i=0;i<anchors.length;i++) {
		if (anchors[i].name == aname) {
			ele = anchors[i];
			i = anchors.length;
		}
	}
	
	// set scroll target
	if (window.scrollY)
		scrSt = window.scrollY;
	else if (document.documentElement.scrollTop)
		scrSt = document.documentElement.scrollTop;
	else
		scrSt = document.body.scrollTop;

	scrDist = ele.offsetTop - scrSt;
	scrDur = 500;
	scrTime = 0;
	scrInt = 10;
	
	// set interval
	clearInterval(scrollInt);
	scrollInt = setInterval( scrollPage, scrInt );
}

function easeInOut(t,b,c,d){
	return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
}

//------------- Estadísticas -------------------------//

var myConn_estadistica_impresiones = new XHConn();
var terminado_estadistica_impresiones = function (oXML) { document.getElementById('estadistica_inmuebles').innerHTML = oXML.responseText; };
function include_estadistica_impresiones (idlocalidad, pagina){
	myConn_estadistica_impresiones.connect("http://www.oktomanota.com/ofertas/estadistica_impresiones.php", "GET", "idlocalidad="+idlocalidad+"&pagina="+pagina, terminado_estadistica_impresiones);
}

var myConn_est_imp_estatico= new XHConn();
var terminado_est_imp_estatico = function (oXML) { document.getElementById('estadistica_inmuebles').innerHTML = oXML.responseText; };
function include_estadistica_est_imp_estatico (id1, id2, id3, id4, id5){
	if (!(id1)){ id1 = ""; }
	if (!(id2)){ id2 = ""; }
	if (!(id3)){ id3 = ""; }
	if (!(id4)){ id4 = ""; }
	if (!(id5)){ id5 = ""; }
	
	myConn_est_imp_estatico.connect("http://www.oktomanota.com/buscador_inmo/estadisticas/estadistica_impresiones_estatico.php", "GET", "id1="+id1+"&id2="+id2+"&id3="+id3+"&id4="+id4+"&id5="+id5, terminado_est_imp_estatico);
}

function insert_contenedor_estadisticas(){
	document.write('<div id="estadistica_inmuebles" style="position: absolute; z-index:3;"></div>');
}

document.onload = insert_contenedor_estadisticas();

//---------------------------------------------------------------------//


