jQuery(document).ready(function() {

	// Grš§e der Slides berechnen
	var ch = document.documentElement.clientHeight; // Hšhe des Viewports
	var cw = document.documentElement.clientWidth; // Breite des Viewports
	var tm = 180; // oberer Rand
	var lm = 510; // linker Rand
	var mw = 976; // maximale Breite
	var mh = 732; // maximale Hšhe
	var h = (ch - tm);
	var w = (cw - lm);
	if (h > mh) {
		h = mh;
	}
	if (w > mw) {
		w = mw;
	}
	$(".slide").css({
		"height": h, 
		"width": w
	});

    $(".preview img").css("opacity", "0" );
    $(".preview img").animate( { opacity:"0.4"}, 900 );
    $(".preview:first-child img").animate( { opacity:"1"}, 900 );

    // scroll buttons
    $("a.scroll.down").click(function() {
     	var tp = (parseInt($("#field_preview").css("top")));
		var n = $("#slideshow img").length;
     	var hp = (n * 125);
		if (-tp < (hp - 375)) {
				$("#field_preview").animate({top:"-=375px"}, "slow");
			}
		});
    $("a.scroll.up").click(function() {
     	var tp = (parseInt($("#field_preview").css("top")));
    	if (tp < 0) {
    		$("#field_preview").animate({top:"+=375px"}, "slow");
    	}
   	});

    // scrolling preview images 
	$("#slideshow img").click(function() {
		var n = $("#slideshow img").length;
		var i = $("#slideshow img").index(this);
		var j = (i + 1) % 3; // Dreiergruppen per Modulo
		if (j == 0 && i < n-1) {
			$("#field_preview").animate({top:"-=375px"}, "slow");
		}
		else if (i == n-1) {
			$("#field_preview").animate({top:"0"}, "fast");
		}
	});
	
});
