window.addEvent('domready', function() {
mapa_cargado=0;//variable para indicar cuando el mapa se ha cargado
/*
	//Efecto hover en el menu principal
	$$('#menu ul li').addEvents({
		'mouseover': function(){
			$(this).removeClass("boton").addClass("botonhover"); //Efecto mouseover
		},
		'mouseout': function(){
			$(this).removeClass("botonhover").addClass("boton"); //Efecto mouseover
		}
	});
	* */
	new dropdownmenu('menu_list', 'ul.submenu');
});

function requestLoading(){
	//if (typeof(mapa_cargado) != 'undefined'){	
		if (mapa_cargado==1 || mapa_cargado==3) {//en 1 es la primera vez del mapa y en 3 es cualquier otra vez del mapa. En 0 nunca se ha entrado
			mapa_cargado=2;//valor para indicar que se ha salido del mapa
			GUnload();
		}
	//}
	$('contenidopagina').set('html', '<div style="text-align:center;width:100%;margin:15px 0 30px 0;"><strong>Espere unos momentos mientras se carga la informaci&oacute;n...</strong><br/><br/><br/><img style="margin:auto; display:block;" src="style/loading.gif" alt="Cargando..." title="Por favor espere"/></div>');	
}
function divLoading(iddiv){
	$(iddiv).set('html', '<strong>Espere unos momentos mientras se carga la informaci&oacute;n...</strong><br /><strong>Wait a second while information is loaded...</strong><br /><img style="margin:auto; display:block;" src="style/loading.gif" alt="Cargando.../ Loading..." title="Por favor espere / Please wait"/>');	
	$(iddiv).setStyle('height','40px');
	$(iddiv).setStyle('visibility','visible');
}
function divRemoveLoading(iddiv){
	$(iddiv).set('text', '');
	$(iddiv).setStyle('visibility','hidden');	
	$(iddiv).setStyle('height','0px');
}
function requestFailed(){
	$('contenidopagina').set('html', '<strong>Intente nuevamente / Try again</strong>');
}


//Crea el evento clic para enlaces para ver inmueble desde el mapa
function clickMapaInmuebleDetalles(direccion){
	var req = new Request.HTML({
		url:direccion,//
		onSuccess: function(tree,elements,html) {
			paginaInmuebles(tree,elements,html);
		},
		onFailure: requestFailed,
		onRequest: requestLoading
	}).send();
	
	return false;
}


function clickVerMapa(event){
	var req = new Request.HTML({
		url:this.get('name'),//
		evalScripts:false, // don't ask mootools to process js
		evalResponse:false, // we will process it manually
		onSuccess: function(tree,elements,html,javascript) {
			//new Asset.javascript("http://maps.google.com/maps?file=api&amp;v=2&amp;hl=es&amp;key="+ $('body').get('class'));
			/*var myFx = new Fx.Tween($('contenidopagina'),{property: 'opacity',duration: 250, transition: Fx.Transitions.linear,link: 'chain'});
			myFx.start(0.4, 1);						
			*/
			$('contenidopagina').set('text', '');
			//Inject the new DOM elements into the results div.	
			$('contenidopagina').adopt(tree);
			if (mapa_cargado==0){//en 0 nunca se ha abierto el mapa, solo se debe hacer una vez
				new Asset.javascript('js/mapa.js'); 
				//new Asset.javascript('js/mapa.js',{load: function(){ alert(predlat); $exec(javascript);}});
				$('contenidopagina').store("codigojs",javascript);
				window.addEvent('domready', function() {
					esperacargarmapa=setInterval("cargaMapa()",2000);
					//$exec(javascript); // process javascript manually AFTER element is loaded
				});
				window.addEvent('unload', function() {
					GUnload();
				});
			}
			else{
				map.setCenter(new GLatLng(predlat, predlng),predzoom);
				map.addControl(new GSmallMapControl());	
				//map.addControl(new GOverviewMapControl());
			}
	
			//$exec(javascript); // process javascript manually AFTER element is loaded
			
			//new Asset.javascript("http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAtvOZR6_u-Lm394gH6TSjZBQ92WsOSS44KcnVG83Y12NRJb7pWBQ6pfFC5NRfg1IhgaTM9tg8IxxHzw");
			//if (javascript) $exec(javascript);
			
		},
		onFailure: requestFailed,
		onRequest: requestLoading
	}).send();
	return false;
}

function cargaMapa(){
	if (typeof(customIcons) != 'undefined'){
		if (mapa_cargado==1){//en 1 es la primera vez del mapa, solo se debe hacer una vez
			javascript=$('contenidopagina').retrieve("codigojs");
			$('contenidopagina').store("codigojs","");
			$exec(javascript);			
		}
		clearInterval(esperacargarmapa);
		map_load();
	}
}


function mostrar_ocultar(id){
    if (document.getElementById(id).style.display == ''){
        //$(id).slide('in')
		document.getElementById(id).style.display = 'none';
    }else{
        //$(id).slide('out');
		document.getElementById(id).style.display = '';
    }
	return false;
}


var dropdownmenu = new Class({
 Implements: [Events],
 initialize: function(menu, links){
  this.menu = $(menu);
  this.submenu = links;
  $$(this.submenu).each(function(item){
   item.setStyles({'visibility': 'hidden'});
  });
  this.attach();
 },
 attach: function() {
  this.menu.getChildren('li').addEvents({
    'mouseenter': function(e){      
      $$(this.submenu).set({
        'duration': 0,        
       'visibility' : 'hidden',
       'opacity': 0.9
		});
      this.getChildren("ul")
      .set({
      'duration': 0,
      'visibility' : 'visible',
      'opacity': 1
     });
   },
    'mouseleave': function(e){
		$$(this.submenu).set({
        'duration': 0,
       'visibility' : 'hidden',
       'opacity': 0
		});
	}.bind(this)
  });
 }
});


