function doResize() {
  if(!$('#bg')) return;
   if(jQuery.browser.msie) window.scrollTo(0,0);
  
  $('#background-content').css({'width':'1px','height':'1px'});
  $('#bg').css({'width':'1px','height':'1px'});

  var divWidth = $(window).width();
  var divHeight = $(window).height();  
  var imageHeight = $(window).height(); 
  var imageWidth = $(window).width();

  if (divWidth < 1200) {
    divWidth = 1200;
  } 
  if (divHeight < 800) {
    divHeight = 800;
  }

  imageWidth = imageHeight * 1200/800;
  if (imageWidth < divWidth) {
    imageWidth = divWidth;
    imageHeight = imageWidth * 800/1200;
  }
  
  if (imageHeight < divHeight) {
    imageHeight = divHeight;
    imageWidth = imageHeight * 1200/800;
  }
  $('#bg').css({
    'visibility':"visible", 'width':imageWidth + "px", 'height':imageHeight + "px",
    'marginLeft':Math.round((divWidth -imageWidth)/2)+"px"
  });
  $('#background-content').css({'width':divWidth + "px", 'height':divHeight + "px", 'display':"block"});
}

//Event.observe(window,'resize',doResize);


function setupPage(){
  var bg = $('#bg'),  p = $(window).height()* $(window).width();
  if(bg && bg.attr('alt')){
    if(p<=500000) bg.attr('src','fileadmin/backgrounds/'+bg.attr('alt')+'_S.jpg');
    if(p>500000 && p<=720000) bg.attr('src', 'fileadmin/backgrounds/'+bg.attr('alt')+'_M.jpg');
    if(p>720000) bg.attr('src','fileadmin/backgrounds/'+bg.attr('alt')+'_L.jpg');
    bg.attr('alt','');
    bg.attr('title','');    
  }
  doResize();
  if(typeof executeOnPageLoad == 'function') executeOnPageLoad.defer();
};

$(window).bind('resize',doResize);
$(document).ready(setupPage);
