/*

  (c) 2006 TUFaT.com. All Rights Reserved

  Additional info:
  This script was inspired from
  http://www.java2s.com/Code/JavaScript/Event/MouseDragandDrop.htm

*/

var inputMoveSelectFix;

var isInDrag = false;
var isInResize = false;

var dragElem = null;

var dragZIndex = 200;

var IEFnReturnFalse = new Function ("return false");

function mouseMoveHandler (e) {
  var ev;

  if (! e)
    ev = window.event;
  else
    ev = e;

  if (isInDrag) {
    var newX = ev.clientX - dragElem.custVar_dX;
    var newY = ev.clientY - dragElem.custVar_dY;

    if (newX < 0)
      newX = 0;

    if (newY < 0)
      newY = 0;

    var maxX = (appletWidth - Math.round (dragElem.winWidth * 40 / 100));
    var maxY = (appletHeight - Math.round (dragElem.winHeight * 40 / 100));

    if (newX > maxX)
      newX = maxX;

    if (newY > maxY)
      newY = maxY;

    dragElem.style["left"] = newX + "px";
    dragElem.style["top"] =  newY + "px";
  }

  if (isInResize) {
    var newX = ev.clientX - dragElem.custVar_X;
    var newY = ev.clientY - dragElem.custVar_Y;

    if (newX > (appletWidth - 4))
      newX = appletWidth - 4;
    if (newY > (appletHeight - 4))
      newY = appletHeight - 4;

    if (newX >= dragElem.MINWINWIDTH/3+dragElem.MINWINWIDTH/3) {
	//if (newX >= 220) {
	
      dragElem.style["width"] = newX + "px";
      dragElem.winWidth = newX;
    }

    if (newY >= dragElem.MINWINHEIGHT) {
      dragElem.style["height"] = newY + "px";
      dragElem.winHeight = newY;
    }
  }
}

function mouseDownHandler (e) {
  var ev;

  if (! e)
    ev = window.event;
  else
    ev = e;

  var downOn = (ev.target ? ev.target : ev.srcElement);

  switch (downOn.className) {
    case "windowTitle":
      isInDrag = true;
      dragElem = downOn.elementToDrag;

      if (!dragElem)
        return;

      dragElem.custVar_X = parseInt (dragElem.style["left"] + 0);
      dragElem.custVar_Y = parseInt (dragElem.style["top"] + 0);

      dragElem.custVar_dX = ev.clientX - dragElem.custVar_X;
      dragElem.custVar_dY = ev.clientY - dragElem.custVar_Y;

      dragElem.style["zIndex"] = dragZIndex++;

      document.onmousemove = mouseMoveHandler;
      break;

    case "windowResizeHandler":
	
      isInResize = true;
      dragElem = downOn.elementToDrag;

      if (!dragElem)
        return;

      dragElem.custVar_X = parseInt (dragElem.style["left"] + 0);
      dragElem.custVar_Y = parseInt (dragElem.style["top"] + 0);

      dragElem.style["zIndex"] = dragZIndex++;

      document.onmousemove = mouseMoveHandler;
      break;

    default:
  }

  if (isInDrag || isInResize) {
    document.onselectstart = IEFnReturnFalse;
    return false;
  }
}

function mouseUpHandler (e) {
  isInDrag = false;
  isInResize = false;
  document.onmousemove = null;
  document.onselectstart = null;
  dragElem = null;
}

document.onmousedown = mouseDownHandler;
document.onmouseup = mouseUpHandler;

