// JavaScript Document
function setCookie(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/";
}
// This functions reads & returns the cookie value of the specified cookie (by cookie name)
function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}

function popup() {
	// Put popup code here
	$("#whoopsPanel").fadeIn();
}

// timer init
var ms = 180000; // 3 minutes
var dtNow = new Date();
var strNow = new Date().toString();

// initialize timer only if cookie does not exist
if (getCookie("PopedUp") == "") {
    setCookie("PopedUp", strNow, 7);

    // wait x milliseconds for popup
    setTimeout(popup, ms);

}
else {
    var strCookie = getCookie("PopedUp");
    var dtCookie = new Date(strCookie);
    var diff = dtNow.getTime() - dtCookie.getTime();

    if (diff < ms) {
        // wait the remaining time since the cookie has been set
        setTimeout(popup, ms - diff);
    }
}

var docHeight, docWidth, messageOffset;
$(document).ready(function() {
	docHeight = $(document).height();
	docWidth = $(document).width();
	messageOffset = (docWidth/2)-200;
	$("#whoopsMessage").css('left', messageOffset);
	$("#whoopsPanel").css('height', docHeight);
	$("#whoopsPanel").css('width', docWidth);
	$("#whoopsPanel").click(function() {
		$("#whoopsPanel").fadeOut();
	});
});

$(window).resize(function() {
	docHeight = $(document).height();
	docWidth = $(document).width();
	$("#whoopsPanel").css('height', docHeight);
	$("#whoopsPanel").css('width', docWidth);
	messageOffset = (docWidth/2)-200;
	$("#whoopsMessage").css('left', messageOffset);
});
