﻿/* Function definitions */
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

// constants definitions
var POPUPMSG_COOKIE = '__PopupMessage';
var POPUPMSG_ELEMENT = '#popup-message';

// act when document is fully loaded
jQuery(document).ready(function() {
    // read the cookie
    var msg = readCookie(POPUPMSG_COOKIE);
    if (msg) {

        // set the message and show it
        jQuery(POPUPMSG_ELEMENT).html(msg);
        jQuery(POPUPMSG_ELEMENT).show();
        // hide the message after X seconds or when the box is clicked
        setTimeout(function() { jQuery(POPUPMSG_ELEMENT).hide() }, 10000);
        jQuery(POPUPMSG_ELEMENT).click(function() {
            jQuery(POPUPMSG_ELEMENT).hide();
        });

        // now erase the cookie
        eraseCookie(POPUPMSG_COOKIE);
    }
});
