window.onload = function() {
  ApplyAutoWidth('.AUTOIMAGEWIDTH');
  AdjustFoot();
  ModifyMenu();
  var brHeight = $("#bodyRight").outerHeight(true);
  var bcHeight = $("#bodyCenter").innerHeight();
  if(bcHeight - brHeight < 0) {
    $("#bodyCenter").height(brHeight);
  }
}

function ModifyMenu() {
  var items = $("#mainmenu li");
  var mainmenuWidth = 0;
  $.each(items, function() {  mainmenuWidth += $(this).outerWidth();});
  $("#mainmenu").css("margin-left",($("#bodyMenu").innerWidth() - mainmenuWidth) / 2);
}

function AdjustFoot() {
  var cHeight = Math.max($("#bodyLeft").height(), $("#bodyContent").height());
  var maxHeight = Math.max(cHeight + $("#bodyFoot").height(), $("#bodyRight").height());
  $("#bodyCenter").height(maxHeight);
  $("#bodyFoot").slideDown(200);
}

function ApplyAutoWidth(id) {
  var maxWidth = $(id).innerWidth();
  var pLeft = $(id).css('padding-left');
  if(parseInt(pLeft) > 0) {
    maxWidth -= parseInt(pLeft);
  }
  var pRight = $(id).css('padding-right');
  if(parseInt(pRight) > 0) {
    maxWidth -= parseInt(pRight);
  }
  var imgkey = id + ' * img';
  var items = $(imgkey);
  $.each(items, function() {
    var item = $(this);
    var width = item.outerWidth(true);
    if(width > maxWidth) {
      item.css('width', maxWidth + item.innerWidth() - width);
    }
  });
  imgkey = id + ' img';
  items = $(imgkey);
  $.each(items, function() {
    var item = $(this);
    var width = item.outerWidth(true);
    if(width > maxWidth) {
      item.css('width', maxWidth + item.innerWidth() - width);
    }
  });
}

