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




var browser = new Browser();

function setLayout() { 

  if (browser.version > 5) {

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

    content_top = getElementTop(sections[1]);
    page_bottom = getBottomOfPage();

    // get webfactory toolbar height or set to zero if not available.
    webfactory_toolbar = document.getElementById('webfactory-toolbar');
    if(webfactory_toolbar == null)
      webfactory_toolbar_height = 0;
    else
      webfactory_toolbar_height = webfactory_toolbar.offsetHeight;

    // 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 < 4; n++) {
      if(sections[n] != null)
        setElementHeight(sections[n], content_base);
    }
  
    // position footer (also adds toolbar height when in edit mode)
    var spacing;
    spacing = 10;
    positionElement(sections[4], (content_base + spacing), null);
  }
}




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


// 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;
x3  }

  // 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 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;
  //DEBUGGING:
  //alert(height);
  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_section) {
  // return the lowest base position of content.

  var content_base, current_section_base;

  content_base = 0;
  for (n=0; n < content_section.length; n++) {
    if(content_section[n] != null) {
      current_section_base = content_section[n].offsetHeight + content_section[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) {
    elTop = element.offsetTop;
    height = value - elTop;
    element.style.height = height + "px";
  }
}






