var t;

var linkFunction = function(){
	
	var externalLink = $(this).attr("href").split('//')[1]
	
	
	if(document.domain != externalLink){
		if(document.domain != externalLink.split('.')[1]){
			comfirmLink($(this).attr("href"));
			return false;
		}else{
			return true;	
		}
	}
};

function comfirmLink(a){
	window.eLink = a;
	
	$.blockUI({
		message:
			$("<div id='disclaimer' style='display:none'>\
	 <p>You are leaving www.thevisioncareinstitute.co.uk and are being automatically redirected to: </p><p class='redirect'>"+a+"</p> <p>This link will take you to a web site to which our Privacy Policy does not apply. You are solely responsible for your interactions with that web site. We encourage you to read the <a href='privacy.aspx'>privacy policy</a> of every web site you visit.<br /></p>\
		<div class='buttons'> If your browser does not take you there in a few moments, please click <br/>\
			[ <a href='javascript:void(0);' onclick='goToLink(); clearTimeout(t); return false'>Confirm</a> ] or you may\
			[ <a href='javascript:void(0);' onclick='unblockWindow(); clearTimeout(t); return false'>Cancel</a> ].\
		<br/><br/></div></div>" ),
			css: {opacity: '0.95', cursor:'auto', width:'460px',height:'auto',border:'1px solid #0E1215'},
	        overlayCSS: {backgroundColor:'#fff', opacity:'0',cursor: 'auto'}  
	});
	t = setTimeout("goToLink()",8000);
}


function unblockWindow(){
	setTimeout(jQuery.unblockUI, 1);
}

function goToLink() {
	unblockWindow();
	window.open(window.eLink);
}


$(window).bind("load", function() {

	$("a[href]").each(function(e) {
		var href = $(this).attr('href');
		var clHref = $(this).attr('class');

		if(href.indexOf('http') != -1)
		{
			var currentHrefArr = location.href.split("/");
			var linkHrefArr = href.split("/");		
			var classHrefArr = clHref;
					
			if(currentHrefArr[2] != linkHrefArr[2]) {
				switch (clHref) {
				    // Mark links as privacyPolicyException if the privacy policy DOES apply to them
				    case "privacyPolicyException":
				    break
					case "buildAppWindow":
  						$(this).click(function(){
							buildAppWindow();
							return false;
						});
					break			  
					case "acuminder":
						$(this).click(function(){
							buildAcuminderApp();
							return false;
						});
					break
					case "AppEn":
						$(this).click(function(){
							buildRebatesApp('https://www.jnjvision.com/acuvueRebate/rebate.do?name=allbrands2008&hl=en');
							return false;
						});
					break
					case "AppEs":
						$(this).click(function(){
							buildRebatesApp('https://www.jnjvision.com/acuvueRebate/rebate.do?name=allbrands2008&hl=es');
							return false;
						});
					break				
				    default:
						$(this).click(linkFunction);
				}
			}
		}
	});
	
});
	