'use strict';

var ee = require('event-emitter/lib/core').methods;

module.exports = function (domjs) {
	var fn = domjs.div, current, show, hide, toggle;

	show = function () {
		if (current === this) return;
		if (current) hide.call(current);
		this.classList.remove('hidden');
		this.ownerDocument.body.classList.remove('modal-off');
		if (this.curtain !== false) {
			this.ownerDocument.body.classList.add('modal-on');
		}
		this.classList.add('visible');
		current = this;
		ee.emit.call(this, 'show');
	};

	hide = function () {
		if (current === this) {
			this.classList.remove('visible');
			this.ownerDocument.body.classList.remove('modal-on');
			this.ownerDocument.body.classList.add('modal-off');
			this.classList.add('hidden');
			current = null;
			ee.emit.call(this, 'hide');
		}
	};

	toggle = function () {
		(this.classList.contains('hidden') ? 'show' : 'hide').call(this);
	};

	domjs.document.addEventListener('click', function (e) {
		var target;
		if (!current) return;
		target = e.target;
		if (target === current) return;
		if (current.persistent) return;
		if (!Boolean(current.compareDocumentPosition(target) & 16)) {
			hide.call(current);
		}
	}, true);

	return function (/* options */) {
		var options = Object(arguments[0]), self, curtain;
		if (options.curtain != null) {
			curtain = false;
			delete options.curtain;
		}
		self = fn.apply(this, arguments);
		if (curtain != null) self.curtain = curtain;
		self.persistent = options.persistent;
		self.show = show.bind(self);
		self.hide = hide.bind(self);
		self.toggle = toggle.bind(self);

		if (!self.classList.contains('visible')) {
			self.classList.add('hidden');
		} else {
			if (current) hide.call(current);
			current = self;
		}
		return self;
	};
};

@@ -54,18 +54,10 @@
 re')
-.methods
 ;%0A
+
 %0Amod
@@ -464,35 +464,26 @@
 this;%0A%09%09
-ee
+this
 .emit
-.call(this, 
+(
 'show');
@@ -744,27 +744,18 @@
 %0A%09%09%09
-ee
+this
 .emit
-.call(this, 
+(
 'hid
@@ -1333,16 +1333,19 @@
 %09self = 
+ee(
 fn.apply
@@ -1361,16 +1361,17 @@
 guments)
+)
 ;%0A%09%09if (

Make modal an event emitter