$(document).ready(function(){



// scroll 
	var scrollheight = 104;
	var scrollpaneHeight;
	var scrollpaneOffset;
	var scrollpaneOffsetTop;
	var scrollpaneBottom;
	var suitentextHeight;
	var suitentextOffset;
	var suitentextOffsetTop;
	var suitentextBottom;


	$('.scrollup').click(function () {
		scrollVars();
		if(scrollpaneOffsetTop < suitentextOffsetTop ){
			$(".suitentext").animate({"top": "+=" + scrollheight + "px"}, 800);
		}
	});


	$('.scrolldown').click(function () {
		scrollVars();
		if(scrollpaneBottom < suitentextBottom){
			$(".suitentext").animate({"top": "-=" + scrollheight + "px"}, 800);
		}
	});

	function scrollVars(){
		scrollpaneHeight = $(".scroll-pane").height();
		scrollpaneOffset = $('.scroll-pane').offset();
		suitentextOffsetTop = scrollpaneOffset.top;
		scrollpaneBottom = scrollpaneOffset.top + scrollpaneHeight;

		suitentextHeight = $(".suitentext").height();
		suitentextOffset = $('.suitentext').offset();
		scrollpaneOffsetTop = suitentextOffset.top;
		suitentextBottom = suitentextOffset.top + suitentextHeight;
	}




// BG Preload
	$('#background_preload_img').load(function() {
		//$('#loader_bg').animate({ opacity: 0},{duration: 800, complete: fadeInElement2 });
		$('#loader_bg').animate({ opacity: 0},{duration: 800});
		$('#loader_bg').queue(function () { $(this).css({ "display": "none" }); });
	});
	$('#background_preload_img').error(function() { 
		//alert('Handler for .error() called.') 
	});





// Diverse Elemente einblenden --------------------------------------------------------------------
/*		
	$('#navigation,#content,#imagesnav,#imagesbox,#logo,#topnav,#address').animate({ opacity: 0},{duration: 0});
*/	
	//	$('#background_preload_img').bind('load', function (e) {fadeInElement1(); });
/*
	function fadeInElement1(){ $('#page_background').animate({ opacity: 1},{duration: 800, complete: fadeInElement2}); }
	function fadeInElement2(){ 
		$('#navigation').animate({ opacity: 1},{duration: 400});
		$('#content').animate({ opacity: 1},{duration: 800});
		$('#imagesbox, #imagesnav').animate({ opacity: 1},{duration: 1200, complete: fadeInElement3});
	}
	function fadeInElement3(){ 
		$('#logo,#address').animate({ opacity: 1},{duration: 600, complete: fadeInElement4});
	}
	function fadeInElement4(){ 
		$('#topnav').animate({ opacity: 1},{duration: 3000});
	}
*/



// init --------------------------------------------------------------------------------
	$('#bigimage_bg').animate({ opacity: 0},{duration: 0});



// Bildschirmgröße --------------------------------------------------------------------------------
	createTopBottomHeight(); // onLoad
	$(window).smartresize(function(){ createTopBottomHeight(); }); // onResize
	function createTopBottomHeight(){
		var x = document.documentElement.clientWidth;
		var y = document.documentElement.clientHeight; 
		var minHeight = 37;
		var middleHeight = 515;
		var myTopHeight = Math.floor((y - middleHeight) /2)-2;
		var myBottomHeight = Math.ceil((y - middleHeight) /2)-2;
		
		if(myTopHeight<=minHeight) { myTopHeight = minHeight; }
		if(myBottomHeight<=minHeight) { myBottomHeight = minHeight; }
		$('#top').css({ "height": myTopHeight+"px" });
		$('#bottom').css({ "height": myBottomHeight+"px" });
		
		$('#topnav').css({ "top": (myBottomHeight-18)+"px" });
		
	}
	
// Bilderset --------------------------------------------------------------------------------------
	var imageclickcounter = 1; 
	var thumbs = $('.thumb').length;
	var sliderlength = thumbs * 214;
	$('#imagesslider').css({
		"width": sliderlength +'px'
	});	

	checkSliderNav();

	$('#imagesnav_right').click(function(){
		if(thumbs > 3 && imageclickcounter <= (thumbs-3)){
			$('#imagesslider').animate({"left": "-=214px"}, 600);
			imageclickcounter += 2;
		}
		checkSliderNav();
	});

	$('#imagesnav_left').click(function(){
		if(thumbs > 3 && imageclickcounter >= 2){
			$('#imagesslider').animate({"left": "+=214px"}, 600);
			imageclickcounter -= 2;
		}
		checkSliderNav();
	});


	function checkSliderNav(){
		if(thumbs <= 3){
			$('#imagesnav_left > img').attr("src","design/bt_links_50.png");
			$('#imagesnav_right > img').attr("src","design/bt_rechts_50.png");
			$('#imagesnav_left').css({ "cursor": 'default' });
			$('#imagesnav_right').css({ "cursor": 'default' });
		}else{
			if(imageclickcounter <= (thumbs-3)){
				$('#imagesnav_right > img').attr("src","design/bt_rechts.png");
				$('#imagesnav_right').css({ "cursor": 'pointer' });
			}else{
				$('#imagesnav_right > img').attr("src","design/bt_rechts_50.png");
				$('#imagesnav_right').css({ "cursor": 'auto' });
			}
			if(imageclickcounter >= 2){
				$('#imagesnav_left > img').attr("src","design/bt_links.png");
				$('#imagesnav_left').css({ "cursor": 'pointer' });
			}else{
				$('#imagesnav_left > img').attr("src","design/bt_links_50.png");
				$('#imagesnav_left').css({ "cursor": 'auto' });
			}
		}
	}


// Slideshow-------------------------------------------------------------------------------------
//$('#address').click(function(){
//	doClick();
//	return false;
//});

var imagecounter = 0;
function doClick(){
		//	
	if(imagecounter >= ($(".contentimage").length)){ 
		imagecounter = 0; 
		bildnavigation_null_position = ($(".contentimage").length)*107;
		$('#imagesslider').animate({"left": "0px"}, 600);
		imageclickcounter -= $(".contentimage").length;
		checkSliderNav();
	}else{
		// Counter für Bildscroller aktualisieren
		//imageclickcounter += 1;
	}
	
		// Bildnavigation um 1 Bild scrollen
	if(imagecounter>0) { $('#imagesslider').animate({"left": "-"+imagecounter*107+"px"}, 600); }
	
	
		// Nächstes BIld einblenden
	$(".contentimage").eq(imagecounter).trigger('click');
	
	imageclickcounter += 1;
	imagecounter++;
	checkSliderNav();
	
};







// ContentBilder anzeigen -------------------------------------------------------------------------
		
		var aktuellesBild = 0;
		var myImageCounter = 0; 
		$('.contentimage').click(function(){
			
			imagecounter = aktuellesBild = $('.contentimage').index(this);
			
			var myImage = $(this).attr('href');
			if($('#bigimage').length == 1){ 
				$('#bigimage img.myimage').eq(myImageCounter).animate({ opacity: 0},{duration: 1000});
				$('#bigimage').append('<img src="' + myImage + '" width="980" height="515" class="myimage" alt="" title="">')
				myImageCounter++;
				$('#bigimage img.myimage').eq(myImageCounter).animate({ opacity: 0},{duration: 0});
				$('#bigimage img.myimage').load(function() {
					$('#bigimage img.myimage').eq(myImageCounter).animate({ opacity: 1},{duration: 1000});
				});	
			}else{
				$('#bigimage_bg').animate({ opacity: 0.90},{duration: 500});
				$('#imagesbox').animate({ top: 450 },{duration: 600});
				$('#imagesnav').animate({ top: 473 },{duration: 600});
				$('#content').after('<div id="bigimage"><img src="design/close.png" width="36" height="36" class="close" alt="" title=""><img src="' + myImage + '" width="980" height="515" class="myimage" alt="" title=""></div>');
				
				$('#bigimage').animate({ opacity: 0},{duration: 0});
				$('.myimage').animate({ opacity: 0},{duration: 0});				
				$('#bigimage').animate({ opacity: 1},{duration: 1500});	
				$('#bigimage img.myimage').load(function() {
					$('.myimage').animate({ opacity: 1},{duration: 1500});
				});
			}
			return false;
		});
		$('.close').live('click', function() {
			$('#bigimage_bg').animate({ opacity: 0},{duration: 500});
			$('#imagesbox').animate({ top: '385px'},{duration: 600});
			$('#imagesnav').animate({ top: '408px'},{duration: 600});
			$('#bigimage').animate({ opacity: 0}, 500, function() { 
				$('#bigimage').remove(); 
				myImageCounter = 0;
			});
		});
		

	
		
// Navigation -------------------------------------------------------------------------------------
	$('.navitem img').mouseover(function() {
		$(this).stop(true, false).animate({ 
			left: "20px",
			queue: 'false'
		}, 400, 'easeOutQuad' );
	});

	$('.navitem img').mouseout(function() {
		$(this).stop(true, false).animate({
			left: "0px",
			queue: 'false'
		}, 600, 'easeInQuad' );
	});
	


});



// ---------------------------------------------------------------------------
// debouncing function from John Hann
// http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
(function($,sr){

  var debounce = function (func, threshold, execAsap) {
      var timeout;
 
      return function debounced () {
          var obj = this, args = arguments;
          function delayed () {
              if (!execAsap)
                  func.apply(obj, args);
              timeout = null; 
          };
 
          if (timeout)
              clearTimeout(timeout);
          else if (execAsap)
              func.apply(obj, args);
 
          timeout = setTimeout(delayed, threshold || 100); 
      };
  }
	// smartresize 
	jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
 
})(jQuery,'smartresize');








// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
    return -c * ((t=t/d-1)*t*t*t - 1) + b;
};




