$(function () {
    function init() {
        return pushnotify.server.sendMessage('').done(function () {
            console.log('init notify done');
        });
    }
    var lastNotify = null;
    var pushnotify = $.connection.pushnotify;
    var timeout = 1;
    $.connection.hub.start().done(init);
    $.connection.hub.stateChanged(function (state) {
        switch (state.newState) {
            case $.signalR.connectionState.connected:
                {
                    timeout = 1;
                }
                break;
            case $.signalR.connectionState.disconnected:
                {
                    timeout++;
                    if (timeout > 15) timeout = 15;
                    setTimeout(function () {
                        $.connection.hub.start().done(init);
                    }, timeout * 1000);
                }
                break;
        }
    });

    pushnotify.client.shownotify = function (notify) {        
        if (notify.uubid && $.cookie('UUBIdIS') == notify.uubid) {
            $('.popup-notify-active').remove();
            lastNotify = null;
        }
        else {
            if (lastNotify != notify.message) {
                lastNotify = notify.message;
                var $w = createNotify(notify);
                //setTimeout(function () {
                //    dismissNotify($w);
                //}, 60000);
            }
        }
    }


})
function dismissNotify($w) {
    $w.remove();
}

function createNotify(notify) {
    var $w = $('.popup-notify-warning.popup-notify-template').clone();
    $w.find('.popup-notify-subject').text(notify.message);
    console.log(notify);
    $('.popup-notify-group').append($w);
    $w.removeClass('popup-notify-template');
    $w.addClass('popup-notify-active');
    return $w;
}