/*
rules:
  * banner display initially displays after 'initial_display_interval' interval
  * banner message image swaps after 'swap_interval' interval (if active and not closed)
  * banner message image swaps 'max_swap' number of times
  * banner reshows after a close after 'reshow_interval' interval
  * banner reshows 'max_reshows' number of times
  * current datetime within time windows if time rule is active
  * timer runs every 'timer_interval' milliseconds to check status
time intervals:
  initial banner display - 40s
  swap reminder - 2m
  reshow after closed - 3m
session cookies:
  www.creativeinfo.net.banner.time.session	// initial timestamp (session start)
  www.creativeinfo.net.banner.time.display	// timestamp banner shown
  www.creativeinfo.net.banner.time.closed	// timestamp banner closed
  www.creativeinfo.net.banner.count.swap	// swap count for session
  www.creativeinfo.net.banner.count.reshow	// reshow count for session
*/
/* getValue(key) contains static, default values used in display rules */
function getValue(key)
{
	var value = null;
	switch(key)
	{
		// initial display interval in seconds
		case "initial_display_interval":
			//value = 4;
			value = 5;
			break
		// active swap display interval in seconds
		case "swap_interval":
			//value = 7;
			value = 0;
			break
		// closed reshow display interval in seconds
		case "reshow_interval":
			//value = 7;
			value = 180;
			break
		// number of times to swap
		case "max_swap":
			value = 0;
			break
		// number of times to reshow
		case "max_reshow":
			value = 2;
			break
		// timer interval in ms
		case "timer_interval":
			value = 5000;
			break
		// time rule active (1) /inactive (0)
		case "time_rule_active":
			value = "0";
			break
		// days the time rule is active
		// 0 = SUN, .. , 6 = SAT
		case "time_rule_days":
			value = "12345";
			break
		// time start if time rule active
		// use UTC time; +5 hours for CDT
		// 14 = 9AM
		case "time_start":
			value = "14";
			break
		// time end if time rule inactive
		// use UTC time; +5 hours for CDT
		// 22 = 5PM
		case "time_end":
			value = "22";
			break
		// base url and image for banner message a
		case "banner_msg_img_a":
			value = "";
			break
		// base url and image for banner message b
		case "banner_msg_img_b":
//			value = "/images/naqPrompt/bannerMessage_b.png";
			value = "";
			break
	}
	return value;
}
/* main function each page calls to start timer for page */
function display_banner_start()
{
	// check whether to initially show or hide display
	if (getDisplayTime() != null && getClosedTime() == null)
		showDisplayBanner();
	else
		hideDisplayBanner();
	// check timestamp
	timestamp();
	// start timer
	timer();
}
/* hides banner display */
function hideDisplayBanner()
{
	$("#catfish").hide();
}
/* shows banner display (sets visible) */
function showDisplayBanner()
{
	$("#catfish").show();
}
/* shows and hides banner display based on current state */
function toggleBannerDisplay()
{
	$("#catfish").slideToggle("normal");
}
/* manages session start timestamp */
function timestamp()
{
	// check to see if we have a current, valid timestamp cookie
	if ($.cookie('www.creativeinfo.net.banner.time.session') == null)
		// no existing cookie, so set with current timestamp
		setSessionStart(new Date());
	else
	{
		// cookie exists, get age (in milliseconds)
		var age = diff(new Date(getSessionStart()));
		// check age, if older than 2 hours (7200s) reset 
		if (age > 7200)
		{
			setSessionStart(new Date());
			setDisplayTime(null);
			setClosedTime(null);
		}
	}
}
/* sets cookie value for session start time */
function setSessionStart(value)
{
	$.cookie('www.creativeinfo.net.banner.time.session', value);	
}
/* returns cookie value for session start time */
function getSessionStart()
{
	return $.cookie('www.creativeinfo.net.banner.time.session');
}
/* sets cookie value for last display time */
function setDisplayTime(value)
{
	$.cookie('www.creativeinfo.net.banner.time.display', value);
}
/* returns cookie value for last display time */
function getDisplayTime()
{
	return $.cookie('www.creativeinfo.net.banner.time.display');
}
/* sets cookie value for closed time */
function setClosedTime(value)
{
	$.cookie('www.creativeinfo.net.banner.time.closed', value);
}
/* returns cookie value for closed time */
function getClosedTime()
{
	return $.cookie('www.creativeinfo.net.banner.time.closed');
}
/* sets cookie value for swap count */
function incrementSwapCount()
{
	if (getSwapCount() != null)
		$.cookie('www.creativeinfo.net.banner.count.swap', parseInt(getSwapCount())+1);
	else
		$.cookie('www.creativeinfo.net.banner.count.swap', 1);
}
/* returns cookie value for swap count */
function getSwapCount()
{
	if ($.cookie('www.creativeinfo.net.banner.count.swap') == null)
		return 0;
	else
		return parseInt($.cookie('www.creativeinfo.net.banner.count.swap'));
}
/* sets cookie value for reshow count */
function incrementReshowCount()
{
	if (getReshowCount() != null)
		$.cookie('www.creativeinfo.net.banner.count.reshow', parseInt(getReshowCount())+1);
	else
		$.cookie('www.creativeinfo.net.banner.count.reshow', 1);
}
/* returns cookie value for reshow count */
function getReshowCount()
{
	if ($.cookie('www.creativeinfo.net.banner.count.reshow') == null)
		return 0;
	else
		return parseInt($.cookie('www.creativeinfo.net.banner.count.reshow'));
}
/* returns of a diff (in seconds) of current timestamp to parameter */
function diff(timestamp)
{
	return (new Date().getTime()/1000 - new Date(timestamp).getTime()/1000);
}

