/*
Plugin Name: OgreAlert
Plugin URI: https://plugins.cleverogre.com/plugin/ogrealert/
Description: OgreAlert is a plugin developed by CleverOgre in Pensacola, Florida.
Version: 0.1.9
Author: CleverOgre
Author URI: https://cleverogre.com/
Icon1x: https://plugins.cleverogre.com/plugin/ogrealert/?asset=icon-sm
Icon2x: https://plugins.cleverogre.com/plugin/ogrealert/?asset=icon
BannerHigh: https://plugins.cleverogre.com/plugin/ogrealert/?asset=banner
BannerLow: https://plugins.cleverogre.com/plugin/ogrealert/?asset=banner-sm
Text Domain: ogrealert
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Copyright: © 2020 CleverOgre, Inc. All rights reserved.

    .-'''-.
   '   _    \                                                 .---.
 /   /` '.   \                        __.....__               |   |      __.....__
.   |     \  '  .--./)            .-''         '.             |   |  .-''         '.
|   '      |  '/.''\\   .-,.--.  /     .-''"'-.  `.           |   | /     .-''"'-.  `. .-,.--.      .|
\    \     / /| |  | |  |  .-. |/     /________\   \    __    |   |/     /________\   \|  .-. |   .' |_
 `.   ` ..' /  \`-' /   | |  | ||                  | .:--.'.  |   ||                  || |  | | .'     |
    '-...-'`   /("'`    | |  | |\    .-------------'/ |   \ | |   |\    .-------------'| |  | |'--.  .-'
               \ '---.  | |  '-  \    '-.____...---.`" __ | | |   | \    '-.____...---.| |  '-    |  |
                /'""'.\ | |       `.             .'  .'.''| | |   |  `.             .' | |        |  |
               ||     ||| |         `''-...... -'   / /   | |_'---'    `''-...... -'   | |        |  '.'
               \'. __// |_|                         \ \._,\ '/                         |_|        |   /
                `'---'                               `--'  `"                                     `'-'
*/

jQuery(function ($) {
    if (typeof ogrealert === 'undefined' || ogrealert == null || ogrealert.length <= 0) return falase;

    // Cookie functions

    function createCookie(name, value, seconds) {
        var expires = "";
        if (seconds) {
            var date = new Date();
            date.setTime(date.getTime() + (seconds * 1000));
            expires = "; expires=" + date.toUTCString();
        }
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    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 eraseCookie(name) {
        createCookie(name, "", -1 * 24 * 60 * 60); // -1 days
    }

    // Message Functions

    function validMessage(message) {
        if (message === null || message === false || message.length <= 0) {
            return false;
        }

        var cookieTime = readCookie('ogrealert_' + message.attr('data-id'));
        if (cookieTime !== null) cookieTime = parseInt(cookieTime, 10);
        if (isNaN(cookieTime) || cookieTime === null) cookieTime = false;

        if (message.attr('data-dismiss-type') != 'none' && cookieTime != false && Math.floor(Date.now() / 1000) - cookieTime < parseInt(message.attr('data-dismiss-duration'), 10)) {
            removeMessage(message);
            return false;
        } else {
            message.show();
            return true;
        }
    }

    function openMessage(message) {
        if (message === null || message === false || message.length <= 0) {
            return false;
        }

        message.addClass('ogrealert-message-active ogrealert-message-open');

        return true;
    }

    function closeMessage(message) {
        if (message === null || message === false || message.length <= 0) {
            return false;
        }

        createCookie('ogrealert_' + message.attr('data-id'), Math.floor(Date.now() / 1000), parseInt(message.attr('data-dismiss-duration'), 10)); // Save Cookie

        message.removeClass('ogrealert-message-open');

        messageAnimation(message).then(function () {
            message.removeClass('ogrealert-message-active');
            removeMessage(message);
        });

        return true;
    }

    function messageAnimation(message) {
        var d = $.Deferred();

        message.addClass('ogrealert-message-transition');
        message.addClass('ogrealert-message-transition-' + ogrealert.transition_animation);

        switch (ogrealert.transition_animation) {
            case 'fade':
                message.fadeOut(ogrealert.transition_duration, function () {
                    message.removeClass('ogrealert-message-transition');
                    message.removeClass('ogrealert-message-transition-' + ogrealert.transition_animation);

                    d.resolve(ogrealert.transition_animation);
                });
                break;
            case 'custom':
                setTimeout(function () {
                    message.removeClass('ogrealert-message-transition');
                    message.removeClass('ogrealert-message-transition-' + ogrealert.transition_animation);

                    d.resolve(ogrealert.transition_animation);
                }, ogrealert.transition_duration);
                break;
            case 'slide':
            default:
                message.slideUp(ogrealert.transition_duration, function () {
                    message.removeClass('ogrealert-message-transition');
                    message.removeClass('ogrealert-message-transition-' + ogrealert.transition_animation);

                    d.resolve(ogrealert.transition_animation);
                });
                break;
        }

        return d.promise();
    }

    function removeMessage(message) {
        if (message === null || message === false || message.length <= 0) {
            return false;
        }

        message.parent().remove();

        if ($('section.ogrealert-messages ul.ogrealert-messages__list').children().length <= 0) {
            $('section.ogrealert-messages').remove();
        }
    }

    // Events

    function dismissClick(e, message) {
        e.preventDefault();
        closeMessage(message);
        return true;
    }

    function documentReady() {
        // Setup Elements

        var messages = $('article.ogrealert-message');
        if (messages === null || messages === false || messages.length <= 0) {
            return;
        }

        messages.each(function () {
            var message = $(this);

            if (!validMessage(message)) {
                return;
            }

            var dismiss = message.find('a.ogrealert-dismiss');
            if (dismiss === null || dismiss === false || dismiss.length <= 0) {
                return;
            }

            // Setup Events
            if (typeof $.fn.on != 'undefined') {
                dismiss.on('click', function (e) { return dismissClick(e, message); });
            } else {
                dismiss.click(function (e) { return dismissClick(e, message); });
            }

            // Open Message
            openMessage(message);
        });
    }

    $(document).ready(documentReady);
});
