var gal, version = 'n3';



$(window).addEvent('load',function(){


	if($$('.tx-chgallery-pi1').length == 1)
	{
			gal = new LindaGally();
	}

	new DatePicker('.f_ankunft_ipt', {
		pickerClass: 'datepicker_dashboard',
		toggleElements: '.date_toggler_ankunft',
		inputOutputFormat: 'Ymd',
		format: 'd.m.Y',
		days: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
		months: ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember']
	});

	new DatePicker('.f_abreise_ipt', {
		pickerClass: 'datepicker_dashboard',
		toggleElements: '.date_toggler_abreise',
		format: 'd.m.Y',
		inputOutputFormat: 'Ymd',
		days: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
		months: ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember']
	});
	
	$$('#socialbuttons a').addEvents({
									 
									 mouseenter : function(){
										this.tween('opacity',0.65); 
									 },
									 
									 mouseleave : function(){
										this.tween('opacity',1); 
									 }
									 
									 });
	
	$$('#searchbox input').addEvents({
									 
									 focus : function(){
										if(!this.retrieve('initText')){
											this.store('initText',this.get('value'));	
										}
										
										this.set('value','');
									 },
									 
									 blur : function(){
										 if(this.get('value') == '')
											{
												this.set('value',this.retrieve('initText'));	
											}
									 }
									 
									 });
	
	$('goBooking').addEvent('click',function($e){

											 $e.stop();
											// if(this.retrieve('fired'))return;											 
											 $('ccol').set({
														   	tween : {
																duration : 444,
																onComplete : function(){
																				$('ccol').empty();
																				new Element('iframe',{
																								name : 'booking-iframe',
																								border : '0',
																								marginheight : '0',
																								marginwidth : '0',
																								frameborder : '0',
																								width : 475,
																								height : 800
																							 }).inject('ccol');
																				
																				$('booking-form').submit();
																				this.subject.setStyle('opacity',1);
																				$('goBooking').store('fired',true);
																}
															}
														   }).tween('opacity',0);
											 });


/*
									   $('sbookingbox').addEvent('click',function(){
																				 $('ccol').empty();
																				 new Element('iframe',{
																							 	src : 'https://wbe.42-gmbh.de/Havenhostel/',
																								border : '0',
																								marginheight : '0',
																								marginwidth : '0',
																								frameborder : '0',
																								width : 475,
																								height : 800
																							 }).inject('ccol');
																				 
																			 });
	*/									   
									   
									   $$('#navi a img, #subnavi .connect').set({ tween : { duration : 222 } });
									   
									   $$('#navi li a')
									   .removeProperty('onmouseover')
									   .removeProperty('onmouseout')
									   .addEvents({
											mouseenter : function(){
												//this.getFirst().set('src',eval(this.getFirst().get('name')+'_h').get('src'));
												this.getFirst().tween('opacity',0.4);
											},
											mouseleave : function(){
												//this.getFirst().set('src',eval(this.getFirst().get('name')+'_n').get('src'));
												this.getFirst().tween('opacity',1);
											},
											click : function(){
												//this.getFirst().set('src',eval(this.getFirst().get('name')+'_n').get('src'));
												this.getFirst().tween('opacity',0);
											}
										}).each(function(item){
												item.setStyle('backgroundImage','url('+$(eval(item.getFirst().get('name')+'_h')).get('src') +')');
										});
									   
									   
									   $$('#subnavi li').addEvents({
											mouseenter : function(){
												if(this.getParents()[2].get('tag') == 'div')
												{
													if(!this.hasClass('active')){
//														this.getLast().setStyles({display : 'block', opacity : 0}).tween('opacity',1);
//														$$('#subnavi li.active .connect')[0].tween('opacity',0);
														this.getLast().setStyle('display', 'block');
														$$('#subnavi li.active .connect').setStyle('display', 'none');														
													}
												}
											},
											
											mouseleave : function()
											{
												if(this.getParents()[2].get('tag') == 'div')
												{
													if(!this.hasClass('active')){
//														this.getLast().setStyles({display : 'block', opacity : 0}).tween('opacity',0);
//														$$('#subnavi li.active .connect')[0].tween('opacity',1);
														this.getLast().setStyle('display', 'none');
														$$('#subnavi li.active .connect').setStyle('display', 'block');
													}
												}
											}																   
										});
									   
								   });





