/* http://www.carronmedia.com/extend-google-analytics-with-jquery/ */
/*
function testGaq() {
    this.push = function(obj) {
        console.log(obj)
        alert(obj)
    }
}
var _gaq = new testGaq()
*/
$(function() {
    if (typeof _gaq !== 'undefined') {
        var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
        $('a').each(function(){
            var href = $(this).attr('href');

            if (typeof href !== 'undefined') {
                if ($(this).attr('rel') == 'gatrack') {
                    $(this).click(function() {
                        var extLink = href.replace(/^https?\:\/\//i, '');
                        _gaq.push(['_trackEvent', 'Tracking', 'Manual', extLink]);
                    });
                }
                else if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){
                    $(this).click(function() {
                        var extLink = href.replace(/^https?\:\/\//i, '');
                        _gaq.push(['_trackEvent', 'Tracking', 'External', extLink]);
                    });
                }
                else if (href.match(/^mailto\:/i)){
                    $(this).click(function() {
                        var mailLink = href.replace(/^mailto\:/i, '');
                        _gaq.push(['_trackEvent', 'Tracking', 'Email', mailLink]);
                    });
                }
                else if (href.match(filetypes)){
                    $(this).click(function() {
                        var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                        var filePath = href.replace(/^https?\:\/\/(www.)mydomain\.com\//i, '');
                        _gaq.push(['_trackEvent', 'Tracking', 'Download - ' + extension, filePath]);
                    });
                }
            }
        });
    }
});
