(function ($) {
	$(document).ready(function(){
		$('body').prepend('<div id="fond_accueil"></div>');
		
		// set required css options
		$('#fond_accueil').css('overflow', 'hidden').css('z-index', 0);
		// set required css options for fullscreen mode
		$('#fond_accueil').css('position', 'fixed').css('width', '100%').css('height', '100%').css('top', 0).css('left', 0);
	
		// hide all images, set position absolute
		$('#fond_accueil img').hide().css('position', 'absolute'); 
		img = new Image();
		$(img).load(function(){
			$(this).css('position', 'absolute').hide();
			$('#fond_accueil').prepend(this);	
			var thisimg = $('#fond_accueil img');		
			superbgResize();
			$(thisimg).show();
			
		}).attr("src", "/images/fond-meuble-04.jpg");
	});
	// bind resize-event to resize actual image
	$(window).bind('resize', function() {
		superbgResize();
	});

	// fix for IE6
	$.superbg_isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
	if ($.superbg_isIE6 ) {
		$('#' + options.id).css('position', 'absolute').width($(window).width()).height($(window).height());
		$(window).bind('scroll', function() {
			superbgScrollIE6();
		});
	}
	
	// calculate image size, top and left position
	function superbgCalcSize (imgw, imgh) {		

		// get browser dimensions
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
		
		// calculate ratio
		var ratio = imgh / imgw;

		// calculate new size
		var newheight = 0; var newwidth = 0;
		if ((browserheight / browserwidth) > ratio) {
			newheight = browserheight;
			newwidth = Math.round(browserheight / ratio);
		} else {
			newheight = Math.round(browserwidth * ratio);
			newwidth = browserwidth;
		}
		
		// calculate new left and top position
		var newleft = Math.round((browserwidth - newwidth) / 2);
		var newtop = Math.round((browserheight - newheight) / 2);
		
		var rcarr = [newwidth, newheight, newleft, newtop];
		return rcarr;
		
	}
	
		// handle resize-event, resize active image
	function superbgResize () {	
	
		// get active image
		var thisimg = $('#fond_accueil img');

		// calculate size and position
		var dimensions = superbgCalcSize($(thisimg).width(), $(thisimg).height());
		var newwidth = dimensions[0];
		var newheight = dimensions[1];
		var newleft = dimensions[2];
		var newtop = dimensions[3];

		// set new width/height
		$(thisimg).css('width', newwidth + 'px');
		$(thisimg).css('height', newheight + 'px');

		// fix for IE6
		if ($.superbg_isIE6) {
			$('#' + options.id).width(newwidth).height(newheight);
			$(thisimg).width(newwidth);
			$(thisimg).height(newheight);
		}
		
		// set new left position
		$(thisimg).css('left', newleft + 'px');

		$(thisimg).css('top', newtop + 'px');

	}
		// fix for IE6, handle scrolling-event
	function superbgScrollIE6 () {
		// set top of the container
		$('#fond_accueil').css('top', document.documentElement.scrollTop + 'px');
		
	};
})(jQuery);
