/**
 * Primitive class for any object that appears on mouseover of an element and disappears on mouseout of that element or it's child.
 */
PAMWF.PopUp = Class.create({
	_over: false,
	_open: false,
	_myDiv: null,
	_myHandle: null,
	_source: null,
	_defaults: null,
	_group: 'popups',
	_class: 'TooltipBox',
	_timeOut: 1000,
	_fadeIn: .1,
	_fadeOut: .3,
	_mouseOverBound: null,
	_mouseOutBound: null,
		
	initialize: function ( obj ) {
		this._myHandle = obj;
		this._myDiv = this._myHandle.next('.'+this._class);
		
		// Pull the target out of the DOM
		this._myDiv = this._myDiv.remove();
		
		// Now reinsert at end of body.
		$$('BODY')[0].insert({bottom: this._myDiv});
		
		this._mouseOverBound = this._mouseOver.bindAsEventListener(this);
		this._mouseOutBound = this._mouseOut.bindAsEventListener(this);
		
		Event.observe( this._myDiv, 'mouseover', this._mouseOverBound );
		Event.observe( this._myDiv, 'mouseout', this._mouseOutBound );
		Event.observe( this._myHandle, 'mouseover', this._mouseOverBound );
		Event.observe( this._myHandle, 'mouseout', this._mouseOutBound );
	},
	
	destroy: function() {
		Event.stopObserving( this._myDiv, 'mouseover', this._mouseOverBound );
		Event.stopObserving( this._myDiv, 'mouseout', this._mouseOutBound );
		Event.stopObserving( this._myHandle, 'mouseover', this._mouseOverBound );
		Event.stopObserving( this._myHandle, 'mouseout', this._mouseOutBound );
		this._myDiv.remove();
	},
	
	_mouseOver: function ()
	{
		this._over = true;
				
		if (!this._open)
		{
			this._appear();
			this._open = true;
		}
	},
	
	_appear: function()
	{
		this._myDiv.clonePosition(this._myHandle, { setWidth: false, setHeight: false, offsetTop: this._myHandle.getHeight()});
		new Effect.Appear( this._myDiv, {duration: this._fadeIn}, {queue: {position:'end', scope: this._group }} );
	},
	
	_hide: function()
	{
		new Effect.Fade( this._myDiv, {duration: this._fadeOut}, {queue: {position:'end', scope: this._group }} );
	},

	_mouseOut: function ()
	{
		this._over = false;		
		setTimeout( this._doTimeOut.bindAsEventListener(this), this._timeOut );
	},
	
	_doTimeOut: function ()
	{
		if ( !this._over && this._open )
		{
			this._hide();
			this._open = false;
		}
	}
});