/*
westernu.edu - Google Analytics advanced integration
v1.0 2012-01-09 initial
*/

var crossDomains = '(westernu.edu|westernupcc.com)',
    baseDomain = location.hostname.match(RegExp(crossDomains + '$'))[1],
	gaProdAccount = 'UA-26630268-1',
	_gaq = _gaq || [];
_gaq.push(['_setAccount', gaProdAccount],
	['_setDomainName', baseDomain],
	['_setAllowAnchor', true],
	['_setAllowLinker', true]
);

(function() {
    var ga = document.createElement('script');
    ga.type = 'text/javascript';
    ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ga, s);
})();

var addthis_config = {};

try {
	// 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
	addthis_config = {
		data_ga_property: gaProdAccount,
		data_ga_social: true
	};	

	if(typeof jQuery === 'undefined') {
		_gaq.push(['_trackPageview']);
	} else {
		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
				// test if search page, if so, keep search term
			_gaq.push(['_trackPageview', (location.pathname === '/search'? location.pathname+'?q='+$(':input[name=search]:last').val():location.pathname)],
				function() {
					if (/[#&]*__utm.+$/.test(location.hash)) {
						location.hash = location.hash.replace(/[#&]*__utm.+$/, '');
					}
				}
			);
	
			// 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) {
				if(/proxy.westernu.edu\/login\?url=/.test(this.href)) {
					_gaq.push(['_trackEvent', 'outbound', 'click', this.href.match(/\?url=(.+)/)[1], 0, true]);
				}
				else {
					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)) {
				var d = (document.referrer.match(/\/\/(www\.)*([^\/:]+)/)[2]);
				if (d !== location.hostname.replace(/www\./, '')) {
					_gaq.push(['_trackEvent', 'inbound', 'click', d, 0, true]);
				}
			}
	
			// 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]);
}