/* checks for the time rule, and if active checks time properties */
function timeRuleActive(date)
{
	var active = 0;
	if (getValue("time_rule_active") == 0)
	{
		active = 1;
	}
	else if ((getValue("time_rule_active") == 1) && 
			(getValue("time_rule_days").toString().indexOf(date.getUTCDay().toString()) != -1) &&
			(date.getUTCHours() >= parseInt(getValue("time_start"))) && 
			(date.getUTCHours() <= parseInt(getValue("time_end"))))
	{
		active = 1;
	}
	return active;
}
/* main function containing rules/logic for displaying banner popup */
function display()
{
	// get current datetime object
	var date = new Date();
	// time check - if time rule is active and we are in the window
	if (timeRuleActive(date) == 1)
	{
		// initial display logic
		if (getDisplayTime() == null && getClosedTime() == null)
		{
			if (getSessionStart() != null && diff(getSessionStart()) > parseInt(getValue("initial_display_interval")))
			{
				setDisplayTime(date);
				toggleBannerDisplay();
				return;
			}
		}
		// swap logic
		if (getDisplayTime() != null && getClosedTime() == null && getSwapCount() <= parseInt(getValue("max_swap")))
		{
			if (diff(getDisplayTime()) > parseInt(getValue("swap_interval")))
			{
				setDisplayTime(date);
				incrementSwapCount();
				$("#bannerMessageImage").fadeOut("slow", function() {
					if ($("#bannerMessageImage").attr("src") == getValue("banner_msg_img_a"))
						$("#bannerMessageImage").attr("src", getValue("banner_msg_img_b"));
					else
						$("#bannerMessageImage").attr("src", getValue("banner_msg_img_a"));
					$("#bannerMessageImage").fadeIn("slow"); }
				);
				return;
			}
		}
		// reshow logic
		if (getClosedTime() != null && getDisplayTime() == null)
		{
			if (diff(getClosedTime()) > parseInt(getValue("reshow_interval")) && 
				getReshowCount() <= parseInt(getValue("max_reshow")))
			{
				setClosedTime(null);
				setDisplayTime(date);
				incrementReshowCount();
				$.cookie('www.creativeinfo.net.banner.count.swap', 0);
				toggleBannerDisplay();
				return;
			}
		}
	}
}
/* function called clicking the close link in the ui; updates cookies and hides display */
function closeDisplay()
{
	setClosedTime(new Date());
	setDisplayTime(null);
	toggleBannerDisplay();
}
/* timer function that calls the display logic at defined interval */
function timer()
{
	$.timer(getValue("timer_interval"), function (timer) {
		display();
	    timer.reset(getValue("timer_interval"));
	});
}
/* reset (nullify) all session cookies for banner display */
function reset()
{
	// explicitly clear (null/zero) cookie values
	$.cookie('www.creativeinfo.net.banner.time.session', null);
	$.cookie('www.creativeinfo.net.banner.time.display', null);
	$.cookie('www.creativeinfo.net.banner.time.closed', null);
	$.cookie('www.creativeinfo.net.banner.count.reshow', 0);
	$.cookie('www.creativeinfo.net.banner.count.swap', 0);
}

