jQuery(document).ready(function($) {

    function getNotifications() {
        $.ajax({
            type: "post",
            url: "app/includes/ajax.php",
            data: { action: "get_new_notifications" }
        }).done(function(data) {
            try {
                //add a new style 'foo'
                data = JSON.parse(data);
                if (data.status === 'okay') {
                    $.notify.addStyle('foo', {
                        html: "<div>" +
                            "<div class='clearfix'>" +
                            "<div class='text' data-notify-html='title'/>" +
                            "<div class='buttons'>" +
                            "<button class='no'>Ignore</button>" +
                            "<button class='yes' data-notify-text='button' data-id=" +
                            data.id +
                            " data-action=" +
                            data.action +
                            "></button>" +
                            "</div>" +
                            "</div>" +
                            "</div>"
                    });
                    $.notify({
                        title: data.content,
                        button: 'Take Action'
                    }, {
                        style: 'foo',
                        autoHide: false,
                        clickToHide: false,
                        position: "right bottom"
                    });
                }

            } catch (err) {

            } finally {}
        })
    }

    getNotifications();
    setInterval(getNotifications, 60000);
    //listen for click events from this style
    $(document).on('click', '.notifyjs-foo-base', function() {
        //programmatically trigger propogating hide event
        $(this).trigger('notify-hide');
    });
    $(document).on('dblclick', '.notifyjs-foo-base', function() {
        //show button text
        var action = $(this).find(".yes").data("action");
        //hide notification
        $(this).trigger('notify-hide');

        window.location = action;
    });
})