// LR GWS Tracking Script v4.0

/*----------------------------------------
  Required Files
  1. /system/logging/google.js (this file)
  2. /c/script/jquery.cookie.js
  3. /system/logging/gaconfig.js
  4. /c/script/jquery.js
  5. /system/logging/jquery.google.js
  6. /system/logging/jquery.google.marketmap.qa.js
  7. /system/logging/jquery.google.marketmap.js
  8. /system/logging/jquery.google.forms.js

/*----------------------------------------
1. Document Domain
----------------------------------------*/
document.domain='landrover.com';

/*----------------------------------------
2. Script Path
----------------------------------------*/
var tPath = '/system/logging';
var gaTracking = null;

/*----------------------------------------
3. Blackberry Mobile script
----------------------------------------*/
var ua = navigator.userAgent.toLowerCase();
var m = location.pathname.substr(0,10);
if(ua.indexOf('blackberry') != -1 && m.indexOf('/us/en/') != -1) {
	location.href = 'http://landrover.mobi/?c=redirect';
} else if (ua.indexOf('blackberry') != -1 && m.indexOf('/za/en/') != -1) {
	if(m.indexOf('/rr/') != -1) {
		location.href = 'http://mobi.rangerover.co.za/';
	};
} else if (ua.indexOf('blackberry') != -1 && m.indexOf('/gb/en/') != -1) {
	location.href = 'http://m.landrover.co.uk';
};

/*----------------------------------------
4. Mobile Script
----------------------------------------*/
if(m.indexOf('/us/en/') != -1 || m.indexOf('/za/en/') != -1 || m.indexOf('/gb/en/') != -1) {
	var mobileDeviceNames = "ACER|Alcatel|AUDIOVOX|BlackBerry|CDM|Ericsson|iPhone|LG\/b|LGE|Motorola|MOT|NEC|Nokia|Panasonic|QCI|SAGEM|SAMSUNG|SEC|Sanyo|Sendo|SHARP|SIE-|SonyEricsson|Telit|Telit_mobile_Terminals|TSM|MMP|webOS|Android|Droid|HTC|mobile|Opera|Symbian|IEMobile";
	var findMobileAgent = mobileDeviceNames.split("|");
	if (screen.width < 700) {
		for(var a=findMobileAgent.length -1; a >= 0; --a) {
			if(ua.indexOf(findMobileAgent[a].toLowerCase()) != -1) {
				if(m.indexOf('/us/en/') != -1) {
					location.href='http://landrover.mobi/?c=redirect';
				} else if (m.indexOf('/za/en/rr/') != -1) {
					location.href='http://mobi.rangerover.co.za';
				} else if (m.indexOf('/gb/en/') != -1) {
					location.href='http://m.landrover.co.uk';
				};
			};
		};
	};
};

/*----------------------------------------
4. Obsolete Functions
----------------------------------------*/
function sbe(url) {
};

function trackEventGH(loc, val) {
};

function trackurl(url) {
	gaTracking = url;
};

/*----------------------------------------
4. Google Analytics variables
----------------------------------------*/
{
	var gaDone = false;
	document.write("<scr"+"ipt language='Javascript' type='text/javascript' src='"+tPath+"/gaconfig.js' onload='gaContinue(this);' onreadystatechange='gaContinue(this);'></scr"+"ipt>");

	function gaContinue(elem) {
		if ( !gaDone &&
			(!elem.readyState || elem.readyState == "loaded" || elem.readyState == "complete") ) {
				gaDone = true;
				launchGATracking('false');
				elem.onload = elem.onreadystatechange = null;
			}
	}
};

/*----------------------------------------
4. Load main script when jQuery Available
----------------------------------------*/
function launchGATracking(val) {
	if(val == 'false') {
		if(typeof jQuery == 'undefined') {
			window.setTimeout("launchGATracking('false');",500);
		} else {
			launchGATracking('true');
		};
	} else if(val == 'true') {
		if(typeof gaConfig != 'undefined') {
			$.ajax({
				url: tPath + '/jquery.google.js',
				type: 'GET',
				dataType: 'script',
				cache: true
			});
		} else {
			window.setTimeout("launchGATracking('true')",500);
		};
	}
}
