/* site specific scripts */

function siteInit(){
  // Called with the body onload event.
  // Customise according to site requirements.
  if(document.body.className.indexOf("HomePage") >= 0){
    sizeHomeNewsList();
    arrangeHomeTeasers();
    redrawLayout();
  }
  if(document.body.className.indexOf("SectionFrontPage") >= 0){
    adjustSectionTeasers();
    redrawLayout();
  }
}

function setContentToMeetFooter(fPosX, changeElements) {
  // Function sets the length of changeElements to 
  // meet with the top of the fPosX section.
  // Note: Due to padding in main-content, need to 
  // handle separately (use setMainContentToMeetFooter()).

  var contentBase = fPosX;
  for(n = 0; n < changeElements.length; n++) {
    if(changeElements[n] != null) {
      if(changeElements[n].id != "main-right") {
        setElementHeight(changeElements[n], contentBase);
      }
    }
  }
}

function adjustSectionTeasers() {
  var teaserContentArea = document.getElementById("sub_content");
  if(teaserContentArea) {
    var teasers = getSubElementsByClass(teaserContentArea, "teaser");
    var height = 0;
    var partnerOne = null;
    var partnerOneHeight = 0;
    var partnerTwo = null;
    var partnerTwoHeight = 0;
    var borders = 2;
    for(var i in teasers) {
      if(i==0 || i%2==0) {
        partnerOne = teasers[i];
        partnerOneHeight = partnerOne.offsetHeight - borders;
        if(++i < teasers.length) {
          partnerTwo = teasers[i];
          partnerTwoHeight = partnerTwo.offsetHeight - borders;
          if(partnerTwoHeight < partnerOneHeight) {
            partnerTwo.style.height = partnerOneHeight + "px";
          }
          else {
            if(partnerOneHeight < partnerTwoHeight) {
              partnerOne.style.height = partnerTwoHeight + "px";
            }
          }
        } 
      }
    }
  }
}

function sizeHomeNewsList() {
  var topContent = document.getElementById("main_content");
  var mainBg = document.getElementById("main-background");
  var newsList = getSubElementByClass("latestNewsList");
/*
  var newsList = document.getElementById("main_content-item-3");
  if(topContent && newsList) {
    // value 4, is taken from padding set between teasers.
    topMargin = 8;
    //newsList.style.height = String((Number(topContent.offsetHeight) - 5) - Number(topMargin)) + "px";
    //newsList.style.marginTop = topMargin + "px";
  }
*/
  if(topContent) {
    var maxHeight = 0;
    children = getSubElements(topContent);
    maxHeight = getMaximumHeight(children);
    if(topContent.offsetHeight < maxHeight) {
      topContent.style.height = maxHeight + "px";
    }
    if(mainBg) {
      mainBg.style.height = maxHeight + "px";
    }
  }
}

function arrangeHomeTeasers() {
  var teaserContentArea = document.getElementById("teaser_content");
  if(teaserContentArea) {
    var teasers = getSubElements(teaserContentArea);
    var headings = new Array();
    for(var i in teasers) {
      headings[i] = getSubElementByClass(teasers[i], "title");
    }
    var maxHeadingHeight = getMaximumHeight(headings);
    setPixelHeights(headings, maxHeadingHeight);
    var maxTeaserHeight = getMaximumHeight(teasers);
    setTeaserHeights(teasers, maxTeaserHeight);
  }
}

function setTeaserHeights(elements, height) {
  var insideChildren;
  var textDiv;
  for(var i in elements) {
    insideChildren = elements[i].childNodes;
    textDiv = null;
    for(var a in insideChildren) {
      currentNode = insideChildren[a];
      if(currentNode.nodeType == 1) {
        if(currentNode.className == "text") {
          textDiv = currentNode;
          break;
        }
        else {
          textDiv = null;
        }
      }
    }
    if(textDiv) {
      // teasers...
      textHeight = textDiv.offsetHeight;
      teaserHeight = elements[i].offsetHeight;
      difference = height - teaserHeight;
      textDiv.style.height = String(Number(textHeight) + Number(difference)) + "px";
    }
    else {
      // image map...
      // reduce height by 1, to allocate for border.
      var child = getSubElementByClass(elements[i], "editControls");
      if(typeof(elements[i]) == "object" && elements[i] != null) {
        elements[i].style.height = (height - 1) + "px"; 
        if(child) {
            child.style.position = "absolute";
        }
      }
    }
  }  
}

function adjustMessageHeights(message) {
  var content = getSubElementByClass(message, "forumItemContentRow");
  var title = getSubElementByClass(message, "title");
  var element = getSubElementByClass(content, "messageDetails");
  var reqHeight = message.offsetHeight - title.offsetHeight;
  element.style.height = reqHeight + "px";
}

/*******************************
 Accessibility baseFontSizing...
 *******************************/
function displayFontSwitch() {
  if (document.body.className.indexOf('welsh') != -1) {
    SIZE_UP_MSG = "<big>A</big><small>A</small> Maint y Testun";
    SIZE_DOWN_MSG = "<big>A</big><small>A</small> Maint y Testun";
  } else if (document.body.className.indexOf('Microsite') != -1) {
    SIZE_UP_MSG = "<big>A</big><small>A</small> Change Text Size";
    SIZE_DOWN_MSG = "<big>A</big><small>A</small> Change Text Size";
  } else {
    SIZE_UP_MSG = "text size";
    SIZE_DOWN_MSG = "text size";
  }
  if(document.getElementById("font-switch")) {
    // font-switch created with only one per page in mind. 
    // This customises for two per page, on this site.
    fontSwitchId = "font-switch-two";
  }
  else {
    fontSwitchId = "font-switch";
  }
  document.write("<span id=\"" + fontSwitchId + "\" \n");
  document.write("       class=\"button\"\n");
  document.write("       onclick=\"switchFontSize(this)\">\n");
  if(getCookie("baseFontSize") > 0) {
    document.write(SIZE_DOWN_MSG);
  }
  else {
    document.write(SIZE_UP_MSG);
  }
  document.write("</span>");
}

function switchFontSize(element) {
  var fcv = null;
  if(Number(getCookie("baseFontSize"))) {
    fcv = 0;
    element.innerHTML = SIZE_UP_MSG;
  }
  else {
    fcv = 1;
    element.innerHTML = SIZE_DOWN_MSG;
  }
  setCookie("baseFontSize", fcv, null, "/");
  setBaseFontSize(fcv);
  redrawLayout();
}


function getSections() {
  sections = new Array();

  sections[0] = document.getElementById('main-banner');
  sections[1] = document.getElementById('main-left');
  sections[2] = document.getElementById('main-right');
  sections[3] = null;
  sections[4] = document.getElementById('main-content');
  sections[5] = document.getElementById('main-footer');

  return sections;
}

