var defaultSubscribeText = 'e-mail address';
var activationTimer = null;
var pageTracker = null;
var visiblePopup = null;	
var visiblePopupCouponId = null;
var hidePopupOnMouseOut = false;
var couponMouseOutTimer = null;
var couponMouseOutId = null;
var lastRecipient = '';


function focusSubscribeBox (box) {
	box.style.color='#000';

	if (box.value == defaultSubscribeText) {
		box.value = '';
	}
}


function blurSubscribeBox (box) {
	if (!box.value) {
		box.style.color='#aaa';
		box.value = defaultSubscribeText;
	}
}		


function logClick (url) {
	if (pageTracker) {
		//Google Analytics has finished loading
		pageTracker._trackPageview('/outgoing/' . url);
	}
	else {
		//do nothing
	}
}


function mouseOverCoupon (ev, couponId) {
	var div = document.getElementById('coupon' + couponId);
	var children = div.getElementsByTagName('*');
	
	ev = (ev || window.event);
	stopEvent(ev);
	
	if (couponMouseOutTimer && couponMouseOutId == couponId) {
		//prevent false mouseout events from hiding stuff
		clearTimeout(couponMouseOutTimer);
		couponMouseOutId = null;
	}

	setCouponOptions(couponId, 'visible');
	
	//console.log("Mouse over");
}


function mouseOutCoupon (ev, couponId) {

	ev = (ev || window.event);
	stopEvent(ev);

	couponMouseOutId = couponId;

	couponMouseOutTimer = setTimeout(
		function () {
			if (visiblePopupCouponId != couponId) {
				setCouponOptions(couponId, 'hidden');
			}

			//console.log("Mouse out");
			
			/*
			if (hidePopupOnMouseOut) {
				hidePopups();
			}
			*/
		},
		10
	);
	
}


function setCouponOptions (couponId, visibility) {
	var div = document.getElementById("couponOptions" + couponId);
	div.style.visibility = visibility;
}


function startActivation (couponId, merchantDomain, affiliateLink, input, ev) {
	
	var rightClick = false;
	
	if (!ev) { ev = window.event; }
	
	input.select();
	
	if (ev.which) { 
		rightclick = (ev.which == 2); 
	}
	else if (ev.button) {
		rightclick = (ev.button == 2);	
	}
	
	if (rightClick) {
		startActivationTimer(couponId, merchantDomain, affiliateLink, 4000); 
	}
	else {
		showPopup("activationInstructions" + couponId, couponId);
		startActivationTimer(couponId, merchantDomain, affiliateLink, 10000); 
	}
}


function rememberLastRecipient (email) { 
	lastRecipient = email;
}


function showEmailPopup (elementId, couponId) {
	var form = eval("document.coupon" + couponId + "EmailForm");
	form.recipient.value = lastRecipient;
	showPopup(elementId, couponId);
	form.recipient.focus();
}


function showPopup (elementId, couponId, hideOnMouseOut) {
	var div = document.getElementById(elementId);
	hidePopups();
	fadeIn(div, 20, 10);
	visiblePopup = div.id;
	visiblePopupCouponId = couponId;
	
	/*
	if (hideOnMouseOut) {
		hidePopupOnMouseOut = true;
	}
	else {
		hidePopupOnMouseOut = false;
	}
	*/
}


function fadeIn (el, steps, delay, mode) {
	//mode == "visibility" or "display"

	if (!mode) {
		mode = "display";
	}

	if (isIE()) {
		el.style.filter = "progid:DXImageTransform.Microsoft.BasicImage(opacity=0)";
		if (mode == "display") {
			el.style.display = 'block';
		}
		else {
			el.style.visibility = 'visible';
		}
		
		for (i=0; i < steps; i++) {
			setTimeout(
				"document.getElementById('" + el.id + "').style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(opacity="+ i/steps +")'",
				i * delay
			);
		}
		setTimeout(
			function () {
				el.style.filter = "progid:DXImageTransform.Microsoft.BasicImage(opacity=1)";
				el.style.filter = null;
			},
			steps * delay
		);		
	}
	else {
		el.style.opacity = 0;
		if (mode == "display") {
			el.style.display = 'block';
		}
		else {
			el.style.visibility = 'visible';
		}
		
		for (i=0; i < steps; i++) {
			setTimeout(
				"document.getElementById('" + el.id + "').style.opacity = " + i/steps, 
				i * delay
			);
		}
		setTimeout(
			function () {
				el.style.opacity = 1;
			},
			steps * delay
		);
	}
}


function handleKeyDownCouponCode (couponId, merchantDomain, affiliateLink, ev) {
	if (!ev) { ev = window.event; }
	var key = ev.keyCode;
	
	if (key) {
		if (ev.ctrlKey || ev.metaKey) {
			if (key == 67) {
				//watching for ctrl-c or command-c
				activateCoupon(couponId, merchantDomain, affiliateLink);
			}
		}
	}
	return true;		
}


function hidePopups () {
	if (visiblePopup) {
		document.getElementById(visiblePopup).style.display = 'none';
	}
	visiblePopup = null;
	visiblePopupCouponId = null;
}


function activateCoupon (couponId, merchantDomain, affiliateLink) {
	logClick(merchantDomain);
	location.href = affiliateLink;
	cancelActivationTimer();
}


function startActivationTimer (couponId, merchantDomain, affiliateLink, ms) {
	cancelActivationTimer();
	
	activationTimer = setTimeout(
		function () {
			activateCoupon(couponId, merchantDomain, affiliateLink);
		}, 
		ms
	);
}


function cancelActivationTimer () {
	if (activationTimer) {
		clearTimeout(activationTimer);
		activationTimer = null;
	}	
}


function isIE () {
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}


function stopEvent (ev) {
	ev = (ev || window.event);
	if (ev) {
		if (ev.stopPropagation) {
			ev.stopPropagation();
		}
		ev.cancelBubble = true;
	}			
}


function startSendEmail (couponId) {
	var hideDiv = document.getElementById("coupon" + couponId + "EmailFormDefault");
	var showDiv = document.getElementById("coupon" + couponId + "EmailFormSending");
	
	hideDiv.style.display = 'none';
	showDiv.style.display = 'block';
}

