var X											= null;
var Y											= null;
var Z											= null;
var myObj;

function upFunc(e)
	{
	if (BROWSER.isIE) {
		
		document.onmousemove					= null;
		if (Z)
			Z.style.zIndex						= '2';

		}
	else {

		window.onmousemove						= null;
		if (Z)
			Z.style.zIndex						= '2';

		}

	Z											= null;
	myObj										= null;
	}

function downFunc(e)
	{
	var found									= false;

	if (!BROWSER.isIE)
		{
		// GECKO BASED BROWSER

		myObj									= e.target;

		while ((myObj != null))
			{
			if (myObj.id != 'dragDropObj')
				{
				myObj							= myObj.parentNode;
				}
			else
				{
				while (myObj != null)
					{
					if (myObj.id != 'dragdrop')
						{
						myObj					= myObj.parentNode;
						}
					else
						{	
						Z						= myObj;
						var currentZ			= Z.style.zIndex;
						Z.style.zIndex			= parseInt(currentZ) + 2;

						found					= true;
						break;
						}
					}

				break;
				}
			}

		if (found == true)
			{
			document.addEventListener("mousemove",moveFunc,true);

			X									= e.layerX;
			Y									= e.layerY;
			}
		else
			{
			return(true);
			}
		}
	else
		{
		// IE BASED BROWSER

		myObj									= window.event.srcElement;

		while((myObj != null))
			{
			if (myObj.id != 'dragDropObj')
				{
				myObj							= myObj.parentElement;
				}
			else
				{
				while(myObj != null)
					{
					if (myObj.id != 'dragdrop')
						{
						myObj					= myObj.parentElement;
						}
					else
						{
						Z						= myObj;
						var currentZ			= Z.style.zIndex;
						Z.style.zIndex			= currentZ + 2;

						found					= true;
						break;
						}
					}

				break;
				}
			}

		if (found == true)
			{
			document.body.attachEvent('onmousemove',moveFunc);
			X									= window.event.offsetX;
			Y									= window.event.offsetY;
			}
		else
			{
			return(true);
			}

		}
	}

function moveFunc(e)
	{
	var wHeight									= document.body.clientHeight;
	var wWidth									= document.body.clientWidth;

	if (BROWSER.isIE && Z)
		{
		// IE BASED BROWSER

		deltaX									= parseInt(Z.offsetWidth);
		deltaY									= parseInt(Z.offsetHeight);

		leftPosition							= window.event.clientX - X;
		topPosition								= window.event.clientY - Y + document.body.scrollTop;

		if (((leftPosition + deltaX) < wWidth - 0) && (leftPosition > 0))
			{
			Z.style.left						= leftPosition;
			}

		if (((topPosition + deltaY) < (wHeight + document.body.scrollTop)) && (topPosition > 0))
			{
			Z.style.top							= topPosition;
			}
		}
	else if (!BROWSER.isIE && Z)
		{
		// GECKO BASED BROWSER

		leftPosition							= parseInt(e.clientX) - X;
		topPosition								= parseInt(e.clientY) - Y;

		deltaX									= parseInt(Z.offsetWidth);
		deltaY									= parseInt(Z.offsetHeight);

		if (((leftPosition + deltaX) < wWidth - 20) && (leftPosition > 0))
			{
			Z.style.left						= parseInt(e.clientX) - X;
			}
		if (((topPosition + deltaY) < wHeight) && (topPosition > 0))
			{
			Z.style.top							= parseInt(e.clientY) - Y;
			}
		}
		
	return false;
	}

/*	Добавление обработчика событий	*/

if (!BROWSER.isIE)
	{
	// GECKO BASED BROWSER

	document.addEventListener("mousedown",downFunc,true);
	document.addEventListener("mouseup",upFunc,true);
	}
else
	{
	// IE BASED BROWSER

	document.body.attachEvent('onmousedown',downFunc);
	document.body.attachEvent('onmouseup',upFunc);
	}
