var externallinksargs = {
	imagebase: "/renderer/images"
};
var externallinks = {
	imagebase: externallinksargs.imagebase,
	timeout: null,
	excludedomains: [
		"marad.dot.gov", "search.google.dot.gov", "google2.dot.gov"
	],
	closedialog: function() {
		window.clearTimeout(this.timeout);
		
		document.getElementById("externallink-background").style.display = "none";
		document.getElementById("externallink-window").style.display = "none";
		
		if (window.ActiveXObject && (document.getElementById("externallink-iframe"))) {
			document.getElementById("externallink-iframe").style.display = "none";
		}
		
		if (window.detachEvent) {
			window.detachEvent("onresize", externallinks.positiondialog);
		} else if (window.removeEventListener) {
			window.removeEventListener("resize", externallinks.positiondialog, false);
		} else {
			window.onresize = null; 
		}
	},
	opendialog: function(linktext, href) {
		var redirecttext = "Thank you for visiting the Maritime Administration website.<p>" + 
		"You will be transfered in 10 seconds to:<p>" + 
		unescape(href) + "<p>" +
		"<input type='button' value='Cancel' onclick='externallinks.closedialog();'>" +
		"<input type='button' value='Continue' onclick='window.open(\"" + href + "\",\"new_window\");externallinks.closedialog();'><p>" +
		"Please be aware that when you exit our web sites, the privacy policy stated on our web sites may not be the same as that on other web sites. " +
		"In addition, The Departement of Transportation (DOT) cannot attest to the accuracy of information provided by linked sites. " +
		"Linking to a web site does not constitute an endorsement by DOT or any of its employees of the sponsors of the site or the products presented on the site."; 		
				
		document.getElementById("externallink-window").innerHTML = redirecttext;
		document.getElementById("externallink-background").style.display = "block";
		document.getElementById("externallink-window").style.display = "block";
		
		if (window.ActiveXObject && (document.getElementById("externallink-iframe"))) {
			document.getElementById("externallink-iframe").style.display = "block";
		} 
			
		this.positiondialog();
		
		// position dialog
		if (window.attachEvent) {
			window.attachEvent("onresize", externallinks.positiondialog);
		} else if (window.addEventListener) {
			window.addEventListener("resize", externallinks.positiondialog, false);
		} else {
			window.onresize = externallinks.positiondialog; 
		}
		
		if (document.all) {
			document.documentElement.onscroll = externallinks.positiondialog;
		}
		
		this.timeout = window.setTimeout("window.open('" + href + "','new_window');externallinks.closedialog();", 10000);
	},
	positiondialog: function() {
		var left = window.ActiveXObject ? document.documentElement.scrollLeft : 0;
		var top = window.ActiveXObject ? document.documentElement.scrollTop : 0;
		
		document.getElementById("externallink-window").style.left = Math.max((left + (externallinks.getwindowwidth() - document.getElementById("externallink-window").offsetWidth) / 2), 0) + "px";
		document.getElementById("externallink-window").style.top = Math.max((top + (externallinks.getwindowheight() - document.getElementById("externallink-window").offsetHeight) / 2), 0) + "px";
	},
	getwindowwidth: function() {
		return document.documentElement && document.documentElement.clientWidth || document.body && document.body.clientWidth || document.body && document.body.parentNode && document.body.parentNode.clientWidth || 0;
	},
	getwindowheight: function() {
		return document.documentElement && document.documentElement.clientHeight || document.body && document.body.clientHeight || document.body && document.body.parentNode && document.body.parentNode.clientHeight || 0;
	},
	rewritelinks: function() {
		// create case insensitive regular expression; join domains with or operators 
		var regexpexcludedomains = new RegExp(this.excludedomains.join("|"), "i");
		
		// get all anchors
		var links = document.getElementsByTagName("a");
		
		var numberOfLinks = 0;
		
		if (links) {
			numberOfLinks = links.length;
		}
		
		for (var linkIndex = 0; linkIndex < numberOfLinks; linkIndex++) {
			// link does not reference an excluded domain and is not relative
			if ((links[linkIndex].href.indexOf("http:") != -1 || links[linkIndex].href.indexOf("https:") != -1) && links[linkIndex].hostname.search(regexpexcludedomains) == -1) {
				// show div and timer to open link
				links[linkIndex].onclick = new Function('externallinks.opendialog("' + escape(links[linkIndex].innerHTML) + '", "' + links[linkIndex].href + '"); return false;');

				// hide fact that href is now an anchor
				links[linkIndex].onmouseover = new Function('window.status="' + links[linkIndex].href + '"; return true;');
				links[linkIndex].onmouseout = new Function('window.status=""; return true;');

				// add external link image
				// var externalImg = document.createElement("img");
				// externalImg.src = this.imagebase + "/external.png";
				// externalImg.border = 0;
				// externalImg.alt = "Exit Maritime Administration";
				// externalImg.title = "Exit Maritime Administration";
				//externalImg.style.paddingLeft = "5px";
				// externalImg.className = "external_link_img";
				// links[linkIndex].appendChild(externalImg);
				
				// force external links to go through redirect logic
				links[linkIndex].href = '#';
			}
		}

	},
	init: function() {
		if (window.addEventListener) {
			window.addEventListener("load", function() {externallinks.rewritelinks()}, false)
		} else if (window.attachEvent()) {
			window.attachEvent("onload", function() {externallinks.rewritelinks()});
		}
	}
}

externallinks.init();