var humanMsg = {
    setup: function (appendTo, logName, msgOpacity) {
        humanMsg.msgID = 'humanMsg';

        // appendTo is the element the msg is appended to
        if (appendTo == undefined)
            appendTo = 'body';

        // Opacity of the message
        humanMsg.msgOpacity = 0.95;

        if (msgOpacity != undefined)
            humanMsg.msgOpacity = parseFloat(msgOpacity);

        // Inject the message structure
        jQuery(appendTo).append('<div id="' + humanMsg.msgID + '" class="humanMsg"><p></p></div>');
    },

    displayMsg: function (msg) {
        if (msg == '')
            return;

        clearTimeout(humanMsg.t2);

        // Inject message
        jQuery('#' + humanMsg.msgID + ' p').html(msg);

        // work out the center of the screen for it
        var hm = jQuery('#' + humanMsg.msgID);
        var leftToSet = (window.innerWidth - hm.width()) / 2;
        
        // calculate 30% down the screen for the top
        var topToSet = (window.innerHeight - hm.height()) * 0.3;

        hm.css("left", leftToSet);
        hm.css("top", topToSet);

        // Show message
        hm.show().animate({ opacity: humanMsg.msgOpacity }, 200);


        // Watch for mouse & keyboard in 2s
        humanMsg.t1 = setTimeout("humanMsg.bindEvents()", 2000);
        // Remove message after 5s
        humanMsg.t2 = setTimeout("humanMsg.removeMsg()", 5000);
    },

    bindEvents: function () {
        // Remove message if mouse is moved or key is pressed
        jQuery(window)
            .mousemove(humanMsg.removeMsg)
            .click(humanMsg.removeMsg)
            .keypress(humanMsg.removeMsg);
    },

    removeMsg: function () {
        // Unbind mouse & keyboard
        jQuery(window)
            .unbind('mousemove', humanMsg.removeMsg)
            .unbind('click', humanMsg.removeMsg)
            .unbind('keypress', humanMsg.removeMsg);

        // If message is fully transparent, fade it out
        jQuery('#' + humanMsg.msgID).animate({ opacity: 0 }, 500, function () { jQuery('#' + humanMsg.msgID).hide(); });

    }
};

jQuery(document).ready(function () {
    humanMsg.setup();
});
