// global.js for stepback.ca
// site-wide functionality

/**********************************************************************
   Enlargement images
 *********************************************************************/

function preloadEnlargements() {
   if (isDefined(document.getElementsByTagName)) {
      
      // for each 'enlarge' link...
      links = document.getElementsByTagName('a');
      for (var linkIndex = 0; linkIndex < links.length; linkIndex++) {
         var linkElement = links[linkIndex];
         if (linkElement.className.indexOf('enlarge') > -1) {

            // preload image
            (new Image()).src = linkElement.href;
            
            // create a 'span' tag with the same content as the 'a' tag
            // (prevents the default action of the 'a' tag but still allows enlargement in non-js browsers)
            var spanElement = document.createElement('span');
            for (var linkChildrenIndex = 0; linkChildrenIndex < linkElement.childNodes.length; linkChildrenIndex++) {
               spanElement.appendChild(linkElement.childNodes[linkChildrenIndex].cloneNode(true));
            }
            spanElement.className = 'enlarge clickable';
            spanElement.href = linkElement.href;
            
            // attach onclick event to 'span' tag
            addEvent(spanElement, 'click', function() { showEnlargement(this, this.href); }, false);
            
            // replace 'a' tag with 'span'
            linkElement.parentNode.appendChild(spanElement);
            linkElement.style.display = 'none';
         }
      }
   }
}

function showEnlargement(thumbElement, enlargementImage) {

   // fix thumbElement reference in IE
   if (thumbElement == window) {
      thumbElement = event.srcElement.parentNode;
      enlargementImage = thumbElement.href;
   }

   var enlargementBox = document.getElementById('enlargement');
   
   // clean the enlargement box of any old image
   if (enlargementBox.firstChild) {
      enlargementBox.removeChild(enlargementBox.firstChild);
   }
   
   // put new enlargement image into enlargement box
   var imageElement = document.createElement('img');
   imageElement.src = enlargementImage;
   enlargementBox.insertBefore(imageElement, enlargementBox.firstChild);
   
   // position and show enlargement box
   enlargementBox.style.top = getOffsetTop(thumbElement, true) + 'px';
   enlargementBox.style.left = getOffsetLeft(thumbElement, true) + 'px';
   enlargementBox.style.display = 'block';
}

function hideEnlargement() {
   var enlargementBox = document.getElementById('enlargement');

   // hide enlargement box
   enlargementBox.style.display = 'none';
   
   // remove old enlargement image from enlargement box
   enlargementBox.removeChild(enlargementBox.firstChild);
}


/**********************************************************************
   Utilities
 *********************************************************************/

function isDefined(property) {
  return (typeof property != 'undefined');
}

// Simple cross-browser event attachment from scottandrew.com
function addEvent(obj, evType, fn, useCapture) {
   if (obj.addEventListener) {
      obj.addEventListener(evType, fn, useCapture);
      return true;
   }
   else if (obj.attachEvent) {
      var r = obj.attachEvent("on"+evType, fn);
      return r;
   }
}

function getOffsetTop(element, deep) {
   return getOffsetProperty(element, 'Top', deep);
}

function getOffsetLeft(element, deep) {
   return getOffsetProperty(element, 'Left', deep);
}

function getOffsetProperty(element, property, deep) {
   var offsetValue = 0;
   offsetProperty = 'offset' + property;
   
   do {
      offsetValue += element[offsetProperty];
      element = element.offsetParent;
   } while (deep == true && element != document.body && element != null);
   return offsetValue;
}


// call preloadEnlargements when document finishes loading
window.addEvent(window, 'load', preloadEnlargements, false);

