﻿$(function () {
    // The slashes are to ensure the period is in the url, the $ is to make sure it is the end of the url, the i is to make it case insensitive.
    filetypes = /\.doc$|\.xls$|\.exe$|\.zip$|\.pdf$|\.mp3$|\.psd$/i;
    $("a[href]").each(function () {
        var href = $(this).attr('href');
        var hrefArray = href.split('.');
        var extension = hrefArray[hrefArray.length - 1];

        // Track mailto links
        if (href.match(/^mailto\:/i)) {
            var url = href.replace(/^mailto\:/i, "");
            $(this).click(function () { pageTracker._trackPageview("/mailto/" + url); });
        }

        // Track external links
        if (location.host != this.host.replace(/\:80$/i, "")) {
            var url = href.replace(/^http\:\/\/(www\.)*/i, "")
            $(this).click(function () { pageTracker._trackPageview("/outgoing/" + url); });
        }

        // Track downloads (links with a given extension)
        if(href.match(/.*streamfile.aspx?.*/))
        {
            $(this).click(function () { pageTracker._trackPageview("/downloads/" + href.substr(href.indexOf('?'))); });
        }
        else if ((href.match(filetypes)) || ($(this).parent("p").hasClass("file"))) {
            var url = href;
            $(this).click(function () { pageTracker._trackPageview("/downloads/" + url); });
        }

        // Add any custom tracking below:
    });
});
