﻿
$(document).ready(function() {
    var trackers = [pageTracker];
    var filetypes = /\.(zip|rar|7z|arj|exe|pdf|doc*|xls*|ppt*|wav|mp3|m4a|aac|3gp|3g2|asf|avi|dv|flv|mov|mp4|mpg|mpeg|wmv)$/i;

    $("a").each(function() {

        var href = $(this).attr("href");
        if (href == null) return;

        if ((href.match(/^https?\:/i)) && (!href.match(document.domain))) {
            $(this).click(function() {
                var extLink = href.replace(/^https?\:\/\//i, '');

                $.each(trackers,
                        function(index, pageTracker) {
                            pageTracker._trackEvent("External", "Click", extLink);
                        });
            });
        }
        else if (href.match(/^mailto\:/i)) {
            $(this).click(function() {
                var mailLink = href.replace(/^mailto\:/i, '');

                $.each(trackers,
                        function(index, pageTracker) {
                            pageTracker._trackEvent("Email", "Click", extLink);
                        });
            });
        }
        else if (href.match(filetypes)) {
            $(this).click(function() {
                var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                var filePath = href.replace(/^https?\:\/\/(www.)mydomain\.com\//i, '');

                $.each(trackers,
                        function(index, pageTracker) {
                            pageTracker._trackEvent("Download", "Click - " + extension, filePath);
                        });
            });
        }
    });
});