/*	This script adds some usefull functions */

function general(){
	// Open links in a new window
	externalLinks();
	// Add this class to all links who open in a new window
	addNewWindow("introduction","newWindow");
}

/*
Requires: addLoadEvent
How to use: add the class "newWindow" to links with the rel="external" attribute.
*/
function addNewWindow(element,newClass){
	var content = document.getElementById(element);
	var links = content.getElementsByTagName("a");
	
	// Loop through the links
	for (var i=0; i < links.length; i++) {
		if(links[i].getAttribute("rel") == "external"){ // If the attribute "rel" is present
			links[i].setAttribute("class", newClass); // Set the attribute "class" to "newWindow"
		};
	};
}

/* Addloadevent to load functions when DOM ready */
/* How to use: addloadEvent(functionname); */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/* Open links with the rel="external" in a new window */
/* How to use: add the rel="external to a link - <a href="#" rel="external"> */
function externalLinks(){
	if (!document.getElementsByTagName) return false; // Check if the DOM is supported
	var links = document.getElementsByTagName("a"); // Get all the links
	for (var i=0; i < links.length; i++) {
		if (links[i].getAttribute("rel") == "external") { // If a link has the attribute rel="external"
			links[i].onclick = function(){
				window.open(this.getAttribute("href")); // Open it in a new window
				return false;
			}
		};
	};
}

/* Load all functions */
addLoadEvent(general);
