$(function () {
    $(document).click(function () {
        $('.notifications-panel').addClass("hidden");
    });
    $('.get-notifications-panel').click(function (e) {
        e.stopPropagation();
        $('.notifications-panel').toggleClass("hidden");
        $('.notifications-panel ul').empty();
        $.getJSON("/Notifications/GetUnread/", function (result) {
            $.each(result, function (i, field) {
                $('.notifications-panel ul').append('<li><a href="/Notifications/Readed?id='+field.Id+'&link=' + field.Link + '"><h4><img src="' + field.Logo + '" />' + field.Title + '</h4>' + field.Description + '</a></li>');

            });
        });
    });

    var notiCount = parseInt($('.get-notifications-panel').text(), 10);
    if (notiCount > 0) {
        $('.get-notifications-panel .badge').removeClass("hidden");
    }
});

(function ($) {
    $.fn.CookieNotify = function () {
        var cookie = document.cookie.indexOf("AllowCookie");
        if (cookie == -1) {
            $('.cookie-info').show();
        }
        if (cookie > -1) {
            CookieAccept();
        }
        return this;
    };
}(jQuery));

function CookieAccept() {
    var d = new Date();
    d.setTime(d.getTime() + (7 * 24 * 60 * 60 * 1000));
    document.cookie = "AllowCookie=true; expires=" + d.toUTCString() + "; path=/";
    $('.cookie-info').fadeOut({
        duration: 500
    });
    return this;
};

function notifyapi(title, message, img, url) {
    console.log('Notification url: ' + url)
    if (!title || !message) {
        return null;
    }
    if (!("Notification" in window)) {
        alert("Ez a böngésző nem támogatja az értesítéseket");
    }
    else if (Notification.permission === "granted") {
        var options = {
            body: message,
            lang: 'hu-HU',
            icon: img,
            dir: "ltr"
        };
        var notification = new Notification(title, options);
        console.log('Notifiacation icon: '+notification.icon);
        notification.onclick = function (e) {
            window.location.href = url;
            
            notification.close();
        };
    }
    else if (Notification.permission !== 'denied') {
        Notification.requestPermission(function (permission) {
            if (!('permission' in Notification)) {
                Notification.permission = permission;
            }

            if (permission == "granted") {
                var options = {
                    body: "Engedélyezted az értesítéseket",
                    icon: "Content/img/DefaultNotification.png",
                    dir: "ltr"
                };
                var notification = new Notification("Értesítés", options);
            }
        });
    }
}