var lastaction;
var refreshing = 8*1000; // 60 * 1000 = 1 Minute (1000 = millisekunden, 60 = sekunden)
var refreshId=0;

$(document).ready(function() {
	
	lastaction = new Date().getTime(); 
	
	$('a.manualgallery').fadeTo("1",'0.5').hover(function() { $(this).fadeTo('fast','1'); },function() { $(this).fadeTo('fast','0.5'); });
	$('tr.showmore').hover(function() { $(this).addClass('hovered'); },function() { $(this).removeClass("hovered"); }).click(function() {
		if (!$(this).hasClass("active")) {
			$('tr.showmore').removeClass("active");
			$('#renttable tr.data').hide();
			var rel = '#renttable tr.'+$(this).attr("rel");
			$(rel).show();
			$(this).addClass("active");
		}
		return false;
	});;
	
	
	//$('a.notActive.subitem').fadeTo("1",'0.5').hover(function() { $(this).fadeTo('fast','1'); },function() { $(this).fadeTo('fast','0.5'); });
	//$('li.notActive a.item').fadeTo("1",'0.7').hover(function() { $(this).fadeTo('fast','1'); },function() { $(this).fadeTo('fast','0.7'); });
	
    $('.datepicker').datepicker({dateFormat:'yy-mm-dd'});
        
    //$('.lightbox').lightBox();
    
    $('select.refresh').change(function() {
    	$('#bookingform #gotostep').val($('#bookingform #currentstep').val());
    	$('form input[type="submit"]').click();
    });
    
    $('input.submit').click(function() {
        ret = false;
        $('input.required').each(function() {
           
           if($(this).val()=='') {
               if (lang=='de') {
                   alert("Bitte alle mit * markierten Felder ausfüllen!");
               } else {
                   alert("Please fill in all with * marked fields!");
               }
               ret = true;
               return false;
           }
           
        });
        if(ret) { return false; }
     });
     
     var imgnav = new image_navigation();
     imgnav.init();
     
     
     if ($('#MSG_').length > 0) {
    	 setTimeout("hideMessage()",1800);
     }
});

function hideMessage() {
	$('#MSG_').fadeOut("normal");
}
var image_navigation = function() {
   var me = this;
   this.init = function() {
   
   		$('.galleryimages a:first').addClass("active");
   		$('.galleryimages a').click(function() {
   			if (!$(this).hasClass("active")) {
   				//$('div.loader').show();
   				
   				$('.galleryimages a').removeClass("active");
   				$(this).addClass('active');
   				
   				var src_new = $(this).attr("ref");
                changePicture(src_new);
   			}
   			return false;
   		});
   		
   		$('.galleryimages a:last').addClass("last");
   		if ($('.galleryimages') && $('.galleryimages a').length>1) {
			setRefreshTimeout();
		}
   }
}
function changePicture(src_new) {
	$('#bg_image1').fadeOut(200,function() {
        var img_new = new Image();
        $(img_new)
        .hide()
        .load(function() {
       		$('#bg_image1').attr("src",src_new+"_bg.jpg");
       		$('#bg_image1').fadeIn(800,function() { 
       			$('#bg_image2').attr("src",src_new+"_bg.jpg");
       		});
        })
        .attr("src",src_new+"_bg.jpg");
    });
    
    	var aai = 100;
   		var gimg_new = new Image();
   		$(gimg_new).hide().load(function() {
   			
   			var elems = $('img.gallimage').sort(function() { return (Math.round(Math.random())-0.5); }); 
   			elems.each(function () {
   				$(this).fadeOut(aai,function() {
		   			$(this).attr("src",src_new+".jpg");
		   			$(this).fadeIn(500,function() {
		   				$('img.gallimage2',$(this).parent()).attr("src",src_new+".jpg");
		   			});
		   		});
		   		aai = aai+100;
	   		});
	   		
	   		setRefreshTimeout();
   		}).attr("src",src_new+".jpg");
}
function nextImage() {
	var actImage = $('.galleryimages a.active');
	$(actImage).removeClass("active");
	if ($(actImage).hasClass("last")) {
		$('.galleryimages a:first').click();
	} else {
		$(actImage).next().click();
	}
}

function setRefreshTimeout(first) {
	if (refreshId!=0) {
		clearTimeout(refreshId);
	}
	if (first=="1") {
		refreshId = setTimeout("nextImage()",0);
	} else {
		refreshId = setTimeout("nextImage()",refreshing);
	}
}

