/**
* süteikezelés
*/


function setCookie(c_name,value,expiredays){
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name){
    if (document.cookie.length>0){
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1){
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
      }
    }
    return "";
}

/**
*   Galéria Objektum
*   Konstruktor paraméterek: galéria konténer div, bélyegkép konténer div, inicializáláskor hanydik képet mutassa.
*/
var Gallery = new Class({
    initialize: function(name,g_selector,thumb_selector,current){
       this.current = (current)?current:0;
       this.left = 2;
       this.right = 2;
       this.gs = g_selector;
       this.ts = thumb_selector;
       this.state = 'thumb'; //mixed módban melyik állapotban vagyunk       
       this.gal = $$(this.gs+' .gallery_pager div a');
       this.tmb = $$(this.ts+' .gallery_pager div a');     
       this.scrollPos = this.current;                       
       this.speed = 750;
       this.name = name;
       this.oss = 0;
       if(this.gal.length > 0 && this.tmb.length > 0){
            $$(this.gs)[0].set('tween',{duration:this.speed}).setStyles({'opacity':'0.0','z-index':'0','top':'38px'});
            $$(this.ts)[0].setStyles({'opacity':'1.0'});
            this.mode = 'mixed';            
            $$(this.gs+' .scroll_right')[0].setProperty('alt',name+'.next();').addEvent('click',function(e){
                e.stop();
                eval(this.getProperty('alt'));                
            });
            $$(this.ts+' .scroll_right')[0].setProperty('alt',name+'.scrollRight();').addEvent('click',function(e){
                e.stop();
                eval(this.getProperty('alt'));                
            });
            $$(this.gs+' .scroll_left')[0].setProperty('alt',name+'.last();').addEvent('click',function(e){
                e.stop();
                eval(this.getProperty('alt'));                
            });
            $$(this.ts+' .scroll_left')[0].setProperty('alt',name+'.scrollLeft();').addEvent('click',function(e){
                e.stop();
                eval(this.getProperty('alt'));                
            });
            $$(this.gs+' .gallery_close a')[0].setProperty('alt',name+'.close();').addEvent('click',function(e){
                e.stop();
                eval(this.getProperty('alt'));                
            });
            img = this.tmb[0].getElement('img'); 
            this.oss = this.tmb.length;
       }else if(this.gal.length > 0){
            this.mode = 'big';
            $$(this.gs)[0].set('tween',{duration:this.speed}).setStyles({'opacity':'1.0'});
            $$(this.gs+' .scroll_right')[0].setProperty('alt',name+'.next();').addEvent('click',function(e){
                e.stop();
                eval(this.getProperty('alt'));                
            });
            $$(this.gs+' .scroll_left')[0].setProperty('alt',name+'.last();').addEvent('click',function(e){
                e.stop();
                eval(this.getProperty('alt'));                
            });
            $$(this.gs+' .gallery_close a')[0].setProperty('alt',name+'.close();').addEvent('click',function(e){
                e.stop();
                eval(this.getProperty('alt'));                
            });
            img = this.gal[0].getElement('img');  
            this.oss = this.gal.length;          
       }else if(this.tmb.length > 0){
            this.mode = 'thumb';
            img = this.tmb[0].getElement('img'); 
            $$(this.ts+' .scroll_right')[0].setProperty('alt',name+'.scrollLeft();').addEvent('click',function(e){
                e.stop();
                eval(this.getProperty('alt'));                
            });
            $$(this.ts+' .scroll_left')[0].setProperty('alt',name+'.scrollRight();').addEvent('click',function(e){
                e.stop();
                eval(this.getProperty('alt'));                
            });
            this.oss = this.tmb.length;
       }else{
            img = null;
            this.mode = 'none';
       }
       if(img){
            this.oneSize = parseInt(img.width)
                          +parseInt(img.getStyle('margin-left'))
                          +parseInt(img.getStyle('margin-right'))
                          +parseInt(img.getStyle('padding-left'))
                          +parseInt(img.getStyle('padding-right'));                        
       }else{
            this.oneSize = 0;
       }    
       this.images = [];                    
       for(i=0;i<this.gal.length;i++){                        
            kep = $$(this.gs+' img[src="'+this.gal[i].getAttribute('href')+'"]')[0];
            if(i==this.current){kep.setStyle('opacity',1)}else{kep.setStyle('opacity',0);}
            kep.set('tween',{duration: this.speed});            
            this.images[this.images.length] = kep;
            if(i==0)this.gal[0].getParent('div').set('tween',{duration: this.speed});
            this.gal[i].setProperty('alt',name+'.show('+i+');').addEvent('click',function(e){
                e.stop();
                eval(this.getProperty('alt'));                
            });       
       }                                          
       for(i=0;i<this.tmb.length;i++){
            this.tmb[i].setProperty('alt',name+'.show('+i+');');
            this.tmb[i].addEvent('click',function(e){
                e.stop();
                eval(this.getProperty('alt'));                
            });
             if(i==0){
                this.tmb[0].getParent('div').set('tween',{duration: this.speed});
             }
       }
       if(this.images.length>0 ){
            this.scrollTo(this.current);
       }else if(this.tmb.length>0){
            this.scrollTo(Math.min(2,this.tmb.length));
       }
    },        
    show: function(index){        
        if(this.mode == 'big' || (this.mode == 'mixed' && this.state == 'big') ){    
            if (index>=this.images.length){
                index = 0;
            }else if(index<0){
                index = this.images.length-1;
            }
            if(index == this.current) return false;
            last = this.current;
            this.current = index;            
            for(i=0;i<this.images.length;i++){
                if(i!=last && i!=this.current)this.images[i].setStyle('z-index',1);
            } 
            this.images[this.current].setStyle('z-index',3);
            this.images[last].setStyle('z-index',2);           
            this.images[this.current].tween('opacity','1.0');
            this.images[last].tween('opacity','0.0');   
            $$(this.gs+' p')[0].innerHTML = this.images[this.current].getProperty('alt');
            this.scrollTo(this.current);                                             
        }else if (this.mode == 'thumb' || (this.mode == 'mixed' && this.state == 'thumb') ){        
            this.open(index);            
        }        
        return false;
    },
    next: function(){
        return this.show(this.current+1);
    },
    last: function(){
        return this.show(this.current-1);
    },
    close: function(){
       if(this.mode == 'mixed' ){          
            $$('.main_content *').setStyle('visibility','visible');            
            $$(this.gs)[0].tween('opacity','0');
            $$(this.ts)[0].setStyles({'visibility':'visible'});
            $$(this.ts)[0].tween('opacity','1');    
            $$(this.gs)[0].setStyles({'display':'none'});
            this.state = 'thumb';
       }else{
            history.back();
       } 
    },
    open: function(index){
        if(this.mode == 'mixed' ){   
            this.state = 'big';
            $$('.main_content *').setStyle('visibility','hidden');
            $$('.main_content .main_content_dragon').setStyle('visibility','visible');
            $$('.main_content .biggallery *').setStyle('visibility','visible');
            $$(this.gs)[0].setStyles({'display':'block'});
            $$(this.ts)[0].setStyles({'visibility':'hidden','opacity':'0'});            
            $$(this.gs)[0].tween('opacity','1');        
                               
            this.show(index);
       }else{
            alert(index);
       }
    },
    scrollTo: function(index){                                
       last = (this.oss-this.right-1);
       if( index < this.left || this.oss < (this.left+this.right+1) ){
            pos = 0;
       }else if( index > last ){
            pos = this.oneSize*(-last+this.left);
       }else{
            pos = this.oneSize*(-index+this.left);
       }
          
       if(this.mode != 'thumb' ){            
            this.gal[index].getParent('div').tween('left',pos);
            this.gal[this.scrollPos].removeClass('img-selected');
            this.gal[index].addClass('img-selected');
       } 
       if(this.mode != 'big' ){            
            this.tmb[index].getParent('div').tween('left',pos);
            this.tmb[this.scrollPos].removeClass('img-selected');
            this.tmb[index].addClass('img-selected');
       }
       this.scrollPos = index;
    },
    scrollRight: function(){
        z = this.scrollPos+1;            
        if (z>=this.tmb.length){
            z = 0;
        }       
        this.scrollTo(z);
    },
    scrollLeft: function(){
        z = this.scrollPos-1;
        if(z<0){
            z = this.tmb.length-1;
        }     
        this.scrollTo(z);
    }
});

