﻿(function ($) {

    $(document).ready(function () {

        //loop though each anchor element

        //_trackEvent(category, action, opt_label, opt_value, opt_noninteraction)

        $('a').each(function () {

            var href = $(this).attr('href');
            var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3|jpg)$/i;

            if ("undefined" !== typeof href) {
                //check for links starting with http or https, making sure that links to our own domain are excluded
                if ((href.match(/^https?\:/i)) && (!href.match(document.domain))) {
                    $(this).click(function () {
                        var extLink = href.replace(/^https?\:\/\//i, '');
                        // alert('Klik , Externe Link , ' + extLink);
                        pageTracker._trackEvent('Klik', 'Externe Link', extLink);
                    });
                }
                //check for links starting with mailto:
                else if (href.match(/^mailto\:/i)) {
                    $(this).click(function () {
                        var mailLink = href.replace(/^mailto\:/i, '');
                        // alert('Klik , Email Link , ' + mailLink);
                        pageTracker._trackEvent('Klik', 'Email Link', mailLink);
                    });
                }
                //check for links with file extension that match the filetypes regular expression:
                else if (href.match(filetypes)) {
                    $(this).click(function () {
                        var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                        var filePath = href.replace(/^https?\:\/\/(www.)mydomain\.com\//i, '');
                        // alert('Klik , Download Link | ' + extension + " , " + filePath);
                        pageTracker._trackEvent('Klik', 'Download Link | ' + extension, filePath);
                    });
                }

                else if (href.match(/^\/LinkClick.aspx/i)) {
                    $(this).click(function () {
                        var filelink = $(this).attr('href');
                        var linktext = $(this).text();
                        // alert('Klik , LinkClick | ' + linktext + ' , ' + filelink);
                        pageTracker._trackEvent('Klik', 'LinkClick | ' + linktext, filelink);
                    });
                }
            }
        });

    });

})(jQuery);

