
// social: regex string matching social media networks
var social = '(youtube|facebook|twitter|flickr|linkedin|tumblr)\.com';
// downloads: regex matching all file types to be considered as downloads
var downloads = '\\.(zip|mp\\d+|mpe*g|pdf|docx*|pptx*|xlsx*|jpe*g|png|gif|tiff*)$';

// Addthis social tracking integration
var addthis_config = {
    data_ga_property: gaProdAccount,
    data_ga_social: true
};

try {
    typeof jQuery !== 'undefined' && 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));
        };

        // global cross domain linker
        // for any link starting with http, but not pointing to the current domain or any of its subdomains, but matching one of our tracked domains...
        $('a:regex(href,^http.*' + crossDomains + ')').live('click', function(e) {
            if (this.href.match(crossDomains)[1] !== baseDomain) {
                var t = _gat._getTrackerByName();
                this.href = t._getLinkerUrl(this.href, 'true');
            }
        });

        // outbound & siblings links tracking
        $('a[href^="http"]:not([href*="//' + location.host + '"])').live('click', function(e) {
            var r = new RegExp(social);
            if(!RegExp(social).test(this.href)) {
                _gaq.push(['_trackEvent', (this.href.match('^http[^/]+//[^?#]*' + crossDomains) ? 'sibling' : 'outbound'), 'click', this.href.match(/\/\/([^\/]+)/)[1], 0, true]);
            }
        });
        // inbound cross-domain tracking from sibling sites
        if (RegExp('^http[^\/]+\/\/[^?#]*' + crossDomains).test(document.referrer) && !/(stumbleupon)/.test(document.referrer)) {
            var d = (document.referrer.match(/\/\/(www\.)*([^\/:]+)/)[2]);
            if (d !== location.hostname.replace(/www\./, '')) {
                _gaq.push(['_trackEvent', 'inbound', 'click', d, 0, true]);
            }
        }

        // store QueryString arguments in object
        var qs = {};
        location.search.replace(/([^?=&]+)(=([^&]*))?/g, function($0, $1, $2, $3) {
            qs[$1] = unescape($3);
        });

        // internal campaigns tracking
        if (qs.icmp) {
            _gaq.push(['_trackEvent', 'icmp', qs.icmp, document.referrer.match(/\/\/[^\/]+(\/.*)/)[1], 0, true]);
        }

        // Track social media logos at bottom of page
        $('a:regex(href,' + social + ')').live('click', function() {
            _gaq.push(['_trackSocial', this.href.match(/\.([^\.]+)\.com/)[1], 'subscribe']);
        });
        // Email tracking
        $('a:contains("Email this Page")').live('click', function() {
            _gaq.push(['_trackSocial', 'email', 'email']);
        });
        // Print tracking
        $('a:contains("Print this Page")').live('click', function() {
            _gaq.push(['_trackSocial', 'print', 'print']);
        });
        // Chat tracking
        $('a[title^="Chat -"]').live('click', function() {
            _gaq.push(['_trackSocial', $(this).text().trim(), 'chat']);
        });

        // Downloads tracking
        $('a:regex(href,' + downloads + ')').live('click', function(e) {
            _gaq.push(['_trackEvent', 'download', 'click', this.href.replace(/^.*\/\//, '')], ['_trackPageview', '/vpv/' + this.href.replace(/^.*\/\//, '')]);
        });
    });
} catch (e) {
    _gaq.push(['_trackEvent', 'error', e.name, e.message + (e.lineNumber || ''), 0, true]);
}
