var ih; // bacgrkound image height
var iw; // bacgrkound image width
var img; // bacgrkound image element

// when the page is fully loaded
jQuery(document).ready(function() {

      // scroll Hack for IE6
      if (navigator.userAgent.match('MSIE'))
        jQuery(document.body).prepend(jQuery('<div id="mouseScrollHack"></div>'));


    img = jQuery('#background img:first');
    iw = jQuery(img).width();			// image width
    ih = img.height();			// image height
    var pw = jQuery(window).width();	// page width
    var ph = jQuery(window).height();	// page height


});

// resize on window resize
jQuery(window).resize(function() {
  refreshPage();
});

// resize footer on window load
jQuery(window).load(function() {
    var pw = jQuery(window).width();  // page width
    var ph = jQuery(window).height(); // page height

  img = jQuery('#background img:first');
  iw = jQuery(img).width();			// image width
  ih = img.height();			// image height
  resizeBg(iw, ih, pw, ph);

  setTimeout(function(){
      resizeBg(iw, ih, pw, ph);
  },2000);
});



//resize background image
function resizeBg(iw, ih, pw, ph) { //args: image width, image height, page width, page height */
  if (ih > ph && iw < pw) {
    img.css('width', pw+'px');
    img.css('height', ((ih*pw)/iw)+'px');
  } else if (ih < ph && iw > pw) {
    img.css('height', ph+'px');
    img.css('width', ((ph*iw)/ih)+'px');
  } else if (ih > ph && iw > pw) {
    if (((ih*pw)/iw) >= ph) {
      img.css('width', pw+'px');
      img.css('height', ((ih*pw)/iw)+'px');
    } else {
      img.css('height', ph+'px');
      img.css('width', ((ph*iw)/ih)+'px');
    }
  } else if (ih < ph && iw < pw) {
    if (((ih*pw)/iw) >= ph) {
      img.css('width', pw+'px');
      img.css('height', ((ih*pw)/iw)+'px');
    } else {
      img.css('height', ph+'px');
      img.css('width', ((ph*iw)/ih)+'px');
    }
  }
}


function refreshPage(){
    var pw = jQuery(window).width();  // page width
    var ph = jQuery(window).height(); // page height
    resizeBg(iw, ih, pw, ph);
}