var LindaGally = new Class({
   
   images :[],
   index : null,
   
   initialize : function()
   {

	   $$('a[rel=lightbox]').each(function(item,i){

               this.images.push(item.get('href'));
			   item.store('index',i).set({
                'href' : '#',
                events : {
                    click : function($e){
                        $e.stop();
                        gal.show(this)
                    }
                },
				styles : {
					background : 'url(/'+item.getFirst().get('src')+')'
				}
			}).empty();

        },this)
	   
   },
   
   show : function($target)
   {
        this.index = $target.retrieve('index');
        
        
        var dbc = $(document.body).getScrollSize();

        new Element('div',{
            styles : {
                height : dbc.y,
                opacity : 0.5
            },
            'class' : 'shadow',
            events : {
                click : function()
                {
                    gal.close();
                }
            },
            tween : {
                transition : 'quad:in',
                onComplete : function(){
                    
                    
                    $$('.activeImage')[0].set({
                        morph : {
                            onComplete : function(){
                                    gal.loadImg();
                            },
							duration : 0
                        }
                    }).morph({
                            opacity : 1,
                            left : $$('.activeImage')[0].retrieve('dest')[0] - 5,
                            top : $$('.activeImage')[0].retrieve('dest')[1] + $(window).getScroll().y
                        })
                },
                duration : 777
                
            }
        }).inject($(document.body)).tween('width',dbc.x);


        var cloned = $target.clone();

        var tc = $target.getCoordinates('stage');
	

        
        cloned.set({
            'class' : 'activeImage thumb',
            /*
			styles :{
                top : tc.top - 5,
                left : tc.left - 4,
                opacity : 0,
                cursor : 'default'
                
            },
			*/
            events : {
                click : function($e)
                {
                    $e.stop();
                    
                }
            }
        }).store(
                 'dest',
                 [(dbc.x - 110) / 2, ($(window).getCoordinates().height - 110) / 2]
                ).inject(document.body);
   },
   
   loadImg : function(pic){
        
        if(!pic)
        {
            var pic = this.images[this.index].replace('_thumb','');
        }
   
        
        
   
       
        $$('.prev,.next,.close').each(function(item){
           item.dispose(); 
        });
        
        new Asset.image(pic.trim(),
                        {
                            onerror : function(){
                              gal.next();  
                            },
                            onload: function(){
                                 
                                var mc = {
                                        width:this.width,
                                        height:this.height
                                }
								

                                
                                var wc = $(window).getCoordinates();
                                wc.height -= 20;
                                    
                                
                                var f = {
                                    y : mc.height > wc.height?mc.height/wc.height:1,
                                    x : mc.width > wc.width?mc.width/wc.width:1
                                }
                                
								
                               
                                
                                var faktor = Math.max(f.x,f.y);
                                

                                
                                mc.width /= faktor;
                                mc.height /= faktor;

                                if($$('.activeImage img').length == 1)
                                {
                                    $$('.activeImage img')[0].set({
																  src:this.src
																  /*,
																  width : mc.width,
																  height : mc.height
																  */
																  });
                                }
                                else
                                {
                                    this.setStyles({
                                            height : '100%',
                                            width : '100%'
                                        }).inject($$('.activeImage')[0]);
                                }
                                
                                $$('.activeImage')[0].set({
                                        morph : {
                                            onComplete : function(){
                                                gal.makeControls();
                                            },
											duration : 0
                                        }
                                    }).morph().setStyles({
                                        height : mc.height,
                                        width : mc.width,
                                        left : (wc.width - mc.width) / 2,
                                        top  : ((wc.height - mc.height) / 2) + $(window).getScroll().y
                                })
                            }
                        });

   },
   
   close : function()
   {
        $$('.activeImage,.shadow').each(function(item){
            item.dispose();
        });
        this.index = null;
   },
   
   next : function()
   {
        this.index = this.images[this.index + 1]?this.index+1:0;
        this.loadImg(this.images[this.index].replace('_thumb',''));
        
   },
   
   prev : function()
   {
        this.index = this.images[this.index - 1]?this.index-1:this.images.length - 1;
        this.loadImg(this.images[this.index].replace('_thumb',''));
   },
   
   makeControls : function()
   {
        new Element('a',{
                'class' : 'prev',
                text : 'zur\u00FCck',
                href : '#',
                events :{
                    click : function($e){
                        $e.stop();
                        gal.prev();
                    }
                },
                styles : {
                    opacity : 0
                }
        }).inject($$('.activeImage')[0]).tween('opacity',1);
        

        new Element('a',{
                'class' : 'next',
                text : 'weiter',
                href : '#',
                events :{
                    click : function($e){
                        $e.stop();
                        gal.next();
                    }
                },
                styles : {
                    opacity : 0
                }
        }).inject($$('.activeImage')[0]).tween('opacity',1);
        
        new Element('a',{
                'class' : 'close',
                text : 'schliessen',
                href : '#',
                events :{
                    click : function($e){
                        $e.stop();
                        gal.close();
                    }
                },
                styles : {
                    opacity : 0
                }
        }).inject($$('.activeImage')[0]).tween('opacity',1);        
        
   }
});

window.addEvents({
    'keydown' : function($e)
    {
       if(gal && gal.index != null)
       {
            if($e.key == 'esc')gal.close();
            if($e.key == 'left')gal.prev();
            if($e.key == 'right')gal.next();
            
       }
    }
});

Validation = function(){};
