// JavaScript Document
var oMapSearch = new function(){

	var cuadro = null;
	var cuadroTipo = null;
	var contenido = null;
	var blocker = null;	
	var iniciado = false;
	var accion = null;
	var tipo = null;
	var loader = null;
	var aAnt = null;
	var cod = '';
	

	var iniciar = function(){
		iniciado = true;
		cuadro = $('cuadroMSearch');
		cuadroTipo = $('cuadroMSearchType');
		contenido = $('contenidoMSearch');
		blocker = $('blockMSearch');
		loader = $('loadingMapaSearch');
		if(Nav.esIE || Nav.esIE7) blocker.style.filter = 'alpha(opacity=45)';
		else blocker.style.opacity = ".45";
	}
	
	this.findDates = function(code){
		cod = code;
		if(!iniciado)iniciar();
		mostrarBlockeador(true);
		loader.style.display = 'block';
		centrarCuadro(loader);
		accion = 'lista';
		var v = 'codigo'+ SEP_IGUAL + code + SEP_AND+ 'file' + SEP_IGUAL + 'mapaBusqueda.php' + SEP_AND + 'dirBase' + SEP_IGUAL + '../' + SEP_AND;
		req.pedir(URL_ROOT + 'request/cargar.php',v);
	}
	
	var sacarEspacios = function(texto){
		var tex = texto.toLowerCase().replace('<span>','');

		tex = tex.replace('</span>','');
		var part = trim(tex).split(' ');
		
		var dev = '';
		for(i=0;i<part.length;i++){ dev += part[i]+'_'; }
		return dev.substr(0,dev.length-1);
	}
	
	this.setType = function(a,nomTipo,e){
		if(aAnt)aAnt.className = 'mapaTipo';
		aAnt = a;
		a.className = 'mapaSearchASel';
		tipo = sacarEspacios(nomTipo).toLowerCase();
		mostrarCuadroTipo(a);
		if(e)StopEvent(e);
	}
	
	var mostrarCuadroTipo = function(a){
		a.parentNode.appendChild(cuadroTipo);
		cuadroTipo.style.marginTop = '-'+a.offsetHeight+'px';
		cuadroTipo.style.marginLeft = '0px';
		if(Nav.esIE)cuadroTipo.style.marginLeft = '-60px';
		cuadroTipo.style.display = 'block';
		var block = $('blockType');
		block.style.display = 'block';
		block.style.width = block.parentNode.offsetWidth+'px';
		block.style.height = block.parentNode.offsetHeight+'px';

		if(Nav.esIE || Nav.esIE7) block.style.filter = 'alpha(opacity=45)';
		else block.style.opacity = ".45";
	}
	
	this.showLoaded = function(){
		if(this.country){
			iniciar();
			mostrarBlockeador(true);
			cuadro.style.display = 'block';
			centrarCuadro(cuadro);
		}		
	}
	
	var mostrarBlockeador = function(mostrar){
		if(mostrar){
			blocker.style.width = (blocker.parentNode.offsetWidth-6)+'px';
			blocker.style.height = (blocker.parentNode.offsetHeight-38)+'px';
			blocker.style.display = 'block';
		}
		else blocker.style.display = 'none';
	}
	
	var onRequestLoad = function(){
		if(!req.respuestaXML){
			alert(req.respuestaHTML);
			return false;
		}
		var d = req.respuestaXML;
		switch(accion){
			case 'lista':
				if(Nav.esIE)cuadro.innerHTML = req.respuestaXML.text;
				else cuadro.innerHTML = req.respuestaXML.textContent;
				loader.style.display = 'none';
				cuadro.style.visibility = 'hidden';
				cuadro.style.display = 'block';
				centrarCuadro(cuadro);
				cuadro.style.visibility = 'visible';				
			break;
		}	
	}
	
	this.redimensionar = function(){
		if(iniciado){
			if(cuadro.style.display == 'block' || cuadroTipo.style.display == 'block'){
				mostrarBlockeador(true);
				centrarCuadro(cuadro);
				centrarCuadro(cuadroTipo);			
			}
		}	
	}
	
	var centrarCuadro = function(contenedor){
		var ancho = blocker.parentNode.offsetWidth-6;
		var alto = blocker.parentNode.offsetHeight-38;
		contenedor.style.marginLeft = Math.round((ancho-contenedor.offsetWidth)/2)+'px';	
		contenedor.style.marginTop = Math.round((alto-contenedor.offsetHeight)/2)+'px';			
	}
	
	this.cerrarCuadro = function(){
		mostrarBlockeador();
		document.body.appendChild(cuadroTipo);
		cuadro.style.display = 'none';
		if(aAnt)aAnt.className = 'mapaTipo';
		aAnt = null;
	}
	
	this.cerrarCuadroTipo = function(){
		cuadroTipo.style.display = 'none';
		$('blockType').style.display  = 'none';
		tipo = '';
		if(aAnt)aAnt.className = 'mapaTipo';
	}
	
	this.findP = function(a,e,t){
		
		/*oCookie.unset('country');
		oCookie.set('country', cod);*/
		oCookie.unset('blk');
		oCookie.unset('search');
		var nomPais = $('nomPais').innerHTML.toLowerCase();
		nomPais = nomPais.replace('á','a');
		nomPais = nomPais.replace('é','e');
		nomPais = nomPais.replace('í','i');
		nomPais = nomPais.replace('ó','o');
		nomPais = nomPais.replace('ú','u');		
		var path = tipo+'+'+trim(nomPais)+'+'+t;
		oCookie.set('key', path);		
		var url = oMSearch.url;
		var part = url.split('/');
		url = '';
		
		if(!this.online){
			var restar = 1;
			if(part[4] != 'Propiedades' && part[4] != 'Properties')restar = 2;
		
			for(i=0;i<part.length-restar;i++){
				url += part[i]+'/';
			}
			var idioma = part[3];
		}
		else{
			for(i=0;i<2;i++){
				url += part[i]+'/';
			}
		    var idioma = part[1];
		}

		var prop = 'Propiedades';
		if(idioma == 'en')prop = 'Properties';


		url += trim(nomPais.replace(' ','-'))+'/'+prop;

		document.location.href = url;
	}

	
	var req = new Request(onRequestLoad);
}
AddEvent(window, 'load', function(){
	oMapSearch.showLoaded();
	AddEvent(window, 'resize', function(){ oMapSearch.redimensionar(); });
});