// gaAddons FREE v1.1, Copyright 2011, Stephane Hamel - http://gaAddons.com
// Licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License
// http://jsfiddle.net/shamel67/VzbPw/
// nhotalling - corrected error in _trackDownloads regEx param
// _trackDownloads
jQuery(document).ready(function($) {
    // helper function - allow regex as jQuery selector
    $.expr[':'].regex = function(e, i, m) {
        var mP = m[3].split(','),
            l = /^(data|css):/,
            a = {
                method: mP[0].match(l) ? mP[0].split(':')[0] : 'attr',
                property: mP.shift().replace(l, '')
            },
            r = new RegExp(mP.join('').replace(/^\s+|\s+$/g, ''), 'ig');
        return r.test($(e)[a.method](a.property));
    };
    $('a:regex(href,\\.(zip|mp\\d+|mpe*g|pdf|docx*|pptx*|xlsx*|jpe*g|png|gif|tiff*)$)').live('click', function(e) {
        //this.href.replace(/^.*\/\//, '')
        _gaq.push(['_trackEvent', 'download', 'click', this.href.substring(this.href.lastIndexOf('/')+1)]); // filename only
    });
});
// _trackMailTo
jQuery(document).ready(function($) {
    $('a[href^="mailto"]').live('click', function(e) {
        _gaq.push(['_trackSocial', 'email', 'send', this.href.replace(/^mailto:/i, '')]);
    });
});
// _trackOutbound
// nhotalling - modified to catch liveperson clicks
jQuery(document).ready(function($) {

    $('a[href^="http"]:not([href*="//' + location.host + '"])').live('click', function(e) {
        var link = this.href.match(/\/\/([^\/]+)/)[1]; // domain only
        // set HubSpot cookie if this is first page visit
        if(lpMTagConfig.visitorVar.length == 0) {
            lpMTagConfig.visitorVar[0] = "hstoken=" + getCookie('hubspotutk');
        }
        if (link == 'server.iad.liveperson.net') {
            _gaq.push(['_trackEvent', 'chatlink', 'click', this.id]);
        } else {
            _gaq.push(['_trackEvent', 'outbound', 'click', link]);
        }            
    });
});
// _trackError: track 404 - Page not found
if (document.title.search(/Page Not Found/i) !== -1) {
    jQuery(document).ready(function($) {
        _gaq.push(['_trackPageview', '/vpv/404/' + location.host + location.pathname + '?from=' + document.referrer]);
    });
}
// gaAddons runs before main.js - add getCookie here
function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length,c.length);
        };
    }
    return null;
}
