/*
 * Copyright (c) 2013 Alex Gibson
 * Released under MIT license
 * http://alxgbsn.co.uk
 */

/*global clearInterval: false, clearTimeout: false, document: false, event: false, frames: false, history: false, Image: false, location: false, name: false, navigator: false, Option: false, parent: false, screen: false, setInterval: false, setTimeout: false, window: false, XMLHttpRequest: false, console: false */
/*global webkitNotifications: false */

(function (window, document) {
	'use strict';

	function Notify(options) {

		var i;

		this.options = {
			icon: '',
			title: 'Insert title',
			message: 'Insert message',
			notifyShow: null,
			notifyClose: null,
			notifyClick: null,
			notifyError: null
		};

		//User defined options for notification content
		if (typeof options === 'object') {
			for (i in options) {
				if (options.hasOwnProperty(i)) {
					this.options[i] = options[i];
				}
			}
		}

		//User defined callback when notification is displayed
		if (typeof this.options.notifyShow === 'function') {
			this.onShowCallback = this.options.notifyShow;
		}

		//User defined callback when notification is closed
		if (typeof this.options.notifyClose === 'function') {
			this.onCloseCallback = this.options.notifyClose;
		}

		//User defined callback when notification is clicked
		if (typeof this.options.notifyClick === 'function') {
			this.onClickCallback = this.options.notifyClick;
		}

		//User defined callback when notification is clicked
		if (typeof this.options.notifyError === 'function') {
			this.onErrorCallback = this.options.notifyError;
		}

		if (window.webkitNotifications) {
			this.notifications = window.webkitNotifications;
		} else if (window.Notifications) {
			this.notifications = window.Notifications;
		} else {
			console.warn('Web Notifications are not currently supported by this browser');
			return;
		}
	}

	Notify.prototype.requestPermission = function () {
		var that = this;
		this.notifications.requestPermission(function () {
			console.log(that.notifications.checkPermission());
			switch (that.notifications.checkPermission()) {
			case 0:
				that.createNotification();
				break;
			}
		});
	};

	Notify.prototype.show = function () {
		if (!this.notifications) { return; }
		if (this.notifications.checkPermission() === 0) {
			this.createNotification();
		} else {
			this.requestPermission();
		}
	};

	Notify.prototype.createNotification = function () {
		this.myNotify = this.notifications.createNotification(this.options.icon, this.options.title, this.options.message);
		this.myNotify.addEventListener('show', this, false);
		this.myNotify.addEventListener('close', this, false);
		this.myNotify.addEventListener('click', this, false);
		this.myNotify.addEventListener('error', this, false);
		this.myNotify.show();
	};

	Notify.prototype.onShowNotification = function (e) {
		if (this.onShowCallback) {
			this.onShowCallback();
		}
	};

	Notify.prototype.onCloseNotification = function (e) {
		if (this.onCloseCallback) {
			this.onCloseCallback();
		}
		this.removeEvents();
	};

	Notify.prototype.onClickNotification = function (e) {
		if (this.onClickCallback) {
			this.onClickCallback();
		}
		this.removeEvents();
	};

	Notify.prototype.onErrorNotification = function (e) {
		if (this.onErrorCallback) {
			this.onErrorCallback();
		}
	};

	Notify.prototype.removeEvents = function () {
		this.myNotify.removeEventListener('show', this, false);
		this.myNotify.removeEventListener('close', this, false);
		this.myNotify.removeEventListener('click', this, false);
	};

	Notify.prototype.destroy = function () {
		this.removeEvents();
		this.myNotify = null;
		this.notifications = null;
	};

	Notify.prototype.handleEvent = function (e) {
		switch (e.type) {
		case 'show': this.onShowNotification(e); break;
		case 'close': this.onCloseNotification(e); break;
		case 'click': this.onClickNotification(e); break;
		case 'error': this.onErrorNotification(e); break;
		}
	};

	//public
	window.Notify = Notify;

}(window, document));
@@ -434,16 +434,38 @@
 cations:
+ false, Notifications:
  false *
@@ -820,16 +820,17 @@
 ect') %7B%0A
+%0A
 %09%09%09for (
@@ -928,38 +928,22 @@
 %09%7D%0A%09%09%09%7D%0A
-%09%09%7D%0A%0A%09%09//User defined 
+%0A%09%09%09//
 callback
@@ -974,16 +974,17 @@
 splayed%0A
+%09
 %09%09if (ty
@@ -1029,24 +1029,25 @@
 tion') %7B%0A%09%09%09
+%09
 this.onShowC
@@ -1083,36 +1083,25 @@
 Show;%0A%09%09
+%09
 %7D%0A%0A%09%09
+%09
 //
-User defined 
 callback
@@ -1129,16 +1129,17 @@
  closed%0A
+%09
 %09%09if (ty
@@ -1181,32 +1181,33 @@
 function') %7B%0A%09%09%09
+%09
 this.onCloseCall
@@ -1239,38 +1239,27 @@
 yClose;%0A
+%09
 %09%09%7D%0A%0A%09%09
+%09
 //
-User defined 
 callback
@@ -1280,32 +1280,33 @@
 tion is clicked%0A
+%09
 %09%09if (typeof thi
@@ -1344,24 +1344,25 @@
 tion') %7B%0A%09%09%09
+%09
 this.onClick
@@ -1404,28 +1404,17 @@
 ;%0A%09%09
+%09
 %7D%0A%0A%09%09
+%09
 //
-User defined 
 call
@@ -1447,16 +1447,17 @@
 clicked%0A
+%09
 %09%09if (ty
@@ -1503,24 +1503,25 @@
 tion') %7B%0A%09%09%09
+%09
 this.onError
@@ -1557,16 +1557,21 @@
 yError;%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09i

Tidy up