﻿function setCtrlpMode(mode)
{
  if( mode == 1 )
  {
    oqp_ctrlp_ctrl_canvas.title = "Close";
    
    ctrlp_mode = mode;
    ctrlp_state = 1;
    oqp_ctrlp_ctrl_canvas.style.marginLeft = '-49px';
    if( direction == 1 )
    {
      OQP_Body_Main.style.left = '230px';
      OQP_Body_Main.style.right = '0px';
    }
    else
    {
      OQP_Body_Main.style.left = '0px';
      OQP_Body_Main.style.right = '230px';
    }
  }
  else if( mode == -1 )
  {
    oqp_ctrlp_ctrl_canvas.title = "Pin";
    
    ctrlp_mode = mode;
    ctrlp_state = 0;
    oqp_ctrlp_ctrl_canvas.style.marginLeft = -32 + (direction*(-17) - 17) + 'px';
    if( direction == 1 )
    {
      OQP_Body_Main.style.left = '31px';
      OQP_Body_Main.style.right = '0px';
      OQP_Ctrlp.style.left = "-230px";
    }
    else
    {
      OQP_Body_Main.style.left = '0px';
      OQP_Body_Main.style.right = '31px';
      OQP_Ctrlp.style.right = "-230px";
    }
    
    mousepos = 0;
    setFocusCtrlp(false);
  }
  
  OQP_Text_scroller.resizeY();
  OQP_Text_scrollbar.resizeY();
}
function toggleCtrlpMode()
{
  if(ctrlp_mode == 1)
  { setCtrlpMode(-1); }
  else
  { setCtrlpMode(1); }
}

function setFocusCtrlp(bool)
{
  focus = bool;
  if(!focus)
  { hideCtrlp(); }
}

function hideCtrlp()
{
  if( ctrlp_mode == -1 && !focus )
  {
    ctrlp_state = 0;
    if(direction == 1)
    { ctrlp_moveleft_LTR(20,-230); }
    else
    { ctrlp_moveright_RTL(20,-230); }
  }
}
function flyoutCtrlp()
{
  if(ctrlp_mode == -1)
  {
    ctrlp_state = 1;
    if(direction == 1)
    { ctrlp_moveright_LTR(20,0); }
    else
    { ctrlp_moveleft_RTL(20,0); }
  }
}

function ctrlp_moveleft_LTR(dx, endpos)
{
  if( ctrlp_state == 0 )
  {
    var current = parseInt(OQP_Ctrlp.style.left) - dx;
    
    if(current <= endpos)
    { OQP_Ctrlp.style.left = endpos + "px"; }
    else
    {
      OQP_Ctrlp.style.left = current + "px";
      setTimeout('ctrlp_moveleft_LTR('+ dx +','+ endpos +')',15);
    }
  }
}
function ctrlp_moveright_LTR(dx, endpos)
{
  if( ctrlp_state == 1 )
  {
    var current = parseInt(OQP_Ctrlp.style.left) + dx;
    
    if(current >= endpos)
    { OQP_Ctrlp.style.left = endpos + "px"; }
    else
    {
      OQP_Ctrlp.style.left = current + "px";
      setTimeout('ctrlp_moveright_LTR('+ dx +','+ endpos +')',15);
    }
  }
}
function ctrlp_moveleft_RTL(dx, endpos)
{
  if( ctrlp_state == 1 )
  {
    var current = parseInt(OQP_Ctrlp.style.right) + dx;
    
    if(current >= endpos)
    { OQP_Ctrlp.style.right = endpos + "px"; }
    else
    {
      OQP_Ctrlp.style.right = current + "px";
      setTimeout('ctrlp_moveleft_RTL('+ dx +','+ endpos +')',15);
    }
  }
}
function ctrlp_moveright_RTL(dx, endpos)
{
  if( ctrlp_state == 0 )
  {
    var current = parseInt(OQP_Ctrlp.style.right) - dx;
    
    if(current <= endpos)
    { OQP_Ctrlp.style.right = endpos + "px"; }
    else
    {
      OQP_Ctrlp.style.right = current + "px";
      setTimeout('ctrlp_moveright_RTL('+ dx +','+ endpos +')',15);
    }
  }
}
