/* site specific scripts */

function siteInit(){
  // Called with the body onload event.
  // Customise according to site requirements.
  if(browser.isIE) {
    fixExplorerCornersBug();
  }
}

function redrawLayout() {
  resetSectionHeights(getSections());
  setLayout();
  if(browser.isIE) {
    fixExplorerCornersBug();
  }
}

function Browser() {
                                                                                                                                                             
  var ua, s, i;
                                                                                                                                                             
  this.isIE    = false;  // Internet Explorer
  this.isNS    = false;  // Netscape
  this.isMac   = false; // Macintosh
  this.version = null;
                                                                                                                                                             
  ua = navigator.userAgent;
                                                                                                                                                             
  s = "Mac";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isMac = true;
  }
                                                                                                                                                             
  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;
  }
                                                                                                                                                             
  // 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;
  }
}

var browser = new Browser();


function fixExplorerCornersBug() {
  var divs = document.getElementById("page").getElementsByTagName("li");
  var roundedElements = new Array();
  var innerHeight = 0;
  var innerWidth = 0;
  var newHeight = 0;
  var newWidth = 0;
                                                                                                                                                             
  for(var i in divs) {
    if(typeof divs[i] == "object" && divs[i] != null && divs[i].nodeType == 1) {
      if(divs[i].className.indexOf("roundedElement") >= 0) {
        roundedElements.push(divs[i]);
      }
    }
  }
                                                                                                                                                             
  for(var i in roundedElements) {
    adjustCorners(roundedElements[i]);
  }
}

function adjustCorners(el) {
  var elHeight = el.offsetHeight;
  var elWidth = el.offsetWidth;
  var children = getSubElementsByClass(el, "bgElement");
  var child = null;
  var childBottom = null;
  var childRight = null;
  var newChildBottom = null;
  var newChildRight = null;

  for(var i in children) {
    child = children[i];
    if(getStyleValue(child, "display") != "none") {
     
      // XXX: Hack to make sure that IE is getting the correct height.  IE 6 is gettting the wrong offsetHeight so it is being
      // set explicitly here to 12px.
      childBottom = child.offsetTop + 12;
      if(childBottom < elHeight) {
        newChildBottom = getStyleValue(child, "bottom").replace("px","") - (elHeight - childBottom);
      }
      childRight = elWidth - (child.offsetWidth + child.offsetLeft);
      if(childRight > 0) {
        newChildRight = getStyleValue(child, "right").replace("px","") - 1;
      }
      if(child.className.indexOf("bgOne") >= 0) {
      }
      if(child.className.indexOf("bgTwo") >= 0 && newChildRight) {
        child.style.right = newChildRight + "px";
      }
      if(child.className.indexOf("bgThree") >= 0 && newChildBottom) {
        child.style.bottom = newChildBottom + "px";
      }
      if(child.className.indexOf("bgFour") >= 0) {
        if(childRight > 0 && newChildRight) {
          child.style.right = newChildRight + "px";
        }
        if(childBottom < elHeight && newChildBottom) {
          child.style.bottom = newChildBottom + "px";
        }
      }
    }
  }
}

function getSubElementsByClass(element, classname) {
  var children = element.childNodes;
  var elementsByClass = new Array();
  var index = 0;
  for(var i in children) {
    if(children[i].nodeType == 1) {
      if(children[i].className.indexOf(classname) >= 0) {
        elementsByClass[index] = children[i];
        index++;
      }
    }
  }
  return elementsByClass;
}

function getStyleValue(el, prop) {
  var styleValue = null;
  if(el.currentStyle) {
    styleValue = el.currentStyle[prop];
  }
  else {
    if(window.getComputedStyle) {
      styleValue = document.defaultView.getComputedStyle(el, null).getPropertyValue(prop);
    }
  }
  return styleValue;
}


