$(function() {
	var hostname = location.hostname;
	var externalLinkClassname = 'externallink';
	var documentLinkClassname = 'documentlink';
	
	// These line for testing only.
	if (hostname == 'localhost') {
    	hostname = 'www.presbyterian.org';
    }

    // Find all links that point to other hosts.
	var externalUrls = $('a[href]').filter('[href^="http://"],[href^="https://"]').not('[href*="//' + hostname + '/"]');
    // Add 'externallink' to the class attribute.
    externalUrls.addClass(externalLinkClassname);

    // Find all links that point to this host, but autoforward to external URLs.
    var externalAutoforwards = $('a[href]').filter('[href^="http://' + hostname + '"],[href^="https://' + hostname + '"],[href^="/"]').filter('[href*="/site/autoforwards/3rd_party_sites/"],[href*="/site/autoforwards/other_novant_sites/"]');
    // Add 'externallink' to the class attribute.
    externalAutoforwards.addClass(externalLinkClassname);

    // Tell all links with the proper class attributes to open in a new window.
	$('a.' + externalLinkClassname).attr('target', '_blank');


    // Find all links that point to this host, but autoforward to external URLs.
    var documentLinks = $('a[href]').filter('[href^="http://' + hostname + '"],[href^="https://' + hostname + '"],[href^="/"]').filter('[href*="/_media/local/docs/"],[href*="/_media/shared/docs/"]');
    // Add 'externallink' to the class attribute.
    documentLinks.addClass(documentLinkClassname);

	$('a.' + documentLinkClassname).attr('target', '_blank');
});
