/****************************************************
Author: AF
*/

function addLinkerEvents() {
    
    

    $j("a").each(function() {
        var link;
        var href;
        var sections;
        var isDoc;
        var isExt;
        var isMail;
        var domain;
        var pos;

        link = $j(this);
        href = link.attr("href");
        if (typeof (href) != "undefined" && href.indexOf("#") != 0) {

            domain = "";
            if (startsWith(href, "mailto:")) {
                isMail = true;
                domain = href.split(":")[1];
            }
            else
                isMail = false;

            if (startsWith(href, "http")) {
                sections = href.split("//");
                if (sections.length > 1) {
                    domain = sections[1].split("/")[0].toLowerCase();
                }
            }

            var ext = "";

            pos = href.lastIndexOf(".");
            if (pos != -1) {
                ext = href.substring(++pos).toLowerCase();
            }

            isDoc = (ext.length != 0 && $j.inArray(ext, docExt) != -1);
            isExt = (domain.length != 0 && $j.inArray(domain, localDomain) == -1);

            if (isMail) {
                link.click(function() { _gaq.push(['_trackEvent', 'MailTo', domain, document.location.href]); });
            }
            else if (isExt) {
                if (isDoc) {
                    link.click(function() { _gaq.push(['_trackEvent', 'External File', href, document.location.href]); });
                }
                else {
                    link.click(function() { _gaq.push(['_trackEvent', 'External Page', href, document.location.href]); });
                }
            }
            else {
                if (isDoc) {
                    link.click(function() { _gaq.push(['_trackEvent', 'Internal File', ext, href]); }); 
                }
                else {
                    link.click(function() { _gaq.push(['_trackEvent', 'Navigation', document.location.href, href]); });
                }
            }
        }
    });
}

function startsWith(text, part) {
    if (text.toLowerCase().indexOf(part.toLowerCase()) == 0)
        return true;
    else
        return false;
}
