//var jsTrustedUrlsVar = 'http://jaguar.tagworldwide.com,http://accessories.jaguar.com,http://bluetooth.jaguar.com,http://www.jaguarlandrovercareers.com,http://www.jaguarcollection.co.uk/,http://accessories.jaguar.com,http://www.jaguarclassicparts.com,http://www.jaguar.com,http://forms.jaguar.com,http://preview01.jaguar.com,http://global.jaguar.com,http://www.bluetooth.jaguar.com,http://rules.config.jaguar.com,http://www.jaguarcarinsurance.co.uk,https://javaprod.euroaccoutservices.com,http://jaguarauc.2ndbyte.com,http://bluetooth.jaguar.co.uk,https://jaguarukforms.aplicor.net,http://used.jaguar.co.uk';

//var sbExternalWarnTitleLabel = 'WARNING';
//var sbExternalWarnMessageLabel = 'You are about to leave Jaguar.com. Please note that Jaguar cannot be held responsible for any content or validity outside of this domain. Please click on Accept to go ahead, or on Reject to close this window.';
//var sbExternalWarnContinueLabel = 'ACCEPT';
//var sbExternalWarnCancelLabel = 'REJECT';


$(function(){
	/* Only check links that start with "http://", so that links
	   to anchors within the current page are not checked needlessly */
	$('a[href^="http://"]').click(function(){
		return checkLink(this);
	});
});


function checkLink(elem) {
	var trustedLink = false;
	
	// Split the trusted urls list (jsTrustedUrlsVar) into an array
	var trustedUrls = jsTrustedUrlsVar.split(',');
	
	// Cache the "href" attribute of the link so it doesnt have to be re-fetched within the loop
	var href = $(elem).attr('href');
	$(trustedUrls).each(function(){
		if (href.indexOf(this) != -1){
			trustedLink = true;
		}
	});
	if (trustedLink == true) {
		return true;
	} else {
		showExtLinkConfirm(elem);
		return false;
	}
}

function showExtLinkConfirm(elem) {
	var confDialogue = $('#dialog-confirm-ext-link')
	if(confDialogue.length < 1) {
		$('body').append('<div id="dialog-confirm-ext-link" title="'+sbExternalWarnTitleLabel+'">'+
			'<p>'+sbExternalWarnMessageLabel+'</p>'+
		'</div>')
	}
	
	var dialogueButtons = {};
	dialogueButtons[sbExternalWarnContinueLabel] = function() {
		$(this).dialog('close');
		var target = $(elem).attr('target');
		if (target == '_blank') {
			window.open($(elem).attr('href'));
		} else {
			window.location.href = $(elem).attr('href');
		}
	};
	dialogueButtons[sbExternalWarnCancelLabel] = function() {
		$(this).dialog('close');
		return false;
	}
	
	$( "#dialog-confirm-ext-link" ).dialog({
		resizable: false,
		height:140,
		modal: true,
		buttons: dialogueButtons
	});
}
