/*
 ========================================
 Name: Popup & Popup Manager
 Version: 1.2
 Author: Apples To Oranges - Ryan Nichols
 Date: August 4th, 2006 
 Requires: moo.fx & prototype.lite
 ========================================
 */
 
ato = {};
ato.ui = {};
ato.ui.popUp = function(options) {
	//public
	this.element = null;
	this.hideClass = 'hide'
	this.showTriggers = [];
	this.hideTriggers = [];	
	this.toggleTriggers = [];
	this.hoverTriggers = [];
	this.autoBodyClickHide = true;
	this.isExclusive = false;
	if(options && typeof(options) == 'object') { Object.extend(this, options); }
		
	//private
	this._bodySignalId = null;
	this._isShown = false;
	this._isOver = false;
	
	//events
	this.onShow = null;
	this.onHide = null;
		
	this._initiate();
}

Object.extend(ato.ui.popUp.prototype, {	 

	//public functions 
	show : function() {
		var self = this;
		Element.removeClassName(self.element, self.hideClass);
		self._isShown = true;
		if(self.autoBodyClickHide) { 
			document.body.onclick = function() {
				if(self._isShown && !self._isOver) { self.hide(); }
			}
			self.element.onmouseover = function() { self._isOver = true; }
			self.element.onmouseout = function() { self._isOver = false; }
		}
		if(self.onShow && typeof(self.onShow) == 'function') { self.onShow(self); }
	},

	hide : function() {
        var self = this;		
        Element.addClassName(self.element, self.hideClass);
		self._isShown = false;
		self._isOver = false;
		document.body.onclick = null;
		if(self.onHide && typeof(self.onHide) == 'function') { self.onHide(self); }
	},

	'toggle': function() {	
		if(this._isShown) { this.hide(); } else { this.show(); }		
	},

	//pseudo constructor
	_initiate : function() {
        var self = this;
		self.element = $(self.element);
		self.element.style.zIndex = '20000';		
        self.showTriggers.each(function(e) {
        	e.onclick = function() { self.show(); return false;  }        	
		});
		self.hideTriggers.each(function(e) {
			e.onclick = function() { self.hide(); return false; }
		});
		self.toggleTriggers.each(function(e) {
			e.onclick = function() { self.toggle(); return false; }
		});
		self.hoverTriggers.each(function(e) {
			e.onmouseover = function() { self.show(); return false; }
			e.onmouseout = function() { self.hide(); return false; }
		});
	}

	//private methods	
});

ato.ui.PopUpManager = function(options) {
	//public
	this.popUps = [];
	this.current = null;
	if(options && typeof(options) == 'object') { Object.extend(this, options); }	
}

Object.extend(ato.ui.PopUpManager.prototype, {
	//public functions 
	showAll : function() {
		this.popUps.each(function(e) { e.show(); });
	},

	hideAll : function(a) {
		this.popUps.each(function(e) { if(e != a) { e.hide(); } });
	},

	toggleAll : function() {	
		this.popUps.each(function(e) { e.toggle(); });		
	},

	add : function (e) {
		this.popUps.push(e);
		e.panelManager = this;
		e.onShow = this._onShow;
	},

	create : function(options) {
		var n = new ato.ui.popUp(options);
		this.add(n);		
		return n;
	},

	_onShow : function(e) {	
		s = e.panelManager;
		if(e.isExclusive) {
			s.hideAll(e);
			
		}
	}

});