$(document).ready(function() {
    $('a').each(function() {

        var href = $(this).attr('href');
        var filetypes = /\.(zip|exe|pdf|doc|xls|ppt|mp3|eps|docx|rar|csv|gz|dmg|xml)$/i;
        var domains = /^(debat\.qmag\.dk|www\.qmag\.dk|www\.q\.dk)/i;


        if (href) {
            //Website
            if ((href.match(/^https?\:/i)) && (!href.match(document.domain)) && (!href.match(filetypes))) {
                $(this).click(function() {
                    var extLink = href.replace(/^https?\:\/\//i, '');
                    
                    if (extLink.match(domains)) {
                        _gaq.push(['_link', href]); return false;
                    }
                    else {
                        _gaq.push(['_trackEvent', 'External Link Click', 'Web', extLink]);
                    }

                });
            };

            //E-mail
            if (href.match(/^mailto\:/i)) {
                $(this).click(function() {
                    var mailLink = href.replace(/^mailto\:/i, '');
                    _gaq.push(['_trackEvent', 'External Link Click', 'Email', mailLink]);
                });
            }

            //File
            if (href.match(filetypes)) {
                $(this).click(function() {
                    var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                    _gaq.push(['_trackEvent', 'External Link Click', 'Download - ' + extension, href]);
                });
            }

        }

    });

});


