/*********************************************************************
 * Global JavaScript file for paulhefford.com                        *
 * Please see <http://www.paulhefford.com/smallprint> for terms      *
 *********************************************************************/

/* Appends an additional function 'newFunction' to the 'onload' event of the document */
function addLoadEvent(newFunction) {
 var oldOnLoad = window.onload;
 if (typeof oldOnLoad === 'function') {
  window.onload = function() {
   oldOnLoad();
   newFunction();
  };
 } else {
  window.onload = newFunction;
 }
}

/* Adds the element 'newSibling' as the next sibling of the element 'target'. Compliments the standard 'insertBefore' function */
function insertAfter(newSibling, target) {
 var parent = target.parentNode;
 if (parent.lastChild === target) {
  parent.appendChild(newSibling);
 } else {
  parent.insertBefore(newSibling, target.nextSibling);
 }
}

/* Adds the class 'value' to the element 'target' */
function addClass(target, value) {
 if (target.className) {
  target.className = target.className + ' ' + value;
 } else {
  target.className = value;
 }
}

/* Identical to the standard function 'getElementsByTagName' except the return value is not a reference to the document */
function getCopyOfElementsByTagName(tagname, node) {
 if (typeof node === 'undefined') {
  node = document;
 }

 var elements = node.getElementsByTagName(tagname);
 var result = [];
 for (var i = 0; i < elements.length; i++) {
  result.push(elements[i]);
 }

 return result;
}

/* Adds various events to the element with the ID 'targetId', for example: image rollover events */
function addButtonImageEvents(targetId, imgPrefix) {
 var target = document.getElementById(targetId);
 if (!target) {
  return false;
 }

 var imgOn		= new Image();
 var imgOff		= new Image();
 var imgDown	= new Image();

 imgOn.src		= '/images/' + imgPrefix + '-on.gif';
 imgOff.src		= '/images/' + imgPrefix + '-off.gif';
 imgDown.src	= '/images/' + imgPrefix + '-down.gif';

 target.onmouseover	= function() {target.src = imgOn.src;};
 target.onmouseout	= function() {target.src = imgOff.src;};
 target.onmousedown	= function() {target.src = imgDown.src;};
 target.onmouseup	= function() {target.src = imgOn.src;};
}

/* Adds the class 'external' to all links which point to an external resource */
function addExternalLinkClass() {
 var links = document.getElementsByTagName('a');

 for (var i = 0; i < links.length; i++) {
  var linkurl = links[i].getAttribute('href');
  if (linkurl.substring(0, 4) === 'http') {
   addClass(links[i], 'external');
  }
 }
}

/* Adds various events to the search submit button in the main menu, e.g.: image rollover events */
function addSearchButtonImageEvents() {
 addButtonImageEvents('menuSearchSubmit', 'search');
}

/* Initialises Google Analytics */
function gaInit() {
 if (typeof _gat === 'undefined') {
  return false;
 }

 var pageTracker = _gat._getTracker('UA-1118200-1');
 if (!(_gat.hasFired)) {
  pageTracker._trackPageview();
  _gat.hasFired = true;
 }
}

/* Adds the Google Analytics code to the page */
function addGaStatistics() {
 var targetTags = document.getElementsByTagName('head');
 if (targetTags.length < 1) {
  return false;
 }

 var gaJs = document.createElement('script');
 gaJs.setAttribute('type', 'text/javascript');
 gaJs.setAttribute('src', 'http://www.google-analytics.com/ga.js');
 gaJs.onload = gaInit;

 /* IE workaround, as 'onload' event on 'script' element does not fire. */
 gaJs.onreadystatechange = function() {
  if ((gaJs.readyState === 'loaded') || (gaJs.readyState === 'complete')) {
   gaInit();
  }
 };

 targetTags[0].appendChild(gaJs);
}

/* Add various functions to the 'onload' event of the document */
//addLoadEvent(addExternalLinkClass);
addLoadEvent(addGaStatistics);
addLoadEvent(addSearchButtonImageEvents);