/**
 * Fullscreenr - lightweight full screen background jquery plugin
 * By Jan Schneiders
 * Version 1.0
 * www.nanotux.com
 **/
(function($) {
    $.fn.fullscreenr = function(options) {
        if (options.bgID === undefined) alert('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);
        $(document).ready(function() {
            $(options.bgID).fullscreenrResizer(options);
        });
        $(window).bind("resize", function() {
            $(options.bgID).fullscreenrResizer(options);
        });
        return this;
    };
    $.fn.fullscreenrResizer = function(options) {
        //Calculate aspect ratio of the browser
        var browserwidth = parseInt($(window).width());
        var browserheight = parseInt($(window).height());
        var aspectRatioBrowser = browserwidth / browserheight;

        //Decide what image to use
        var currentImage;
        var width, height;
        if (aspectRatioBrowser > options.wideScreenAspectRatio) {
            $(options.bgID).hide();
            $(options.widescreenBgId).show();
            currentImage = options.widescreenBgId;
        } else {
            $(options.bgID).show();
            $(options.widescreenBgId).hide();
            currentImage = options.bgID;
        }
        // Set bg size, calculate current image aspect ratio
        var ratio = parseInt($(currentImage).attr("height")) / parseInt($(currentImage).attr("width"));

        // Scale the image
        if ((browserheight / browserwidth) > ratio) {
            $(currentImage).height(browserheight);
            $(currentImage).width(browserheight / ratio);
        } else {
            $(currentImage).width(browserwidth);
            $(currentImage).height(browserwidth * ratio);
        }
        // Center the image
        $(currentImage).css('left', (browserwidth - $(currentImage).width()) / 2);
        $(currentImage).css('top', (browserheight - $(currentImage).height()) / 2);


        return this;
    };
})(jQuery);
