
Cuke.FloatButton = new CukeFloatButton();

function CukeFloatButton()
{
	this.CurrentFloatButton = null;
	this.CurrentThreadID = 0;
	this.DragInProgress = false;
}
CukeFloatButton.prototype.Add=function( box, pane )
{
	var div = this._createSystem( box, pane );
	var self = this;
	xAddEventListener( box, 'MouseOver', function(){self.ShowButton( box );}, true);
	xAddEventListener( box, 'MouseOut', function(){self.HideButton( box );}, true);
	xAddEventListener( div, 'MouseOver', function(){self.ShowButton( box );}, true);
	xAddEventListener( div, 'MouseOut', function(){self.HideButton( box );}, true);
};
CukeFloatButton.prototype.ShowButton=function( box )
{
	if(typeof(Cuke)!='undefined')
	{
		if( !this.DragInProgress )
		{
			if(this.CurrentFloatButton != null) this.CurrentFloatButton.style.visibility = 'hidden';
			this.CurrentFloatButton = box.CukeFloatButton;
			this._stickButton( box, xPageX(box.CukeFloatButton), xPageY(box.CukeFloatButton) );
			box.CukeFloatButton.style.visibility = 'visible';
		}
		this.CurrentThreadID++;
	}
};
CukeFloatButton.prototype.HideButton=function( box )
{
    if(typeof(Cuke)!='undefined')
    {
        var tID = this.CurrentThreadID;
	    setTimeout( function(){if(tID!=Cuke.FloatButton.CurrentThreadID||Cuke.FloatButton.DragInProgress)return;box.CukeFloatButton.style.visibility='hidden';},1000);
    }
};
CukeFloatButton.prototype._stickButton=function( box, x, y )
{
	var bx=xPageX(box), by=xPageY(box), div=box.CukeFloatButton;
	var p = xProjectRect( x, y, bx-xWidth(div), by-xHeight(div), bx+xWidth(box), by+xHeight(box) );
	xMoveTo(div,p[0],p[1]);
};
CukeFloatButton.prototype._createSystem=function( box, pane )
{
	var div = xCreateElement('div');
	div.style.visibility = 'hidden';
	div.style.zIndex = 20000;
	div.className = 'CukeMovableButton';
	
	var grip = xCreateElement('div');
	grip.className += 'Grip';
	if( Cuke.BrowserInfo.IsIE ) grip.innerHTML = '<img src="'+Cuke.Skin+'x.gif"/>';
	
	var self = this;
	xEnableDrag(grip, 
		function(ele,mx,my){self.DragInProgress=true;self.CurrentThreadID++;}, 
		function(ele,mdx,mdy,ev){self._stickButton( box, ev.pageX, ev.pageY );}, 
		function(ele,mx,my){self.DragInProgress=false;self.HideButton(box);});
	if( xStr(pane) )
	{
		var e = xCreateElement('div');
		e.className = 'Pane';
		e.innerHTML = pane;
		pane = e;
	} 
	else pane.className = xStrAssumeWord(pane.className,'Pane');
	xAppendChild( div, grip );
	xAppendChild( div, pane );
	box.CukeFloatButton = div;
	xMoveTo( div, xPageX(box)+xWidth(box), xPageY(box) );
	if( !this._allDivs )
	{
		this._allDivs = xCreateElement('div');
		this._allDivs.style.visibility = 'hidden';
		xAppendChild(document.body,this._allDivs);
	}
	xAppendChild( this._allDivs, div );
	if( Cuke.BrowserInfo.IsIE ) xWidth( div, 1 );
	xWidth( div, xWidth(pane) );
	return div;
};
function xProjectRect( x, y, xA, yA, xB, yB )
{
	if( x >= xA )
	{
		if( x < xB ) 
		{
			if( y < yA ) return [x,yA];
			if( y <= yB ) 
			{ 
				var nx=(x-(xA+xB)/2)*(yB-yA),ny=(y-(yA+yB)/2)*(xB-xA);
				if( nx < 0 )
					if( ny < 0 ) 
						return nx > ny ? [x,yA] : [xA,y];
					else return -nx < ny ? [x,yB] : [xA,y];
				else if( ny > 0 ) return nx > ny ? [xB,y] : [x,yB];
				return nx > -ny ? [xB,y] : [x,yA];
			}
			return [x,yB];
		}
		if( y < yA ) return [xB,yA];
		if( y <= yB ) return [xB,y];
		return [xB,yB];
	}
	if( y < yA ) return [xA,yA];
	if( y <= yB ) return [xA,y];
	return [xA,yB];
}

