// Isaac Z. Schlueter
// Make outside links pop up in other windows, in a strict standards-compliant way.

// Put your domain here.  URLs inside this domain won't be touched by this function.
// Don't put a http://.  No trailing slash.
// If you use a subdomain, like 'mysite.example.com' or even www., then other subdomains at this domain name will pop up.
// Separate multiple same-window URLs with a pipe |
// var myBaseUrl = 'localhost/isaacschlueter|localhost/tests|geocities.com/bryht|isaacschlueter.com';
var myBaseUrl = 'thomassito.com|tomsito.com';
//var myBaseUrl = 'localhost';


// the rel attrib value that we should use for popping up. Links to outside URLs will pop up, as will any links
// with this REL, and this REL will be added to any external links that don't already have a REL.
var poppinRel = 'external';


// stop editing if you don't know what you're doing.  
// If you want to see how the code is actually executed, then go to the end.

// original concept by Porter Glendinning http://www.serve.com/apg/workshop/replacingTarget/better.html
// modified by Isaac Z. Schlueter http://isaacschlueter.com to be a bit cooler and regexish and more x-browser happy
myBaseUrl = myBaseUrl.match('(https?://)?(.*)')[2];
function makeEmPoppin() {
	// Fetch all the links in the document.
	if( document.links ) var links = document.links;
	else var links = document.getElementsByTagName('a');

	// loop through all links.
	for (var i=0;links[i];i++) {
		var a = links[i];
		
		// If the element doesn't have an href, skip it.
		if (!a.href) continue;
		
		// handle it.  muchas gracias, Regular-Expressions.info.  
		// http://www.regular-expressions.info/javascriptexample.html
		if ((!a.href.indexOf('http://') && !a.href.match('^http://([^.]+[.])?(' + myBaseUrl + ')')) || (!a.href.indexOf('https://') && !a.href.match('^https://([^.]+[.])?(' + myBaseUrl + ')')) || a.getAttribute('rel') == poppinRel) {
			if( !a.rel ) a.setAttribute('rel',poppinRel);
			addEvent(a,'click',PopWin);
		}
	}
}

function PopWin(e) {
	if (window.event && window.event.srcElement) a = window.event.srcElement
	else if (e && e.target) a = e.target
	if (!a) return;

	a = getParent(a,'a');
	// Open a new window with the link's href.
	var newwin = window.open(a.href);

	// The thought is that if the new window didn't (popups blocked or whatever) 
	// we want to return true so the link is follow normally.
	if( newwin ) 
	{
		if( e.returnValue ) e.returnValue = false;
		if( e.preventDefault ) e.preventDefault();
		return false;
	}
	else return true;
}

function getParent(el, pTagName)
{
	if (el == null) return null;
	else if(el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) return el;
	else return getParent(el.parentNode, pTagName);
}

// by Scott Andrew
// add an eventlistener to browsers that can do it somehow.
function addEvent(obj, evType, fn)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(evType, fn, true);
		return true;
	}
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent('on'+evType, fn);
		return r;
	}
	else return false;
}

// done defining, now attach code.
addEvent(window,'load',makeEmPoppin);