function ScrollToElement(theElement){
  var selectedPosX = 0;
  var selectedPosY = 0;              
  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }                        		      
  window.scrollTo(selectedPosX,selectedPosY);
}

function overtext(id,value,noClear){
    if(!$(id))return;
    if(!noClear){
        $(id).addEvent("focus",function(){
             if ($(this).value==value) $(this).value="";
        });
    }
    $(id).addEvent("blur",function(){
         if ($(this).value=="") $(this).value=value;
    });
}

function setFormViaAjax(id,noSend,success){
    if(!($(id))) return;  
    $(id).addEvent('submit',function(e){
        e.stop();          
        mess = "";
        ok = true;
        url_ = $(id).getProperty("action");
        method_ = $(id).getProperty("method");
        inputs = $$("#"+id+" input, #"+id+" textarea, #"+id+" select");
        data_ = "";
        for(i=0;i<inputs.length;i++){
            alt = inputs[i].getProperty("alt");
            name = inputs[i].getProperty('name');
            lab = $$('label[for="'+name+'"]'); 
            if(alt == "bind"){
                if(inputs[i].value==""){
                    lab.addClass('error');
                    ok = false;
                    
                    //mess+=(inputs[i].getProperty("title")+"\r\n");
                }else{
                    lab.removeClass('error');
                }
            }else if( alt ){
                if(inputs[i].value=="" || inputs[i].value==alt ){
                    ok = false;
                    lab.addClass('error');
                   // mess+=(inputs[i].getProperty("title")+"\r\n");
                }else{
                    lab.removeClass('error');
                }
            }else{
                lab.removeClass('error');
            }
            if(data_.length>0)data_+="&";
            type = inputs[i].getProperty("type");
            if(type!="checkbox" || inputs[i].getProperty("checked") ){
                data_+=(inputs[i].getProperty("name")+"="+inputs[i].value);           
            }
        }
        if(!ok){
            if(mess)alert(mess);          
        }else{
            if(!noSend){
                var jsonRequest = new Request.JSON({url:url_, 
    	        	onSuccess: function(resp){
    		           if ( !resp.ok ){
    		        	   alert(resp.mess); 
                           $(id).removeClass("sending");                      
    		           }
    		           else{
    		        	   if ( success != undefined ) {
    		        		   window.location.href= success;
    		        	   }
    		        	   else {
	                           $(id).addClass("sent");
	                           alert(resp.mess);	                         
	                           /*ha popupok voltak eltüntetem*/	                          
    		        	   }
                           /*--end--*/
    		           }
    	        	}
            	}).send({method:method_,data:data_});
             }else{
                $(id).submit();
             }                  
        }
    });
}

