// initialise plugins
$(document).ready(function() {
	var timeout = 0;
	imgsLoaded = new Array();
	$("#thumbs img").each(function() {
	$(this).bind ("click", function(){
				$("#thumbs img").removeClass("aktiv");
				$(this).addClass("aktiv");
				//console.log("timeout stop!");
				window.clearTimeout(timeout);			
				$("#rotation img").fadeOut(2000);
													   
														
				var thumbnail = $(this).attr("src");	
				var bigpic = thumbnail.substring(0,thumbnail.length-10);
				bigpic = bigpic + ".jpg";
				//preloader zeigen falls bild noch nicht geladen:
				if($.inArray(bigpic,imgsLoaded) == -1) {
					$('#imgpreloader').fadeIn(100);
				}
				var previewIMG = new Image();
				previewIMG.src = bigpic;
				previewIMG.onload = function() {
					//ladearray ergaenzen:
					if($.inArray(bigpic,imgsLoaded) == -1) {
						imgsLoaded.push(bigpic);
					}					
					//preloader ausblenden:
					$('#imgpreloader').fadeOut(100);					
					$('#rotation').append(previewIMG);
					$('#rotation IMG:last').hide();
					$('#rotation IMG:last').fadeIn(2000, function(){
						if($('#rotation IMG').length > 1) {
							$('#rotation IMG:first').remove();
						}
					});
					//window.clearTimeout(timeout);
					timeout = window.setTimeout(function(){
							//console.log("timeout gestartet");
							if($("#thumbs img").last().hasClass("aktiv")) {
								$("#thumbs img:first").trigger("click");
							} else {
								$("#thumbs .aktiv").next().trigger("click");
								$("#thumbs .aktiv").next().trigger("next",1);
							}
					},8200);
				//img onload ende:	
				}
						

				return false;
	
		
	});	
	
});
//erstes thumbnail ausloesen: 
$("#thumbs img:first").trigger("click");
/*
var interval = setInterval(function(){
		if($("#thumbs .aktiv").next()) {
			var text = $("#thumbs .aktiv").next();
			//alert(text);
			text.trigger("click");
		} else {
			$("#thumbs img:first").trigger("click");			
		}
},5000);
*/
$("#caroufredselImg").carouFredSel({
		width : 960,
	    auto : false,
	    prev : {   
	        button  : ".previous",
	        key     : "left"
	    },
	    next : {
	        button  : ".next",
	        key     : "right"
	    }
});
// innerfade nur wenn keine thmbnails:
if($('#thumbs').length == 0) {
	$('#rotation').innerfade({
		speed:2000, 
		timeout: 6200, 
		type: 'sequence', 
		containerheight: '484px' 
	}); 
}

// end of Ready
});
