function ini() {	
    //create the tooltips
	preparaTips();
    var tips = new Tips('.tiphab',{  
        className: 'tip-hab',
        hideDelay: 50,  
        showDelay: 50
    });
	if(Browser.Engine.trident && !Browser.Engine.trident5) {
		if(!Browser.Engine.trident5) {
			$('iecss').href = 'css/ie.css';
		}
		tips.addEvents({'hide':tipHide, 'show':tipShow});
	}
	else {
		$$('.overLang').addEvents({'mouseenter':overSimple, 'mouseleave':outSimple});	
	}
	$$('.mSeccion').addEvents({'mouseenter':overSeccion, 'mouseleave':outSeccion});
	$$('.ajax').addEvent('click', mSeccionClick);
	$$('.capac').addEvents({'mouseenter':overEstado, 'mouseleave':outEstado, 'click':capacClick});
	$$('.tiphab').addEvents({'mouseenter':miniOver, 'mouseleave':miniOut, 'click': miniClick});
	$$('.over').addEvents({'mouseenter':overSimple, 'mouseleave':outSimple});
	$$('.fReserva').addEvent('submit', prepararReserva);
	t = setTimeout('cargaInicial()', 1000);
}

trackAnalitycs = function(path)
{
	if(pageTracker)
	{
		pageTracker._trackPageview(path);
	}
}

function cargaInicial() {
	clearTimeout(t);
	// cargar banner superior
	new Request({
		url: '',
		method: 'get',
		onSuccess: function(a){
			$('banner').set('html', a);
			if(secActual == 'apart') {
				new Request({
					url: 'includes/modules/ajax_video_rooms.php',
					method: 'get',
					data: 'hab='+ordActual,
					onSuccess: function(a){
						if($('video')!=null){
							$('video').set('html', a);
						}
						cargarResto();
					}
				}).send();
			}
			else {
				cargarResto();
			}
		}
	}).send();	
}


