/**
 * jQuery.fullBg
 * Copyright (c) 2010 c.bavota - http://bavotasan.com
 * Dual licensed under MIT and GPL.
 * Date: 02/23/2010
**/

(function($) {
	$.fn.fullBg = function(){
		var bgImg = $(this);
		bgImg.addClass('hintergrund');
		
		function resizeImg() {	
			resize(bgImg,true);	
		} 
				
		function firstResizeImg() {			
			var id = bgImg.attr("id");
			$("#hintergrund").after('<div class="hintergrundFix"></div>');
			
			if (!bgImg.complete) {
				$(bgImg).load(function(){
					resize(bgImg);
				});
				resize(bgImg);
			} else {
				resize(bgImg);
			}
		}
		
		function resize(bgImg,show) {
			if ($.browser.msie  && parseInt($.browser.version) == 7) {
				bgImg.show();
			}
			var imgwidth = bgImg.width();
			var imgheight = bgImg.height();
			
			if(imgwidth > 200 && imgheight > 200) {
				var winwidth = $(window).width();
				var winheight = $(window).height();
		
				var widthratio = winwidth / imgwidth;
				var heightratio = winheight / imgheight;
				
				var widthdiff = heightratio * imgwidth;
				var heightdiff = widthratio * imgheight;
				
				var heightdiffImg = imgheight-winheight;
				$(".hintergrundFix").css({height: heightdiffImg+'px'});
			
				if((widthdiff-winwidth) > 0) {
					var centerimage = ((widthdiff-winwidth)/2)*(-1);
				} else {
					var centerimage = 0;
				}
				
				if(heightdiff>winheight) {
					bgImg.css({
						width: winwidth+'px',
						height: heightdiff+'px',
						'margin-left': centerimage+'px'
					});
				} else {
					bgImg.css({
						width: widthdiff+'px',
						height: winheight+'px',
						'margin-left': centerimage+'px'
					});
				}
				$(window).load(function() {
					if ($.browser.msie  && parseInt($.browser.version) == 7) {
						// Wurde bereits oben ausgeführt
					} else {
						bgImg.show();
					}
				});
			}
		}
		
		firstResizeImg();
		$(window).resize(function() {
			resizeImg();
		});
	};
})(jQuery)

