(function($) {

	$.fn.externalLinks = function(settings) {
		
		settings = jQuery.extend({
			// Overlay copy defaults - localise in footer.jsp
			titleAlpha: 'You\'re about to leave disney.co.uk',	
			copyAlpha:	'Different terms of use and privacy policy will apply.',
			closeButtonCopy: 'Close',
			backButtonCopy: 'Go back',
			continueButtonCopy: 'Continue'
		},settings);
		
		
		
		//DISNEY OVERLAY OUTBOUND LINKS
		this.click(function (event) {
				
			//stops the link doing anything whilst in prescence of js
			//event.preventDefault();
		
			var outboundLink = $(this).attr("href");
			//alert(outboundLink);	
			
			//print the overlay
			$("body").append($('<div id="outboundContainer"></div><div id="outboundSection"><div id="outboundContent"><h4>'+ settings.titleAlpha +'<span>'+ settings.titleBeta +'</span></h4><p class="copy">'+ settings.copyAlpha +'</p><p id="outboundGoBack"><a class="outboundNav outboundCloseLink" href="#" title="'+ settings.backButtonCopy +'">'+ settings.backButtonCopy +'</a></p><p id="outboundContinue"><a id="outBoundContinue" class="outboundNav outboundCloseLink" href="' + outboundLink + '" title="'+ settings.continueButtonCopy +'" target="_blank">'+ settings.continueButtonCopy +'</a></p><p id="outboundClose"><a class="outboundCloseLink" href="#" title="'+ settings.closeButtonCopy +'"><span>'+ settings.closeButtonCopy +'</span></a></p></div></div>'));
			
			//size the overlay to document
			$('#outboundContainer').css({'height':($(document).height())+'px'});
			$(window).resize(function(){$('#outboundContainer').css({'height':($(document).height())+'px'});});
			
			//$('#outboundSection').css({'top':($(window).height()/2)+'px'});
			//$(window).resize(function(){$('#outboundSection').css({'top':($(window).height()/2)+'px'});});
			
			//CSS hides from search engines 
			$("embed, object, select, iframe").hide();
			$("div#outboundContainer").show();
			$("div#outboundSection").fadeIn();



			$("a.outboundCloseLink").click(function () {
				//$window.open(this.href); NOT WORKING
				//removes appended
				$("div#outboundSection").remove();
				$("div#outboundContainer").remove();
				$("embed, object, select, iframe").show();

	  		});
			
			//$("a#outBoundContinue").attr("href", outboundLink ); THIS ALSO WORKS AS POST APPEND TO ABOVE CODE
	
			var arrPageSize = ___getPageSize();
			var arrPageScroll = ___getPageScroll();
			$("#outboundSection").css({
				top:arrPageScroll[1] + ((arrPageSize[3] / 2)-40 )
			});
			
		return false;
			
  		});
		
		
		//CREATE AN ARRAY OF PAGE SCROLL VALUES FOR PAGE HEIGHT AND WIDTH
		function ___getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
		

		//GET AN ARRAY OF PAGE SCROLL VALUES
		function ___getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {// Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};
		
		
		
	
	};
})(jQuery);
