/*============================================================================================
 Fichier Javascript
 Fichier contenant divers Fonction de déplacement generique
 Attention en cours de développement

 AUTEUR:					Cedric POUYEZ
 CREATION:					08/04/2009
 VARIABLE GLOBALES:			
 MODIFICATIONS:				
===============================================================================================
	copyright 2009 Alban BALLIEUX - ballieuxa@laon.noirot
	http://www.phpmyportal.info

	This file is part of phpMyPortal.

    phpMyPortal is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or
    any later version.

    phpMyPortal is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with phpMyPortal; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===============================================================================================*/
var Compteur;
var Mere;
var SAVEvent;
var Target;
/**
 * 
 */
function Init_DragAndDrop(){
	AjouteEvent('onmousedown',PInitDrag);
	AjouteEvent('onmousemove',Sync_Drag);
	AjouteEvent('onmouseup',Drop);
	Compteur = 0;
	Mere = '';
}

var OLDId = '' ;
var OldZI = 0 ;
var blockdrag = false ;
var metcompteur = false; 

/**
 * 
 * @param {Object} e
 */
function PInitDrag(e){
	if(!e) e = window.event;
	SAVEvent = e;
	metcompteur = true;
	
	Target = ETarget(e) ;
	
// Je boucle pour recherche si l'un des pere est dragable
	while(Target.tagName!='HTML' && !isset(Target.getAttribute('drag'))){Target=Target.parentNode;}

	if (Btn_Gauche_Mouse(e) && isset(Target.getAttribute('drag'))) {
		PX = e.clientX;
		PY = e.clientY;
		setTimeout('CPT('+PX+','+PY+')', 20);
	}
}

/**
 * 
 * @param {Object} PX
 * @param {Object} PY
 */
function CPT(PX,PY){
	Compteur++;
	if (metcompteur == true) {
		if (Compteur < 5) {
			setTimeout('CPT('+PX+','+PY+')', 20);
		}
		else {
			DebutDrag(PX,PY);
		}
	}else{
		Compteur = 0;
	}
	return;
}

/**
 * 
 * @param {Object} PX
 * @param {Object} PY
 */
function DebutDrag(PX,PY){
	TargetI = Target ;
	
	if(isset(Target.getAttribute('drag')) ){
		if (Target.getAttribute('drag') == 'on') {
			blockdrag = true; 
			Zone = TargetI;
			Mere = Zone.parentNode;
			Zone.style.position = 'absolute';
			Zone.style.left = (PX + 2) + 'px';
			Zone.style.top = (PY + 2 ) + 'px';
			document.body.appendChild(Zone);
			OLDId = Zone.id;
			Zone.id = "zonedrag";
			OldZI = getCssValue('zonedrag','zIndex') ;
			Zone.style.zIndex = 100000000;
			document.body.style.MozUserSelect = 'none';
			if (!GetPtId("imgdrag")) {
				ZoneImg = document.createElement('span');
				ZoneImg.id = "imgdrag";
				ZoneImg.style.position = 'absolute';
				document.body.appendChild(ZoneImg);
			}
		}
	}
}

function Sync_Drag(e){
	if (!e) var e = window.event;
	Target = ETarget(e) ;
	if (blockdrag) {
		Zone = GetPtId("zonedrag");
		Zone.style.left = (e.clientX + 2) + 'px';
		Zone.style.top = (e.clientY + 2 ) + 'px';
		ZoneImg = GetPtId("imgdrag") ;
		ZoneImg.style.left = (e.clientX - 40 ) + 'px';
		ZoneImg.style.top = (e.clientY - 40 ) + 'px';
		ZoneImg.style.display = 'block';
		if(isset(Target.getAttribute('rec_drag'))){
			ZoneImg.className = 'AUTODRAG';
		}else{
			ZoneImg.className = 'INTERDRAG';
		}
		ChangeOpac('imgdrag',80);
	} 
}

function Drop(e){
	if (!e) var e = window.event;
	Target = ETarget(e);
	Compteur = 0;
	
	if (blockdrag && isset(Target.getAttribute('rec_drag'))) {
		Target.id = 'zone_dest';
		Dest = Target;
		Retour = false ;
	}else {
		Dest = Mere;
		Retour = true ;
	}
	Zone = GetPtId("zonedrag");
	if (isset(Target) && Zone != false) {
		
		Zone.id = OLDId;
		Dest.appendChild(Zone);
		Zone.style.position = 'relative';
		Zone.style.left = '';
		Zone.style.top = '';
		Zone.style.zIndex = OldZI;
		ZoneImg = GetPtId("imgdrag");
		ZoneImg.style.display = 'none';
		blockdrag = false;
		if (!Retour) {
			Genere_Zone_Compl_Si_Besoin('zone_dest', 1);
			Target.id = '';
			if (isset(Zone.getAttribute('ondrop'))) {
				TMacrosDrop = new Array();
				TMacrosDrop['cpt'] = 0;
				action = Zone.getAttribute('ondrop');
				action = Macro(action,Dest);
				eval(action);
			}
		}
	}
	blockdrag = false;
}

