(function($){
	$.fn.supersized = function() {
		$.inAnimation = false;
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
		
		$('#fadeDieBilder').hide();
		$('#content').hide();
		
		$(window).bind("load", function(){
		});
		
		$(document).ready(function() {
			$('#fadeDieBilder').resizenow();
		});
		
		$(window).bind("resize", function(){
    		$('#fadeDieBilder').resizenow(); 
		});
	};
	
	//Adjust image size
	$.fn.resizenow = function() {
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);

		//var element = this;
		
		var imagewidth = 0;
		var imageheight = 0;
		
	  	//return element.each(function() {
			
			//var t = $('img', element);
	  		
	  		//Resize each image seperately
	  		$('#fadeDieBilder').find('img').each(function(){
				//var ratio = ($(this).height()/$(this).width()).toFixed(2);	//Define image ratio
				if ( $.browser.msie ) {
					var slideHeight = $(this).height();
					var slideWidth = $(this).width();
				 } else {
					var slideHeight = $(this).attr("height");
					var slideWidth = $(this).attr("width");
				 }
				
				var ratio = (slideHeight/slideWidth).toFixed(2);	//Define image ratio
				thisSlide = $(this);
				
				var browserwidth = $(window).width();
				var browserheight = $(window).height();
				browserwidth = browserwidth - 225 - 40; // abstand links - rand rechts
				browserheight = browserheight - 165 - 20; // abstand oben - rand unten
				
				if ($('#imageThumbs').length > 0) {
					browserheight = browserheight - $('#imageThumbs').height(); // - menüspace höhe
				}
					
				
				var browserratio = browserheight/browserwidth;
				/**Resize image to proper ratio**/
				if (browserratio > ratio){
					//if (ratio <= 1){
						resizeWidth();
					//} else {
					//	resizeHeight(); //If landscapes are set to fit
					//}
				} else {
					//if (ratio > 1) {
						resizeHeight();
					//} else {
					//	resizeWidth(); //If portraits are set to fit
					//}
				}
				/**End Image Resize**/
				
				/**Resize Functions**/
				function resizeWidth(){
					// Max 1920 * 1080
					if(browserwidth > 1920) {
						browserwidth = 1920;
					}
					
					if (browserwidth * ratio > 1080) {
						thisSlide.height(1080);
						thisSlide.width(1080 / ratio);
					} else {
						thisSlide.width(browserwidth);
						thisSlide.height(browserwidth * ratio);
					}
				};
				
				function resizeHeight(){
					// Max 1920 * 1080
					if(browserheight > 1080) {
						browserheight = 1080;
					}
					
					if (browserheight / ratio > 1920) {
						thisSlide.width(1920);
						thisSlide.height(1920 * ratio);
					} else {
						thisSlide.height(browserheight);
						thisSlide.width(browserheight / ratio);
					}
				};
				
				/*
				//Resize image to proper ratio
				if (browserratio > ratio) {
					$(this).width(browserwidth);
					$(this).height(browserwidth * ratio);
					// $(this).height(browserheight);
					$(this).children().width(browserwidth);
					$(this).children().height(browserwidth * ratio);
				} else {
					$(this).height(browserheight);
					$(this).width(browserheight / ratio);
					$(this).children().height(browserheight);
					$(this).children().width(browserheight / ratio);
					// $(this).children().width(browserwidth);
				}
				*/
				//Gather browser and current image size
				
				
				if (thisSlide.width() > imagewidth){
					imagewidth = thisSlide.width();
				}
				if (thisSlide.height() > imageheight){
					imageheight = thisSlide.height();
				}
				//var imagewidth = $(this).width() > 0 ? $(this).width() : (options.startwidth + 40);
				//var imageheight = $(this).height() > 0 ? $(this).height() : (options.startheight + 40);
				$(this).closest('div').css("overflow",'visible');
			});
			
			$('#imageThumbs').css('width', imagewidth);
			  
			//if ($('#imageThumbs .scrollableArea').width() > imagewidth && $('#imageThumbs').width() < imagewidth)
			  //$('#imageThumbs').css('width', imagewidth);
			
			$('#imageThumbs').css('top', imageheight + 10);
			
			$('#fadeDieBilder').fadeIn('fast');
			$('#content').show();
			$('#loading').hide();
			
			return false;
		//});
	};
	
})(jQuery);
