/* site specific scripts */

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

function getSections() {
  body = document.getElementsByTagName("body")[0];

  sections = new Array();
  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');
  /*if(body.className.indexOf("HomePage") > 0) {
    sections[6] = document.getElementById('search-box');
  }*/

  return sections;
}

function setLeftHeight(){
  sections = getSections();
  height = sections[5].offsetTop - 200;
  sections[1].style.height = height+'px';
  sections[5].style.visibility = 'visible';
}

/*function redrawLayout() {
  var specialResets = new Array ();
  specialResets[0] = document.getElementById("page");
  resetSectionHeights(getSections());
  resetSectionHeights(specialResets);
  setLayout();
}*/

function addMoreLink(id){
  var ele = $(id);
  var container = ele.getOffsetParent();
  if(hasOverflow(container, ele)){
    var content = createLink('unclip', 'scrollContent', 'moreLink', 'More');
    container.insert(content);
  }
}

function addDownLink(id){
  var ele = $(id);
  var container = ele.getOffsetParent();
  var content = createLink('scrollDown', 'scrollContent', 'downLink', 'Up');
  container.insert(content);
  hideLink(ele, 'downLink');
}

function createLink(func, element, klass, link_title){
  var content = '<a href="#" class="'+klass+'" onclick="'+func+'(\''+element+'\')">'+link_title+'</a>';
  return content;
}

function unclip(id){
  var ele = $(id);
  var container = ele.getOffsetParent();
  var move = container.getHeight() - 10;
  if(hasOverflow(container, ele)){
    new Effect.Move(id,{x:0, y: -move});
  }

  var con_bottom = getBottom(container);
  var new_bottom = getBottom(ele) - move;
  if(new_bottom <= con_bottom){
    hideLink(ele, 'moreLink')
  }

  if(hasScrolled(id)){
    showLink(ele, 'downLink');
  }
}

function hasScrolled(id){
  var ele = $(id);
  var container = ele.getOffsetParent()
  var current_top = ele.cumulativeOffset()[1]
  var new_top = current_top + container.getHeight() - 10
  var container_top = container.cumulativeOffset()[1]
  if(new_top == container_top){
    return false;
  } else {
    return true;
  }
}

function scrollDown(id){
  var ele = $(id);
  var container = ele.getOffsetParent();
  var move = container.getHeight() - 10;
  new Effect.Move(id,{x:0, y:move});

  if(!hasScrolled(id)){
    hideLink(ele, 'downLink');
  }
  if((getBottom(ele)+move) > getBottom(container)){
    showLink(ele, 'moreLink');
  }
}

function showLink(element, link_class){
  var link = element.next('a.'+link_class);
  link.show()
}

function hideLink(element, link_class){
  var link = element.next('a.'+link_class);
  link.hide()
}

function hasOverflow(container, element){
  var con_height = container.getHeight();
  var con_bottom = getBottom(container);
  var ele_bottom = getBottom(element);
  if(ele_bottom > con_bottom){
    return true;
  } else {
    return false;
  }
}

function getBottom(element){
  var bottom = element.cumulativeOffset();
  return bottom[1] + element.getHeight();
}

