var Notification = window.Notification || window.mozNotification || window.webkitNotification;
var enabledNotifications,
    grantedPermission;

function askPermission () {
    Notification.requestPermission(function (permission) {
        // console.log(permission);
	if (permission == "granted") {
            grantedPermission = true;
        }
    else {
        grantedPermission = false;
    }
    });
}

function toggleNotifications() {
    if (enabledNotifications === true) {
        enabledNotifications = false;
    } else if (!enabledNotifications && grantedPermission === true){
        enabledNotifications = true;
    } else {
        askPermission();
    }
}


function showNotification(name, body, icon, timeout) {
    if (enabledNotifications) {
        var instance = new Notification(
                name, {
                    body: body,
                    icon: icon
                }
        );

        instance.onclick = function () {
            // Something to do
        };
        instance.onerror = function () {
            // Something to do
        };
        instance.onshow = function () {
        setTimeout(function(){
        instance.close();
        }, timeout * 1000); 
        };
        instance.onclose = function () {
            // Something to do
        };
    }
}
