﻿/* Returns true iff the reference should be treated as local (same site). */
function isLocalHref(href) {

    if (!href) {
        /* Treat empty href like a normal (local site) href. */
        return true;
    }

    // Matches the initial part of a potentially non-local href attribute value,
    // grouping scheme and hostname.
    var domainRegEx = new RegExp("^ *([^:/]+)://([^/]*)/?", "g");
    var matches = domainRegEx.exec(href);

    // assume relative local/internal link if scheme is not specified.
    if (!matches) {
        return true;
    }

    // the href URL is on the same (internal) host
    // (IE will add the host to all relative links).
    var domain = matches[2];
    if (window.location.host == domain) {
        return true;
    }

    // Don't open a new window for some URL schemes (e.g. mailto "scheme").
    var scheme = matches[1];
    if (scheme == "mailto") return true;

    // certain absolute URL domains may be classified as local (e.g. same company, partner etc.).
    //if (domain.match("^(.*\.)?example\.com$")) {
    //    return true;
    //}

    // otherwise this should be classed as an external link.
    return false;
}

/* Returns true iff the reference is to a document which will not send tracking data. */
function isDocumentHref(href) {
    if (href && href.match(/\.(pdf|doc|xls|ppt)$/i)) {
        return true;
    }
    return false;
}

/* Handle external links and documents, UI and tracking. */
function processLinks() {

    var $linkElements = $('a, area');

    $linkElements.each(function () {
        /* Adjust link handling where necessary (e.g. opening in a new window). */
        var target = $(this).attr('target');
        var rel = $(this).attr('rel');
        var title = $(this).attr('title');
        var href = $(this).attr('href');

        if (target) {
            /* Already handled, leave as is. */
        }
        else if (rel || !isLocalHref(href) || isDocumentHref(href)) {
            /* 'rel' attribute generally indicates handling through a new window. */
            /* External links and document should be handled through a new window. */
            $(this).attr('target', '_blank');
            if (!title) {
                $(this).attr('title', 'opens in a new window'); // TODO resource
            }
        }

        /* And also track outbound and document links (GA). */
        if (!isLocalHref(href)) {
            /* External link, event tracking to avoid additional page view count. */
            $(this).click(function () {
                try {
                    if (typeof _gaq != "undefined" && _gaq) {
                        _gaq.push(['_trackEvent', 'external_link', 'click', href + '']);
                        /* Uncomment for timeout if tracking is slow. */
                        // setTimeout('document.location = "' + href + '"', 100);
                        // return false;
                    }
                } catch (e) { }
            });
        }
        else if (isDocumentHref(href)) {
            /* Local document link, page tracking. */
            $(this).click(function () {
                try {
                    if (typeof _gaq != "undefined" && _gaq) {
                        _gaq.push(['_trackPageview', '/document/' + href]);
                    }
                } catch (e) { }
            });
        }
    });
}

/* Update links. */
$(document).ready(function () {
    processLinks();
});

