// JavaScript Document
var main=new Array();
var links=new Array();


function cargar() {
	url= "cargar.php?"+Math.random();
	var jsonRequest = new Request.JSON({
		 url: url
		,onComplete: function(d){
			main[0] = d.datos[0];
			main[1] = d.datos[1];
			main[2] = d.datos[2];
			links[0] = d.url[0].url;
			links[1] = d.url[1].url;
			links[2] = d.url[2].url;
			
			setTimeout("cargarItem('0',main[0],links[0]);",0);
			setTimeout("cargarItem('1',main[1],links[1]);",400);
			setTimeout("cargarItem('2',main[2],links[2]);",800);
		}
	}).get();
}		

function cargarItem(pos,d,u) {
	id = "inmo0"+pos;

	dir = "fotos/216x162/";
	foto = "round.php?source=" + dir + d.foto ;

	Array.each.delay(3000,this,[
		[foto],
		function(src){var img = new Image();img.src = src;}
	]);

	
	new Fx.Tween(id,{duration: 1000}).start('opacity',0).chain(
		function() {
			
			$("f0"+pos).set('src', '');				
			
			nombre =  trim(d.descripcion);
			nombre = (nombre.length==0) ? d.tipo : nombre;
			dir = "fotos/216x162/";
			foto = "round.php?source=" + dir + d.foto ;
			
			$("f0"+pos).set('src', foto);
			
//			if (location.hostname=='eltresporciento.com' || location.hostname=='fulldominio.com') {
				$("t0"+pos).set('text', (d.municipio) );
				$("l0"+pos).set('text', (d.urbanizacion));				
//			} else {
//				$("t0"+pos).set('text', decode_utf8(d.municipio) );
//				$("l0"+pos).set('text', decode_utf8(d.urbanizacion));
//			}
			
			$("p0"+pos).set('text', formatCurrency(d.precio));

			$("linkfoto0"+pos).set('href', u );
			$("linkver0"+pos).set('href', u );
			if (d.id_status==5) {
				$("f0V"+pos).setStyle('display', '');
			}
			else {
				$("f0V"+pos).setStyle('display', 'none');
			}

			if (d.id_status==2) {
				$("f0R"+pos).setStyle('display', '');
			}
			else {
				$("f0R"+pos).setStyle('display', 'none');
			}

			this.start('opacity',1);
			
		}
	);
}


window.addEvent('domready', function(){

	var cd01 = new CountDown({
		element: $("countdown"),
		start: 5,
		finish: 0,
		onComplete: function() {
			this.options.start=10;
			this.start('cargar()');
		}
	}).start();


	$('standard').getElements('div.record').each( function( elem ){
		elem.getElements('img.confoto').each( function( elem2 ){
			x = elem2.src;
			i = new Image(); 
			i.src =  x.replace("round.php?radius=5&source=","round.php?radius=5&colour=E6E3DE&source=");
		})
	});


	$('standard').getElements('div.record').addEvents({
		'mouseover' : function(e){
			
			if (this.getElement('img.sinfoto')) {
				this.getElement('img.sinfoto').src="imagenes/sinfoto2.jpg";
			}

			if (this.getElement('img.confoto')) {
				
				x = this.getElement('img.confoto').src;
				this.getElement('img.confoto').src = x.replace("round.php?radius=5&source=","round.php?radius=5&colour=E6E3DE&source=");
			}
			
		},
		'mouseout' : function(e){ 

			if (this.getElement('img.sinfoto')) {
				this.getElement('img.sinfoto').src="imagenes/sinfoto.jpg";
			}				

			if (this.getElement('img.confoto')) {
				x = this.getElement('img.confoto').src;
				this.getElement('img.confoto').src = x.replace("round.php?radius=5&colour=E6E3DE&source=","round.php?radius=5&source=");
			}

		}
	});
	

	$("cb_estado").addEvent("change", function() {
			this.getSelected().each(function(el) {
			var jsonRequest = new Request.JSON({
				 url: "cargar_municipios.php"
				,onComplete: function(d){
					x = $("cb_municipio");
					x.disabled = true;
					x.length=1;
					for (i=0;i<d.datos.length;i++) {
						var opt = x.appendChild(document.createElement('option'));
						opt.value = d.datos[i].id;
			
//						if (location.hostname=='eltresporciento.com' || location.hostname=='fulldominio.com') {
							opt.text = d.datos[i].descripcion;
//						} else {
//							opt.text =decode_utf8(d.datos[i].descripcion);
//						}						

					}
					x.disabled = false;							
				}
			}).get({'id': el.value});				
		});
	});

	$("cb_tipo").addEvent("change", function() {
			this.getSelected().each(function(el) {
			var jsonRequest = new Request.JSON({
				 url: "cargar_subtipos.php"
				,onComplete: function(d){
					x = $("cb_subtipo");
					x.disabled = true;
					x.length=1;
					for (i=0;i<d.datos.length;i++) {
						var opt = x.appendChild(document.createElement('option'));
						opt.value = d.datos[i].id;

//						if (location.hostname=='eltresporciento.com' || location.hostname=='fulldominio.com') {
							opt.text = d.datos[i].descripcion;
//						} else {
//							opt.text =decode_utf8(d.datos[i].descripcion);
//						}						

					}
					x.disabled = false;							
				}
			}).get({'id': el.value});				
		});
	});
	
		$("z1").addEvents({
			"focus" : function(e){ 
				this.select();
			}
			,
			"blur" : function(e){ 
				if (trim(this.value)!='') {
					v = right('00'+trim(this.value),2);
					this.value= v;
				}
				
			}
		});

	$("z2").addEvents({
		"focus" : function(e){ 
			this.select();
		}
		,
		"blur" : function(e){ 
			v = right('0000'+trim(this.value),4);
			if (v=='0000') {v = '';}
			this.value= v;
		}
	});


	$("buscar_frm").addEvents({
	  "submit" : function(e){ 
		
			new Event(e).stop();

			codigo=(right('00'+trim(this.elements["z1"].value),2) + right('0000'+trim(this.elements["z2"].value),4));
			datos= encodeToHex("0|0|0|0|0");

			location.href="inmuebles-"+codigo+"-"+this.elements["cb_tipo"].value+"-"+this.elements["cb_subtipo"].value+"-"+this.elements["cb_estado"].value+"-"+this.elements["cb_municipio"].value+"-"+this.elements["cb_precio"].value+"-"+0+"-"+ datos +"-"+1+".htm";

		}			  	  
	}); 	

});	