/* PAGE INITIALIZATION FUNCTIONS */
var addEvent = function(obj, type, fn) { //http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/
  if (obj.addEventListener) {
    obj.addEventListener(type, fn, false);
  } else if (obj.attachEvent) {
    obj.attachEvent('on' + type, function() { return fn.apply(obj, new Array(window.event));});
  }
}
addEvent(window, 'load', preProcessPage);

function preProcessPage() {
	if (!document.getElementsByTagName || (!document.getElementById || (!document.createElement || !document.createTextNode))) return false;
	processPage();
}

function processPage() {
  //page hooks go here
  processBlocks();
  processFormInputs();
  processLinks();
}

/* INDIVIDUAL ELEMENT INITIALIZATIONS */
function processLinks() {
  var theAnchors = document.getElementsByTagName('a');
  var fixAnchor = function(theElement) {
    if (theElement.getAttribute('name')) {
      setAnchor(theElement);
    }
  }
  forEach(theAnchors,fixAnchor);
}
function processFormInputs() {
  var theFormInputs = document.getElementsByTagName('input');
  var fixInput = function(theElement) {
    if (theElement.getAttribute('type') && (theElement.getAttribute('type') == 'submit')) {
      var newLink = document.createElement('a');
      newLink.setAttribute('href','#');
      newLink.className = theElement.className;
      addClass(newLink,'image_button');
      var link_inner = document.createElement('span');
      if (theElement.className && /noText/.test(theElement.className)) {
        link_inner.innerHTML = "&nbsp;";
        link_inner.className = 'noText';
      } else {
        link_inner.innerHTML = theElement.getAttribute('value');
      }
      newLink.appendChild(link_inner);
      newLink.onclick = function() {
        return doFormSubmit(this);
      }
      addClass(theElement,'no_show');
      theElement.parentNode.insertBefore(newLink,theElement);
    }
  }
  forEach(theFormInputs,fixInput);
}
function doFormSubmit(theElement) {
  var parentForm = getParentElement(theElement, 'form');
  if (parentForm.nodeName.toLowerCase() == 'form') {
    parentForm.submit();
    return false;
  }
}
function processBlocks() {
  if (document.getElementById('blocks')) {
    var blockHolder = document.getElementById('blocks');
    var allBlocks = new Array();
    var holderChildren = blockHolder.getElementsByTagName('div');
    var maxHeight = 0;
    var checkBlock = function(theElement) {
      if (theElement.className && /block_inner/.test(theElement.className)) {
        allBlocks.push(theElement);
        var thisHeight = theElement.offsetHeight;
        if (thisHeight > maxHeight) {
          maxHeight = thisHeight;
        }
      } 
    }
    forEach(holderChildren,checkBlock);
    var setBlock = function(theElement) {
      theElement.style.minHeight = maxHeight + 'px';
    }
    forEach(allBlocks,setBlock);
  } else if (document.getElementById('main_content_blocks')) {
    var blockHolder = document.getElementById('main_content_blocks');
    var allBlocks = new Array();
    var holderChildren = blockHolder.getElementsByTagName('div');
    var maxHeight = 0;
    var checkBlock = function(theElement) {
      if (theElement.className && /block/.test(theElement.className)) {
        allBlocks.push(theElement.getElementsByTagName('div')[0]);
        var thisHeight = theElement.offsetHeight;
        if (thisHeight > maxHeight) {
          maxHeight = thisHeight;
        }
      } 
    }
    forEach(holderChildren,checkBlock);
    var setBlock = function(theElement) {
      theElement.style.minHeight = maxHeight + 'px';
    }
    forEach(allBlocks,setBlock);
  }
}
function setAnchor(theElement) {
  var thePayload = document.createTextNode(theElement.innerHTML);
  theElement.innerHTML = '';
  theElement.parentNode.insertBefore(thePayload,theElement);
}

/* UTILITY FUNCTIONS */
function forEach(array, action) {
  for (var i=0; i < array.length; i++) {
    action(array[i]);
  }
}

function removeChildNodes(theElement) {
  while (theElement.hasChildNodes()) {
    theElement.removeChild(theElement.firstChild);
  }
}

function getParentElement(theElement, targetNodeName) {
  var bodyNode = document.getElementsByTagName("body")[0];
  var currentParent = theElement;
  var targetNodeName = targetNodeName.toLowerCase();
  while ((currentParent.nodeName.toLowerCase() != targetNodeName) && (currentParent != bodyNode)) {
    currentParent = currentParent.parentNode;
  }
  return currentParent;
}

function stripWhitespace(theString) {
	return theString.replace(/^\s*|\s*$/g,'');
}

function addClass(theElement,theClass) {
	if (!theElement.className) {
		theElement.className = theClass;
	} else if (theElement.className.indexOf(theClass) == -1) {
		theElement.className += (" " + theClass);
	}
}

function removeClass(theElement,theClass) {
  if (theElement.className && theElement.className.indexOf(theClass) != -1) {
    theElement.className = stripWhitespace(theElement.className.replace(theClass,""))
  }
}

function hasClassHook(theElement) {
  return (theElement.className && /js_/.test(theElement.className));
}

function getExtensionFromClass(theElement,theKey) {
	var theExtension = false;
	if (theElement.className) {
    var findExtension = function(thisClass) {
      if (thisClass.indexOf(theKey) == 0) {
        var tempName = thisClass.substring(theKey.length);
        if (tempName.length > 0) {
          theExtension = tempName;
        }
      }
    }
    forEach(theElement.className.split(' '),findExtension);
  }
	return theExtension;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
  }
  return [curleft,curtop];
}
