// JavaScript Document

//CANVI COLOR BOTONS

function canviColorBotons(){ 
	if($$('.boto_reservar')!=null){ 
	//agafem tots els botons amb classe .boto_reservar i li afegim events de mouse
	$$('.boto_reservar').each(function(el){
		el.addEvent('mouseenter', function(){
			el.style.background="#A10004";
			el.style.color="white";
			el.style.borderColor="black";
			});
		el.addEvent('mouseleave', function(){
			el.style.background="#E6E6E6";
			el.style.color="gray";
			el.style.borderColor="#cacaca";
			}
		);
	});
	}

	if($("search_button")!=null){
		$("search_button").onmouseover=function(){$("search_button").style.background="#A10004";
		$("search_button").style.color="white";
		$("search_button").style.borderColor="black";}

		$("search_button").onmouseout=function(){$("search_button").style.background="#E6E6E6";
		$("search_button").style.color="gray";
		$("search_button").style.borderColor="#cacaca";}
	}
}


/*******************************************
***	SLIDESHOW home
********************************************/
//idioma actual
var urlRaiz = 'http://www.dluxehotels.com/'+language+'/hotel';
//total imatges
var slideimages= 5;
//links a hotels
var slidelinks=new Array("/aviles/zen_balagares/5145/0","/venecia/palace_bonvecchiati/7118/0","/milan/nhow_milano/7087/0","/praga/987_prague/8482/0","/atenas/arion_resort__spa_riviera_vouliagmeni/5284/0");
//titols imatges
var slidetitles=new Array("Zen Balagares, Avilés, Spain","Palace Bonvecchiati, Venice, Italy ",'Nhow Milano, Milano, Italy','987 prague, Prague, Czech Republic','Arion Resort & Spa, Athens, Greece');

function slideit(element){
	img_actual=element!=0?"imatge"+(element-1):"imatge"+(slideimages-1);

	//$(img_actual).fade(0);
	var myEffect3 = new Fx.Tween(img_actual, {duration: '1550'});
	myEffect3.start('opacity',0);
	var id = setTimeout("canviarImatge("+element+")",200);
	setTimeout("clearInterval("+id+")",200);
	amagarTitol('titol_imatge',element);
	var id4 = setTimeout("mostrarTitol('titol_imatge',"+element+")",1350);
	setTimeout("clearInterval("+id4+")",1350);
	//si es ultima tornem a comen�ar amb la primera imatge
	element= element!=slideimages-1?element+1:0;
	var id3 = setTimeout("slideit("+element+")",10000);
	setTimeout("clearInterval("+id3+")",11000);
}

function canviarImatge(element){
img_actual=element!=0?"imatge"+(element-1):"imatge"+(slideimages-1);
img_nova="imatge"+element;
	
	//$(img_actual).style.display="none";
	//$(img_nova).style.display="block";
	//mostrem imatge
	//$(img_nova).fade(1);
	var myEffect2 = new Fx.Tween(img_nova, {duration: '1550'});
	myEffect2.start('opacity',1);
        $('link_imatge_home').href=urlRaiz+slidelinks[element];
	
}
function mostrarTitol(id,element){
	var myEffect = new Fx.Morph(id, {duration: '1050', transition: Fx.Transitions.Cubic.easeOut});
		myEffect.start({
		    'height': 30
		});
                 
		$("text_titol_imatge").innerHTML=slidetitles[element];
}
function amagarTitol(id,element){
	var myEffect = new Fx.Morph(id, {duration: '350', transition: Fx.Transitions.Sine.easeOut});	
		myEffect.start({
		    'height': 0
		});

}
//FI SLIDESHOW HOME

function changeHeightElement(id,height){
    var myEffect = new Fx.Morph(id, {duration: '350', transition: Fx.Transitions.Sine.easeOut});
            myEffect.start({
                'height': height
            });
}


//FILTRE CATEGORIES
function filtrarCategories(categoria){

    /*mirem si la categoria actual está mostrada o no
    **i li invertim l'estat*/
    cadena  =   new RegExp("showedCategoryName"+categoria,"g");
    if(cadena.test($(categoria).className)){
        desmarcar_categoria =   true;
        //desmarquem categoria
        $(categoria).removeClass('showedCategoryName'+categoria);
        $(categoria).removeClass('showed');
        $(categoria).style.color='gray';
        $(categoria).style.fontWeight='normal';
        $(categoria).style.backgroundImage  = 'none';
        //desmarquem hotels
        $$('.'+categoria).each(function(el){
                el.removeClass('showedByCategory'+categoria);
                //si es l'ultima categoria de showedByCategory de l'hotel, traiem el showedHotel (classe generica dels mostrats)
                cadena  =   new RegExp("showedByCategory","g");
                if(!cadena.test(el.className))
                    el.removeClass('showedHotel');
        });
    }       
    else{
        desmarcar_categoria =   false;
        $(categoria).addClass('showedCategoryName'+categoria);
        $(categoria).addClass('showed');
        $(categoria).style.color='#A10004';
        $(categoria).style.fontWeight='bold';
        $(categoria).style.background  = 'url(/website/styles/images/check.png) #EDEDED no-repeat 0px -2px';
    }

    //mirem el numero de categories seleccionades actualment
    numCategoriesSeleccionades  =   $$('.showed').length;
    //comprobem si hi ha varies categories seleccionades per, a l'hora de fer el filtratge, UNIR SELECCIONS
    multiplesCategories         =   (numCategoriesSeleccionades>1)?true:false;

    //CAS si hi ha alguna categoria a mostrar
    if(numCategoriesSeleccionades > 0){
 
        //amaguem tots els hotels que no tenen showedByCategory
        $$('.forms_hotels').each(function(elx){
            cadena  =   new RegExp("showedByCategory","g");
            if(!cadena.test(elx.className)){
                elx.style.display='none';
            }
        });

        //indiquem tots els hotels de la categoria com a showedByCategory (a no ser que estiguem desmarcant una categoria)
        if(!desmarcar_categoria){
            $$('.'+categoria).each(function(el){
                cadena  = /showedHotel/;
                if(!cadena.test(el.className)){
                    //tots els hotels mostrats els marquem amb class genèrica per tal de poderlos agafar tots de cop
                    el.addClass('showedHotel');
                }
                    el.addClass('showedByCategory'+categoria);
                });
        }
       //mostrem tots els hotels de les categories seleccionades (showedHotel)
        $$('.showedHotel').each(function(el){
            el.style.display='block';
            //amaguem els que no tinguin alguna de les categories seleccionades
            if(multiplesCategories && numCategoriesSeleccionades>(el.className).match(/showedByCategory/g).length){
                el.style.display='none';
            }           
            });
            countShowedCategories();
    }
    //CAS cap categoria a filtrar (mostrem tots els hotels)
    else{
        //Mostrem tots els hotels
        $$('.forms_hotels').each(function(elx){
            elx.style.display='block';
        });
        countAllCategories();
    }
}
//FI FILTRE CATEGORIES