function setFormDef(){
    inputs = $$("form.sent input,form.sent textarea,form.sent select");
    for(i=0;i<inputs.length;i++){       
        type = inputs[i].getProperty("type");
        if(type == "hidden" || type=="button" || type=="submit" )continue;
        alt = inputs[i].getProperty("alt");
        if(!alt)alt=inputs[i].getProperty("title");
        if(type=="checkbox"){
            if(alt=="1"){
                inputs[i].checked = true;
            }else{
                inputs[i].checked = false;
            }
        }else{                
            if(alt && alt!="bind"){
                inputs[i].value = alt;
            }else{
                inputs[i].value = "";
            }
        }
    }
    z = $$('form.sent');
    for(i=0;i<z.length;i++){
        z[i].removeClass("sent");
    }
    
}

var Slider = new Class({
    initialize: function(name,slider_ul,buttons,fw,bw,time){
        this.time = time;           
        this.current = 0;
        this.name = name;
        this.sds = $$("ul."+slider_ul+" li.sli");       
        this.cnt = this.sds.length;  
        if(this.cnt==0)return;    
        this.buttons = $$("ul."+buttons+" li.slider_page");
        for(i=0;i<this.cnt;i++){  
            this.sds[i].setProperty("style","position:absolute;top:0px;left:opx");
            this.sds[i].set('tween',{duration:2000});
            this.sds[i].setProperty("style","z-index:"+(3+i));
            if(i>0)this.sds[i].tween('opacity',0.0);            
        }
        //this.sds[0].setProperty("style","z-index:6");
        this.animate = true;
        
        for(i=0;i<this.buttons.length;i++){
            this.buttons[i].setProperty('alt',"$clear("+this.name+".an);"+this.name+".set(this.value);");
            this.buttons[i].addEvent('click',function(){
                eval(this.getProperty('alt'));
            });
        }
        
        z = $$("ul."+buttons+" li."+fw);
        for(i=0;i<z.length;i++){
            z[i].setProperty('alt',"$clear("+this.name+".an);"+this.name+".next();");
            z[i].addEvent('click',function(){
                eval(this.getProperty('alt'));
            });
        }
        z = $$("ul."+buttons+" li."+bw);
        for(i=0;i<z.length;i++){
            z[i].setProperty('alt',"$clear("+this.name+".an);"+this.name+".last();");
            z[i].addEvent('click',function(){
                eval(this.getProperty('alt'));
            });
        }
        var anf_ = function(){
            this.next();  
        };
        this.sds[0].setStyle('display','block');
        var anf = anf_.pass([],this);
        this.buttons[0].addClass("active");
        this.an = anf.periodical(this.time);
        
    },
    set: function(cnt){     
        this.sds[this.current].tween('opacity',0.0);
        this.current = (cnt >=0 && cnt<this.cnt )?cnt:0;
        this.sds[this.current].setStyle('display','block');
        this.sds[this.current].tween('opacity',1.0);        
        this.buttons.removeClass("active");
        this.buttons[this.current].addClass("active");        
    },
    next: function(){
        curr = this.current+1;
        if(curr>=this.cnt)curr = 0;
        this.set(curr);
    },
    last: function(){   
        curr = this.current-1;
        if(curr<0)curr = this.cnt-1;
        this.set(curr);
    }
});

