// скрипты, выполняющиеся при загрузке каждой страницы

function getSizeXY() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
} 

function getCssRule(ruleSelectorText) {
  for (var iCssSheet = 0; iCssSheet < document.styleSheets.length; iCssSheet++) {
    var cssSheet = document.styleSheets[ iCssSheet ];
    
    var rules;
    if (document.styleSheets[ iCssSheet ].cssRules) {
      rules = document.styleSheets[ iCssSheet ].cssRules;
    } else if (document.styleSheets[ iCssSheet ].rules) {
      rules = document.styleSheets[ iCssSheet ].rules;
    }

    for (var iRule = 0; iRule < rules.length; iRule++) {
      var cssRule = rules[ iRule ];
      if (cssRule.selectorText == ruleSelectorText) {
        return cssRule;
      }
    }
  }

  return null;
}

function getDocumentHeight() {
  screen.getDocumentHeight = function() {
    var body = document.body;
    var innerHeight =
      ((self.innerHeight)&&!isNaN(self.innerHeight))?self.innerHeight:0;
    if (!document.compatMode || document.compatMode=="CSS1Compat") {
//    var topMargin = parseInt(CSS.get(body,'marginTop'),10) || 0;
//    var bottomMargin = parseInt(CSS.get(body,'marginBottom'), 10) || 0;
    var topMargin = 0;
    var bottomMargin = 20;
    return Math.max(body.offsetHeight + topMargin + bottomMargin,
      document.documentElement.clientHeight,
      document.documentElement.scrollHeight, 0);
  }
  return Math.max(body.scrollHeight, body.clientHeight,
    0);
  };

  return screen.getDocumentHeight();
}

// устанавливает footer внизу страницы
function placeFooter(footerCssStyleName) {
  var footerCssRule = getCssRule(footerCssStyleName);
  
  var sizeXY = getSizeXY();
  var scrollXY = getScrollXY();

  var footerTop = getDocumentHeight();
  footerCssRule.style.position = "absolute";
  var h = parseInt(footerCssRule.style.height.replace(/px/, ""));
  footerTop -= h;
  footerCssRule.style.top = footerTop + "px";
}

function onload_page() {
  placeFooter("#footer");
}