/* site specific scripts */

function siteInit(){
  // Called with the body onload event.
  // Customise according to site requirements.
  if(navigator.userAgent.indexOf('MSIE') >= 0) {
    fixExplorerCornersBug();
  }
  setLinkClicker();
}

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

function setLayout() {
  // Not required.
}

function redrawLayout() {
  // Not required.
}

function printPageComponent() {
  html  = '<button class="button" \n';
  html += '        id="print-page-button" \n';
  html += '        onclick="window.print();" \n';
  html += '        onmouseover="this.className = \'button over\';" \n';
  html += '        onmouseout="this.className = \'button\';" \n';
  html += '        onmouseup="this.blur();" \n';
  html += '        title="Print the current page." \n';
  html += '        type="button">';
  html += '  <span>Print</span>\n';
  html += '  <span class="accessHidden">this page</span>';
  html += '</button>';
  return html;
}

function fixExplorerCornersBug() {
  var divs = document.getElementById("page").getElementsByTagName("div");
  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 || divs[i].className.indexOf("nestedRoundedElement") >= 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") {

      childBottom = child.offsetTop + child.offsetHeight;
      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 hoverHelp(el,action) {
  switch(action) {
    case 0: el.className = "hoverHelp hhClose";
            break;
    case 1: el.className = "hoverHelp hhOpen";
            break;
    default : ;
  }
}

SIZE_DOWN_MSG = "Aa Change size";
SIZE_UP_MSG = "Aa Change size";
function displayFontSwitch() {
  document.write("<a href=\"#top\" \n");
  document.write("   id=\"font-switch\" \n");
  document.write("   onclick=\"switchFontSize(this)\" \n");
  document.write("   onmouseup=\"this.blur();\"> \n");
  if(getCookie("baseFontSize") > 0) {
    document.write(SIZE_DOWN_MSG);
  }
  else {
    document.write(SIZE_UP_MSG);
  }
  document.write("</a>");
}


function printPageComponent() {
  html  = '<a href="#top"';
  html += '   id="print-page"';
  html += '   onclick="window.print();" \n';
  html += '   onmouseup="this.blur();" \n';
  html += '   title="Print the current page."> \n';
  html += '  Print<span class="accessHidden"> this page</span> \n';
  html += '</a> \n';
  return html;
}

function addToClass(el,str) {
  currentClass = el.className;
  if(currentClass.length <= 0) {
    // Class is empty so stick str in
    el.className = str;
  }
  else {
    // Class not empty so make sure to put in preceding space
    el.className = currentClass + " " + str;
  }
}

function removeFromClass(el,str) {
  currentClass = el.className;
  // Do twice to catch any whitespace variants first.
  var withSpace = " " + str;
  el.className = currentClass.replace(withSpace,"");
  el.className = currentClass.replace(str,"");
}

function addStatusLink(str) {
  window.status = str;
}

function removeStatusLink() {
  window.status = "";
}

function setLinkClicker() {
 // Function looks through all links on a page and a
 // javascript call to link_click function if external
 var link;
 var domain = document.domain;
 var ext = [".doc",".xls",".exe",".zip",".pdf", "downloadApplicationForm"];

 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.
   // Set the target based on domain matching.
   if(link.hostname && link.hostname != domain) {
     // external
      addCrossBrowserEvent(link,['click','onclick'],link_ext_click,false);
   } else if (link.href) {
      if (link.href.indexOf('mailto')>-1) {
         addCrossBrowserEvent(link,['click','onclick'],link_mailto_click,false);
      } else {
         for (var i in ext) {
            if (link.href.indexOf(ext[i])>-1) {
               addCrossBrowserEvent(link,['click','onclick'],link_download_click,false);
            }
         }
      }
   }
  }
}

function link_ext_click() { linkTrack('external'); }
function link_download_click() { linkTrack('download'); }
function link_mailto_click() { linkTrack('mailto'); }

function linkTrack(category) {
    if (current_browser.isIE) {
        loc=window.event.srcElement.href;
    }
    else {
        loc=this.href;
    }
    pageTracker._trackPageview ('/' + category + '/'+ loc);
}

