(function($) {
	// Global Namespace
    var jqOptions = {};
    var jq_tag = "#slider";
    // Define the plugin
    $.fn.BgResize = function(options) {
		// Set global to obj passed
		jqOptions = options;
		jqOptions.width = 1920;
		jqOptions.height = 1080;
		jqOptions.heightOffset = 120;
		resizeImages();
    };

	$(window).bind("resize", function() {
		resizeImages();
	});
	
	$.fn._resizeImage = function(id){
    	iw=jqOptions.width;
    	ih=jqOptions.height;
    	_windowWidth = $(window).width();
    	_windowHeight = $(window).height()- jqOptions.heightOffset;

        if (_windowWidth > _windowHeight) {
            if (iw > ih) {
                var fRatio = iw/ih;
                $('#'+id).css("width", _windowWidth + "px");
                $('#'+id).css("height", Math.round(_windowWidth * (1/fRatio)));

                var newIh = Math.round(_windowWidth * (1/fRatio));

                if(newIh < _windowHeight) {
                    var fRatio = ih/iw;
                    $('#'+id).css("height", _windowHeight);
                    $('#'+id).css("width", Math.round(_windowHeight * (1/fRatio)));
                }
            } else {
                var fRatio = ih/iw;
                $('#'+id).css("height", _windowHeight);
                $('#'+id).css("width", Math.round(_windowHeight * (1/fRatio)));
            }
        } else {
            var fRatio = ih/iw;
            $('#'+id).css("height", _windowHeight);
            $('#'+id).css("width", Math.round(_windowHeight * (1/fRatio)));
        }
	}

	function resizeImages() {
        // Resize the img object to the proper ratio of the window.
        $('img', jq_tag).each(
			function(key, value) {
				$.fn._resizeImage($(this).attr("id"));
			}
        );
        $(jq_tag).css({
			"visibility" : "visible"
		});
		// Allow scrolling again
		$("body").css({
            "overflow":"auto"
        });
    }
})(jQuery);
