var posX = 0;
var posY = 0;
var deltaX = 0;
var deltaY = 0;
var calqueDragDrop = "";
var isLimiteDragDrop;


//fonction DragDrop
function startDragDrop ( calque, isLimite )
{
//initialisation du div concerné
	if ( document.getElementById )
	{
		//passage au premier plan du div selectionné
		var calques = document.getElementsByTagName ( "div" );
		var zIndexMax = 0;
		for ( var i=0; i<calques.length; i++ )
		{
			if ( calques[i].className=="calque" )
			{
				if ( calques[i].id != calque.id )
				{
					if ( zIndexMax<parseInt( calques[i].style.zIndex ) )
					{
						zIndexMax = parseInt ( calques[i].style.zIndex );
					}
				}
			}
		}
		calque.style.zIndex = zIndexMax+1;
		
		//initialisation des paramètres
		isLimitDragDrop = isLimite;
		deltaX = posX-calque.offsetLeft;
		deltaY = posY-calque.offsetTop;
		calqueDragDrop = calque;
	}
	return false;
}
//fonction DragDrop

//fonction goDragDrop
function goDragDrop ( e )
{
	//gestion du drag and drop
	if ( document.all )
	{
		posX = event.x+document.body.scrollLeft;
		posY = event.y+document.body.scrollTop;
	}
	else
	{
		posX = e.pageX;
		posY = e.pageY;
	}
	if ( calqueDragDrop != "" )
	{
		var tmpX = posX-deltaX;
		var tmpY = posY-deltaY;
		if ( isLimitDragDrop )
		{
			//on oblige le div à rester visible en tenant compte du scroll éventuel
			if ( tmpX<document.body.scrollLeft )
			{
				tmpX = document.body.scrollLeft;
			}
			if ( tmpY<document.body.scrollTop )
			{
				tmpY = document.body.scrollTop;
			}
			var largeurScrollBarX = ( document.all ) ?22:-14;
			var largeurScrollBarY = ( document.all ) ?4:0;
			var maxX = document.body.scrollLeft + document.body.offsetWidth-calqueDragDrop.offsetWidth-largeurScrollBarX;
			if ( tmpX>maxX )
			{
				tmpX = maxX;
			}
			var maxY = document.body.scrollTop + document.body.offsetHeight-calqueDragDrop.offsetHeight-largeurScrollBarY;
			if ( tmpY>maxY )
			{
				tmpY = maxY;
			}
		}
		calqueDragDrop.style.left = tmpX + "px";
		calqueDragDrop.style.top = tmpY + "px";
		return false;
	}
}
//fonction goDragDrop

//fonction stopDragDrop
function stopDragDrop ()
{
	//fin du drag and drop au relâchement du bouton de la souris
	calqueDragDrop = "";
	return false;
}
//fonction stopDragDrop

document.onmousemove = goDragDrop;
document.onmouseup = stopDragDrop;