function setBackground(src) {

  var rootElement = ("onorientationchange" in window) ? $(document) : $(window), // hack to acccount for iOS position:fixed shortcomings
      imgRatio, bgImg, bgWidth, bgHeight, bgOffset, bgCSS;

  var settings = {
    centeredX: true,         // Should we center the image on the X axis?
    centeredY: true,         // Should we center the image on the Y axis?
    speed: 500                // fadeIn speed for background after image loads (e.g. "fast" or 500)
  };

  var imgSource = $('img#' + src);
  var img = $('div#bg img');

  img.fadeOut(1000, function() {
    bgCSS = {left: 0, top: 0};


    imageWidth = imgSource.width();
    imageHeight = imgSource.height();
    imageRatio = imageWidth / imageHeight;

    windowWidth = $(window).width();
    windowHeight = $(window).height();
    windowRatio = windowWidth / windowHeight;
    
    if (isHorizontale()) {    //if window is horizontale
        Ratio = windowWidth / imageWidth;
        imageWidth = windowWidth;
        imageHeight = (imageHeight * Ratio);
        offset = (imageWidth - windowWidth) / 2;

        if (imageRatio > windowRatio) {
        Ratio = windowHeight / imageHeight;
        imageHeight = windowHeight;
        imageWidth = (imageWidth * Ratio);
        offset = (imageHeight - windowHeight) / 2;
        }
        
       }
    else if (isVerticale()) { //if window is verticale
        Ratio = windowHeight / imageHeight;
        imageHeight = windowHeight;
        imageWidth = (imageWidth * Ratio);
        offset = (imageHeight - windowHeight) / 2;
    }

    img.width(imageWidth);
    img.height(imageHeight);
    img.css(bgCSS);
    jQuery(this).attr('src', src+'.jpg')
  }).fadeIn(1000);
 
 
 
  $(window).resize(adjustBG);



  function adjustBG(fn) {
    bgCSS = {left: 0, top: 0};

    imageWidth = imgSource.width();
    imageHeight = imgSource.height();
    imageRatio = imageWidth / imageHeight;

    windowWidth = $(window).width();
    windowHeight = $(window).height();
    windowRatio = windowWidth / windowHeight;
    
    if (isHorizontale()) {    //if window is horizontale
        Ratio = windowWidth / imageWidth;
        imageWidth = windowWidth;
        imageHeight = (imageHeight * Ratio);
        offset = (imageWidth - windowWidth) / 2;
        bgCSS = $.extend(bgCSS, {top: "-" + offset + "px"});
        
        if (imageRatio > windowRatio) {
        Ratio = windowHeight / imageHeight;
        imageHeight = windowHeight;
        imageWidth = (imageWidth * Ratio);
        offset = (imageHeight - windowHeight) / 2;
        bgCSS = $.extend(bgCSS, {left: "-" + offset + "px"});
        }
        
       }
    else if (isVerticale()) { //if window is verticale
        Ratio = windowHeight / imageHeight;
        imageHeight = windowHeight;
        imageWidth = (imageWidth * Ratio);
        offset = (imageHeight - windowHeight) / 2;
        bgCSS = $.extend(bgCSS, {left: "-" + offset + "px"});
    }
    img.width(imageWidth);
    img.height(imageHeight);
    img.css(bgCSS);
    jQuery(this).attr('src', src+'.jpg')
  }

  function isHorizontale() {
    if ($(window).width() > $(window).height())
      return true;
    return false;
  }
    function isVerticale() {
    if ($(window).height() > $(window).width())
      return true;
    return false;
  }

}
