// auto-resizing background image
var _img, _imgRatio, _w, _h, _bw, _bh;


// resize img when window changes size
// thx for some finishing touches to supersized2 by Sam Dunn // www.onemightyroar.com
function _setImgSize() {
  _bw = jQuery(window).width();
  _bh = jQuery(window).height();

  var _uh = (_bh / _bw > _imgRatio);
  var _newHeight = _uh ? _bh : parseInt(_bw * _imgRatio);
  var _newWidth  = _uh ? parseInt(_bh / _imgRatio) : _bw;

  _logStatus('Resized image to ' + _newWidth + ' x ' + _newHeight);

  _img.width(_newWidth);
  _img.height(_newHeight);

  var _marginleft = _img.width();
  _marginleft = _marginleft / -2;
  _img.css('left', '50%').css('margin-left', _marginleft + 'px');
};

function _logStatus(text) {
  if ('undefined' != typeof console) {
    if ('function' == typeof console.log) {
      console.log(text);
    }
  }
}

// call function to resize image whenever the height and/or width od the browser window is changed.
jQuery(window).resize( function() {
  _setImgSize();
});

jQuery(document).ready( function() {

  if (undefined !== window._bgImage) {
    // create background image
    _img = jQuery('<img>').attr('id', 'bgImage').attr('src', _bgImage);

    // what to do when the image is loaded
    _img.load( function() {
      _w = jQuery(this).width();
      _h = jQuery(this).height();
      _imgRatio = _h / _w;
      _setImgSize();
      jQuery(this).fadeIn(150);
      // jQuery(this).slideDown();
    });

    // append it to the body of the page to actually start loading
    _img.appendTo(jQuery('body'));
  }
});
