$(document).ready(function() {
	var currBuyNowLink = null; // clicked 'buy' link
	
	var hasBuyNowCookie = function() {
		return (location.host == "" || document.cookie && document.cookie.indexOf("buynowviewed") > -1);
	};

	var setBuyNowCookie = function() {
		var d = new Date();
		d = new Date(d.getTime() + 1000*60*60*24*7); // expire in 1 week
		var domain = location.host.replace(/.*\.(\w+\.\w+)$/, "$1"); // loosen domian to e.g. intel.com
		document.cookie = "buynowviewed=true; domain=" + domain + "; expires=" + d.toGMTString();
	};

	$("#shp-now-now-continue, #shp-buy-now-popup .close").click(function(event) {
		$("#shp-buy-now-popup, #popup-screen").hide();
	});

	$("#shp-now-now-continue").click(function() { setBuyNowCookie(); });
	
	$("h3.third-party a[href^=http]").bind('click',function(e) { // this selector may need to be changed
		if (hasBuyNowCookie()) { return; }
		
		/* no cookie */
		e.stopPropagation();
		e.preventDefault();
		
		$("#shp-now-now-continue").attr({ href: this.href, target: this.target });
		
		$("#popup-screen").css({ height: $(document).height(), width: $(document).width() }).show();
		$("#shp-buy-now-popup").css({ left: ($(window).width() * 0.5) - 307, zIndex: 99999999 }).show();
		window.scrollTo(0,0);
	});
	
	$("p.shop-next-desktop a[href^=http]").bind('click',function(e) { // this selector may need to be changed
		if (hasBuyNowCookie()) { return; }
		
		/* no cookie */
		e.stopPropagation();
		e.preventDefault();
		
		$("#shp-now-now-continue").attr({ href: this.href, target: this.target });
		
		$("#popup-screen").css({ height: $(document).height(), width: $(document).width() }).show();
		$("#shp-buy-now-popup").css({ left: ($(window).width() * 0.5) - 307, zIndex: 99999999 }).show();
		window.scrollTo(0,0);
	});
	
	$("p.shop-next-mobile a[href^=http]").bind('click',function(e) { // this selector may need to be changed
		if (hasBuyNowCookie()) { return; }
		
		/* no cookie */
		e.stopPropagation();
		e.preventDefault();
		
		$("#shp-now-now-continue").attr({ href: this.href, target: this.target });
		
		$("#popup-screen").css({ height: $(document).height(), width: $(document).width() }).show();
		$("#shp-buy-now-popup").css({ left: ($(window).width() * 0.5) - 307, zIndex: 99999999 }).show();
		window.scrollTo(0,0);
	});
	
	$("#shp-buy-now-popup .show-full-disclaimer").click(function(e) {
		e.preventDefault();
		$("#shp-buy-now-extra-warnings").toggle();
	});
});

