$.fn.resizeBg = function() {
    var img = $(this).children('img');
    var imgWidth = img.attr('width');
    var imgHeight = img.attr('height');
    var ratio = imgHeight/imgWidth;
    var winWidth = $(window).width();
    var winHeight = $(window).height();
    var winRatio = winHeight/winWidth;
    if (winRatio > ratio) {
        $(this).height(winHeight);
        $(this).width(winHeight / ratio);
        img.height(winHeight);
        img.width(winHeight / ratio);
    } else {
        $(this).width(winWidth);
        $(this).height(winWidth * ratio);
        img.width(winWidth);
        img.height(winWidth * ratio);
    }
};

$(window).bind("resize", function() {
    $('#background').resizeBg();
});

$(document).ready(function() {
    $('#background').resizeBg();
});