function cargarResto() {
	new Request({
		url: 'includes/modules/ajax_sidebar.html',
		method: 'get',
		onSuccess: function(a) {
			if($('animacion')!=null){
				$('animacion').set('html', a);
			}
			new Request({
				url: 'includes/modules/ajax_moving.html',
				method: 'get',
				onSuccess: function(a) {
					if($('moving')!=null){
						$('moving').set('html', a);	
					}
				}
			}).send();
		}
	}).send();
}
function preparaTips() {
	$each($$('.tiphab'), function(elem) {
		var img = elem.getElements('img')[0];
		var content = elem.get('title').split(' :: '); 
		elem.store('capac', content[1]);
		elem.store('img_id', img.getProperty('alt'));
		img.erase('alt');
		//elem.store('tip:title', 
		//'<p class="capacidad">'+content[1]+'</p><p>'+desde+':<br/><span class="precio">'+content[2]+'</span></p>');
		elem.store('tip:title', 
		'<p class="capacidad">'+content[1]+'</p>');
		elem.store('tip:text', content[0]);
		elem.store('tip:titlebg', 'url(images/'+elem.retrieve('img_id')+'.jpg)');
	});
}
function linksExternos(){
	$$('.externo').each(function(elem){
		elem.removeEvents('click');
		elem.addEvent('click', function(e) {
			e.stop();
			open(this.href);
		});
	});
}
function cambiarMails() {
	$$('.mail').each(function(elem) {
		elem.href = elem.href.replace('(ARROBA)', '@');
		var nodo = elem.firstChild;
		nodo.nodeValue = nodo.nodeValue.replace('(ARROBA)', '@');
	});
}
function cambiarImagen(img, i) {
	var src = img.src;
	var ext = src.substring(src.length-4);
	var nom = src.substring(0, src.length-5);
	img.src = nom + i + ext;
}
function overSeccion() {
	var enlace = this;
	if(secActual != enlace.id) {
		var imgs = enlace.getElements('img');		
		cambiarImagen(imgs[0], 1);
	}
}
function outSeccion(e) {
	var enlace = this;
	if(secActual != enlace.id) {
		var imgs =  enlace.getElements('img');
		cambiarImagen(imgs[0], 0);
	}
}
function overEstado() {
	var enlace = this;
	if(enlace.retrieve('estado') != 2) {
		var imgs = enlace.getElements('img');		
		cambiarImagen(imgs[0], 1);
		enlace.store('estado', 1);
	}
}
function outEstado(e) {
	var enlace = this;
	if(enlace.retrieve('estado') != 2) {
		var imgs =  enlace.getElements('img');
		cambiarImagen(imgs[0], 0);
		enlace.store('estado', 0);
	}
}
function capacClick(e) {
	e.stop();
	var enlace = this;
	if(enlace.retrieve('estado') == 2) {
		enlace.store('estado', 1);
		cambiarImagen(enlace.getElements('img')[0], 1);
		desmarcar(enlace.getElements('img').getProperty('alt'));
	}
	else {
		$$('.capac').each(function(elem) {
			if(elem != enlace) {
				elem.store('estado', 0);
				cambiarImagen(elem.getElements('img')[0], 0);
			}
		});
		enlace.store('estado', 2);
		cambiarImagen(enlace.getElements('img')[0], 2);
		marcar(enlace.getElements('img').getProperty('alt'));		
	}
}
function rutaClick(e) {
	e.stop();
	var enlace = this;
	if(enlace.retrieve('estado') == 2) {
		enlace.store('estado', 1);
		cambiarImagen(enlace.getElements('img')[0], 1);
		descargarRuta();
	}
	else {
		$$('.ruta').each(function(elem) {
			if(elem != enlace) {
				elem.store('estado', 0);
				cambiarImagen(elem.getElements('img')[0], 0);
			}
		});
		enlace.store('estado', 2);
		cambiarImagen(enlace.getElements('img')[0], 2);
		cargarRuta(enlace.getProperty('rel'));
		trackAnalitycs(enlace.get("href"));
	}
}
function cargarRuta(desde) {
	var elem = this;
	if(!$defined($('bloqueRuta'))){
		var div = new Element('div', {
			'id': 'bloqueRuta',
			'class': 'bloqueRuta'
		});
		if($('video')!=null){
			$('video').insertBefore(div, $('video').firstChild);
		}
	}
	new Request({
		method: 'get',
		url: 'includes/modules/ajax_ruta.php',
		data: 'd='+desde,
		onSuccess: function(texto) {
			$('bloqueRuta').innerHTML = texto;
			$('accion').addEvent('click', function(e){
				e.stop();
				$$('.ruta').each(function(elem) {
					if(elem.retrieve('estado') == 2) {
						elem.store('estado', 2);
						cambiarImagen(elem.getElements('img')[0], 0);				
					}
				});
				descargarRuta();
			});
			linksExternos();
		}
    }).send();
}
function descargarRuta() {
	$('bloqueRuta').dispose();
}
function marcar(capac) {
	$$('.tiphab').each(function(elem) {
		if(elem.retrieve('capac') != capac) {
			elem.fade(0.3);
			elem.store('estado', 0);
		}
		else {
			elem.fade(1);
			elem.store('estado', 1);
		}
	});
}
function desmarcar(capac) {
	$$('.tiphab').each(function(elem) {
		if(elem.retrieve('capac') != capac) {
			elem.fade(1);
			elem.store('estado', 1);
		}
	});
}
function overSimple() {
	cambiarImagen(this.getElements('img')[0], 1);
}
function outSimple() {
	cambiarImagen(this.getElements('img')[0], 0);
}
function miniOver() {
	if(this.retrieve('estado') != 0) {
		this.set('opacity', '.5');
	}
}
function miniOut() {
	if(this.retrieve('estado') != 0) {
		this.set('opacity', '1');
	}
}
function escondeSelects() {
	if(!window.attachEvent) return false;
	var selects = document.getElementsByTagName('select');
	for(var i=0; i<selects .length; i++ ) {
		selects[i].style.display = 'none';
	}
}
function tipShow(tip) {
	var imgsrc = 'reserva_comp_bg.png';
	if(secActual == 'apart') {
		imgsrc = 'reserva_simp_bg.png';
	}	
	$$('.fReserva').setStyle('background-image', 'url(images/interfaz/'+imgsrc+')');
	$$('select').setStyle('visibility', 'hidden');
}
function tipHide(tip) {
	$$('.fReserva').setStyle('background-image', 'url(images/interfaz/blank.gif)');
	$$('select').setStyle('visibility', 'visible');	
}
function abrirDesc(e) {
	e.stop();
	var elem = this;
	var div = new Element('div', {
		'id': 'descLarga',
		'class': 'descLarga'
	});

	$('hab').insertBefore(div, $('hab').firstChild);
	new Request({
		method: 'get',
		url: 'includes/modules/ajax_leermas.php',
		data: 'hab='+elem.getProperty('rel'),
		onSuccess: function(texto) {
			$('descLarga').innerHTML = texto;
			$('accion').addEvent('click', function(e) {
				e.stop();
				$('descLarga').dispose();
			});
		},
		onComplete:function()
		{
			trackAnalitycs(elem.get("href"));
		}
    }).send();
}
function abrirAbout(e) {
	e.stop();
	var elem = this;
	var div = new Element('div', {
		'id': 'descLarga',
		'class': 'descLarga'
	});

	$('hab').insertBefore(div, $('hab').firstChild);
	new Request({
		method: 'get',
		url: 'includes/modules/ajax_about.php',
		data: 'hab='+elem.getProperty('rel'),
		onSuccess: function(texto) {
			$('descLarga').innerHTML = texto;
			$('accion').addEvent('click', function(e) {
				e.stop();
				$('descLarga').dispose();
			});
		},
		onComplete:function()
		{
			trackAnalitycs(elem.get("href"));
		}
    }).send();
}
function insertarLoader() {
	var loader = new Element('img', {'id':'loader', 'class': 'loader', 'src': 'images/interfaz/loading.gif', 'width': '24', 'height': '24', 'alt': 'Loading'});
	loader.setStyles({
		display: 'block',
		position: 'absolute',
		margin: '-200px 0 0 -12px',
		left: '50%'
	});
	loader.inject($('content'), 'after');
}
function mSeccionClick(e) {
	e.stop();	
	
	var elem = this;
	if((secActual=='apart') || (secActual != elem.rel)) {
		
		$('rightBlock').fade(0.5);
		insertarLoader();
		var aurl = '';
		var jurl = '';
		var smenu = '';
		var isGallery = false;
		switch(elem.rel) {
			case 'apart': {	
				aurl = 'ajax_apart.php';
				jurl = 'ajax_apart.js';
				smenu = 'apart';
				break;
			}
			case 'llegar': {
				aurl = 'ajax_llegar.php';
				jurl = 'ajax_llegar.js';
				smenu = 'llegar';
				break;
			}
			case 'serv': {				
				aurl = 'ajax_serv.php';
				jurl = 'ajax_serv.js';
				smenu = 'serv';
				break;
			}
			case 'gallery': {				
				aurl = 'ajax_gallery.php';
				jurl = 'ajax_gallery.js';
				smenu = 'gallery';
				isGallery = true;
				break;
			}
		}	
		new Request({
			method: 'get',
			url: 'includes/modules/'+aurl,
			onSuccess: function(texto) {
				$('rightBlock').set('html', texto);
				sustituirClase(smenu);				
				new Request({
					method: 'get',
					url: 'js/'+jurl,
					evalResponse: true
				}).send();
				sustituirMenu(smenu);
				$('loader').dispose();
				$('rightBlock').fade(1);
			},
			onComplete:function()
			{
				trackAnalitycs(elem.get("href"));
				if(isGallery) $(document.body).addClass("gallery");
				else $(document.body).removeClass("gallery");
			}
		}).send();
	}
}
function sustituirClase(smenu) {
	if(smenu == 'llegar') {
		$('content').removeClass('serv');
		$('content').removeClass('apart');			
	}
	else if(smenu == 'serv') {
		$('content').removeClass('llegar');
		$('content').removeClass('apart');					
	}
	else {
		$('content').removeClass('serv');
		$('content').removeClass('llegar');
	}
	$('content').addClass(smenu);
}
function miniClick(e) {
	e.stop();
	var elem = this;
	$('rightBlock').fade(0.5);
	insertarLoader();
	aurl = 'ajax_apart.php';
	jurl = 'ajax_apart.js';
	smenu = 'apart';
	new Request({
		method: 'get',
		url: 'includes/modules/'+aurl,
		data: 'hab='+elem.getProperty('rel'),
		onSuccess: function(texto) {
			$('rightBlock').set('html', texto);
			sustituirClase(smenu);
			new Request({
				method: 'get',
				url: 'js/'+jurl,
				evalResponse: true
			}).send();
			if(smenu != secActual) sustituirMenu(smenu);
				$('loader').dispose();
			$('rightBlock').fade(1);
		},
		onComplete:function()
		{
			trackAnalitycs(elem.get("href"));
		}
	}).send();
}
function sustituirMenu(id) {
	cambiarImagen($(id).getElements('img')[0], 2);
	cambiarImagen($(secActual).getElements('img')[0], 0);	
	secActual = id;	
}
function cambiarAlto() {
	if(window.getSize().y >= $('page').getSize().y) {
		$('page').setStyle('height', window.getSize().y);
	}
}
function prepararReserva(e){
	e.stop();
	var form = this;
	form.setProperty('target', '_blank');
	
	var id = form.id.split('_');
	var num = id[1];
	
	var arr_day = $('ARR_day_'+num).value;
	var arr_mes = $('ARR_month_'+num).value;
	var arr_year = $('ARR_year_'+num).value;
	
	var ARR = arr_year+'-';
	ARR += (arr_mes < 10)?'0'+arr_mes.toString()+'-':arr_mes.toString()+'-';
	ARR += (arr_day < 10)?'0'+arr_day.toString()+'-':arr_day.toString();
	
	form.ARR.value = ARR;
	
	var dep_day = $('DEP_day_'+num).value;
	var dep_mes = $('DEP_month_'+num).value;
	var dep_year = $('DEP_year_'+num).value;
	
	var DEP = dep_year+'-';
	DEP += (dep_mes < 10)?'0'+dep_mes.toString()+'-':dep_mes.toString()+'-';
	DEP += (dep_day < 10)?'0'+dep_day.toString()+'-':dep_day.toString();
	
	form.DEP.value = DEP;
	
	form.submit();
	trackAnalitycs("/book");
}

function escribe_flash(pelicula,dimX,dimY,texto){
	document.write('<div style="width:100%;height:100%;">');
	document.write('	<object type="application/x-shockwave-flash" data="'+pelicula+'" width="'+dimX+'" height="'+dimY+'">');
	document.write('		<param name="movie" value="'+pelicula+'" />');
	//document.write('		<param name="allowfullscreen" value="true" />');
	document.write('		<p>');
	document.write('			'+texto);
	document.write('		</p>');
	document.write('	</object>');
	document.write('</div>');
}
