var offsiteWarningTitle = 'now leaving bjupress.com';
var offsiteWarningMsg = 'You are now leaving our BJU Press website and entering an external website.  BJU Press has partnered with this site to provide a service for you, our valued customer. Links do not necessarily constitute an endorsement or guarantee of all of the information, opinions, or products available on such sites.';
var bjuHosts = ['bju.edu', 'bjupress.com', 'bjumg.org', 'gfamissions.org', 'bjucampusstore.com', 'itib.org', 'scienceendowment.org', 'msfund.org', 'dnsfund.org', 'hafund.org', 'allforexcellence.org', 'sacredaudio.com', 'passportlearning.com', 'bju.vfao.com', 'jimberg.com', 'changedintohisimage.com', 'createdforhisglory.com', 'quietinganoisysoul.com', 'whentroublecomes.com', 'bju-csm.symplicity.com', 'gomeetingpoint.com', 'sermonaudio.com'];

// Accessory function to add event handlers on the fly.
function connectEvent (sourceElement, eventName, eventHandler) {
	if (sourceElement.addEventListener) {
		sourceElement.addEventListener(eventName, eventHandler, false);
	} else {
		sourceElement.attachEvent('on' + eventName, eventHandler);
	}
}

// Warn the user that he is following an off-site link and allow the user to cancel.
function showOffsiteWarning (e) {
	// Make sure we have a valid event object.
	if (! e) var e = window.event;
	
	// Cancel the link action so that we can get confirmation first.
	if (e.preventDefault) e.preventDefault();
	else e.returnValue = false;
	
	// Create the warning popup if it doesn't already exist.
	var popup = document.getElementById('offsiteWarning');
	var overlay = document.getElementById('offsiteWarningOverlay');
	if (! popup || ! overlay) {
		// Create object structure;
		popup = document.createElement('div');
		popup.id = 'offsiteWarning';
		popup.appendChild (document.createElement('h3'))
		     .appendChild (document.createTextNode(window.offsiteWarningTitle));
		popup.appendChild (document.createElement('p'))
		     .appendChild (document.createTextNode(window.offsiteWarningMsg));
		popup.appendChild(document.createElement('p')).id = 'offsiteWarningDest';
		popup.appendChild (document.createElement('p'));
		btnYes = popup.lastChild.appendChild(document.createElement('button'));
		btnNo = popup.lastChild.appendChild(document.createElement('button'));
		btnYes.appendChild (document.createTextNode('Continue'));
		btnNo.appendChild (document.createTextNode('Cancel'));
		
		// Create background overlay.
		overlay = window.offsiteWarningOverlay = document.createElement('div');
		overlay.id = 'offsiteWarningOverlay';
		
		// Create link approval/denial function.
		var offsiteLink = function (approved) {
			// Get rid of popup (required even when approved to go off-site so that the pop-up
			// does not stay visible if the user clicks the back button to return to BJU).
			popup.style.display = 'none';
			overlay.style.display = 'none';
			
			// Actually forward to an off-site location only if approved.
			if (approved) document.location = popup.linkURL;
		}
		
		// Assign event handler for buttons.
		connectEvent (btnYes, 'click', function () { offsiteLink (true); });
		connectEvent (btnNo, 'click', function () { offsiteLink (false); });
		
		// Add background overlay and warning popup to document model.
		document.body.appendChild (overlay);
		document.body.appendChild (popup);
	}
	
	// Set URL for use by the button event handler.
	var node = e.target || e.srcElement;
	if (node.tagName == "A") {
		popup.linkURL = (e.target || e.srcElement).href;
	} else {
		while (node.parentNode) {
			node = node.parentNode;
			if (node.tagName == "A") {
				popup.linkURL = node.href;
				break;
			}
		}
	}
	
	// Display popup.
	overlay.style.display = 'block';
	popup.style.display = 'block';
}

// Apply the warning popup to all offsite links on the page.
for (i = 0; i < bjuHosts.length; ++i) bjuHosts[i].replace('.', '\.');
var bjuHostRegex = new RegExp('^(.+\.)?' + bjuHosts.join('|') + '$');
var anchors = document.getElementById('content').getElementsByTagName('a');
for (i = 0; i < anchors.length; ++i) {
	if (anchors[i].href && anchors[i].protocol != 'javascript:' && ! bjuHostRegex.test(anchors[i].hostname))
		connectEvent (anchors[i], 'click', showOffsiteWarning);
}