/* site specific scripts */

function siteInit(){
  // Called with the body onload event.
  // Customise according to site requirements.
  window.onresize = function(){redrawLayout()};
  if(document.body.className.indexOf("HomePage") >= 0){
    adjustContentAreaHeight("content_a");
    adjustTeasers(document.getElementById("content_c"));
    adjustTeasers(document.getElementById("content_d"));
    redrawLayout();
  }
  if(document.body.className.indexOf("SectionFrontPage") >= 0 || document.body.className.indexOf("NewsItemPage") >= 0){
    adjustContentAreaHeight("main_content");
    adjustTeasers(document.getElementById("teaser_area"));
    redrawLayout();
  }
  if(document.body.className.indexOf("NewsItemListPage") >= 0){
    adjustTeasers(document.getElementById("sub_content"));
    redrawLayout();
  }
  if(document.body.className.indexOf("GenericPage") >= 0){
    adjustTeasers(document.getElementById("sub_content"));
    redrawLayout();
  }
  positionMainBackground();
}

function adjustTeasers(teaserContentArea) {
  if(teaserContentArea) {
    var teasers = getSubElementsByClass(teaserContentArea, "teaser");
    if(teasers) {
      var maxTeaserHeight = getMaximumHeight(teasers);
      setPixelHeights(teasers, maxTeaserHeight);
      positionControls(teasers);
    }
  }
}

function adjustContentAreaHeight(contentAreaName) {
  var contentArea = document.getElementById(contentAreaName);
  if(typeof(contentArea) == "object" && contentArea != null) {
    var content = getSubElementsByClass(contentArea, "component");
    if(content) {
      var maxContentHeight = getMaximumHeight(content);
      contentArea.style.height = maxContentHeight + "px";
    }
  }
}

function positionControls(elements) {
  for(var i in elements) {
    var editControls = getSubElementByClass(elements[i], "editControls");
    if(editControls) {
      elements[i].style.position = "relative";
      editControls.style.position = "absolute";
      editControls.style.bottom = "0px";
    }
  }
}

function positionMainBackground() {
  bgDiv = document.getElementById("main-background");
  pageDiv = document.getElementById("page");
  if(bgDiv && pageDiv) {
    bgDiv.style.height = String(Number(pageDiv.offsetHeight) + 7) + "px";
    bgDiv.style.visibility = "visible";
  }
}

function redrawLayout() {
  resetSectionHeights(getSections());
  setLayout();
  positionMainBackground();
}