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

$(document).ready(function() {
	
	lastaction = new Date().getTime();
	if ($('.galleryimages')) {
		$('.galleryimages a:last').addClass("last");
	} 
	
	$('a.manualgallery').fadeTo("1",'0.5').hover(function() { $(this).fadeTo('fast','1'); },function() { $(this).fadeTo('fast','0.5'); }).click(function() {
		changePicture($(this).attr("href"));
		return false;
	});
	
    $('.datepicker').datepicker({dateFormat:'yy-mm-dd'});
        
    $('.lightbox').lightBox();
    
    $('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; }
     });
     
     $('.thumblink').fadeTo("1",'0.5').hover(function() { $(this).fadeTo('fast','1'); },function() { $(this).fadeTo('fast','0.5'); });
     
     var imgnav = new image_navigation();
     imgnav.init();
     
});
var image_navigation = function() {
   var me = this;
   this.init = function() {   		
   		$('.galleryimages a:first').addClass("active");
   		$('.galleryimages a').click(function() {
   			setRefreshTimeout();
   			if (!$(this).hasClass("active")) {
   				//$('div.loader').show();
   				
   				$('.galleryimages a').removeClass("active");
   				$(this).addClass('active');
   				
   				var src_new = $(this).attr("href");
                changePicture(src_new);
   			}
   			return false;
   		});
   		setRefreshTimeout();
   }
}
function changePicture(src_new) {
	$('#the_big_image').fadeOut(2000,function() {
        var img_new = new Image();
        $(img_new)
        .hide()
        .load(function() {
          	$('#the_big_image').attr("src",src_new);
          	$('#the_big_image').fadeIn(2000,function() {
          		$('#the_big_image2').attr("src",src_new);
          	});
        })
        .attr("src",src_new);
    });
}
function nextImage() {
	var actImage = $('.galleryimages a.active');
	$(actImage).removeClass("active");
	if ($(actImage).hasClass("last")) {
		$('.galleryimages a:first').click();
	} else {
		$(actImage).next().click();
	}
	setRefreshTimeout();
}

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

