var Drag = {

	obj : null,

	init : function(o)
	{  	cursor.change("openhand")

  		o.onmousedown	= Drag.start;

		o.hmode			= true ;
		o.vmode			= true ;

		o.root = o ;

		if (isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
				//map.HideAllLayers();
		   	  	cursor.change("closedhand")
				clearInterval(map.time);

				var o = Drag.obj = this;
				e = Drag.fixE(e);
				var y = parseInt(o.root.style.top);
				var x = parseInt(o.root.style.left);
				o.root.onDragStart(x, y);

				o.lastMouseX	= e.clientX;
				o.lastMouseY	= e.clientY;

		  		document.onmousemove	= Drag.drag;
				document.onmouseup		= Drag.end;
				return false;

	},

	drag : function(e)
	{
	  	cursor.change("closedhand")
		map.info=0;
	  	clearInterval(map.time);
		e = Drag.fixE(e);

        var o = Drag.obj, ey = e.clientY, ex= e.clientX, y = parseInt(o.root.style.top), x = parseInt(o.root.style.left), nx, ny;

		nx = x + (ex - o.lastMouseX)
		ny = y + (ey - o.lastMouseY)

//		nx = (nx < 0 && nx + map.mlr < map.scrmapw) ? map.scrmapw - map.mlr : nx;
//		nx = (nx > 0) ? 0 : nx;
//		ny = (ny < 0 && ny + map.mlb < map.scrmaph) ? map.scrmaph - map.mlb : ny;
//		ny = (ny > 0) ? 0 : ny;

		var sx = Math.floor(-constants['min_coord_x']/map.zoom*map.edge0);
		var sy = Math.floor(-constants['min_coord_y']/map.zoom*map.edge0);

		nx = (map.mlr > 1600 && nx < 0 && nx + map.mlr < map.scrmapw + sx) ? map.scrmapw + sx - map.mlr : nx;
nx = (map.mlr <= 1600 && nx + map.mlr < map.scrmapw + sx) ? map.scrmapw + sx - map.mlr : nx;
		nx = (nx > sx) ? sx : nx;
		ny = (map.mlb > 800 && ny < 0 && ny + map.mlb < map.scrmaph + sy) ? map.scrmaph + sy - map.mlb : ny;
ny = (map.mlb <= 800 && ny + map.mlb < map.scrmaph + sy) ? map.scrmaph + sy - map.mlb : ny;
		ny = (ny > sy) ? sy : ny;


		Drag.obj.root.style.left = nx + "px";
		Drag.obj.root.style.top = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);

 	 	map.setParam("proX", Math.round(nx));
 		map.setParam("proY", Math.round(ny));

	  	map.drag(Math.round(nx));
		
//		log(("nx="+ny+" map.mlr="+map.mlb+" map.scrmapw"+map.scrmaph+" sx="+sy),1);
	
    	return false;
	},

	end : function()
	{
	    cursor.change("openhand")
        cursor.check();

		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style.left),
									parseInt(Drag.obj.root.style.top));
		Drag.obj = null;
        if(map.info==0 && typeof(admin) == "undefined")
	  //	map.ShowAllLayers();
	   	map.getObjects();
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};