function countAllCategories(){
    //recorrem totes les categories mostrades
    $$('.list_titol_categoria ').each(function(el){
        //el.style.display='block';
        changeHeightElement(el.id,17);
        el.style.padding    =   '2px 0 2px 30px';
        $('countCategories_'+el.id).innerHTML   =   $$('form.'+el.id).length;
    });
}
function countShowedCategories(){
    //recorrem totes les categories mostrades
    $$('.list_titol_categoria ').each(function(el){
        count   =   0;
        //per cada hotel mostrat
        $$('form.'+el.id).each(function(el1){
            //si esta mostrat
            if(el1.style.display=='block'){
                count++;
            }
        });
        $('countCategories_'+el.id).innerHTML   =   count;
        //si la categoria no conté hotels, l'amaguem'
        if(count==0){
            //el.style.display='none';
            changeHeightElement(el.id,0);
            el.style.padding    =   '0 0 0 30px';
        }
        //si conté hotels la mostrem sempre (per si al desmarcar una categoria n'hi ha d'altres que tornen a tenir hotels)
        else{
            changeHeightElement(el.id,17);
            el.style.padding    =   '2px 0 2px 30px';
        }
    });
}
//UTILITZAT EN porque.php
function mostrar(enla) {
obj = document.getElementById('oculto'+enla);
obj.style.display = (obj.style.display == 'none') ? 'block' : 'none';
}

//indicar dates buides en vermell (buscador lateral)
function showEmptyField(){

	var id= setInterval("canviarColorFons()",1000);
        setTimeout("clearInterval("+id+")",7000);
        setTimeout("hideBafarada()",7000);
      
}
//canviar el color de fons d'un element
function canviarColorFons(){
 $$('.showEmpty').each(function(el){
    var myFx = new Fx.Tween(el);
    if(el.style.border=='1px solid rgb(161, 0, 4)'){
        el.style.border='1px solid #cacaca';     
          myFx.start('background-color', '#ffffff');
    }
    else{
        el.style.border='1px solid rgb(161, 0, 4) ';
          myFx.start('background-color', '#ffffc8');
    }
     });
}
//desapareixer bafarada
function hideBafarada(){
    $$('.bafarada').fade('out');
    //posem boto vermell
    var myFx = new Fx.Tween($('search_button'),{duration: 'long'});
    myFx.start('background-color', '#A10004');
    $('search_button').style.color='white';
    $('search_button').style.border='1px solid black';
    //deixem elements en groc
    $$('.showEmpty').each(function(el){
        el.style.border='1px solid rgb(161, 0, 4) ';
        el.style.backgroundColor =  '#ffffc8';
    });
}
//utilitzat en hotel.php
function CreateBookmarkLink() {

 var title = document.title;
 var url = location.href;
 if (title == '')
   title = "DLuxeHotels.com - Tu buscador de hoteles de lujo";
 if (url == '')
   url = "www.dluxehotels.com";
  // Blogger - Replace with <$BlogItemTitle$> 
  // MovableType - Replace with <$MTEntryTitle$>

  // Blogger - Replace with <$BlogItemPermalinkURL$> 
  // MovableType - Replace with <$MTEntryPermalink$>
  // WordPress - <?php bloginfo('url'); ?>

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite(url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
 }
 
 
function DaysBetween(date1,date2) {
	date1_array = date1.split("/");
	date2_array = date2.split("/");
	date1_int = date1_array[2] + '-' + date1_array[1] + '-' + date1_array[0];
	date2_int = date2_array[2] + '-' + date2_array[1] + '-' + date2_array[0];
	fecha1=new Date(date1_array[2],date1_array[1]-1,date1_array[0]);
	fecha2=new Date(date2_array[2],date2_array[1]-1,date2_array[0]);
	var resta=Math.round((fecha2-fecha1)/1000/3600/24); 
	return resta;
}

//retorna el height inicial d'un element
function obtenirHeightInicial(nomElement){
    heightInicial   =   $(nomElement).offsetHeight;
    $(nomElement).style.height='0px';
}

var heightInicial;
function showHide(nomElement){
    height= heightInicial+'px';
    var myEffect = new Fx.Morph(nomElement, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
    if($(nomElement).style.height==height){
        myEffect.start({
            'height': 0 //Morphs the height from the current to x
        });
    }
    else{
        myEffect.start({
            'height': heightInicial //Morphs the height from the current to x.
        });
    }
}
