/**
* Fullscreenr - lightweight full screen background jquery plugin
* By Jan Schneiders, modified by mikkok / Nitro
* Version 1.0
**/
(function($){	
	//Define if plugin is initialized
	var nFullscreenrInitialized = false;
	
	$.fn.nFullscreenr = function(options) {
		if(options.height === undefined) console.log('Please supply the background image height, default values will now be used. These may be very inaccurate.');
		if(options.width === undefined) console.log('Please supply the background image width, default values will now be used. These may be very inaccurate.');
		if(options.bgID === undefined) console.log('Please supply the background image ID, default #bgimg will now be used.');
		
		var defaults = { width: 1280,  height: 1024, bgID: 'bgimg' };
		var options = $.extend({}, defaults, options); 
		
		var css3support = $('html').hasClass('backgroundsize');
		
		if(!css3support) {
			$(document).ready(function() { $(options.bgID).nFullscreenrResizer(options);	});
			$(window).bind("resize", function() { $(options.bgID).nFullscreenrResizer(options); });
		}
			
		return this; 		
	};	
	
	$.fn.nFullscreenrResizer = function(options) {
		// Set bg size
		var ratio = options.height / options.width;	
		// Get browser window size
		var browserwidth = $(window).width();
		var browserheight = $(window).height();

		if(browserwidth <= options.width) {
			$(this).css('display', 'none');
		}
		else {
			$(this).removeAttr("style");
			// Scale the image
			if (browserwidth > options.width){
			    $(this).height(browserwidth * ratio);
			    $(this).width(browserwidth);
			} else {
			    $(this).width(browserwidth);
			    $(this).height(browserwidth * ratio);
			}
			// Center the image
			$(this).css('left', (browserwidth - $(this).width())/2);
			$(this).css('top', 0);
			$(this).css('position', 'absolute');
			
			if(!nFullscreenrInitialized) {
				nFullscreenrInitialized = true;
				$(this).show('fast');
			}
		
			return this;
		}		
	};
})(jQuery);