var Popup = new Class({
   initialize: function(id,style,parent,controller_selector,ihtml){
       if(!parent || !$(parent)){
           par = document.body;
       }else{
           par = $(parent);
       }       
       this.id = "pu_"+id;          
       pop_div = new Element("div",{
          "id" : this.id,
          "class" : style,
          "styles" : {"display":"none",
                      "position":"absolute",
                      "left":"50%",
                      "margin-left":"-182px"}                   
       });              
       pop_div.appendChild(new Element("div",{"id":this.id+"_content"}));
       pop_div.appendChild(new Element("div",{
           "id":this.id+"_close", 
           "class": style+"_close",
           "styles":{"cursor":"pointer"}
       }));   
           
       par.appendChild(pop_div);
       $(this.id+"_close").setProperty("alt",id+".close();");
       $(this.id+"_close").addEvent('click',function(){
                eval(this.getProperty('alt'));
            });
       
       if(!document.all){
            $(this.id).set("opacity","0");
            $(this.id).style.display="block";
       }else{
            $(this.id).style.display="none";
       }
       
       $(this.id+"_content").load(ihtml);
       
       z = $$(controller_selector);
       for(i=0;i<z.length;i++){
            z[i].setProperty("alt",id+".toggle();");
            z[i].addEvent('click',function(){
                eval(this.getProperty('alt'));
            });
       }      
   },   
   show: function(){     
       var top__ = 0;
       try{
            top__ = document.body.getScroll().y+100;
       }catch(e){
            iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
            top__ = iebody.scrollTop+100;
       }
       $(this.id).style.top = top__+"px";      
       if(!document.all){
            $(this.id).fade(1.0);
       }else{
            $(this.id).style.display="block";
       }       
   },
   close: function(){        
       if(!document.all){
            $(this.id).fade(0.0);
       }else{
            $(this.id).style.display="none";
       }
   },
   toggle: function(){
       if(!document.all){
           op = $(this.id).get("opacity");
           if(op==0){
                this.show();
           }else if(op==1){
                this.close();
           }       
       }else{
           op = $(this.id).style.display;
           if(op=="none"){
                this.show();
           }else{
                this.close();
           }
       }
   }
});
function menu_fix(selector){
     $$(selector).addEvent("click",function(e){
        z = this.getElement('a');
        if(!z)return;
        href = z.getProperty("href");        
        if(z.getProperty("target") == "_blank"){
            window.open(href);
        }else{            
            window.location = href;
        }      
        e.stop();
   });
}

