var isDragging = false;
var objectToDrag;
var obj;
var ecartX;
var ecartY;
var curX;
var curY;
var newPosX;
var newPosY;
var thePadding = 10;
var _RESIZE_E_ = 4;
var _RESIZE_W_ = 3;
var _MOVE_ = 0;
var minWidth = 40;
var zindex=0;


function positionne(p_id, p_posX, p_posY){
	document.getElementById(p_id).style.left = p_posX;
	document.getElementById(p_id).style.top = p_posY;
	document.getElementById(p_id).style.zIndex = 0;
}

function PremierPlan(p_id) {
	// On incrémente le zIndex dans le but d'avoir le zIndex le plus élevé
	zindex++;
	
	// On affecte le nouvel zIndex à la fenetre
	p_id.style.zIndex = zindex;
}


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 beginDrag(p_obj,e){

// On met la fenetre au premier plan
PremierPlan(p_obj);

isDragging = true;
objectToDrag = p_obj;
getPositionCurseur(e);
ecartX = curX - parseInt(objectToDrag.style.left);
ecartY = curY - parseInt(objectToDrag.style.top);

initialWidth = objectToDrag.offsetWidth;
initialHeight = objectToDrag.offsetHeight;
initialCurX = curX;
initialCurY = curY;
// Si c'est IE (correction de la selection)
if(document.all)
{
selection();
}
theAction = modifCurseur(p_obj, e);
}

function drag(e){
if(isDragging == 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_W_ :
//resizeByLeft(curX);
//break;
//case _RESIZE_E_ :
//resizeByRight(curX);
//break;
}
}

}

function modifCurseur(p_obj, e){
getPositionCurseur(e);

// 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_;
}*/

// Déplacement
p_obj.style.cursor = 'move';
return _MOVE_;
}

function endDrag(){

// On met le focus sur le champ de recherche Google
SetFocus('q');

isDragging = false;
objectToDrag.style.cursor = 'pointer';
saveCookie();

// Si c'est IE (correction de la selection)
if(document.all)
{
selection();
}
}

// Empeche la selection
function selection()
{
function voleur(e)
{ return false; }

function bien()
{ return true; }

document.onselectstart=new Function ("return false");

if (window.sidebar)
{
document.onmousedown=voleur;
document.onclick=bien;
}
}

/*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';
}*/

function leaveAllAction(){

	if (isClicked) {

		isClicked = false;

		objectToDrag.style.zIndex = 0;

	}

}

function doubleClick(p_obj){

	if (document.getElementById(p_obj.id + '_display').style.display == 'none')
	{
		document.getElementById(p_obj.id + '_display').style.display = 'block';
		
	}else{
	
		document.getElementById(p_obj.id + '_display').style.display = 'none';
		document.getElementById(p_obj.id ).style.width = '20';
		
	}
}
