// =========================
//
// Drag Class
// Controls Corner of the page
//
// Celso Gomes
// 08.06.06
//
// =========================

var iNextPage = 1;    // Reference to the next odd page opening
var dragTimer;         // setInterval that animates page fold available
var posX = 880;       // initial value to animate page Fold
var dragX = 0;        // Keeps x1 to be used by stopDrag();
var holdX = 0;
var action = "";      // What action the idle should execute
var finished = false; // If turning the page all the way, change page number
var back = false;     // true = flip backwards
var trackMovement;

function startDrag (sender, eventArgs)
{
    trackMovement=true;
    //upPlay();
}

var currX1 = 880;
var nextOddPage = 1;

var pageAnimationType = "none"; // type of animation being performed on the page
var pageAnimationDelta = 0;     // per frame delta for the animation
var pageAnimationTarget = 0;

function beginPageAnimation(type)
{
  if (type == "showFold")
  {
    if (nextOddPage < maxNumPages)
    {
      pageAnimationType = "showFold";
      pageAnimationTarget = 840;
      pageAnimationDelta = 5;
      wpfe.findName("timerStoryboard").begin();
    }
  }
  if (type == "hideFold")
  {
    pageAnimationType = "hideFold";
    pageAnimationTarget = 880;
    pageAnimationDelta = Math.abs((currX1 - pageAnimationTarget));
    wpfe.findName("timerStoryboard").begin();
  }
  else if (type == "finishTurn")
  {
    if (nextOddPage < maxNumPages)
    {
      pageAnimationType = "finishTurn";
      pageAnimationDelta = 10;
      pageAnimationTarget = 460;
      wpfe.findName("timerStoryboard").begin();
    }
  }
  else if (type == "none")
  {
    pageAnimationType = "none";
    pageAnimationDelta = 0;
    pageAnimationTarget = 0;
  }
}

// method that ensures animations maintain correct state once they complete
function onAnimationComplete(type)
{
  if (type == "showFold")
  {
    pageAnimationType = "none";
  }
  else if (type == "hideFold")
  {
    currX1 = 460;
    nextOddPage -= 2;
    pageAnimationType = "none";
  }
  else if (type == "finishTurn")
  {
    currX1 = 880;
    nextOddPage += 2;
    pageAnimationType = "none";
    beginPageAnimation("showFold");
  }
}

function onTimerStoryboardCompleted(sender, eventArgs)
{
  // if we're animating
  if (pageAnimationType != "none")
  {
    // if we are done with the current animation
    if (currX1 - pageAnimationTarget == 0)
    {
      onAnimationComplete(pageAnimationType);
    }
    // if we are not done with the current animation
    else
    {
      // if we are within a delta of the target, draw one last frame with the final value
      if (Math.abs(currX1 - pageAnimationTarget) < pageAnimationDelta) { currX1 = pageAnimationTarget; }
      else if (currX1 < pageAnimationTarget) { currX1 += pageAnimationDelta; }
      else { currX1 -= pageAnimationDelta; }
      wpfe.findName("timerStoryboard").begin();
    }

    // update the scene if possible
    if (nextOddPage < maxNumPages)
      updateScene(nextOddPage, currX1 - 460);
  }
}

var previousMouseMovePosition = 0;
function oddPageMouseDown(sender, eventArgs)
{
  sender.captureMouse();
  previousMouseMovePosition = eventArgs.x + document.body.scrollLeft;
  trackMovement = true;

  // if user clicked on a page that has fully turned
  if ("page0"+getTwoDigitInt(nextOddPage) != sender.name)
  {
    if (nextOddPage < maxNumPages)
    {
      beginPageAnimation("hideFold");
    }
    else
    {
      onAnimationComplete("hideFold");
    }
  }
  else
  {
    pageAnimationType = "none";
  }
}

function oddPageMouseUp(sender, eventArgs)
{
  sender.releaseMouseCapture();
  trackMovement=false;

  // if we are far enough to the left, finish the turn
  if (currX1 < 600)
  {
    beginPageAnimation("finishTurn");
  }
  // otherwise, go back to the folded position
  else
  {
    beginPageAnimation("showFold");
  }
}

function oddPageMouseMove(sender, eventArgs)
{
  // if we have an animation pending, don't animate
  if ((trackMovement) && (pageAnimationType == "none"))
  {
    var _currDelta = ((eventArgs.x + document.body.scrollLeft) - previousMouseMovePosition)*1.05;
    previousMouseMovePosition = eventArgs.x + document.body.scrollLeft;
    currX1 = Math.min(880, Math.max(460, currX1 + _currDelta));
    updateScene(nextOddPage, currX1 - 460);
  }
  // if we are tracking movement but in the middle of an animation, update mouse position
  else if (trackMovement)
  {
    previousMouseMovePosition = eventArgs.x + document.body.scrollLeft;
  }
}

function jumpToPage(newOddPage)
{
  // cancel all animations
  beginPageAnimation("none");

  // goal is nextOddPage == newOddPage + 2
  if (nextOddPage == newOddPage + 2)
    return;

  // if we need to go backwards
  if (nextOddPage > newOddPage + 2)
  {
    if (nextOddPage > maxNumPages)
      nextOddPage -= 2;

    while ((nextOddPage > newOddPage + 2) && (nextOddPage >= 1))
    {
      currX1 = 880;
      updateScene(nextOddPage, currX1 - 460);
      nextOddPage -= 2;
    }

    // if our goal is a valid page
    if (nextOddPage >= 1)
    {
      currX1 = 880;
      updateScene(nextOddPage, currX1 - 460);
    }
    else
    {
      nextOddPage = 1;
    }
    beginPageAnimation("showFold");
  }
  // if we need to go forward
  else
  {
    while ((nextOddPage < newOddPage + 2) && (nextOddPage < maxNumPages))
    {
      currX1 = 460;
      updateScene(nextOddPage, currX1 - 460);
      nextOddPage += 2;
    }

    // if our goal is a valid page
    if (nextOddPage < maxNumPages)
    {
      currX1 = 880;
      updateScene(nextOddPage, currX1 - 460);
      beginPageAnimation("showFold");
    }
  }
}

