
//------------------------------------------------------------------------------------
//var DragText_CurrSpanOver          = null;
var DragText_CopyCurrDragElement   = null;
var DragText_sDragTextControl      = null;
var DragText_nCopyCurrMarkID       = null;
var DragText_SaveMouseMove         = null;
var DragText_SaveMouseUp           = null;
var DragText_SaveSelectStart       = null;
var DragText_ReturnElementLeft     = null;
var DragText_ReturnElementTop      = null;
var DragText_nLeft                 = null;
var DragText_nTop                  = null;
//var DragText_OldSpanOver           = null;
var DragText_OrigianElement        = null;
var DragText_ColorMouseOver        = null;
var DragText_ColorMouseOut         = null;
var DragText_oWordRange            = null;

var OriginalItem                   = null;
var CountResults = 0;
var DisableDraging = false;
var SearchOffsetY = 3;

function DragText_OnMouseMove()
//       ~~~~~~~~~~~~~~~~~~~~
{
    event.srcElement.releaseCapture();
    var ScreenWidth = screen.width;
    var PointerPositionX;
    var DragElemetPositionX;
    var DragElemetPositionY;
    
    if (DragText_CopyCurrDragElement != null)
    {
      var oTextControl = document.getElementById("TEXT_" + DragText_sDragTextControl);
            
      PointerPositionX = event.clientX - document.body.scrollLeft + DragText_CopyCurrDragElement.offsetWidth/2 + 20;      
      
      if (!oTextControl || PointerPositionX > ScreenWidth)
        return;              
                
      // Place the pointer in the middle of the drage element
      DragElemetPositionX = event.clientX + document.body.scrollLeft - DragText_CopyCurrDragElement.offsetWidth/2 - 20;
      DragElemetPositionY = event.clientY + document.body.scrollTop - DragText_CopyCurrDragElement.offsetHeight/2;

      DragText_CopyCurrDragElement.style.top  = DragElemetPositionY;
      DragText_CopyCurrDragElement.style.left = DragElemetPositionX;
//      document.body.style.cursor = "hand";      
      
        
      
      var oWordRange = null;      
     
      var oTextControl = document.getElementById("TEXT_" + DragText_sDragTextControl);
      if (!oTextControl)
        return;      
        
      if (COMMON_IsPointInElement(oTextControl, COMMON_MouseXToAbsoluteLeft(event.clientX), event.clientY + document.body.scrollTop))
      {
        oWordRange = GetWord(SearchOffsetY);                
        if (oWordRange == null || oWordRange.text == null)
          return;
                  
        var sPunct = "\",.'!-:;?)(\n";
        if (sPunct.indexOf(oWordRange.text.charAt(0)) >= 0)
          oWordRange = null;
      }
      
      var bSameWord = false;
      if (oWordRange == null)
      {
        if (DragText_oWordRange == null)
          bSameWord = true;
      }
      else
      {
        if (DragText_oWordRange && oWordRange.isEqual(DragText_oWordRange))
          bSameWord = true;
      }
      if (!bSameWord)
      {
        if (DragText_oWordRange)
          DragText_ClearHovered();                
        DragText_oWordRange = oWordRange;
        if (DragText_oWordRange)
        {

          var sDragText = "<span id=DRAGTEXT_Hovered style=\"background-color:" + oTextControl.sColorDragOver + "\">" + DragText_oWordRange.htmlText + "</span>";
          
          DragText_oWordRange.pasteHTML("");          
          DragText_oWordRange.pasteHTML(sDragText);
        }
      }
    }     
}

//------------------------------------------------------------------------------------
function DragText_OnMouseUp()
//       ~~~~~~~~~~~~~~~~~~
{      
  var DragElemetPositionX;
  var DragElemetPositionY;
  
  //alert(document.body.scrollTop);
  DragElemetPositionX = event.clientX + document.body.scrollLeft - DragText_CopyCurrDragElement.offsetWidth/2 - 20;
  DragElemetPositionY = event.clientY - DragText_CopyCurrDragElement.offsetHeight/2;
    
  var bReturn = false;  
  
  if (DragText_oWordRange)
  {
    var oHovered = document.getElementById("DRAGTEXT_Hovered");
    if (oHovered && oHovered.all(DragText_nCopyCurrMarkID))
    {
      
      // Replace only the current place
      OriginalItem.nMarkID = OriginalItem.nMarkID + "_Moved"; // We moved this element, so we changed the nMarkID
      
      oHovered.children[0].innerHTML = DragText_CopyCurrDragElement.innerHTML;
      DragText_ReplaceAllMarksWithHTML(DragText_CopyCurrDragElement , DragText_nCopyCurrMarkID);
      DragText_CopyCurrDragElement.innerHTML = "";
      DragText_CopyCurrDragElement = null;      
  
    }
    else
      bReturn = true;
    DragText_ClearHovered();
    DragText_oWordRange = null;
  }
  else
    bReturn = true;

  if (bReturn)
  {    
    DragText_nLeft       = DragElemetPositionX;
    DragText_nTop        = DragElemetPositionY;
    DragText_ReturnItemToHisPlace();       
  }  

  //document.body.style.cursor = "default";  
  
  document.onmousemove = DragText_SaveMouseMove;
  document.onmouseup   = DragText_SaveMouseUp;  
  document.onselectstart = DragText_SaveSelectStart;
  
  DragText_SaveMouseMove = null;
  DragText_SaveMouseUp   = null;
  DragText_SaveSelectStart = null;  
}

