/******************************************************/
/* 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 setLayout() { 

  if (current_browser.javascript == 'enabled') {

    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');

    content_top = getElementTop(sections[1]);
    // Commented out this following line and replaced value of page_bottom
    // with zero to prevent pages trying to fill full length of window (TEEN[74])
    // page_bottom = getBottomOfPage();
    page_bottom = 0;

    // New html layout means no longer need to allocate for whether the toolbar is there or not.
    // Eventually this should be removed, but set to zero here so that it can be compared with
    // other prototype javascript copies to see what and why...
    webfactory_toolbar_height = 0; //Set to 5px because below IE6 needs few pixels spacing.

    // get lowest content base to use for footer positioning.
    content_base = webfactory_toolbar_height + getContentBasePosition(sections);

    if(content_base < page_bottom)
      content_base = page_bottom;

    // set length of required sections to meet footer top.
    for(n = 1; n < 5; n++) {
      if(sections[n] != null) {
        setElementHeight(sections[n], content_base);
      }
    }
  
    // position footer (also adds toolbar height when in edit mode)
    var spacing;
    spacing = 0; // Takes into account the border of main-content.
    positionElement(sections[5], (content_base + spacing), null);

    // Special browser fix for height issue in Mozilla 1.6 & Opera
    if((current_browser.version == 1.6 && current_browser.brand == "Gecko") || current_browser.brand == "Opera")
    {
      specialHeight = content_base + sections[5].offsetHeight;
      document.getElementById('centre-position-inside').style.height = specialHeight + 'px';
    }
  }
}




/***********************************/
/* FUNCTION DEFINITIIONS FOLLOW... */
/***********************************/


// FUNCTION: detects the browser for use in later functions.
//
function Browser() 
{
  this.brand   = null;
  this.version = null;
  this.javascript = 'enabled';
  this.standardsCompliant = false;
  ua = navigator.userAgent;
  browsers = ['MSIE',
              'Netscape/',
              'Opera/',
              'Firefox/',
              'Gecko'];

  // Find browser type.
  foundBrowser = '';
  for(var i in browsers){
    foundIndex = ua.indexOf(browsers[i])
    if(foundIndex >= 0){
      foundBrowser = browsers[i];
      break;
    }
  }

  // Set browser properties based on type found.
  switch(foundBrowser){
    case browsers[0]: 
         this.brand = "Internet Explorer";
         this.version = parseFloat(ua.substr(foundIndex + foundBrowser.length));
         standardsCheckNumber = this.version;
         break;

    case browsers[1]: 
         this.brand = "Netscape";
         this.version = parseFloat(ua.substr(foundIndex + foundBrowser.length));
         standardsCheckNumber = this.version;
         break;

    case browsers[2]: 
         this.brand = "Opera";
         this.version = parseFloat(ua.substr(foundIndex + foundBrowser.length));
         standardsCheckNumber = this.version;
         if(this.version < 7)
           this.javascript = 'disabled';
         break;

    case browsers[3]: 
         this.brand = "Firefox";
         this.version = parseFloat(ua.substr(foundIndex + foundBrowser.length));
         standardsCheckNumber = 6; // Set to any number that will pass check.
         break;

    case browsers[4]: 
         this.brand = "Gecko";
         start = (ua.indexOf('rv:') + 3);
         end = 3;
         this.version = parseFloat(ua.substr(start,end));
         standardsCheckNumber = 6.1 // Use old method of setting Gecko browsers to this.
         break;

    default : 
         // Defaults for browser not found..
         this.brand = "Unknown";
         this.version = 1;
         this.javascript = 'disabled';
         standardsCheckNumber = 0;
  }

  // Check to see if standard compliant (uses old method of 
  // loosely checking against version number).
  if(standardsCheckNumber >= 5)
    this.standardsCompliant = true;

}


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 an approximate value representing the browsers content window base. 
  // NOTE: approximated_padding allows for the status bar etc.

  var approximated_padding, height;

  approximated_padding = 50;

  if(navigator.appName == "Microsoft Internet Explorer")
    height = document.body.offsetHeight; // Crappy browser doesn't give required property. Need to find way of accessing window height.
  else
    height = window.innerHeight;

  return height - approximated_padding;
}


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(content_sections) {
  // return the lowest base position of content.

  var content_base, current_section_base;

  content_base = 0;
  for (n=0; n < content_sections.length; n++) {

/*
// DEBUGGING:
alert("Inside getContentBasePosition:\n" + 
      "section = " + content_sections[n].id + "\n" + 
      "offsetHeight = " + content_sections[n].offsetHeight + "\n" +
      "offsetTop = " + content_sections[n].offsetTop);
*/
    if(content_sections[n] != null) {
      current_section_base = content_sections[n].offsetHeight + content_sections[n].offsetTop;
      if (current_section_base > content_base) {
        content_base = current_section_base;
      }
    }
  }
  return content_base;
}


function setElementHeight(element, value) {
  // uses the 'value' passed against the elements top and height
  // to determine if need to reset so that it meets the top of
  // the footer element.
  // 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) {
/* 
alert("Inside setElementHeight:\n" + 
      "element = " + element.id + "\n" + 
      "value = " + value + "\n" + 
      "offsetTop = " + element.offsetTop + "\n" + 
      "setting height = " + (value - element.offsetTop));
*/
    elTop = element.offsetTop;
    height = value - elTop;
    element.style.height = height + "px";
  }
}

