/******************************************************/
/* Summary:                                           */
/* --------                                           */
/*                                                    */
/* Browser(): Detect browser                          */
/* positionFooter: put the footer navigation right at */
/* the bottom of the page, on loading and resizing.   */
/*                                                    */
/******************************************************/




var current_browser = new Browser();


// FUNCTION: detects the browser for use in later functions.
//
function Browser() 
{
  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) 
  {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) 
  {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Opera/";
  if ((i = ua.indexOf(s)) >= 0) 
  {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) 
  {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}



/***********************************/
/* FUNCTION DEFINITIIONS FOLLOW... */
/***********************************/
var mainContentHeight = 0; // This variable is used for the quiz page.
var footerPositionX = 0;

function standardInit() {
  // calls all the standard preparatory functions.
  setLayout();
  setLinkTargets();
}

function setLayout() { 

  if (current_browser.version > 5) {

    sections = new Array();
    // add sections here...
    sections[0] = document.getElementById('main-banner');
    sections[1] = document.getElementById('main-left');
    sections[2] = document.getElementById('main-right');
    sections[3] = document.getElementById('main-background');
    sections[4] = document.getElementById('main-content');
    sections[5] = document.getElementById('main-footer');

    // get lowest content base to use for footer positioning.
    footerHeight = sections[5].offsetHeight;
    contentBase = getContentBasePosition(sections);
    pageBottom = getBottomOfPage() - footerHeight;
    mainContentHeight = sections[4].offsetHeight;

    if(1 && (footerHeight < (pageBottom - contentBase))) 
      footerPositionX = pageBottom;
    else
      footerPositionX = contentBase;

    // Position the main-footer...
    positionElement(sections[5], footerPositionX, null);
    sections[5].style.visibility = 'visible';

    if(1)
        setContentToMeetFooter(footerPositionX);

  }
}

function positionElement(element, x, y) {
  //  position and element using css values.

  var pos; 
  if(element != null) {
    element.style.position = "absolute";
    if(x != null){
      pos = x + "px";
      element.style.top = pos;
    }

    if(y != null){
      pos = y+"px";
      element.style.left = pos;
    }
    element.style.display = "block";
  }

}


function getBottomOfPage() {
  // Return the height of the div#page element 
  // Unlike previous methods, this is only a few 
  // pixels difference between IE & Firefox. 

  var holder = document.getElementById('page');
  var height = holder.offsetHeight;  

  // DEBUGGING section
  //alert("page height = " + holder.offsetHeight);

  return height;
}


function getElementTop(element) {
  // return the top position of an element.

  var top;
  if(element != null)
    top = element.offsetTop;
  else
    top = 0;

  return top;
}


function getContentBasePosition(contentSection) {
  // return the lowest base position of content.

  var contentBase, currentSectionBase;

  contentBase = 0;
  for (n=0; n < contentSection.length; n++) {
    if(contentSection[n] != null) {
      currentSectionBase = contentSection[n].offsetHeight + contentSection[n].offsetTop;
      if (currentSectionBase > contentBase) {
        contentBase = currentSectionBase;
      }
    }
  }
  return contentBase;
}


function setElementHeight(element, value) {
  // NOTE: padding-top on the elements needs to be zero or the length can 
  // work out to be increased by the padding amount, thus giving an 
  // unexpected result.

  var elTop, height;

  if(element != null) {
    elTop = element.offsetTop;
    height = value - elTop;
    element.style.height = height + "px";
  }
}

function setContentToMeetFooter(fPosX){
  // Function sets the length of sections 1-4 (inclusive)
  // to meet with the top of the main-footer section.

  var contentBase = fPosX;
  var mainContentChildren = sections[4].childNodes;

  // Set length of sections 1-3 (see below for sections[4])
  for(n = 1; n <= 3; n++) {
    if(sections[n] != null) {
      setElementHeight(sections[n], contentBase);
    }
  }

  // Due to padding in main-content, need to handle separately...
  for(var index in mainContentChildren){
    firstContent = mainContentChildren[index];
    // Check for first visible element (ie:  content-header, not the 'skip' anchor).
    if(firstContent.nodeType == 1 && firstContent.name != 'skip')
      break;
  }
  setElementHeight(sections[4], contentBase - firstContent.offsetTop);
}


function setLinkTargets() {
  // Function looks through all links on a page and adds
  // correct target location, depending on whether the 
  // link is internal or external.
  var link;
  var domain = document.domain;

  for(var i in document.links) {
    link = document.links[i];

    // Internet Explorer throws up a link that doesn't have
    // .hostname, which causes an error.       
    if(link.hostname){

      // Set the target based on domain matching.
      if(link.hostname != domain) {
        link.target = "_blank";
      }
      else {
        link.target = "_self";
      }
    }
  }
}



