/*

  (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;
  }
  
 // alert(window.event);

	
  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";
	/* 
	
	////
	
	
	
	*/
	var cpDiv = document.getElementById ("colorPicker");
//	alert(dragElem.id);
	if (cpDiv != null && dragElem.id == 'optionsWindow' )
	{

		if ( getBrowser()  == 2)//Firefox
		{
			cpDiv.style["left"] = (newX) + 275 + "px";
    		cpDiv.style["top" ] = (newY + 108 ) + "px";
		}
		
		if ( getBrowser()  == 4)//IE6
		{
			cpDiv.style["left"] = (newX + 255 ) + "px";
			cpDiv.style["top" ] = (newY + 124 ) + "px";
		}
		
		if ( browserISIE7() )//IE7
		{
			cpDiv.style["left"] = (newX + 237 ) + "px";
			cpDiv.style["top" ] = (newY + 111 ) + "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;
    }
	
	
	if (getBrowser ()== 2)
	{
		var index = String(dragElem.id).indexOf ('WinPrivate');
		var number = String(dragElem.id).substr(index +10 );
		var fieldId = "privatetext" + number;
		var privtext = document.getElementById (fieldId);
				
		if (privtext != null &&  String(dragElem.id).indexOf ('WinPrivate') != -1 )
		{
			var privmess = document.getElementById ('userpwMessages' + number );
			if (privmess != null)
			{
				privtext.style["width"] = privmess['offsetWidth'] - 114 + "px"; 
			}
			var italicBtn = document.getElementById ('italicPrivateBtn' + number);
			if (italicBtn != null)
			{
				italicBtn.style["left"] = privmess['offsetWidth'] - 107 + "px"; 
			}
			var boldBtn = document.getElementById ('boldPrivateBtn'  + number);
			if (boldBtn != null)
			{
				boldBtn.style["left"] = privmess['offsetWidth'] - 70 + "px"; 
			}
			var smileBtn = document.getElementById ('smilesPrivateBtn' + number);
			if (smileBtn != null)
			{
				smileBtn.style["left"] = privmess['offsetWidth'] - 33 + "px"; 
			}
			
		}
	}

  }
}

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;

      if (dragElem.id != 'optionsWindow')
	  {
      	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);
	  if (dragElem.id != 'optionsWindow')
	  {
      	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;
