
/**
 * Common JavaScript Functions
 *
 * This file contains JavbaScript functions that are used on a regular basis
 *
 * This file is included in every Header Template since March 13th, 2007 and
 * all its containing functions can be used in every script
 *
 * @copyright 2007 Occhio Design
 * @author    Gijs Wilbrink    
 */


/**
 * Add Load Event
 *
 * Adds a function to the window.onload event without overwriting it
 * 
 * @param  function	func	The function name or declaration that needs to be called on window load
 */ 

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


/**
 * Add Unoad Event
 *
 * Adds a function to the window.onunload event without overwriting it
 * 
 * @param  function	func	The function name or declaration that needs to be called on window unload
 */ 

function addUnloadEvent(func) {
  var oldonunload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  } else {
    window.onunload = function() {
      if (oldonunload) {
        oldonunload();
      }
      func();
    }
  }
}

/**
 * Get Elements By ClassName
 *
 * Gets all elements that have a certain class name 
 *
 * @param  object	oElm			The parent DOM object
 * @param  string	strTagName		The tagname of the wanted DOM objects. Can be "*" for all elements
 * @param  string	strClassName	The wanted class name
 * @return array	An array with DOM objects
 */ 

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	  var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

/**
* Open a link in a external window by adding rel="external" to your link
* http://www.sitepoint.com/article/standards-compliant-world/3
* 
* Before:
*   <a href="document.html" target="_blank">external link</a>
* After:
*   <a href="document.html" rel="external">external link</a>
*/
function externalLinks() 
{
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
}

/*
  Augment an object by replacing its key:value pairs with those
  from other object(s), and adding pairs from other object(s) that don't
  exist in you.  Key:value pairs from later objects will
  overwrite those from earlier objects.
  
  If null is given as the initial object, a new one will be created.
  
  This mutates and returns the object passed as oSelf. The other objects are not changed.
*/
function augment (oSelf, oOther) {
    if (oSelf == null) {
        oSelf = {};
    }
    for (var i = 1; i < arguments.length; i++) {
        var o = arguments[i];
        if (typeof(o) != 'undefined' && o != null) {
            for (var j in o) {
                oSelf[j] = o[j];
            }
        }
    }
    return oSelf;
}

// replace all rel="external" in the website by  target="_blank"
addLoadEvent(externalLinks);