﻿(function($) {
    $.fn.trackHyperlinks = function() {
        var regex = new RegExp('\\.(?:doc|xls|ppt|docx|xlsx|pptx|pdf|zip|txt|exe|msi|wma|mov|avi|wmv|mp3)($|\\&|\\?)');
        return this.each(function() {
            var linkTag = $(this);
            try {
                if (this.href != '' && (this.protocol == 'mailto:' || this.protocol == 'http:' || this.protocol == 'https:')) {
                    if (this.protocol == 'mailto:') {
                        linkTag.click(function() { _gaq.push(['_trackPageview', '/mailto/' + this.href.substring(7)]); });
                    }
                    else if (this.hostname == location.host) {
                        var internalLink = this.pathname;
                        var isDocument = internalLink.match(regex);
                        if (isDocument) {
                            linkTag.click(function() { _gaq.push(['_trackPageview', internalLink]); });
                        }
                    }
                    else {
                        var externalLink = (this.pathname.charAt(0) == '/') ? this.pathname : '/' + this.pathname;
                        if (this.hostname != location.host) externalLink = '/external/' + this.hostname + externalLink;
                        linkTag.click(function() { _gaq.push(['_trackPageview', externalLink]); });
                    }
                }
            }
            catch (e) {
            }
        });
    };
})(jQuery);