﻿function getOutBoundLinks(){
	jQuery('a').each(function(){
		if(jQuery(this).attr('href')){
			var track = false,
				$this = jQuery(this),
				href = $this.attr('href'),
				category = 'outbound',
				event = 'URL:' + href,
				label = location.href,
				linkage = false,
				pageImpression = false;
			
			//external	
			if(href.substr(0, 4) == 'http'){
				track = true;
				if(href.indexOf('https://www.client-inception.co.uk') != -1){
					linkage = true;
					category = 'Apply';
					if(href.indexOf('BG=TDW-SIPP') != -1){
						event = 'apply-SIPP';
					}
					else if(href.indexOf('BG=TDW') != -1){
						event = 'apply-generic';
					}
				}
				else if(href.indexOf('https://www.applyforanaccount.com') != -1){
					linkage = true;
					category = 'Apply';
					event = 'apply-CFD and Spread betting';
				}
				else if(href.indexOf('https://trade.loginandtrade.com') != -1){
					linkage = true;
					category = 'Secure';
					event = 'secure click';
					if(href.indexOf('/itp/siteselector_TDWH.asp') != -1){
						event = 'secure login';
					}
				}
				else if(href.indexOf('https://secure.tdwaterhouse.co.uk') != -1){
					linkage = false;
					category = 'Secure';
					event = 'secure click';
					if(href.indexOf('/webbroker2/login.jsp') != -1){
						event = 'secure login';
					}
				}
				else if(href.indexOf('http://www.trading.tdwaterhouse.co.uk') != -1){
					linkage = true;
				}
				else if(href.indexOf('http://investing.tdwaterhouse.co.uk') != -1){
					linkage = true;
				}
				else if(href.indexOf('http://cfd.tdwaterhouse.co.uk') != -1){
					linkage = true;
				}
				else if(href.indexOf('http://isa.tdwaterhouse.co.uk') != -1){
					linkage = true;
				}
				else if(href.indexOf('http://brand.tdwaterhouse.co.uk') != -1){
					linkage = true;
				}
				else if(href.indexOf('http://funds.tdwaterhouse.co.uk') != -1){
					linkage = true;
				}
			}
			//internal
			else{
				if(href.indexOf('.ashx') != -1 || href.indexOf('.pdf') != -1 || href.indexOf('.xsl') != -1 || href.indexOf('.doc') != -1){
					track = true;
					pageImpression = true;
					category = 'Download';
					event = 'file:' + href.split('/')[href.split('/').length-1];
				}
			}
			
			if(linkage){
				 _gaq.push(function(){
					var tracker = _gaq._getAsyncTracker();
					$this.attr('href', tracker._getLinkerUrl(href));
				});
			}
			
			$this.click(function(){
				if(track){
					_gaq.push(['_trackEvent', category, event, label]);
					
					if(pageImpression){
						_gaq.push(['_trackPageview', href]);
					}
				}
			});
		}
	});
}

function trackThis($node, category, action, linked){
	if($node.attr('href')){
		action = action || 'URL:' + $node.attr('href');
		_gaq.push(['_trackEvent', category, action, location.href]);
		if(linked){
			_gaq.push(['_link', $node.attr('href')]);
		}
	}
}

