//------------------------------------------------------------------------------
// Class:  BriefUserInfo
//------------------------------------------------------------------------------
// Author:  JY
// Date:  2007/03/12
// Description:  This class defines the functions used by the user profile popup.
//------------------------------------------------------------------------------
function BriefUserInfo()
{
	// Properties:
	this.userProfileBlockWidth = 600;
	this.userProfileBlockHeight = 220;
	this.mouseOnPosX = 0;
	this.mouseOnPosY = 0;
	this.userProfileBlockLeft = 0;
	this.userProfileBlockTop = 0;
	this.isMoveDataSaved = false;
	
	// Methods:
	this.show = show;
	this.close = close;
	this.move = move;
	this.moveStart = moveStart;
	this.moveStop = moveStop;
	this.getWindowSize = getWindowSize;
	this.getMousePosition = getMousePosition;
	this.getElementPosition = getElementPosition;
	this.resizeProfileFrameHeight = resizeProfileFrameHeight;

	//--------------------------------------------------------------------------

	function show(userAccountID)
	{
		var userProfileBlock = document.getElementById("userProfileBlock");
		var userProfileFrame = document.getElementById("userProfileFrame");
		var oWindowSize = getWindowSize();
		var widthDiff = oWindowSize.width - this.userProfileBlockWidth;
		var heightDiff = oWindowSize.height - this.userProfileBlockHeight;
		
		if (userProfileBlock)
		{
			userProfileFrame.src = "/Forum/User/BriefUserInfo/?id="+userAccountID+"&"+Math.random();
			userProfileBlock.style.left = ((widthDiff > 0 ? (widthDiff/2) : 0)+oWindowSize.scrollLeft) + "px";
			userProfileBlock.style.top =  ((heightDiff > 0 ? (heightDiff/2) : 0)+oWindowSize.scrollTop) + "px";
			userProfileBlock.style.display = "block";
			document.onmousedown = moveStart;
		}
	}

	//--------------------------------------------------------------------------

	function close()
	{
		var userProfileBlock = document.getElementById("userProfileBlock");
		
		if (userProfileBlock)
		{
			userProfileBlock.style.display = "none";	
			document.onmousedown = null;
		}
	}

	//--------------------------------------------------------------------------
	
	function move(e)
	{
		if ( this.isMoveDataSaved )
		{
			if (!e) e = window.event;		
			var target = e.target || e.srcElement;

			var oMousePos = getMousePosition(e);
			this.mousePosX = oMousePos.x;
			this.mousePosY = oMousePos.y;
			
			var userProfileBlock = document.getElementById("userProfileBlock");
			userProfileBlock.style.left = (this.userProfileBlockLeft+(oMousePos.x-this.mouseOnPosX)) + "px";	
			userProfileBlock.style.top = (this.userProfileBlockTop+(oMousePos.y-this.mouseOnPosY)) + "px";

			return false;
		}
	}

	//--------------------------------------------------------------------------

	function moveStart(e)
	{
		if (!e) e = window.event;		
		var target = e.target || e.srcElement;

		if ( (target.id == "userProfileTitle") && !this.isMoveDataSaved && (e.button <= 1) )
		{
			// save mouse on position
			var oMousePos = getMousePosition(e);
			this.mouseOnPosX = oMousePos.x;
			this.mouseOnPosY = oMousePos.y;

			// save user profile position
			var oBlockPosition = getElementPosition("userProfileBlock");
			this.userProfileBlockLeft = oBlockPosition.x;
			this.userProfileBlockTop = oBlockPosition.y;

			this.isMoveDataSaved = true;
			
			// assign events
			document.onmousemove = move;
			document.onmouseup = moveStop;

			// cancel out any text selections
			document.body.focus();
			
			// prevent text selection in IE
			document.onselectstart = function () { return false; };
			
			// prevent text selection (except IE)
			return false;
		}
	}

	//--------------------------------------------------------------------------

	function moveStop()
	{
		this.isMoveDataSaved = false;
		
		// detach events
		document.onmousemove = null;
		document.onmouseup = null;
		document.onselectstart = null;
	}

	//--------------------------------------------------------------------------

	function getWindowSize()
	{
		var width = height = 0;
		var scrollLeft = scrollTop = 0;
		
		if ( typeof(window.innerWidth) == "number" )
		{
			width = window.innerWidth;
			height = window.innerHeight;
			scrollLeft = window.pageXOffset;
			scrollTop = window.pageYOffset;
		}
		else if ( document.documentElement &&
			(document.documentElement.clientWidth || document.documentElement.clientHeight) )
		{
			width = document.documentElement.clientWidth;
			height = document.documentElement.clientHeight;
			scrollLeft = document.documentElement.scrollLeft;
			scrollTop = document.documentElement.scrollTop;
		}
		else if ( document.body &&
			(document.body.clientWidth || document.body.clientHeight) )
		{
			width = document.body.clientWidth;
			height = document.body.clientHeight;
			scrollLeft = document.body.scrollLeft;
			scrollTop = document.body.scrollTop;
		}

		return {width:width, height:height, scrollLeft:scrollLeft, scrollTop:scrollTop};
	}
	
	//--------------------------------------------------------------------------
	
	function getMousePosition(e)
	{
		var x = y= 0;
		
		if (!e) e = window.event;
		
		if (e.pageX || e.pageY)
		{
			x = e.pageX;
			y = e.pageY;
		}
		else if (e.clientX || e.clientY)
		{
			x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		
		return {x:x, y:y};
	}
	
	//--------------------------------------------------------------------------

	function getElementPosition(id)
	{
		var x = y = 0;
		var obj = document.getElementById(id);
		
		if (obj)
		{
			if (obj.offsetParent)
			{
				x = obj.offsetLeft;
				y = obj.offsetTop;
				while (obj = obj.offsetParent)
				{
					x += obj.offsetLeft;
					y += obj.offsetTop;
				}
			}
		}
		
		return {x:x, y:y};
	}

	//--------------------------------------------------------------------------

	function resizeProfileFrameHeight()
	{
		var iframeObj = document.getElementById("userProfileFrame");

		if (iframeObj.contentDocument)
		{
			iframeObj.height = iframeObj.contentDocument.height + "px";
		}
		else if (iframeObj.contentWindow)
		{
			iframeObj.height = iframeObj.contentWindow.document.body.scrollHeight + "px";
		}
	}
}

// Create BriefUserInfo object.
oBriefUserInfo = new BriefUserInfo();

