/* site specific scripts */

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

  if(current_browser.version == "5.01" || current_browser.version == "5.02") {
    // do nothing, they're not very good.
  }
  else {  
    adjustPrimaryNavigation();

    if(document.body.className.indexOf("HomePage") >= 0) {
      adjustTeasers();
    }
    if(document.body.className.indexOf("GalleryPage") >= 0) {
      adjustGalleryPhotos();
    }  
    if(document.body.className.indexOf("ShopFrontPage") >= 0 || document.body.className.indexOf("ShopCategoryPage") >= 0) {
      adjustShopPageLayout();
    }

    if(current_browser.isIE) {
      fixExplorerCornersBug();
    }
  }
  redrawLayout();
}

function showHelp(link,element) {
  document.getElementById("main-footer").style.zIndex = "1";
  helpElement = document.getElementById(element);
  helpElement.style.display = "block";
  if(link.offsetLeft)
    helpElement.style.left = link.offsetLeft;
  helpElement.style.position = "absolute";
  if(link.offsetTop)
    helpElement.style.top = link.offsetTop;
}
 
function hideHelp(element) {
  helpElement = document.getElementById(element);
  helpElement.style.display = "none";
  document.getElementById("main-footer").style.zIndex = "40";
}

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

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(child.className.indexOf("bgOne") >= 0) {
    }
    if(child.className.indexOf("bgTwo") >= 0) {
      childRight = elWidth - (child.offsetWidth + child.offsetLeft);
      newChildRight = getStyleValue(child, "right").replace("px","") - 1;
      if(childRight > 0) {
        child.style.right = newChildRight + "px";
      }
    }
    if(child.className.indexOf("bgThree") >= 0) {
      childBottom = child.offsetTop + child.offsetHeight;
      if(childBottom < elHeight) {
        newChildBottom = getStyleValue(child, "bottom").replace("px","") - (elHeight - childBottom);
        child.style.bottom = newChildBottom + "px";
      }
    }
    if(child.className.indexOf("bgFour") >= 0) {
      if(childRight > 0) {
        child.style.right = newChildRight + "px";
      }
      if(newChildBottom) {
        child.style.bottom = newChildBottom + "px";
      }
    }
  }
}

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) {
        roundedElements.push(divs[i]);
      }
    }
  }

  for(var i in roundedElements) {
    adjustCorners(roundedElements[i]);
  }
}

function adjustPrimaryNavigation() {
  var divs = document.getElementsByTagName("div");
  var primaryNavigation = document.getElementById("primary-navigation");
  var navigation = new Array();
  var navigationPadding = 7;
  for(var i in divs) {
    if(typeof divs[i] == "object" && divs[i] != null) {
      if(divs[i].className) {
        if(divs[i].className.indexOf("primaryNavLink") >= 0) {
          navigation.push(divs[i]);
        }
      }
    }
  }
  if(navigation.length > 0) {
    var maxHeight = getMaximumHeight(navigation);
    var navigationHeight = Number(navigation[0].offsetHeight);
    for(var a in navigation) {
      if(Number(navigation[a].offsetHeight) < maxHeight) {
        navigation[a].style.height = Number(maxHeight - navigationPadding) + "px";
      }
    }

    if(navigationHeight < maxHeight) {
      var heightDifference = maxHeight - navigationHeight;
      var moveSections = ['main-background', 'breadcrumbs', 'main-left', 'main-content', 'main-right'];
      var currentSection = null;
      var currentSectionTop = 0;
      var newTop = 0;
      for(var i in moveSections) {
        currentSection = document.getElementById(moveSections[i]);
        if(typeof currentSection == "object" && currentSection != null) {
          currentSectionTop = Number(currentSection.offsetTop);
          newTop = currentSectionTop + heightDifference;
          currentSection.style.marginTop = "0";
          currentSection.style.top = newTop + "px";
        }
      }
    }

  }
}

