// usage create an element where the first part of the id is drag, eg drag_name
// set the position of the element to absolute
// the script will do the rest

var currObj = null;
var X = 0;
var Y = 0;

function MouseDown(e)
{
	var select_parent = false;
	if (!e) var e = window.event;
	var target = (window.event) ? e.srcElement : e.target;

    if ( target.id.substring(0,4) == 'drag' )
    {
		currObj = target;
    }

    if( target.parentNode.id )
    {
	    if( target.parentNode.id.substring(0,4) == 'drag' )
	    {
			currObj = target.parentNode;
			select_parent = true;
	    }
	}

	// If it is a close button do not enable dragging
    if ( target.id.substring(0,5) == 'close' || target.id.substring(0,4) == 'hide' )
    {
		currObj = null;
    }

    if( currObj != null )
    {
        if( isMoz() )
        {
            X=e.layerX;
            Y=e.layerY;
        }
        else
        {
            X=event.offsetX;
            Y=event.offsetY;
        }

		if( select_parent )
		{
			target = target.parentNode
		}

        var Width = target.offsetWidth;
        var Height = target.offsetHeight;
		var Pos = findPos(target);

	    var blocking_el = document.createElement('div');
	    blocking_el.setAttribute('id', 'dragBlockingElement');
	    blocking_el.style.backgroundColor = '#000000';
	    blocking_el.style.opacity = 0.15;
	    blocking_el.style.filter = 'alpha(opacity=15)';
	    blocking_el.style.position = 'absolute';
	    blocking_el.style.width = Width + 'px';
	    blocking_el.style.height = Height + 'px';
	    blocking_el.style.top = '0px';
	    blocking_el.style.left = '0px';
		target.appendChild(blocking_el);

		document.onselectstart=new Function ("return false")
    }
}

function MouseMove(e)
{
    if( currObj != null )
    {
        if( isMoz() )
        {
            currObj.style.top = parseInt(e.pageY-Y) + 'px';
            currObj.style.left = parseInt(e.pageX-X) + 'px';
        }
        else
        {
            currObj.style.top = event.clientY-Y + document.body.scrollTop + 'px';
            currObj.style.left = event.clientX-X + document.body.scrollLeft + 'px';
        }
    }
}

function MouseUp()
{
	if( currObj == null)
	{
		return;
	}
    currObj = null;

	var blocking_el = document.getElementById('dragBlockingElement');
	blocking_el.parentNode.removeChild(blocking_el);
	document.onselectstart=new Function ("return true")
}

function init()
{
    document.onmousedown = MouseDown;
    document.onmousemove = MouseMove;
    document.onmouseup = MouseUp;
}

init();