/**
* Fullscreenr - lightweight full screen background jquery plugin
* By Jan Schneiders
* Version 1.0
* www.nanotux.com
**/

/**
* 
**/

(function($){   
	$.fn.fullscreenr = function(options) {
		if(options.height === undefined) alert('Please supply the background image height, default values will now be used. These may be very inaccurate.');
		if(options.width === undefined) alert('Please supply the background image width, default values will now be used. These may be very inaccurate.');
		if(options.bgID === undefined) alert('Please supply the background image ID, default #bgimg will now be used.');
		var defaults = { minwidth: 960, width: 1280,  height: 1024, bgID: 'bgimg' };
		var options = $.extend({}, defaults, options); 
		$(document).ready(function() { $(options.bgID).fullscreenrResizer(options); });
		$(window).bind("resize", function() { $(options.bgID).fullscreenrResizer(options); });      
		return this;
		
		// Erst mal weg mit den Styles, die das Bild ohne Javascript im Header ausrichten
		
	};  
	$.fn.fullscreenrResizer = function(options) {
		// Set bg size
		var ratio = options.height / options.width; 
		// Get browser window size
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
		// Scale the image
		//if ((browserheight/browserwidth) > ratio){
		//    $(this).height(browserheight);
		//    $(this).width(browserheight / ratio);
		//} else {
		//    $(this).width(browserwidth);
		 //   $(this).height(browserwidth * ratio);
		//}
		// Center the image
		//$(this).css('left', (browserwidth - $(this).width())/2);
		//$(this).css('left', (browserwidth - $(this).width())/2);
		
		$( this ).css( 'left',0 ).css( 'margin-left',0 ).width( browserwidth );

		$( this ).parent().find('img').css( 'margin-left',0 );
		
		if ( browserwidth > options.width ){
		// wenn Browser breiter als Bild
			// Breite des Bildes auf die Breite des Browserfensters setzen
			$( this ).parent().find('img').width( browserwidth )
			// Bildhöhe entsprechend der Seitenverhältnisse des Bildes setzen
			.height( browserwidth * ( ratio ) )
			// Bild nach links rausschieben und damit im umgebenden DIV zentrieren 
			.css( 'left', 0 );
			
		} else if( browserwidth < options.minwidth ) {
		// Mindestgroesse unterschritten?
			
			browserwidth = options.minwidth;
			
			// Bild auf seine tatsaechliche Groesse setzen, um nicht unnoetig zu skalieren
			$( this ).parent().find('img').width( options.width ).height( options.height )
			// Und Zentrieren, indem es nach links aus dem Browser geschoben wird, 
			// Annahme: Das wrappende DIV ist immer so breit wie das Browserfenster
			.css( 'left', ( browserwidth - options.width ) / 2 );
			
			/*
			$( this ).width( options.minwidth );
			// Bild auf seine tatsaechliche Groesse setzen, um nicht unnoetig zu skalieren
			$( this ).parent().find('img').width( options.width ).height( options.height )
			// Und Zentrieren, indem es nach links aus dem Browser geschoben wird, 
			// Annahme: Das wrappende DIV ist immer so breit wie das Browserfenster
			.css( 'left', options.width - options.minwidth / 2 );
			*/
			
		} else {
		// Sonst
			// Bild auf seine tatsaechliche Groesse setzen, um nicht unnoetig zu skalieren
			$( this ).parent().find('img').width( options.width ).height( options.height )
			// Und Zentrieren, indem es nach links aus dem Browser geschoben wird, 
			// Annahme: Das wrappende DIV ist immer so breit wie das Browserfenster
			.css( 'left', ( browserwidth - options.width ) / 2 );
			
		}
		//$(this).css('top', (browserheight - $(this).height())/2);
		return this;        
	};
})(jQuery);