function adjustTeasers() {
  var teaserContentArea = document.getElementById("teaser_content");
  if(teaserContentArea) {
    var teasers = getSubElementsByClass(teaserContentArea, "teaser");
    var height = 0;
    var partnerOne = null;
    var partnerOneHeight = 0;
    var partnerTwo = null;
    var partnerTwoHeight = 0;
    var padding = 15 + 8;
    for(var i in teasers) {
      if(i==0 || i%2==0) {
        partnerOne = teasers[i];
        partnerOneHeight = partnerOne.offsetHeight - padding;
        if(++i < teasers.length) {
          partnerTwo = teasers[i];
          partnerTwoHeight = partnerTwo.offsetHeight - padding;
          if(partnerTwoHeight < partnerOneHeight) {
            partnerTwo.style.height = partnerOneHeight + "px";
          }
          else {
            if(partnerOneHeight < partnerTwoHeight) {
              partnerOne.style.height = partnerTwoHeight + "px";
            }
          }
        } 
      }
    }
  }
}

function adjustShopPageLayout() {
  var divElements = document.getElementsByTagName("div");
  var productRows = new Array();
  var products = new Array();
  var summaries = new Array();
  var maxHeight; 

  for(var i in divElements) {
    if(divElements[i].className == "productRow") {
      productRows.push(divElements[i]);
    }
  }
  for(var a in productRows) {
    maxHeight = 0;
    summaries = [];
    products = getSubElementsByClass(productRows[a], "product");
    for(var b in products) {
      summaries.push(getSubElementByClass(products[b], "productSummary"));
    }
    if(summaries.length > 0) {
      maxHeight = getMaximumHeight(summaries);
      if(maxHeight > 0) {
        for(var c in summaries) {
          summaries[c].style.height = maxHeight + "px";
        }
      }
    }
  }
}

function adjustGalleryPhotos() {
  var galleryPhotoArea = document.getElementById("gallery_photos");
  if(galleryPhotoArea) {
    var galleryPhotos = getSubElementsByClass(galleryPhotoArea, "galleryPhoto");
    var maxElementHeight = getMaximumHeight(galleryPhotos);
    var count = 0;
    for(var i in galleryPhotos) {
      // allow for rubbish browser handling on float.
      galleryPhotos[i].style.styleFloat = "left";
      galleryPhotos[i].style.cssFloat = "left";
      galleryPhotos[i].setAttribute("style","float:left");

      galleryPhotos[i].style.height = maxElementHeight + "px";
      if(++count % 3 != 0) {
        galleryPhotos[i].style.marginRight = "8px";
      }
    }
  }
}

function generateNickname(firstname, nickname){
    name = document.getElementById(firstname)
    nick = document.getElementById(nickname)
    nick.value = name.value
}

function calculateGiftaid(donation_amount){
    donation_amount = document.getElementById(donation_amount)
    gift_aid = document.getElementById('gift')
    gift_aid.value = donation_amount.value * 1.22
}

SIZE_UP_MSG = "AA text size";
SIZE_DOWN_MSG = "AA text size";
function displayFontSwitch(identifier) {
  document.write('<a href="#"\n');
  document.write('   id="font-switch-' + identifier + '"\n');
  document.write('   class="fontSwitch"\n');
  document.write('   onclick="switchFontSize(this); return false;">\n');
  if(getCookie("baseFontSize") > 0) {
    document.write(SIZE_DOWN_MSG);
  }
  else {
    document.write(SIZE_UP_MSG);
  }
  document.write('</a>\n');
}

function switchFontSize(element) {
  var fontSizeSwitches = new Array();
  var fontSwitch = null;
  var fcv = null;
  for(i=1; i < 2; i++) {
    name = "font-switch-" + i;
    fontSwitch = document.getElementById(name);
    if(fontSwitch) {
      fontSizeSwitches.push(fontSwitch);
    }
    else {
      break;
    }
  }
  if(Number(getCookie("baseFontSize"))) {
    fcv = 0;
    for(var i in fontSizeSwitches) {
      fontSizeSwitches[i].firstChild.nodeValue = SIZE_UP_MSG;
    }
  }
  else {
    fcv = 1;
    for(var i in fontSizeSwitches) {
      fontSizeSwitches[i].firstChild.nodeValue = SIZE_DOWN_MSG;
    }
  }
  setCookie("baseFontSize", fcv, null, "/");
  setBaseFontSize(fcv);
  redrawLayout();
}
