/*
~~~~~~~~~~~( CAFFEINE FUELLED Link Fixer )~~~~~~~~~~~
copyright Richard Meredith, www.caffeinefuelled.net
Feel free to use this script on any site you want, but 
please keep the copyright notice in place. Let me know if
you find an problems with it.

This script automagically fixes links that are like:
	<a href="www.google.com">google</a>
and adds the http:// protocol to the front:
	<a href="http://www.google.com">google</a>
To /help/ prevent embarrasing 404 errors

It then goes and edits all of your external links to make them open in a new window, 
and adds a user-defined message to the title text (so people know it is a popup)

It will also open local files, that match a certain path, in a new window, ie, any file
in the "/examples/" directory (or deeper - "/examples/some/more/directories/filename.html")
You can specify as many of these paths as you want.

Additionally, you can add a class that will open the links in new window.  This class is user
defined as well, and you can also have multiple classes on the links:
Assuming your class is "popper":
<a href="file.html" class="popper">link</a>
<a href="file.html" class="popper ProperClass">

The second version will open in a new window, but will also take the CSS styles for "ProperClass"
*/


//your title message, that will be added onto the existing html one:
//set to "" if you don't want a bonus message
var titleText="(öppnas i nytt fönster)"

//this is an array of internal paths that will open in a new window
//add as many as you want to the list.
//IMPORTANT: include the leading slash!!
var extPath = new Array(
	"/examples/",
	"/dummypath/"
)
//enter the class here, use "" if you want this feature disabled
var linkClass="popper";



//don't edit below here ;)
var myHost=window.location.hostname;
var myPath=removeFileName(window.location.pathname);

var testString = "linkHost!=myHost || classOpen ";
for (whichPath=0;whichPath<extPath.length;whichPath++){
	testString +=  "|| linkPath.substr(0,"+extPath[whichPath].length+")=="+extPath[whichPath];
}
function fixLinks() {
	for(whichLink=0;whichLink<document.links.length;whichLink++){
		var linkPath=document.links[whichLink].pathname;
		if(document.all){linkPath="/"+linkPath}
		if(linkPath.substring(myPath.length,myPath.length+3)=="www"){
			document.links[whichLink].href=linkPath="http://"+linkPath.substring(myPath.length,linkPath.length);
		}
		var linkHost=document.links[whichLink].hostname;
		var classOpen=checkClass(document.links[whichLink].className);
		if(eval(testString)){
			document.links[whichLink].onclick=doOpen;
			document.links[whichLink].title+=" "+titleText;
		}
	}
}
function removeFileName(thePath){
	if(thePath.substring(thePath.length-1,thePath.length)!="/"){
		var chunks=(thePath+"*").split("/"), thePath="";
		for(whichChunk=0;whichChunk<chunks.length-1;whichChunk++){
			thePath+=chunks[whichChunk]+"/";
		}
	}
	return thePath;
}
function checkClass(testClass){
	var retVal=false
	if(testClass!=""){
		var indClass=testClass.split(" ");
		for(whichClass=0;whichClass<indClass.length;whichClass++){
			 if(indClass[whichClass]==linkClass){retVal=true}
		}
	}
	return retVal;
}
function doOpen() {	window.open(this.href);	return false;}

window.onload = fixLinks;