
var atoPopUp = function(options) {
	//public
	this.element = null;
	this.hideClass = 'hide'
	this.showTriggers = [];
	this.hideTriggers = [];	
	this.toggleTriggers = [];
	this.autoBodyClickHide = true;
	if(options && typeof(options) == 'object') { update(this, options); }
		
	//private
	this._bodySignalId = null;
	this._isShown = false;
	this._isOver = false;
	
	//events
	this.onShow = null;
	this.onHide = null;
		
	this._initiate();
}

update(atoPopUp.prototype, {	 

	//public functions 
	'show': function() {
		var self = this;
		appear(self.element, {duration: .2});		
		self._isShown = true;
		if(self.autoBodyClickHide) { 
			self._bodySignalId = connect(document.body, 'onclick', function() { if(self._isShown && !self._isOver) { self.hide(); }});
			connect(self.element, 'onmouseover', function() { self._isOver = true; });
			connect(self.element, 'onmouseout', function() { self._isOver = false; });
		}
		signal(self, 'onShow');
	},

	'hide': function() {
        var self = this;		
        fade(self.element, {duration: .2});
		self._isShown = false;
		self._isOver = false;
		disconnect(self._bodySignalId);
		signal(self, 'onHide');
	},

	'toggle': function() {	
		if(this._isShown) { this.hide(); } else { this.show(); }		
	},

	//pseudo constructor
	'_initiate': function() {
        var self = this;
		self.element = $(self.element);
		
        hideElement(self.element);
        removeElementClass(self.element, 'hide');
		forEach(this.showTriggers, function(e) {
			connect(e, 'onclick', function(j) { self.show(); j.stop();});
		});
		forEach(this.hideTriggers, function(e) {
			connect(e, 'onclick', function(j) { self.hide(); j.stop();});
		});
		forEach(this.toggleTriggers, function(e) {
			connect(e, 'onclick', function(j) { self.toggle(); j.stop();});
		});
	}

	//private methods	
});

var atoPopUpManager = function(options) {
	//public
	this.popUps = [];
	if(options && typeof(options) == 'object') { update(this, options); }	
}

update(atoPopUpManager.prototype, {
	//public functions 
	'showAll': function() {
		forEach(this.popUps, function(e) { e.show(); });
	},

	'hideAll': function() {
		forEach(this.popUps, function(e) { e.hide(); });
	},

	'toggleAll': function() {	
		forEach(this.popUps, function(e) { e.toggle(); });		
	},

	'add': function (e) {
		if(isArrayLike(e)) { this.popUps = concat(this.popUps, e); } else { this.popUps.push(e); }		
	},

	'create': function(options) {
		var n = new atoPopUp(options);
		this.add(n);
		return n;
	}

});

var popUpManager = new atoPopUpManager();

