/**
 * A1DomDrag -  simple drag & drop for DOM model browsers (15.02.2008)
 * @namespace A1DomDrag
 * @author Marc Bufe
 * @version $Rev$
*/

var A1DomDrag = {

	obj : null,
	oldPosX : null,

  /**
  inits Dragging
  @function init
  @param {object} o - the object whis is draggable by mouse
  @param {object} oRoot -  the root object in which the object is draggable
  @param {int optional} minX - how many pixels object can be moved?
  @param {int optional} maxX - how many pixels object can be moved?
  @param {int optional} minY - how many pixels object can be moved?
  @param {int optional} maxY - how many pixels object can be moved?
  */
	init : function(o, oRoot, minX, maxX, minY, maxY)
	{
    // prerequisites: check for correct position css property and
    // set style if not set, otherwise css property is unknown and cannot be addressed properly!
    if (o.style.position != 'absolute' && o.style.position != 'relative') alert (o.id + " is neither relative nor absolute!");
    if (isNaN(parseInt(o.style.left))) o.style.left   = "0px";
		if (isNaN(parseInt(o.style.top ))) o.style.top    = "0px";

		o.onmousedown	= A1DomDrag.start;

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.onDragStart	= new Function();
    o.onDrag		  = new Function();
  	o.onDragEnd	  = new Function();
	},

  /**
  starts Dragging on mouse click down in the object
  @function start
  @param {event} e - the overgiven event
  */
	start : function(e)
	{
		var o = A1DomDrag.obj = this;		
		
		// oldDragX = o.offsetLeft; //document.getElementById(o);
		// oldDragY = o.offsetTop;
		
		if (typeof e == 'undefined') e = window.event;
		var y = parseInt(o.style.top );
		var x = parseInt(o.style.left);
		o.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

    if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
		if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;

		if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
		if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;

		document.onmousemove	= A1DomDrag.drag;
		document.onmouseup		= A1DomDrag.end;

		return false;
	},

  /**
  drags the object by moving the mouse
  @function drag
  @param {event} e - the overgiven event
  */

	drag : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		var o = A1DomDrag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.style.top);
		var x = parseInt(o.style.left);
		var nx, ny;

		if (o.minX != null) ex = Math.max(ex, o.minMouseX);
		if (o.maxX != null) ex = Math.min(ex, o.maxMouseX);
		if (o.minY != null) ey = Math.max(ey, o.minMouseY);
		if (o.maxY != null) ey = Math.min(ey, o.maxMouseY);

		nx = x + (ex - o.lastMouseX);
		ny = y + (ey - o.lastMouseY);

		A1DomDrag.obj.style["left"] = nx + "px";
		A1DomDrag.obj.style["top"] = ny + "px";
		A1DomDrag.obj.lastMouseX	= ex;
		A1DomDrag.obj.lastMouseY	= ey;

                // nasty insert drag code
		// window.status = nx + " " + ny + " " + x+ " "+oldDragX + " " +oldDragY;
	//oldBaseTileX = baseTileX;
	//oldBaseTileY = baseTileY;
		/*
		baseTileX = baseTileX + -Math.ceil ((o.offsetLeft) / tileWidth );
		baseTileY = baseTileY + -Math.ceil ((o.offsetTop)  / tileHeight);
		window.status = baseX + " " + baseY + " " + baseTileX + " " + baseTileY;
		if ((oldBaseTileX != baseTileX) || (oldBaseTileY != baseTileY)) {
		  isMapOutdated = true;		
		  mapRedraw();
		}
*/
		A1DomDrag.obj.onDrag(nx, ny);
				
		return false;
	},

  /**
  ends Dragging on mouse click up in the object
  @function end
  @param {event} e - the overgiven event
  */
	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		A1DomDrag.obj = null;
		//alert(document.getElementById('map').offsetLeft);
	}
};
