var isClicked = false;
var objectToDrag = 0;
var ecartX;
var ecartY;
var curX;
var curY;
var initialHeight;
var initialWidth;
var initialCurX;
var initialCurY;
var theAction;
var minWidth = 250;
var minHeight = 250;
var thePadding = 6;

var _MOVE_ 			= 0;
var _RESIZE_N_ 	= 1;
var _RESIZE_S_ 	= 2;
var _RESIZE_W_ 	= 3;
var _RESIZE_E_ 	= 4;
var _RESIZE_NW_ = 5;
var _RESIZE_NE_ = 6;
var _RESIZE_SW_ = 7;
var _RESIZE_SE_ = 8;

function positionne(p_id, p_posX, p_pos_Y){
	document.getElementById(p_id).style.left = p_posX;
	document.getElementById(p_id).style.top = p_pos_Y;
	document.getElementById(p_id).style.zIndex = 0;
}

function getPositionCurseur(e){
	//ie
	if(document.all){
		curX = event.clientX;
		curY = event.clientY;
	}

	//netscape 4
	if(document.layers){
		curX = e.pageX;
		curY = e.pageY;
	}

	//mozilla
	if(document.getElementById){
		curX = e.clientX;
		curY = e.clientY;
	}
}

function downAction(p_obj,e){
	isClicked = true;
	objectToDrag = p_obj;
	getPositionCurseur(e);
	ecartX = curX - parseInt(objectToDrag.style.left);
	ecartY = curY - parseInt(objectToDrag.style.top);
	objectToDrag.style.zIndex = ++MAX_z;

	initialWidth = objectToDrag.offsetWidth;
	initialHeight = objectToDrag.offsetHeight;
	initialCurX = curX;
	initialCurY = curY;
	theAction = modifCurseur(p_obj, e);
}

function moveAction(p_obj,e){
	if(isClicked == false){
		modifCurseur(p_obj, e);
	}
}

function moveAllAction(e){
	var newPosX;
	var newPosY;

	if(isClicked == true){
		getPositionCurseur(e);
		switch (theAction){
			case _MOVE_ :
			newPosX = curX - ecartX;
			newPosY = curY - ecartY;
			objectToDrag.style.left = newPosX + 'px';
			objectToDrag.style.top = newPosY + 'px';
			break;
			case _RESIZE_N_ :
			resizeByTop(curY);
			break;
			case _RESIZE_S_ :
			resizeByBottom(curY);
			break;
			case _RESIZE_W_ :
			resizeByLeft(curX);
			break;
			case _RESIZE_E_ :
			resizeByRight(curX);
			break;
			case _RESIZE_NW_ :
			resizeByTop(curY);
			resizeByLeft(curX);
			break;
			case _RESIZE_NE_ :
			resizeByTop(curY);
			resizeByRight(curX);
			break;
			case _RESIZE_SW_ :
			resizeByBottom(curY);
			resizeByLeft(curX);
			break;
			case _RESIZE_SE_ :
			resizeByBottom(curY);
			resizeByRight(curX);
			break;
		}
	}
}

function leaveAction(p_obj){
	if(p_obj == objectToDrag){
		isClicked = false;
		p_obj.style.zIndex = MAX_z; //apres move elle reste on top
	}
}

function leaveAllAction(){
	if (isClicked) {
		isClicked = false;
		objectToDrag.style.zIndex = MAX_z;
		// ajout ed 25/12/06
		r  = '/xWindow/objet.php?a=set&oBD_ID='+objectToDrag.title;
		r += '&g='+objectToDrag.offsetLeft;
		r += '&h='+objectToDrag.offsetTop;
		r += '&L='+objectToDrag.offsetWidth;
		r += '&H='+objectToDrag.offsetHeight;
//		r += '&z='+this.winStack[this.nWIN].z_index;
		doAjax(r, '');
//		alert(r);
	}
}

function modifCurseur(p_obj, e){
	getPositionCurseur(e);

	// haut gauche
	if(((curY - parseInt(p_obj.style.top)) < thePadding) &&
	((curX - parseInt(p_obj.style.left)) < thePadding )){
		p_obj.style.cursor = 'nw-resize';
		return _RESIZE_NW_;
	}

	// haut droit
	if(((curY - parseInt(p_obj.style.top)) < thePadding) &&
	(((parseInt(p_obj.style.left) +  p_obj.offsetWidth) - curX) < thePadding)){
		p_obj.style.cursor = 'ne-resize';
		return _RESIZE_NE_;
	}

	// bas gauche
	if((((parseInt(p_obj.style.top) +  p_obj.offsetHeight) - curY) < thePadding) &&
	((curX - parseInt(p_obj.style.left)) < thePadding )){
		p_obj.style.cursor = 'sw-resize';
		return _RESIZE_SW_;
	}

	// bas droit
	if((((parseInt(p_obj.style.top) +  p_obj.offsetHeight) - curY) < thePadding) &&
	(((parseInt(p_obj.style.left) +  p_obj.offsetWidth) - curX) < thePadding)){
		p_obj.style.cursor = 'se-resize';
		return _RESIZE_SE_;
	}

	// cote gauche
	if((curX - parseInt(p_obj.style.left)) < thePadding ){
		p_obj.style.cursor = 'w-resize';
		return _RESIZE_W_;
	}

	// cote droit
	if(((parseInt(p_obj.style.left) +  p_obj.offsetWidth) - curX) < thePadding){
		p_obj.style.cursor = 'e-resize';
		return _RESIZE_E_;
	}

	// haut
	if((curY - parseInt(p_obj.style.top)) < thePadding){
		p_obj.style.cursor = 'n-resize';
		return _RESIZE_N_;
	}

	// bas
	if(((parseInt(p_obj.style.top) +  p_obj.offsetHeight) - curY) < thePadding){
		p_obj.style.cursor = 's-resize';
		return _RESIZE_S_;
	}

	p_obj.style.cursor = 'move';
	return _MOVE_;
}

function resizeByTop(p_curY){
	var newPosY;
	var newHeight;
	newHeight = (initialHeight + (initialCurY - p_curY));
	if(newHeight > minHeight){
		newPosY = p_curY - ecartY;
		objectToDrag.style.top = newPosY + 'px';
		objectToDrag.style.height = newHeight + 'px';
	}
}

function resizeByBottom(p_curY){
	var newHeight;
	newHeight = (initialHeight - (initialCurY - p_curY));
	if(newHeight > minHeight)
	objectToDrag.style.height = newHeight + 'px';
}

function resizeByLeft(p_curX){
	var newPosX;
	var newWidth;
	newWidth = (initialWidth + (initialCurX - p_curX));
	if(newWidth > minWidth){
		newPosX = p_curX - ecartX;
		objectToDrag.style.left = newPosX + 'px';
		objectToDrag.style.width = newWidth + 'px';
	}
}

function resizeByRight(p_curX){
	var newWidth;
	newWidth = (initialWidth - (initialCurX - p_curX));
	if(newWidth > minWidth)
	objectToDrag.style.width = newWidth + 'px';
}