var lastaction;
var refreshing = 7*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");
		setRefreshTimeout(1);
	} 
	
	$('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;
	});

     var imgnav = new image_navigation();
     imgnav.init();
     
});
var image_navigation = function() {
   var me = this;
   this.init = function() {
   
   		if ($('.galleryimages a').length > 0) {
   			//$('#').append("<div class='loader'><img src='/img/loader.gif' /></div>");
   		}
   		
   		$('.galleryimages a:last').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;
   		});
   }
}
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);
	}
}

