/*
.........................
:: Javascript a medida. ::
.........................
*/

/*-------------------------------
::  MENU DESPLEGABLE  ::
--------------------------------*/

function init() {
	var x = $('nav').getElementsByTagName('li');
	for (var i=0;i<x.length;i++)
	{
        addEvent(x[i],"click",mostrarSubNav);
        addEvent(x[i],"focus",mostrarSubNav);
		addEvent(x[i],"blur",ocultarSubNavs);
		addEvent(x[i],"mouseover",anularHover);
	}
    addEvent($('header'),"mouseover",ocultarSubNavs);
    addEvent($('closemenu'),"mouseover",ocultarSubNavs);
}

function mostrarSubNav(e) {
    ocultarSubNavs();
    this.className = this.className += ' over';
}

function ocultarSubNav(e) {
	if (this.className.indexOf('over')!=-1) this.className = this.className.replace(/over/g,'');
}

function ocultarSubNavs() { 
    var x = $('nav').getElementsByTagName('li');
    for (var i=0;i<x.length;i++)
    {
        if (x[i].className.indexOf('point')!=-1) {
            if (x[i].className.indexOf('over')!=-1) x[i].className = x[i].className.replace(/over/g,'');
        }
    }
}

function anularHover(e)
{
    if (this.className.indexOf('nohover')==-1) this.className += ' nohover';
}

/*------------------
::   detectar IE  ::
---------------------*/
function isIE()
{
    var userAgent   =   navigator.userAgent.toLowerCase();
    return (document.all && userAgent.indexOf('msie')!=-1);
}

/*-------------------------------
::   prototype in ARRAY  ::
--------------------------------*/

Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};	

/*-------------------------------
::   control de EVENTOS   ::
--------------------------------*/

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);

/*--------------------------------
::        CALENDARIO         ::
--------------------------------*/

function abre(prefijo, n){
	//posicionaCalendarios();

	var cal=$(prefijo + 'cal'+n);
	if(n==1)
		cal.src='calendario.php?pcal=' + prefijo + '&n=1&fecha='+document.getElementById(prefijo + 'f1h-'+n).value; /*no ponemos ftope, porque ser� el dia de hoy */
	else if(n==2)
		cal.src='calendario.php?pcal=' + prefijo + '&n=2&fecha='+document.getElementById(prefijo + 'f1h-'+n).value+'&ftope='+document.getElementById(prefijo + 'f1h-1').value;
	fadeCalendar(prefijo, n,0,100,20);
	/*cal.style.display="block";*/
}

function fadeCalendar(prefijo, num,ini,fin,salto){
	var idCal=prefijo + 'cal'+num;
	if((salto>0 && ini<=fin) || (salto<0 && ini>=fin)){
		if(ini==0)
			$(idCal).style.display="none";
		else
			$(idCal).style.display="block";
			
		if (window.ActiveXObject) $(idCal).style.filter="alpha(opacity="+(ini)+")";
		$(idCal).style.opacity=ini/100;
		setTimeout('fadeCalendar("' + prefijo +  '", '+num+','+(ini+salto)+','+fin+','+salto+')' ,50);
	}
}




function setData(prefijo, n,data,humana){
	fadeCalendar(prefijo, n,80,0,-20);

/*	for(i=1;i<=2;i++){
		if($('cal'+i)){
			$('cal'+i).style.display="none";
		}
	}
	*/
	document.getElementById(prefijo + 'f1h-'+n).value=data;
	document.getElementById(prefijo + 'f1-'+n).innerHTML=humana;
	
	if(n==1)
		abre(prefijo, 2);

	if(n==2 && $(prefijo + 'f1h-1').value!='' && $(prefijo + 'f1h-2').value!=''){
		marcaBoton('222','fff',1);
	}	
}

function marcaBoton(ini,fin,salto){
	ini=ini.toLowerCase();
	fin=fin.toLowerCase();
	$('botonVer').style.background="#"+ini;
	$('_botonVer').style.background="#"+ini;
	if(ini!=fin){
		if(ini.substr(0,1)!=fin.substr(0,1))	siguienteR=(parseInt(ini.substr(0,1),16)+salto).toString(16);
		else									siguienteR=ini.substr(0,1);
		if(ini.substr(1,1)!=fin.substr(1,1))	siguienteG=(parseInt(ini.substr(1,1),16)+salto).toString(16);
		else									siguienteG=ini.substr(1,1);
		if(ini.substr(2,1)!=fin.substr(2,1))	siguienteB=(parseInt(ini.substr(2,1),16)+salto).toString(16);
		else									siguienteB=ini.substr(2,1);

		setTimeout('marcaBoton("'+siguienteR+siguienteG+siguienteB+'","'+fin+'",'+salto+')' ,20);
	}else{
    $('botonVer').style.color='#222';
    $('_botonVer').style.color='#222';
    }
}


function posicionaCalendarios(prefijo){
	/*Calendario1*/
	posicion= new Position.cumulativeOffset($(prefijo + 'f1-1'));
	posxBase=posicion.first();
	posyBase=posicion.last();
	cal=$(prefijo + 'cal1');
    cal.style.left=posxBase+"px";
	cal.style.top=(posyBase-144)+"px";

	/*Calendario1*/
	posicion= new Position.cumulativeOffset($(prefijo + 'f1-2'));
	posxBase=posicion.first();
	posyBase=posicion.last();
	cal=$(prefijo + 'cal2');
	cal.style.left=posxBase+"px";
	cal.style.top=(posyBase-144)+"px";
}					


/************/
/* RESERVA */ /* 219 es la id de President Park */
/**********/

function abreReserva(idioma){
	if($('f1h-1').value.length>1 && $('f1h-2').value.length>1){
		window.open('http://www.husa.es/'+idioma+'/gen_fichaHotel.php?id=16&f1fEnter='+$('f1h-1').value+'&f1fExit='+$('f1h-2').value,'reserva');
	}
}

function abreReservaNew(){
	if (($('f1h-1').value.length>1 && $('f1h-2').value.length>1) ||
        ($('_f1h-1').value.length>1 && $('_f1h-2').value.length>1)) {
		return true;
	}
	else return false;
}
