﻿function initExternalLinks(e) {

	// Fetch all the a elements in the document.
	var links = document.getElementsByTagName('a');
	
	// Loop through the a elements in reverse order
	// for speed.
	for (var i = links.length; i != 0; i--) {
	
		// Pull out the element for this iteration.
		var a = links[i-1];
	
		// If the element doesn't have an href, skip it.
		if (!a.href) continue;
	
		// If the url does not contain "http://"
		//if (!a.href.indexOf('http:\/\/') && (a.href.indexOf('http:\/\/www.imcstudios.com\/') && a.href.indexOf('http:\/\/imcstudios.com\/'))) a.onclick = PopWin;
		if (!a.href.indexOf('http:\/\/') && (a.href.indexOf('http:\/\/www.imcstudios.com\/') && a.href.indexOf('http:\/\/imcstudios.com\/'))) {
			/*
			emod = (e) ? (e.eventPhase) ? "W3C" : "NN4" : (window.event) ? "IE4+" : "unknown";
			
			switch (emod)
			{
				case "IE4+":
					a.onclick = PopWin;
					break;
				case "W3C":
					//a.addEventListener('click', PopWin, false);
					a.onclick = PopWin;
					break;
				default:
					a.onclick = PopWin;
					break;								
			}
			*/
			a.onclick = PopWin;
		
		}
	}
}

function PopWin(e) {
	
	// Accommodate IE's non-standard event handling.
	if (!e) var e = window.event;
	var a = e.target ? e.target : e.srcElement;
	
	//if (a == '[object]' || a == 'img')
	if(a.tagName != 'A')
	{
	 	a = e.target ? e.target.parentNode : e.srcElement.parentNode;
	}
	
	// Encode the URL by first replacing all instances of "/" and then doing an escape
	// function call to encode the other non-safe characters
	var encodedURL = a.href.replace(/(\/)/g, "%2F");
	//encodedURL = escape(encodedURL);
	// Open a new window with the link's href.
	var newwin = window.open('http:\/\/www.imcstudios.com\/redirect\/?url=' + encodedURL);
	//var newwin = window.open('http:\/\/www.imcstudios.com\/redirect\/?url=' + encodedURL);
	
	// 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. Not sure
	// if this works, but it doesn't seem to hinder.
	return !newwin;
}

window.onload = initExternalLinks;