function closelayout(){
    z = $$('.layout_desc');
    for(i=0;i<z.length;i++){
        z.setStyle('display','none');
    }
}

function bookingButton(){
    z = $$('input[value="Book"].button');
    for(i=0;i<z.length;i++){
        z[i].addEvent('click',function(){
            location.href='/booking'; 
        });
    }
}

function share_btn() {
    z = $$('input[value="Share"].button');
    for(i=0;i<z.length;i++){
        z[i].addEvent('click',function(){
           window.open('http://www.facebook.com/share.php?u='+location.href,'Share','width=800,height=450');		
        });
    }   	
}

var toremove;

function removeFix(){    
    toremove.removeClass('selected');    
}

function left_top_navigation(){
    z = $$('ul.about_menu1 li');
    speed = 550;
    for(i=0;i<z.length;i++){
        z[i].addEvent('click',function(e){
            l = this.getElements('ul');
            ll = this.getElements('li')
            if(ll.length>0 && this.getProperty('class')!='selected' ){
                k = this.getElements('a');
                if(k.length > 0 ){
                    location.href = k[0].getProperty('href');
                }  
                
                k = $$('ul.about_menu1 li.selected');
                for(j=0;j<k.length;j++){              
                    toremove = k[j];                              
                    setTimeout('removeFix()',speed);
                    u = k[j].getElement('ul');                    
                    u.set('morph',{duration:speed});
                    u.morph({'height':'0px','padding-top':'0px'});                                                                      
                }      
                l[0].set('morph',{duration:speed});     
                l[0].setStyle('height','0px');     
                l[0].morph({'height':'145px','padding-top':'10px'});               
                this.addClass('selected');                                                
            }else{
                k = this.getElements('a');
                if(k.length > 0 ){
                    location.href = k[0].getProperty('href');
                }                
            }
            e.stop();
        })
    }
}

function equal(list,lasts,reference){
    lens = [];
    for(i=0;i<list.length;i++){
        lens[i]=0;
        for(j=0;j<list[i].length;j++){
            lens[i]+=list[i][j].getSize().y;
        }
    }
    max = reference.getSize().y;   
    
    for(i=0;i<lens.length;i++){
        if(lens[i]<max){
            volt = parseInt(lasts[i].getStyle('height'));;                                     
            lasts[i].setStyle('height',volt+(max-lens[i]));            
        }
    }
}

var Music = new Class({
    initialize:function(){
        //console.log(getCookie('isMusic'));
        var list = [js_folder+'sound/01_Endless_Stories.mp3',
                    js_folder+'sound/02_Gnossienne_NT-1_(Buddha-Bar_Remix).mp3',
                    js_folder+'sound/03_El_Corazon.mp3',
                    js_folder+'sound/04_Sun_Heat_Silence.mp3',
                    js_folder+'sound/06_Is_That_Better.mp3',
                    js_folder+'sound/07_Sol_Y_Luna.mp3',
                    js_folder+'sound/09_Belifast.mp3',
                    js_folder+'sound/10_Golden_Ring.mp3',
                    js_folder+'sound/09_Belifast.mp3'];
        Playlist.loadSounds(list,{        
            'onComplete' : function(){ Playlist.playRandom(); }
        });    
        $('music').addEvent('click',function(e){
            z = $('switch_music').getProperty('class'); 
            imag = $$('#music img')[0];                   
            if(z == 'active'){
                Playlist.stopSounds();
                $('switch_music').innerHTML='Off';
                $('switch_music').removeClass('active');
                imag.setProperty('src',img_folder+'music_off.png');   
                setCookie('isMusic','1',1);          
            }else{            
                Playlist.playRandom();
                $('switch_music').innerHTML='On';
                $('switch_music').addClass('active');
                imag.setProperty('src',img_folder+'music_on.png');
                setCookie('isMusic','0',1); 
            }
            e.stop();
        });
        if(getCookie('isMusic')!='1'){                  
            setTimeout('music.start()',500);
        }
    },
    start:function(){          
       imag = $$('#music img')[0];      
       try{
            Playlist.playRandom();
       }catch(e){            
            setTimeout('music.start()',500);
            return;
       }
       $('switch_music').innerHTML='On';
       $('switch_music').addClass('active');
       imag.setProperty('src',img_folder+'music_on.png'); 
       setCookie('isMusic','0',1);
    },
    stop:function(){
        imag = $$('#music img')[0];
        Playlist.stopSounds();
        $('switch_music').innerHTML='Off';
        $('switch_music').removeClass('active');
        imag.setProperty('src',img_folder+'music_off.png');
        setCookie('isMusic','1',1); 
    }
});

