/* --------------------------------------------------------------------------
	--------------------------------------------------------------------------
	Clase para Drag-Drop
	Le pasamos el tipo de movimiento: 
		0 Drag-Drop por zonas, 1 Drag-drop ZX, 2-Redimensionar.
--------------------------------------------------------------------------
-------------------------------------------------------------------------- */
function WBEDragDropManager()
{
	this.object;
	this.w_helper = new WBEJsHelper();
	this.mouseXY;
	this.isMoving = false;
	this.startValues = new Object();		// Valores iniciales antes de empezar
	this.movingValues = new Object();		// Valores de movimiento
	this.endValues = new Object();		// Valores de final de la colocacion
	this.zIndex = 0;		// Profundidad
	this.type = 0;		// Resize, DragDrop(defecto)
	this.MinOffSet = 10;		// Mimino desplazamiento que se debe producir en pixeles.
	this.yRefCoord = 0;		// Px donde inicia el eje Y para posiciones globales
	this.xRefCoord	= 0;	// Px donde inicia el eje X para posiciones globales
	this.doAutoScroll = false;
		
	/* Configuracion de drag-drop */
	this.setZoneMoving = function () {	this.type=0;	};
	this.setXYMoving = function () {	this.type=1;	};
	this.setResizing = function () {	this.type=2;	};
	

	/* Continua con la ejecucion del resto de eventos */
	this.continueWithEvents = function (event) {
		if (window.event) {
			window.event.cancelBubble=true;
			window.event.returnValue=false;
		} else if (event) {
			event.preventDefault();
		};
	};

/*	-----------------------------------------------------
	Comprueba que se ha producido un minimo desplazamiento.
	Devuelve true si se ha producido.
	----------------------------------------------------- */
	this.checkMinOffSet = function (event, mouseXY) {
		this.continueWithEvents(event);
		return ( (this.startValues.cursorX - this.MinOffSet) < mouseXY.x && 
			  mouseXY.x < (this.startValues.cursorX + this.MinOffSet) &&
			(this.startValues.cursorY - this.MinOffSet) < mouseXY.y && 
				mouseXY.y < (this.startValues.cursorY + this.MinOffSet) );
	};
	
	/////////////////////////////////////////
	/////////////// Manejadores /////////////
	/////////////////////////////////////////
	/* Inicio del drag */
	this.start = function(event, id)
	{
		// Recuperamos el elemento sobre el que realizamos
		if (id) {
			this.object = document.getElementById(id);
		} else {
			// Coge el elemento sobre el que hemos realizado el evento
			if (window.event)
				this.object = window.event.srcElement;
			else if (event.target)
				this.object = event.target;

			// Si es un nodo de texto, coge el padre
			if (this.object.nodeType == 3)
  				this.object = this.object.parentNode;
		}

		// El objeto que vamos a mover, por defecto es el objeto que inicializamos.
		this.movingValues.movingObj = new WBEDivManager(this.object.id);

		this.continueWithEvents(event);

		// Posición actual del raton.
		this.mouseXY = this.w_helper.getMouseXY(event);

		// Guarda valores iniciales de posicion, tamanyo y desplazamiento.
		this.resetValues();

		// Actualiza el z z-index.
		if (this.zIndex==0) this.zIndex = this.w_helper.getMaxZindex();	// Inicializa
		this.object.style.zIndex = this.zIndex++;

		this.isMoving = false;
	};

	/* Inicializa los valores de las capas que se mueven */
	this.resetValues = function() {
		this.movingValues.movingObj = new WBEDivManager(this.object.id);
		if (this.mouseXY) {
			this.startValues.cursorX = this.mouseXY.x;
			this.startValues.cursorY = this.mouseXY.y;
			this.endValues.left =		this.mouseXY.x;
			this.endValues.top =		this.mouseXY.y;
		}
		this.startValues.x  = ( parseInt(this.movingValues.movingObj.x0, 10) || 0);
		this.startValues.y   = ( parseInt(this.movingValues.movingObj.y0,  10) || 0);
		this.startValues.z = 	this.movingValues.movingObj.div.style.zIndex;
		this.startValues.top  = ( parseInt(this.object.offsetTop, 10) || 0);
		this.startValues.left   = ( parseInt(this.object.offsetLeft,  10) || 0);
		this.startValues.h = 	this.movingValues.movingObj.h;
		this.startValues.w = 	this.movingValues.movingObj.w;
	}
	
	/* Funcion del movimiento de drag and drop */
	this.move = function (event)
	{
		// Posicion del raton
		this.mouseXY = this.w_helper.getMouseXY(event);
		
		// Hacemos el scroll para movimiento XY
		if (this.doAutoScroll)
			this.w_helper.doPageScroll(this.mouseXY);

		this.continueWithEvents(event);
	
		switch (this.type)
		{
			case 0:	// Posicionamiento por zonas
				if (this.mouseXY.x<0 || this.mouseXY.y<0) return;
				this.movingValues.movingObj.moveToXY(this.mouseXY.x +3, this.mouseXY.y +3); // Inc de px para el mouse over
				break;
			case 1: // Posicionamiento XY
				// El movimiento XY es relativo a la capa padre
				if (this.mouseXY.x<0 || this.mouseXY.y<0) return;
				this.movingValues.movingObj.moveToXY(this.startValues.left + (this.mouseXY.x - this.startValues.cursorX) ,
												this.startValues.top + (this.mouseXY.y - this.startValues.cursorY));
				break;
			case 2:	// Redimensionar
				this.movingValues.movingObj.w = 100 + (this.mouseXY.x - this.startValues.cursorX );
				this.movingValues.movingObj.h = 100 + (this.mouseXY.y - this.startValues.cursorY);
				if (this.movingValues.movingObj.w<100) this.movingValues.movingObj.w = null;
				if (this.movingValues.movingObj.h<100) this.movingValues.movingObj.h = null;
				this.movingValues.movingObj.resize(this.movingValues.movingObj.w, this.movingValues.movingObj.h);
				break;
		}
	}

	/* Evento de parar el drag-drop */
	this.stop = function (event) {
		// Posición del ratón actual
		this.mouseXY = this.w_helper.getMouseXY(event);
		this.isMoving = false;
	}

	/* Reset de todas las variables de este desplazador */
	this.reset = function () {
		this.object = null;
	}

	this.manageMouseMoveHandler = function (handler, remove) {
		this.w_helper.handleEvent(document, "mousemove", handler, remove);
	};

	this.manageMouseUpHandler = function (handler, remove) {
		this.w_helper.handleEvent(document, "mouseup", handler, remove);
	};
};
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////