﻿/* Hyperlink.js / Jared McGuire */

//document.observe('dom:loaded', function() {
//    ScanForExternalLinks();
 //   ScanForEmailLinks();
//});

// Scans for external links and updates the target to a new window.
// <a href="http://digitalev.com">Website</a>  ==>  <a href="http://digitalev.com" target="_blank" class="externalLink">Website</a>
function ScanForExternalLinks() {
    var domain = document.domain.replace("www.", "");
    var httpRegEx = new RegExp("^http(s)?://", "i");
    var domainRegEx = new RegExp("^http(s)?://((.*)\.)?" + domain.replace(".", "\."), "i");
    $A(document.getElementsByTagName("a")).each(function(link) {
        if ((httpRegEx.test(link.href) == true) && (domainRegEx.test(link.href) == false) && (link.rel == "")) {
            if (link.className.startsWith('noExternalLink') == false) {
                if ((isDomainConsideredExternal(link.href) == true) && (link.href != "http://www.google.com/intl/en-US_US/help/terms_maps.html") && (link.href.startsWith("http://google.com/maps?") == false)) {
                    link.target = "_blank";
                    link.className = link.className + " externalLink";
                    var emptySpan = document.createElement('span');
                    emptySpan.className = "icon";
                    link.appendChild(emptySpan);
                }
            }
        }
    });
}

function isDomainConsideredExternal(linkUrl) {
    var linkUrlWithoutProtocol = linkUrl.replace(/http:\/\//g, "");
    linkUrlWithoutProtocol = linkUrlWithoutProtocol.replace(/https:\/\//g, "");

    var domainSeparatorIndex = linkUrlWithoutProtocol.indexOf(".");
    if (domainSeparatorIndex > 0) {
        var testDomain = linkUrlWithoutProtocol.substring(domainSeparatorIndex + 1);
        if (testDomain.startsWith("opkansas.org") == false) {
            return true;
        }
    }
    return false;
}

// Scans for pseudo email links and updates to real email links.
// <a href="#email:mcguirej#digitalev.com"></a>  ==>  <a href="mailto:mcguirej@digitalev.com" class="emailLink">mcguirej@digitalev.com</a>
function ScanForEmailLinks() {
    var domain = document.domain.replace("www.", "");
    var emailRegEx = new RegExp("^#?email:", "i");
    $A(document.getElementsByTagName("a")).each(function(link) {
        var href = link.href.replace(document.location, "");
        if (emailRegEx.test(href) == true) {
            var email = href.replace(emailRegEx, "").replace("#", "@");
            link.innerHTML = email;
            link.href = "mailto:" + email;
            link.className = "emailLink";
        }
    });
}

/*
var ViewTracker = (function() {
    var __expireDateTime, __plugins = {};
    function __addEventListener(e, eventType, eventHandler, useCapture) { if (e.addEventListener) { e.addEventListener(eventType, eventHandler, useCapture); return true; } else if (e.attachEvent) { return e.attachEvent('on' + eventType, eventHandler); } e['on' + eventType] = eventHandler; }
    function __beforeUnloadHandler() { if (typeof __expireDateTime !== 'undefined') { var n; do { n = new Date(); } while (n.getTime() < __expireDateTime); } __executeMethod('unload'); }
    function __executeMethod(methodName, callback) { var result = '', i, pluginMethod; for (i in __plugins) { pluginMethod = __plugins[i][methodName]; if (typeof pluginMethod === 'function') { result += pluginMethod(callback); } } return result; }
    function __getImage(url, delay) { var now = new Date(), image = new Image(1, 1); __expireDateTime = now.getTime() + delay; image.onLoad = function() { }; image.src = url; }
    function __getRequest(a, id) { return '/App_/ViewTracker.ashx?m=' + a + '&id=' + id + '&t=' + Math.random(); }
    __addEventListener(window, 'beforeunload', __beforeUnloadHandler, false);
    return { TrackDownload: function(id) { __getImage(__getRequest('d', id), 500); return true; } };
} ());
*/