var music = false;

function limitDate(mon,day){
	if(!$(mon)) return;
	ld = 'feb 2012';
	$(mon).addEvent('change',function(e){        
		z = $$('#'+day+' option');
		dsp = (this.value==ld);	
		if(dsp){
		  for(i=0;i<z.length;i++){
			if(z[i].value!=0 && z[i].value<15){		
			  if(dsp) z[i].destroy();
			}
		  }
		}else{
		  for(i=0;i<z.length;i++){		
		      z[i].destroy();			      
		  }
		  for(i=0;i<32;i++){	      
		      $(day).options[$(day).options.length] = new Option((i==0)?'dd':i, i);
		  }
		}	
	});
	if($(mon).value==ld){
		z = $$('#'+day+' option');
		for(i=0;i<z.length;i++){
		if(z[i].value!=0 && z[i].value<15){		
		   z[i].destroy();
		}
	  }
	}
}

window.addEvent('domready',function(){  
    menu_fix('ul.menu_fix li');  
    share_btn();         
    gallery = new Gallery('gallery','div.biggallery','div.gallery_thumbs',0);
    if(scroll_to==true){
        ScrollToElement($$('a[name="jump"]')[0]);
    }
    bookingButton();        
    if($('main_contact')){
        setFormViaAjax('main_contact',true);
    }  
    list = [$$('.left_col'),$$('.right_col')];
    lasts = [$$('.lmm_bottom')[0],$$('.main_content')[0]];
    equal(list,lasts,$('content'));
    slider = new Slider('slider','slider','slider_pager','slider_forward','slider_back',6000);         
    
    //dec 15 a min dátum
    limitDate('booking_a_mon','booking_a_day');
    limitDate('booking_d_mon','booking_d_day');
    limitDate('booking_a_mon_1','booking_a_day_1');
    limitDate('booking_d_mon_1','booking_d_day_1');  
        
});
window.addEvent('load',function(){        
    list = [$$('.left_col'),$$('.right_col')];
    lasts = [$$('.lmm_bottom')[0],$$('.main_content')[0]];
    equal(list,lasts,$('content'));
    music = new Music();        
});

jQuery(function () {
	var $ = jQuery;
	var langDropdownCont = $('#languageDropdownContainer');
	langDropdownCont.width(langDropdownCont.width());
	$('#languages').width(langDropdownCont.width());
	var langDropdown = $('#languageDropdown');
	var origHeight = langDropdown.height();
	var newRule = '#languageDropdownContainer { height: '+ origHeight +'px; } ';
	$('#ad').attr('rel',$('#ad a').last().attr('href')).css('cursor','pointer');
	$('#ad').click(function(){
		location.href = $(this).attr('rel');
	});
	setTimeout(function () {
		langDropdown.css('position', 'absolute');
		langDropdown.css('bottom', '0px');
		langDropdownCont.height(0);
		var current = null;
		$('#languages').hover(function () {
			current = 'down';
			langDropdownCont.stop();
			langDropdownCont.animate({
				height: origHeight
			});
			/*
			langDropdownCont.slideDown(200, function () {
				langDropdownCont.css('height', '');
			});
			*/
		}, function () {
			current = 'up';
			langDropdownCont.stop();
			langDropdownCont.animate({
				height: 0
			});
			//langDropdownCont.slideUp(200);
		})
	}, 0);
});