//------------------------------------------------------------------------------------

function DragText_SelectStart ()
//       ~~~~~~~~~~~~~~~~~~~~
{
  event.cancelBubble = true;
  return false;
}
//------------------------------------------------------------------------------------
function DragText_StartElementDrag (sDragTextControl, oCopyCurrDragElement, nCopyCurrMarkID)
//       ~~~~~~~~~~~~~~~~~~~~~~~~~
{  
  //if the last DragElement hasn't been returned to his place yet
  if(DragText_CopyCurrDragElement != null)
  {
     return;
  }  
  DragText_sDragTextControl    = sDragTextControl;
  DragText_CopyCurrDragElement = oCopyCurrDragElement;
  DragText_nCopyCurrMarkID     = nCopyCurrMarkID;
  
  DragText_ReturnElementLeft = event.clientX + document.body.scrollLeft - DragText_CopyCurrDragElement.offsetWidth/2 - 20; 
  //DragText_ReturnElementTop  = event.clientY + document.body.scrollTop - DragText_CopyCurrDragElement.offsetHeight/2;
  DragText_ReturnElementTop  = event.clientY - DragText_CopyCurrDragElement.offsetHeight/2;
    
  DragText_SaveMouseMove = document.onmousemove;
  document.onmousemove   = DragText_OnMouseMove;
  DragText_SaveMouseUp   = document.onmouseup;
  document.onmouseup     = DragText_OnMouseUp;
  DragText_SaveSelectStart = document.onselectstart;
  document.onselectstart = DragText_SelectStart;  
}

//------------------------------------------------------------------------------------

function DragText_ReplaceAllMarksWithHTML(oReplaceHtml , nMarkID)
//       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{ 
  for(i = 0; i < document.all.length; i++)
    if ((document.all(i).tagName == "SPAN") &&(document.all(i).id == nMarkID))
      document.all(i).innerHTML = oReplaceHtml.innerHTML; 
  
}
//------------------------------------------------------------------------------------

function DragText_ReplaceCurrentMarkWithHTML(oReplaceHtml , nMarkID)
//       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{ 
  for(i = 0; i < document.all.length; i++)
    if ((document.all(i).tagName == "SPAN") &&(document.all(i).id == nMarkID))
      document.all(i).innerHTML = oReplaceHtml.innerHTML; 
 
}
//------------------------------------------------------------------------------------

function DragText_ClearHovered ()
//       ~~~~~~~~~~~~~~~~~~~~~
{
  var oHovered = document.getElementById("DRAGTEXT_Hovered");
  if (oHovered)
    oHovered.outerHTML = oHovered.innerHTML;
}
//------------------------------------------------------------------------------------

function DragText_ReturnItemToHisPlace()
//       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
  DragText_SetPos(DragText_CopyCurrDragElement, DragText_nLeft + document.body.scrollLeft, DragText_nTop + document.body.scrollTop)    
  
  horizontal    = DragText_ReturnElementLeft - DragText_nLeft;
  perpendicular = DragText_ReturnElementTop - DragText_nTop;
  
  if (horizontal > 0)
    nLeftAdded = 10
  else
    nLeftAdded = -10;
  if (perpendicular > 0)
    nTopAdded = 10
  else
    nTopAdded = -10;

  if  (Math.abs(horizontal) > Math.abs(perpendicular))
  {   
    if (perpendicular != 0)
      ratio = Math.abs(horizontal / perpendicular)
    else
      ratio = 0;
    DragText_nLeft += nLeftAdded;
    
    if (ratio != 0)
      DragText_nTop += nTopAdded/ratio;
    
    if (Math.abs(DragText_nLeft - DragText_ReturnElementLeft) > 10)
    {
      setTimeout("DragText_ReturnItemToHisPlace()", 20)
    }
    else 
    {
      if(DragText_CopyCurrDragElement)
      {
        DragText_CopyCurrDragElement.style.left = DragText_nLeft;
        DragText_CopyCurrDragElement.style.top = DragText_nTop; 
        DragText_CopyCurrDragElement.style.display = "none";
        DragText_CopyCurrDragElement = null;
      }
      
    } 
  }
  else
  {
    if (horizontal != 0)
      ratio = Math.abs(perpendicular / horizontal)
    else
      ratio = 0;
    DragText_nTop += nTopAdded;
    
    if (ratio != 0)
      DragText_nLeft += nLeftAdded/ratio;
      
    if (Math.abs(DragText_nTop - DragText_ReturnElementTop) > 10)
      setTimeout("DragText_ReturnItemToHisPlace()", 20)
    else 
    {
      if(DragText_CopyCurrDragElement)
      {
        DragText_CopyCurrDragElement.style.left = DragText_nLeft;
        DragText_CopyCurrDragElement.style.top = DragText_nTop; 
        DragText_CopyCurrDragElement.style.display = "none";
        DragText_CopyCurrDragElement = null;      
      }
    }
  }
}
//------------------------------------------------------------------------------------

function DragText_SetPos (oElement, nLeft, nTop)
//       ~~~~~~~~~~~~~~~
{
  if (oElement != null)
  {
    oElement.style.left = nLeft;
    oElement.style.top  = nTop; 
  }
}
//-----------------------------------------------------------------
