if ("undefined" == typeof(RadDockNamespace))
{
	RadDockNamespace = new Object();
}

/*************************************************
 *
 * Resize dir flags
 *
 *************************************************/
RadDockNamespace.ResizeDirFlags = {
	NoResize : 0
	, North : 1
	, South : 2
	, East : 4
	, West : 8
	, SE : 6
	, NE : 5
	, SW : 10
	, NW : 9
	, NSEW : 15
};

/*************************************************
 *
 * InitMoveableObject
 *
 *************************************************/
RadDockNamespace.InitMoveableObject = function(element)
{
	if (!element)
		return;
		
	Object.Extend(element, RadDockNamespace.RadMoveableObject);

	element.__offsetWidth = (element.offsetWidth ? element.offsetWidth : parseInt(element.style.width));
	element.__offsetHeight = (element.offsetHeight ? element.offsetHeight : parseInt(element.style.height));
};

/*************************************************
 *
 * RadMoveableObject object
 *
 *************************************************/
RadDockNamespace.RadMoveableObject = {

	//////////////////////////////////////////////////
	// Properties
	
	//	min/max allowed width
	MinWidth : 20,
	MaxWidth : 100000,
	
	//	min/max allowed height
	MinHeight : 20,
	MaxHeight : 100000,
	
	// allowed resize dirs
	AllowedResizeDir : RadDockNamespace.ResizeDirFlags.NSEW,
	
	__offsetWidth : 0,
	__offsetHeight : 0,
	
	//////////////////////////////////////////////////
	// Functions
	
	/*************************************************
	*
	* MoveBy
	*
	*************************************************/
	MoveBy : function (dx, dy)
	{
		var x = RadDockNamespace.ParseInt(this.style.left, 0);
		var y = RadDockNamespace.ParseInt(this.style.top, 0);
		
		this.MoveTo(x + dx, y + dy);
	},
		
	/*************************************************
 	 *
	 * MoveTo
	 *
	 *************************************************/
	MoveTo : function (x, y, useEffects)
	{
		x = RadDockNamespace.ParseInt(x, 0);	
		y = RadDockNamespace.ParseInt(y, 0);
		
		if (!useEffects) {
			this.style.position = "absolute";
			
			this.style.left = (x + "px");
			this.style.top = (y + "px");
			if (this.Overlay) {
				this.Overlay.style.left = this.style.left;
				this.Overlay.style.top = this.style.top;
			}
	//		alert(this.Overlay.outerHTML)
		}
		else 
		{
			
			/** make sure the object is undocked and 
				move it to its real position, but with absolute coords	
			**/
			var currentRect = this.GetRect();			
			this.Undock();			
			this.MoveTo(currentRect.left, currentRect.top);
			
			// now performe the move to the new position using effect
			var t = new RadEffect.MoveTo( 
			{ 
				object : this,
				destX  : x,
				destY  : y
			}

	);




		}
	},
	
	FixIeHeight : function (oElem)//BUGS in IE in DOCTYPE strict mode
	{	
		if (!document.all) return;
		
		if ("CSS1Compat" == document.compatMode) 
		{
			var oRect = this.GetRect();
			var oHeight = oRect.height;
			var difference = (oHeight - parseInt(oElem.style.height));		
			if (difference > 0)
			{	
				var newHeight = (parseInt(oElem.style.height) - difference); 
				if (newHeight > 0) oElem.style.height = newHeight + "px";
			}
		}
			//alert ("FixIeHeight " + oRect.height + " " + oElem.style.height);
	},
	
	/*************************************************
	 *
	 * SetSize
	 *
	 *************************************************/
	SetSize : function (width, height)
	{
		width = RadDockNamespace.ParseInt(width, -1);
		height = RadDockNamespace.ParseInt(height, -1);
		
		if (width > -1)
		{
			this.style.width = width + "px";
			if (this.Overlay) {
				this.Overlay.style.width = this.style.width;
			}
		}

		//alert('1 ->' + height)
		if (height > -1)
		{
			this.style.height = height + "px";
			if (this.Overlay) {
				this.Overlay.style.height = this.style.height;
			}
		}

		//alert('2 ->' + this.offsetHeight)
		// this.FixIeHeight is undefined in Firefox
		RadDockNamespace.FixIeHeight(this);
		if (this.Overlay) {
			RadDockNamespace.FixIeHeight(this.Overlay);
		}
		//alert('3 ->' + this.offsetHeight)
	},	
	
	/*************************************************
	 *
	 * Inflate
	 *
	 *************************************************/
	Inflate : function (offsetLeft, offsetTop, offsetWidth, offsetHeight)
	{
		var rc = this.GetRect();
		
		var top = rc.top + offsetTop;	
		var left = rc.left + offsetLeft;

		if (top < 0)
		{
			offsetTop = -rc.top;
		}
		
		if (left < 0)
		{
			offsetLeft = -rc.left;
		}
		
		top = rc.top + offsetTop;	
		left = rc.left + offsetLeft;
		
		if (null == offsetWidth)
			offsetWidth = -offsetLeft;
		
		if (null == offsetHeight)
			offsetHeight = -offsetTop;
			
		var width = rc.width + offsetWidth;
		var height = rc.height + offsetHeight;
		
		width = Math.max(this.MinWidth, width);
		width = Math.min(this.MaxWidth, width);
		
		height = Math.max(this.MinHeight, height);
		height = Math.min(this.MaxHeight, height);
		
		if (rc.width != width)
		{
			this.MoveBy(offsetLeft, 0);
			this.SetSize(width, null);
		}
		
		if (rc.height != height)
		{
			this.MoveBy(0, offsetTop);	
			this.SetSize(null, height);
		}
	},
	
	/*************************************************
	 *
	 * BringToFront
	 *
	 *************************************************/
	BringToFront : function ()
	{	
		var maxZIndex = 0;
		var zIndex = 0;
		
		var siblings = this.parentNode.childNodes;
		var node;
		for (var i = 0; i < siblings.length; i++)
		{
			node = siblings[i];
			if (1 != node.nodeType)
				continue;
				
			zIndex = parseInt(node.style.zIndex);
			if (zIndex > maxZIndex)
			{
				maxZIndex = zIndex;
			}
		}
		
		this.style.zIndex = (maxZIndex + 2);
		if (this.Overlay) {
			this.Overlay.style.zIndex = this.style.zIndex - 1;
		}

		if (!document.all)
		{
			var parentNode = this.parentNode;
			parentNode.removeChild(this);
			parentNode.appendChild(this);
		}
	},
	
	/*************************************************
	 *
	 * Resize
	 *
	 *************************************************/
	Resize : function (dir, dX, dY)
	{
		switch (dir.toLowerCase())
		{
			case "n-resize":
				this.Inflate(0, dY, null, null);
				break;
				
			case "s-resize":
				this.Inflate(0, 0, 0, dY);
				break;
				
			case "w-resize":
				this.Inflate(dX, 0, null, null);
				break;
				
			case "e-resize":
				this.Inflate(0, 0, dX, 0);
				break;
				
			case "ne-resize":
				this.Inflate(0, dY, dX, null);
				break;
				
			case "nw-resize":
				this.Inflate(dX, dY, null, null);
				break;
				
			case "se-resize":
				this.Inflate(0, 0, dX, dY);
				break;
				
			case "sw-resize":
				this.Inflate(dX, 0, null, dY);
				break;
		}
	},
	
	/*************************************************
	 *
	 * GetRect
	 *
	 *************************************************/
	GetRect : function (element)
	{
		return RadGetElementRect(element || this);
	},
	
	/*************************************************
	 *
	 * SetRect
	 *
	 *************************************************/
	SetRect : function (rect)
	{
		this.MoveTo(rect.left, rect.top);
		this.SetSize(rect.width, rect.height);
	},
	
	/*************************************************
	 *
	 * CalcResizeDir
	 *
	 *************************************************/
	CalcResizeDir : function (eventArgs)
	{
		var x = eventArgs.clientX + RadDockNamespace.GetScrollLeft();
		var y = eventArgs.clientY + RadDockNamespace.GetScrollTop();
		
		var rc = this.GetRect();	
		
		var borderWidth = RadDockNamespace.ParseInt(this.style.borderWidth, 0) + 10;
		
		var resizeDir = "";
		
		if (((this.AllowedResizeDir & RadDockNamespace.ResizeDirFlags.North) > 0)
			&& (rc.top <= y)
			&& (y <= (rc.top + borderWidth)))
		{
			// up
			resizeDir = "n";
		}
		else if (((this.AllowedResizeDir & RadDockNamespace.ResizeDirFlags.South) > 0)
				&& ((rc.bottom - borderWidth) <= y)
				&& (y <= rc.bottom))
		{
			// down
			resizeDir = "s";
		}
		
		if (((this.AllowedResizeDir & RadDockNamespace.ResizeDirFlags.West) > 0) 
			&& (rc.left <= x) 
			&& (x <= (rc.left + borderWidth)))
		{
			// left
			resizeDir += "w";
		}
		else if (((this.AllowedResizeDir & RadDockNamespace.ResizeDirFlags.East) > 0) 
				&& ((rc.right - borderWidth) <= x)
				&& (x <= rc.right))
		{
			// right
			resizeDir += "e";
		}
		
		if (resizeDir)
		{
			resizeDir += "-resize";
		}
		
		return resizeDir;
	},
	
	/*************************************************
	 *
	 * IsVisible
	 *
	 *************************************************/
	IsVisible : function ()
	{
		return (this.style.display != "none");
	},
	
	/*************************************************
	 *
	 * Show
	 *
	 *************************************************/
	Show : function (show)
	{
		if (show == false)
		{
			this.__offsetWidth = this.offsetWidth;
			this.__offsetHeight = this.offsetHeight;
		
			this.OldDisplayStyle = this.style.display;	
			this.style.display = "none";

			if (this.Overlay) {
				this.Overlay.style.display = this.style.display;
			}
		}
		else
		{
			this.style.display = this.OldDisplayStyle ? this.OldDisplayStyle : "";
			if (this.Overlay) {
				this.Overlay.style.display = this.style.display;
			}
		}
		
		if (this.OnShowHide)
		{
			this.OnShowHide();
		}
	},	
	
	/*************************************************
	 *
	 * Hide
	 *
	 *************************************************/
	Hide : function ()
	{
		this.Show(false);
	}
};


//BEGIN_ATLAS_NOTIFY
if (typeof(Sys) != "undefined")
{
    if (Sys.Application != null && Sys.Application.notifyScriptLoaded != null)
    {
        Sys.Application.notifyScriptLoaded();
    }
}
//END_ATLAS_NOTIFY
