/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/


var in_drag = false;


function disableBubble(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}


function remove(id)
{

	var object = findObj(id);

	object.parentNode.removeChild(object);

	xmlconn.request("/mypage/edit/deleted?widgetID=" + escape(id));   // tell web server
}




var Drag = {

	obj : null,

	init : function(o, canResize, oRoot, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;
		o.onmousemove	= Drag.setCursor;
		
		o.hmode			= true ;
		o.vmode			= true ;
		
		o.canResize = canResize;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
		o.root.onDragResize	= new Function();
		
	},
	

	setCursor : function(e)
	{
		if(e==null)	 e = window.event;
	
		if(in_drag)	return;
	
		var o = Drag.obj = this;

		Drag.obj.style.cursor = Drag.resizeMode(e, o)?"se-resize":"move";

		return false;
	},


	resizeMode : function(e, o)
	{


		if(!o.canResize)	return(false);

		var y = parseInt(o.style.top);
		var x = parseInt(o.style.left);
		var width = parseInt( o.style.width );
		var height = parseInt( o.style.height );

		var resize_mode = ( Math.abs ( x + width - e.clientX) <= 5 &&
		    Math.abs ( y + height - e.clientY) <= 5);

		return resize_mode;
	},





	start : function(e)
	{
		var o = Drag.obj = this;

		if(in_drag)	return;

		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var width = parseInt( o.root.style.width );
		var height = parseInt( o.root.style.height );

		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;
		o.startMouseX	= e.clientX;
		o.startMouseY	= e.clientY;
		o.origWidth = width;
		o.origHeight = height;


		var resize_mode =  Drag.resizeMode(e, o);

		if(resize_mode)	{

			document.onmousemove	= Drag.dragResize;

			o.root.onDragEnd	= Drag.recordResize;
				
		} else	{
		
			setOpacity(o, 60);
			document.onmousemove	= Drag.dragMove;

			o.root.onDragEnd	= Drag.recordDrag;
		}		
		
		document.onmouseup		= Drag.end;

		in_drag = true;

		return false;
	},



	dragMove : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		nx = x + ((ex - o.lastMouseX) * 1);
		ny = y + ((ey - o.lastMouseY) * 1);

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},



	dragResize : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var width = parseInt( o.root.style.width );
		var height = parseInt( o.root.style.height );
		var nwidth, nheight;

		nwidth = o.origWidth + (ex - o.startMouseX);
		nheight = o.origHeight + (ey - o.startMouseY);

		var origAspect = o.origWidth / o.origHeight;
		
		var min_height = 30;
		var min_width = origAspect * min_height;
		
		if(nheight < min_height)	nheight = min_height;
		if(nwidth < min_width)		nwidth = min_width;


		var nAspect = nwidth / nheight;

		if(nAspect>origAspect)	{
		
			nwidth = o.origWidth * (nheight / o.origHeight);
		}
		else if(nAspect<origAspect)	{
		
			nheight = o.origHeight * (nwidth / o.origWidth);
		}



		Drag.obj.root.style["width"] = nwidth + "px";
		Drag.obj.root.style["height"] = nheight + "px";

		Drag.obj.root.onDragResize(nwidth, nheight);
		
		return false;
	},









	end : function()
	{
		

		setOpacity(Drag.obj, 100);
	
		document.onmousemove = null;
		document.onmouseup   = null;
		
		Drag.obj.root.onDragEnd(Drag.obj.root);
		Drag.obj = null;
	
		in_drag = false;
	},


	recordDrag : function(obj)	{
	
		xmlconn.request("/mypage/edit/dragged?widgetID=" + escape(this.id) + "&new_top=" + parseInt(obj.style["top"]) + "&new_left=" + parseInt(obj.style["left"]));   // tell web server
	},
	
	recordResize : function(obj)	{

		xmlconn.request("/mypage/edit/resized?widgetID=" + escape(this.id) + "&new_width=" + parseInt(obj.style["width"]) + "&new_height=" + parseInt(obj.style["height"]));   // tell web server
	},
	
	
	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;
	}
	
};