/*!
 * Google Analytics Utilities for Deloitte.ca
 *
 * Copyright 2010, Adviso
 * Date: June 14th, 2010
 */
function textToUrl(text)
{
  return text.replace(/[\/_|+ -'"]+/g,'-').replace(/[йки]+/g,'e').replace(/[аб]+/g,'a').toLowerCase();
}

function gaTrackPageView(virtualUrl){
	if(virtualUrl){
		pageTracker._trackPageview(virtualUrl);
		secondTracker._trackPageview(virtualUrl);
	} else {
		pageTracker._trackPageview();
		secondTracker._trackPageview();		
	}
}

function gaTrackEvent(category, action, label){
	if(!label) {
		label = document.location.pathname + document.location.search;
	}
	
	pageTracker._trackEvent(category, action, label);
	secondTracker._trackEvent(category, action, label);	
}

function gaTrackPaveViewEvent(category, action, label){
	var url = document.location.pathname + document.location.search;
	var virtualUrl = (url.match(/\?/i)) ? url + "&" : url + "?";
	virtualUrl += "ga_category=" + textToUrl(category) + "&ga_action=" + textToUrl(action);
	
	label = label || url;
				
	pageTracker._trackPageview(virtualUrl);
	secondTracker._trackPageview(virtualUrl);
	
	pageTracker._trackEvent(category, action, label);
	secondTracker._trackEvent(category, action, label);	
}

function gaSetCustomVar(index, name, value, opt_scope){
	pageTracker._setCustomVar(index, name, value, opt_scope);
	secondTracker._setCustomVar(index, name, value, opt_scope);
}

// Only once the Dom is ready - Using jQuery
$(function() {
/*!
 * JAVASCRIPT CLICK TRACKING LIBRARY
 * By Erik Vold ( http://erikvold.com/ )
 *
 * Adapted by Adviso to exclude delay from links where 
 * window.open is called on an onclick event.
 *
 */
(function(){
	function _clickTrackingLib(){
		var matchAry = [], RegExpCache = [/^(http|ftp|chrome|file|view-source)/i];

		if ( document.body.addEventListener ) {
			// w3c
			var addClickEvent = function( ele, evt ){
				ele.addEventListener( "click", evt, false );
			};
		}
		else if ( document.body.attachEvent ) {
			var addClickEvent = function( ele, evt ){
				ele.attachEvent( "onclick", evt );
			};
		}

		if ( document.body.removeEventListener ) {
			// w3c
			var removeClickEvent = function( ele, evt ){
				ele.removeEventListener( "click", evt, false );
			};
		}
		else if ( document.body.detachEvent ) {
			var removeClickEvent = function( ele, evt ){
				ele.detachEvent( "onclick", evt );
			};
		}

		var delayClickEventHandler = function( e, delay ){
			if ( !e ) var e = window.event;
			
			// Fix by Adviso taken from gatag script - Deal with children of "a" tags
			while (e.tagName != "A") {
				e = e.parentNode;
			}
			
			var targ;
			if ( e.target ) targ = e.target;
			else if ( e.srcElement ) targ = e.srcElement;
			// Safari..
			if ( targ.nodeType == 3 ) targ = targ.parentNode;

			setTimeout( 'window.location = "' + targ.href + '"', delay );

			// disable the click event default functionality
			if (e.preventDefault) e.preventDefault(); // w3c
			else e.returnValue  = false; // for ie

			return false;
		};

		this.addMatch = function( match ){
			matchAry.push( match );
		};

		this.addMatches = function( matches ){
			matchAry = matchAry.concat( matches );
		};

		this.attachTrackingFunctions = function( links, delay ){
			if ( matchAry.length == 0 ) return;

			var links = ( links && links.href ) ? [links] : ( links && links.length ) ? links : ( !links ) ? document.links : [];

			if (!links.length) return;

			var link,
				matchAryLen = matchAry.length,
				delay = (delay != undefined) ? ((delay) ? delay : false) : 99;

			if (delay) {
				var delayFunc = function(e){
					delayClickEventHandler(e, delay);
				};
			}

			// loop through the links
			for (var i = links.length - 1; i >= 0; i--) {
				link = links[i];

				// get a reference to the current link which can be used later
				(function( link ){
					// loop through the the remaining match functions in the array of provided match functions
					for ( var j = 0; j < matchAryLen; j++ ) {
						// check link with provided match function
						if ( matchAry[j].match( link ) ) {
							// Add provided click event wrapped in another lambda providing a reference to the link element
							(function( j ){
								// Attach the wrapper lambda to the link's click event
								addClickEvent( link, function( e ){
									matchAry[j].trackingFunc( e, link );
								});
							})( j );
              // check if a delay function should be applied
              if ( !(
                delay === false ||
                ( link.target && ( link.target == "_blank" || link.target == "_new" ) ) ||
                !link.protocol.match( RegExpCache[0] ) ||
                String(link.onclick).match(/window\.open/i) )
              ) addClickEvent( link, delayFunc );              
						}
					}
				})( link );
      }

			return;
		};
	}

	// create the main clickTrackingLib instance
	clickTrackingLib = new _clickTrackingLib();

	// if you want to make new click tracking helper objects, then use this method.
	clickTrackingLib._clickTrackingLib = _clickTrackingLib;
})();
/*!
 * JAVASCRIPT CLICK TRACKING LIBRARY PRESETS
 * By Erik Vold ( http://erikvold.com/ )
 *
 * Adapted and customized by Adviso for Deloitte.ca
 *
 */
(function(){
	if(clickTrackingLib == undefined) clickTrackingLib = {};

	var RegExpCache = [
			/(^|\s)external(\s|$)/i, //external links
			/\.(docx*|xlsx*|pptx*|xls|doc|ppt|xpi|eps|jpg|png|svg|pdf|zip|txt|vsd|vxd|rar|exe|wma|mov|avi|wmv|mp3|rss)$/i,			
			/^http/i
		];

	var matchPresets = {
		'rel-external': function( a ){
			// matches rel-external links
			return ( a.getAttribute("rel") && a.getAttribute("rel").match(RegExpCache[0]) ) ? true : false;
		},
		'external-hostname': function( a ){
			// matches external hostname links
			return ( a.hostname && (a.hostname != document.location.hostname) ) ? true : false;
		},
		'all-external': function( a ){
			// matches all rel-external and external hostname links
			return ( matchPresets['rel-external'](a) || matchPresets['external-hostname'](a) ) ? true : false;
		},
		'filetypes': function( a ){
			// matches file names
			return ( a.pathname.match(RegExpCache[1]) ) ? true : false;
		},
		'non-rel-external': function( a ){
			// matches non-rel-external links
			return ( !a.getAttribute("rel") || !a.getAttribute("rel").match(RegExpCache[0]) ) ? true : false;
		},
		'internal-hostname': function( a ){
			// matches internal hostname links
			return ( a.protocol.match(RegExpCache[2]) && a.hostname == document.location.hostname ) ? true : false;
		},
		'all-internal': function( a ){
			// matches non-rel-external links or internal hostname links
			return ( matchPresets['non-rel-external'](a) || matchPresets['internal-hostname'](a) ) ? true : false;
		},
		'mailto': function( a ){
			// matches mailto links
			return ( a.protocol.match(/^mailto/i) ) ? true : false;
		},
		'pdf': function( a ){
			// matches pdf files
			return ( a.pathname.match(/\.pdf$/i) ) ? true : false;
		},
		//CUSTOM PRESETS
		'deloitte-print-link': function( a ){
			return ( a.id == 'printLink' ) ? true : false;
		},
		'deloitte-twitter': function( a ){
			return ( a.hostname == 'twitter.com' ) ? true : false;
		},
		'deloitte-social-media': function( a ){
			return ( a.hostname.match(/twitter\.com|youtube\.com|facebook\.com/i) ) ? true : false;
		},
		'deloitte-addthis': function( a ){
			return ( a.hostname.match(/addthis\.com/i) ) ? true : false;
		},			
		'deloitte-feeds': function( a ){
			return ( a.hostname.match(/^feeds\./i) ) ? true : false;
		},
		'deloitte-podcast': function( a ){
			return ( a.hostname.match(/apple\.com/i) || a.href.match(/media\.deloitte\.ca\/podcast/i) ) ? true : false;
		},
		'deloitte-weekly-tax-higlights': function( a ){
			return ( a.href.match(/deloitte\.ca\/(en|fr)\/Services\/tax\/highlights/i) ) ? true : false;
		},
		'deloitte-career': function( a ){
			return ( a.href.match(/(\/view\/(en_ca\/ca\/careers|fr_ca\/ca\/carrieres)|deloitte\.ca\/fr\/careers)/i) || a.hostname.match(/careers\.deloitte\.com/i) ) ? true : false;
		},
		'deloitte-off-platform': function( a ){	//Class Action?
			return ( a.href.match(/(\/view\/(en_ca\/ca\/careers|fr_ca\/ca\/carrieres)|deloitte\.ca\/fr\/careers)/i) || a.hostname.match(/((careers|corpo)\.deloitte\.com|bankruptcy\.deloitte\.ca)/i) ) ? true : false;
    },
		'deloitte-platform': function( a ){
			return ( a.href.match(/www\.deloitte\.ca\/AlumniDirectory/i) || a.href.match(/deloitte\.ca\/(en|fr)\/Services\/tax\/highlights/i) || a.hostname.match(/(events\.deloitte\-canada\.12hna\.com|www\.canadas50best\.com|en\.fast50\.ca)/i) ) ? true : false;
		},
		'deloitte-events': function( a ){ //Should match events pages with hyperlink to event.on24.com
			return ( a.hostname.match(/iaseminars\.com/i) || ( document.location.href.match(/\/view\/(fr_CA\/ca\/evenements|en_CA\/ca\/ca\-events\-en)/i) && a.hostname.match(/event\.on24\.com/i) ) ) ? true : false;
		},
		'deloitte-engagement': function( a ){ //Print, share, mailto
			return ( matchPresets['deloitte-print-link'](a) || matchPresets['mailto'](a) || matchPresets['mailto'](a)) ? true : false;
		},
		'deloitte-external-hostname': function( a ){
			return ( a.hostname && (a.hostname != document.location.hostname) && !a.hostname.match(/deloitte\.(ca|com)/i) && !matchPresets['deloitte-feeds'](a) && !matchPresets['filetypes'](a) && !matchPresets['deloitte-social-media'](a) && !matchPresets['deloitte-addthis'](a) && !matchPresets['deloitte-podcast'](a) && !matchPresets['deloitte-events'](a) && !matchPresets['deloitte-off-platform'](a) && !matchPresets['deloitte-platform'](a)) ? true : false;	
		}
	};
	
	clickTrackingLib.getMatchPreset = matchPresets;
})();
/*!
 * Custom Tracking for Deloitte.ca
 *
 * By Adviso
 *
 */
var test = new clickTrackingLib._clickTrackingLib();
var matchAry = [
	{	
		match: clickTrackingLib.getMatchPreset["deloitte-off-platform"],
		trackingFunc: function(e, link){
			gaTrackEvent('Deloitte Off Platform', link.href);
		}
	},
	{	
		match: clickTrackingLib.getMatchPreset["deloitte-platform"],
		trackingFunc: function(e, link){
			gaTrackEvent('Deloitte Platform', link.href);
		}
	},
	{	
		match: clickTrackingLib.getMatchPreset["deloitte-external-hostname"],
		trackingFunc: function(e, link){
			gaTrackEvent('Outbound', link.href);
		}
	},
{
		match: clickTrackingLib.getMatchPreset["mailto"],
		trackingFunc: function(e, link){
			gaTrackEvent('Engagement', link.href.match(/[^\:]*$/i)[0]);
		}
	},
	{
		match: clickTrackingLib.getMatchPreset["deloitte-print-link"],
		trackingFunc: function(e, link){
			gaTrackEvent('Engagement', 'Print a page');						
		}
	},
	{
		match: clickTrackingLib.getMatchPreset["deloitte-feeds"],
		trackingFunc: function(e, link){
			gaTrackEvent('Engagement', link.href);
		}
	},
	{
		match: clickTrackingLib.getMatchPreset["pdf"],
		trackingFunc: function(e, link){
      gaTrackEvent('PDF Files',	link.pathname.match(/[^\/]*\.pdf/i)[0]);
		}
	},
	{
		match: clickTrackingLib.getMatchPreset["deloitte-social-media"],
		trackingFunc: function(e, link){
      var action = link.hostname.match(/(Facebook|Twitter|Youtube)/i)[0];
			gaTrackEvent('Social Media', action);
		}
	},
	{
		match: clickTrackingLib.getMatchPreset["deloitte-addthis"],
		trackingFunc: function(e, link){
			gaTrackEvent('Social Media', "AddThis");
		}
	},
	{
		match: clickTrackingLib.getMatchPreset["deloitte-podcast"],
		trackingFunc: function(e, link){
			gaTrackEvent('Podcasts', link.innerHTML, link.pathname);
		}
	},
	{
		match: clickTrackingLib.getMatchPreset["deloitte-events"],
		trackingFunc: function(e, link){
			//gaTrackEvent('Events', document.title, link.href);
      gaTrackEvent('Events', 'Outbound registrations', document.title);
		}
	},
	{
		match: clickTrackingLib.getMatchPreset["deloitte-weekly-tax-higlights"],
		trackingFunc: function(e, link){
			gaTrackEvent('Weekly Tax Highlights', link.href);
		}
	}
];

test.addMatches( matchAry );
test.attachTrackingFunctions(null,50);
});
