  function initit()
  {
   document.onmousedown=mouseclick;
  }
  
//-------------------------------------------------------------------------------------
// doDrag event - called when dragging
function doDrag(e) {	
        if (!e) var e = window.event;

        // Get mouse coords in Xbrowser way 
        if (e.pageX || e.pageY)
        {
        	posx = e.pageX;
        	posy = e.pageY;
        }
        else if (e.clientX || e.clientY)
        {
        	posx = e.clientX + document.body.scrollLeft;
        	posy = e.clientY + document.body.scrollTop;
        }
       
        var difX=posx-window.lastX;
        var difY=posy-window.lastY;

//document.morf.debug.value=posx+'/'+posy;


	// Retrieves the x and y position for the widget and adding 
	// the dif value.
        // h=1*document.getElementById(widget_id).style.height.substr(0,document.getElementById(widget_id).style.height.length-2);
        var newX1 = parseInt(widget.style.left)+difX;
        var newY1 = parseInt(widget.style.top)+difY;

        // Sets the new position 
        widget.style.left=newX1+"px";
        widget.style.top=newY1+"px";

	// Stores the current mouse position.
	//
        window.lastX=posx;
        window.lastY=posy;
}

//----------------------------------------------------------------------------
// Called when the mouse button is released
function endDrag(e) {
	

        //gird=parseInt(document.desimorf.gird.value);
	// Snap to gird
        //var newX1 = gird*Math.round(parseInt(document.getElementById(widget_id).style.left)/gird);
        //var newY1 = gird*Math.round(parseInt(document.getElementById(widget_id).style.top)/gird);
        var newX1 = parseInt(widget.style.left);
        var newY1 = parseInt(widget.style.top);

        widget.style.left=newX1+"px";
        widget.style.top=newY1+"px";
	// Releases the onmousemove event.
        document.onmousemove=getMouseXY;
        document.onmouseup=null;
}



function mouseclick(e)
{
 if (!e) var e = window.event;

 // Get mouse coords in Xbrowser way 
 if (e.pageX || e.pageY)
 {
 	posx = e.pageX;
 	posy = e.pageY;
 }
 else if (e.clientX || e.clientY)
 {
 	posx = e.clientX + document.body.scrollLeft;
 	posy = e.clientY + document.body.scrollTop;
 }

 
 // Stores the current mouse position for further use.
 window.lastX=parseInt(posx);
 window.lastY=parseInt(posy);

 if(e.button<2) //left button
 {
    var tg = (e.target) ? e.target : e.srcElement
    var widget_id=tg.getAttribute("ID");
    if(widget_id) //we're on a widget 
    {  
       widget=new getObj(widget_id);

       if(widget_id=='orgdiv')
       {
           //make sure we're in front
           widget.style.zIndex="5000";

           document.onmousemove=doDrag;
           document.onmouseup=endDrag;
                  
           
        }
        else widget='';
     }
  }
}

//---------------------Be cool, be cross-browser ! ------------------------------
function getObj(name)
{
  if(!name) return;
  if(name=='') return;
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}

