///////////////////////////////////////////////////////////////////////////////
//
//  navigationManager.js
//
// 
// © 2007 Microsoft Corporation. All Rights Reserved.
//
// This file is licensed as part of the Silverlight 1.0 SDK, for details look here: http://go.microsoft.com/fwlink/?LinkID=89144&clcid=0x409
//
///////////////////////////////////////////////////////////////////////////////

// Controls the navigation between pages, and keeps up-to-date state
NavigationManager = function(plugIn, maxNumPages, bookDetails) {
    this.plugIn = plugIn;
    this.maxNumPages = maxNumPages;
    this.bookDetails = bookDetails;
    
    this.timer = this.plugIn.content.findname("timerStoryboard");
    this.timer.addEventListener("completed", Silverlight.createDelegate(this, this.onTick));
    
    this.pageTimer = this.plugIn.content.findname("pageTurnCheckStoryboard");
    
    this.draggingValue = 0;
    this.spreadNumber;
    
    // animation variables
    this.pageAnimationType = "none";
    this.pageAnimationDelta = 0;
    this.pageAnimationTarget = 0;
    
    // whether to track movement, and previous position if so
    this.trackMovement = false;
    this.previousMouseMovePosition = 0;
    
    this.currX1 = 880;   
    
    this.nextOddPage = 1;
}

NavigationManager.prototype.beginPageAnimation = function(type)
{
  if (type == "showFold")
  {
    if (this.nextOddPage < this.maxNumPages)
    {
      this.pageAnimationType = "showFold";
      this.pageAnimationTarget = 880;
      this.pageAnimationDelta = 5;
      this.timer.begin();
    }
  }
  if (type == "hideFold")
  {
    this.pageAnimationType = "hideFold";
    this.pageAnimationTarget = 880;
    this.pageAnimationDelta = Math.abs((this.currX1 - this.pageAnimationTarget));
    this.timer.begin();
  }
  else if (type == "finishTurn")
  {
    if (this.nextOddPage < this.maxNumPages)
    {
      this.pageAnimationType = "finishTurn";
      this.pageAnimationDelta = 10;
      this.pageAnimationTarget = 460;
      this.timer.begin();
    }
  }
  else if (type == "none")
  {
    this.pageAnimationType = "none";
    this.pageAnimationDelta = 0;
    this.pageAnimationTarget = 0;
  }
}

// method that ensures animations maintain correct state once they complete
NavigationManager.prototype.onAnimationComplete = function(type)
{
  if (type == "showFold")
  {
    this.pageAnimationType = "none";
    this.enablePageTurn();
  }
  else if (type == "hideFold")
  {
    this.currX1 = 460;
    this.nextOddPage -= 2;
    this.pageAnimationType = "none";
    this.spreadNumber = ((this.nextOddPage - 1) / 2);
    this.pageTimer.Begin();
    this.enablePageTurn();
  }
  else if (type == "finishTurn")
  {
    this.currX1 = 880;
    this.nextOddPage += 2;
    this.pageAnimationType = "none";
    //this.beginPageAnimation("showFold");
    this.spreadNumber = ((this.nextOddPage - 1) / 2);
    this.pageTimer.Begin();
    this.enablePageTurn();
  }
}

NavigationManager.prototype.onTick = function(sender, eventArgs)
{
  // if we're animating
  if (this.pageAnimationType != "none")
  {
    // if we are done with the current animation
    if (this.currX1 - this.pageAnimationTarget == 0)
    {
      this.onAnimationComplete(this.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(this.currX1 - this.pageAnimationTarget) < this.pageAnimationDelta) { this.currX1 = this.pageAnimationTarget; }
      else if (this.currX1 < this.pageAnimationTarget) { this.currX1 += this.pageAnimationDelta; }
      else { this.currX1 -= this.pageAnimationDelta; }
      this.timer.begin();
    }

    // update the scene if possible
    if (this.nextOddPage < this.maxNumPages)
    {
      this.updateScene(this.nextOddPage, this.currX1 - 460, false);
    }
  }
}

NavigationManager.prototype.oddPageMouseDown = function(sender, eventArgs)
{ 
    if((this.nextOddPage) == 3 && this.bookDetails.backCoverImage == true)
        {
            this.updatePoints(false, this.nextOddPage - 2);
            this.plugIn.content.findName("closeBookTimeline").begin();
            this.nextOddPage = 1;
            this.spreadNumber = 0;
            
            this.disablePageTurn();
        }
        else
        {
            sender.captureMouse();
            this.trackMovement = true;
            switch(this.draggingValue)
                {
                case 0:
                    var _currDelta = ((eventArgs.getPosition(null).x + document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).x + document.body.scrollLeft;
                break
                case 90:
                    var _currDelta = ((eventArgs.getPosition(null).y + document.body.scrollTop) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).y + document.body.scrollTop;
                break
                case 180:
                    var _currDelta = ((eventArgs.getPosition(null).x - document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).X - document.body.scrollLeft;
                break
                case 270:
                    var _currDelta = ((eventArgs.getPosition(null).y + document.body.scrollTop) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).y + document.body.scrollTop;
                break
                default:
                    var _currDelta = ((eventArgs.getPosition(null).x + document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).x + document.body.scrollLeft;
                }
            // if user clicked on a page that has fully turned
            if ("page0"+getTwoDigitInt(this.nextOddPage) != sender.name)
            {
                if (this.nextOddPage < this.maxNumPages)
                {
                    this.beginPageAnimation("hideFold");
                }
                else
                {
                    this.onAnimationComplete("hideFold");
                }
            }
            else
            {
                this.pageAnimationType = "none";
            }
        }
}   

NavigationManager.prototype.oddPageMouseUp = function(sender, eventArgs)
{
if((this.nextOddPage) == 1 && this.bookDetails.backCoverImage == true)
        {
        }
        else
        {
          sender.releaseMouseCapture();
          this.trackMovement = false;

          // if we are far enough to the left, finish the turn
          if (this.currX1 < 600)
          {
            this.beginPageAnimation("finishTurn");
            this.disablePageTurn();
          }
          // otherwise, go back to the folded position
          else
          {
            this.beginPageAnimation("showFold");
            this.disablePageTurn();
          }
         } 
}
  
  

NavigationManager.prototype.oddPageMouseMove = function(sender, eventArgs)
{     
      // if we have an animation pending, don't animate
      if ((this.trackMovement) && (this.pageAnimationType == "none"))
      {
        switch(this.draggingValue)
                {
                case 0:
                    var _currDelta = ((eventArgs.getPosition(null).x + document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).x + document.body.scrollLeft;
                break
                case 90:
                    var _currDelta = ((eventArgs.getPosition(null).y + document.body.scrollTop) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).y + document.body.scrollTop;
                break
                case 180:
                    var _currDelta = ((eventArgs.getPosition(null).x - document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).X - document.body.scrollLeft;
                break
                case 270:
                    var _currDelta = ((eventArgs.getPosition(null).y + document.body.scrollTop) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).y + document.body.scrollTop;
                break
                default:
                    var _currDelta = ((eventArgs.getPosition(null).x + document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).x + document.body.scrollLeft;
                }
        
        if(this.bookDetails.rightToLeft == false)
        {
            if(this.draggingValue == 180 || this.draggingValue == 270)
            {
                this.currX1 = Math.min(880, Math.max(460, this.currX1 - _currDelta));
            }
            else
            {
                this.currX1 = Math.min(880, Math.max(460, this.currX1 + _currDelta));
            }
        }
        else
        {
            this.currX1 = Math.min(880, Math.max(460, this.currX1 - _currDelta));
        }
        this.updateScene(this.nextOddPage, this.currX1 - 460, false);
      }
      // if we are tracking movement but in the middle of an animation, update mouse position
      else if (this.trackMovement)
      {
        switch(this.draggingValue)
                {
                case 0:
                    var _currDelta = ((eventArgs.getPosition(null).x + document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).x + document.body.scrollLeft;
                break
                case 90:
                    var _currDelta = ((eventArgs.getPosition(null).y + document.body.scrollTop) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).y + document.body.scrollTop;
                break
                case 180:
                    var _currDelta = ((eventArgs.getPosition(null).x - document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).X - document.body.scrollLeft;
                break
                case 270:
                    var _currDelta = ((eventArgs.getPosition(null).y + document.body.scrollTop) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).y + document.body.scrollTop;
                break
                default:
                    var _currDelta = ((eventArgs.getPosition(null).x + document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).x + document.body.scrollLeft;
                }
      }
}

NavigationManager.prototype.jumpToPage = function(newOddPage)
{
  // cancel all animations
  this.beginPageAnimation("none");

  // goal is this.nextOddPage == newOddPage + 2
  if (this.nextOddPage == newOddPage + 2)
    return;

  // if we need to go backwards
  if (this.nextOddPage > newOddPage + 2)
  {
    if (this.nextOddPage > this.maxNumPages)
      this.nextOddPage -= 2;
    if(newOddPage == ((this.maxNumPages * 1) - 3))
        this.nextOddPage = newOddPage + 2;
    else if(newOddPage == ((this.maxNumPages * 1) - 5))
        this.nextOddPage = newOddPage + 4;
    else
        this.nextOddPage = newOddPage + 6;
        
    while ((this.nextOddPage > newOddPage + 2) && (this.nextOddPage >= 1))
    {
      this.currX1 = 880;
      if(newOddPage == -1)
        this.updateScene(this.nextOddPage, this.currX1 - 460, true);
      else
        this.updateScene(this.nextOddPage, this.currX1 - 460, false);
      this.nextOddPage -= 2;
    }
    // if our goal is a valid page
    if (this.nextOddPage >= 1)
    {
      this.currX1 = 880;
      if(newOddPage == -1)
        this.updateScene(this.nextOddPage, this.currX1 - 460, true);
      else
        this.updateScene(this.nextOddPage, this.currX1 - 460, false);
    }
    else
    {
      this.nextOddPage = 1;
    }
    this.beginPageAnimation("showFold");
  }
  // if we need to go forward
  else
  {
      if(newOddPage == 1)
        this.nextOddPage = 1
      else
        this.nextOddPage = newOddPage - 2;
        
        while ((this.nextOddPage < newOddPage + 2) && (this.nextOddPage < this.maxNumPages))
        {
          this.currX1 = 460;
          //we do this check because if ifc is not in scene while update scene on page 3 occurs we hit a problem.
          //which obviously occurs in this one instance
          if(newOddPage == 5)
            this.updateScene(this.nextOddPage, this.currX1 - 460, true);
          else
            this.updateScene(this.nextOddPage, this.currX1 - 460, false);
          this.nextOddPage += 2;
        }

        // if our goal is a valid page
        if (this.nextOddPage < this.maxNumPages)
        {
          this.currX1 = 880;
          this.updateScene(this.nextOddPage, this.currX1 - 460, false);
          this.beginPageAnimation("showFold");
        }
  }
}

// @oddPageNumber: number of the odd page that is currently the next
// @x1: point where the bottom edges of the odd and even pages intersect, in even page coordinates.  Ranges from 0 to 420.
NavigationManager.prototype.updateScene = function(oddPageNumber, x1, skipFC) {
    // variables related to odd page
    var oddPoint1 = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "Point1");
    var oddPoint2 = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "Point2");
    var oddPoint3 = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "Point3");
    var oddRotate = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "Rotate");
    var oddTranslate = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "Translate");
    var foldShadow = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "FoldShadow");
    var foldShadowRotate = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "FoldShadowRotate");
    var foldShadowTranslate = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "FoldShadowTranslate");
    var shadowBehindPage01 = this.plugIn.content.findName("shadowBehindPage01");

    // variables related to the even page
    var evenPoint1 = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber - 1) + "Point1");
    var evenPoint2 = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber - 1) + "Point2");
    var evenPoint3 = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber - 1) + "Point3");
    var shadowOnEvenPage = this.plugIn.content.findName("shadowOnEvenPage");
    if (oddPageNumber == 3 && this.bookDetails.backCoverImage == true && skipFC == false) {
        this.plugIn.content.findName("ifc")["Width"] = "420";
        this.plugIn.content.findName("fc")["Width"] = "0";
        this.plugIn.content.findName("ifc")["Canvas.Left"] = "0";
        this.plugIn.content.findName("ifcTranslate")["X"] = "0";
        this.plugIn.content.findName("ifcSkew")["AngleY"] = "0";
    }
    if (oddPageNumber == 1 && this.bookDetails.backCoverImage == true && skipFC == false) {
        this.plugIn.content.findName("ifc")["Width"] = "0";
        this.plugIn.content.findName("fc")["Width"] = "420";
        this.plugIn.content.findName("ifc")["Canvas.Left"] = "420";
        this.plugIn.content.findName("fcTranslate")["y"] = "0";
        this.plugIn.content.findName("fcSkew")["AngleY"] = "0";
    }
    // _alpha: angle between horizontal axis and bottom edge of odd page.
    //         this can be any function of x1.
    var _alpha = 90 / 420 * x1;

    // update point 1 of even page
    evenPoint1.point = x1 + ",570";
    var shadowStr = x1 + ",570 ";
    shadowStr += Math.min((x1 + 30), 420) + ",570 ";

    // _leftEdgeAngle: angle between horizontal axis and left edge of odd page
    var _leftEdgeAngle = 90 - _alpha;
    var _bottomLeftCornerX = x1 - Math.cos(_alpha * Math.PI / 180) * (420 - x1);
    var _bottomLeftCornerY = Math.sin(_alpha * Math.PI / 180) * (420 - x1);

    // update odd page's rotate and translate transform
    oddRotate.angle = _alpha;
    oddTranslate.x = 420 + _bottomLeftCornerX;
    oddTranslate.y = (-1) * _bottomLeftCornerY;

    // how much of the odd page's left edge can be seen
    var _visibleLeftEdgeHeight = (420 - _bottomLeftCornerX) / Math.cos(_leftEdgeAngle * Math.PI / 180);

    // if the top left corner can be seen
    if (_visibleLeftEdgeHeight >= 570) {
        // height between top left corner of the odd page and top edge of the even page
        var _topLeftCornerY = _bottomLeftCornerY + Math.sin(_leftEdgeAngle * Math.PI / 180) * 570;

        // _visibleTopEdgeWidth: width of top edge of odd page that is visible
        var _visibleTopEdgeWidth;
        // _x2: x coordinate of the point where top edges of odd and even pages intersect
        var _x2;

        if (_topLeftCornerY > 570) {
            _visibleTopEdgeWidth = (_topLeftCornerY - 570) / Math.sin(_alpha * Math.PI / 180);
            _x2 = _bottomLeftCornerX + Math.cos(_leftEdgeAngle * Math.PI / 180) * 570 + (_topLeftCornerY - 570) / Math.tan(_alpha * Math.PI / 180);
        }
        else {
            _visibleTopEdgeWidth = 420;
            _x2 = 0;
        }

        oddPoint1.point = "0, 0";
        oddPoint2.point = _visibleTopEdgeWidth + ", 0";

        // update foldShadow properties
        var _foldAngle = 90 - (180 / Math.PI) * Math.atan(570 / (420 - x1 - _visibleTopEdgeWidth));
        foldShadowRotate.angle = (-1) * _foldAngle;

        // need to adjust the position of the foldShadow so that it covers the entire folded edge
        var _foldShadowAdjustment = 40;
        foldShadowTranslate.x = _visibleTopEdgeWidth - 20 * Math.cos(_foldAngle * Math.PI / 180) - _foldShadowAdjustment * Math.cos((90 - _foldAngle) * Math.PI / 180);
        foldShadowTranslate.y = 20 * Math.sin(_foldAngle * Math.PI / 180) - _foldShadowAdjustment * Math.sin((90 - _foldAngle) * Math.PI / 180); ;

        evenPoint2.point = _x2 + ",0";
        evenPoint3.point = _x2 + ",0";

        shadowStr += Math.min((_x2 + 10), 420) + ",0 " + _x2 + ",0";
    }
    // if the top left corner cannot be seen
    else {
        // update points of the odd page, which are both the same
        oddPoint1.point = "0, " + (570 - _visibleLeftEdgeHeight);
        oddPoint2.point = "0, " + (570 - _visibleLeftEdgeHeight);

        // update points of the even page
        var _y2 = 570 - _bottomLeftCornerY - (420 - _bottomLeftCornerX) * Math.tan(_leftEdgeAngle * Math.PI / 180);
        evenPoint2.point = "420," + _y2;
        evenPoint3.point = "420,0";

        // update shadow string on top of the even page
        shadowStr += "420," + _y2 + " 420," + _y2;

        // update foldShadow properties
        var _foldAngle;
        if (x1 != 420) {
            _foldAngle = 90 - (180 / Math.PI) * Math.atan(_visibleLeftEdgeHeight / (420 - x1));
        }
        else {
            //the arctangent of infinity is 90
            _foldAngle = 0;
        }

        foldShadowRotate.angle = (-1) * _foldAngle;
        foldShadowTranslate.x = (-1) * 20 * Math.cos(_foldAngle * Math.PI / 180);
        foldShadowTranslate.y = (570 - _visibleLeftEdgeHeight) + 20 * Math.sin(_foldAngle * Math.PI / 180);
    }

    oddPoint3.point = (420 - x1) + ", 570";
    shadowOnEvenPage.points = shadowStr;

    if (x1 < 15) {
        shadowOnEvenPage.opacity = 0.25 * (x1 / 15);
        //foldShadow.opacity = 0.6*(x1/15);
        if (oddPageNumber > 1) { shadowBehindPage01.opacity = 0.8; }
        else { shadowBehindPage01.opacity = 0.8 - 0.8 * (x1 / 15); }
    }
    else {
        shadowOnEvenPage.opacity = 0.25;
        //foldShadow.opacity = 0.6;
        if (oddPageNumber > 1) { shadowBehindPage01.opacity = 0.8; }
        else { shadowBehindPage01.opacity = 0; }
    }
    /*oddRotate.CenterY = "0";
    oddRotate.CenterX = "0";
    oddRotate.angle = "0";
    oddPoint1.point = "420, 570";
    oddPoint2.point = "420, 0"
    oddTranslate.X = "0";
    oddPoint3.point = "0, -1050"
    evenPoint1.point = "420, 570";
    evenPoint2.point = "420, 570";
    evenPoint3.point = "420, -1050";*/
    //alert(oddPageNumber);

    /*document.getElementById("debugBox").value = //document.getElementById("debugBox").value + 
    "\n"+"x1:" + x1 + 
    "\n"+"oddpageNumber:" + oddPageNumber + 
    "\n" + "evenpoint1: " + x1 + ",570" + 
    "\n" + "evenpoint2: " + "420," + _y2 + 
    "\n" + "evenpoint3: " + "420," + "0" + 
    "\n" + "oddpoint1: " + "0, 0" +
    "\n" + "oddpoint2: " + _visibleTopEdgeWidth + ", 0" +
    "\n" + "oddpoint3: " + (420 - x1) + ", 570" + 
    "\n" + "oddRotate: " + _alpha +
    "\n" + "oddtranslatex" + (420 + _bottomLeftCornerX) +
    "\n" + "oddtranslatey" + ((-1) * _bottomLeftCornerY) 
    ; */
}

NavigationManager.prototype.updatePoints = function(cover, oddPageNumber)
{ //document.getElementById("searchBox").value =(oddPageNumber);
    var oddPoint1 = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "Point1");
  var oddPoint2 = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "Point2");
  var oddPoint3 = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "Point3");
  var oddRotate = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "Rotate");
  var oddTranslate = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "Translate");
  var foldShadow = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "FoldShadow");
  var foldShadowRotate = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "FoldShadowRotate");
  var foldShadowTranslate = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber) + "FoldShadowTranslate");
  var shadowBehindPage01 = this.plugIn.content.findName("shadowBehindPage01");

  // variables related to the even page
  var evenPoint1 = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber-1) + "Point1");
  var evenPoint2 = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber-1) + "Point2");
  var evenPoint3 = this.plugIn.content.findName("page" + getTwoDigitInt(oddPageNumber-1) + "Point3");
  var shadowOnEvenPage = this.plugIn.content.findName("shadowOnEvenPage");

    if(cover == true)
    {
    oddRotate.CenterY = "0";
    oddRotate.CenterX = "0";
    oddRotate.angle = "0";
    oddPoint1.point = "420, 570";
    oddPoint2.point = "420, 0"
    oddTranslate.X = "0";
    oddPoint3.point = "0, -1050"
    evenPoint1.point = "420, 570";
    evenPoint2.point = "420, 570";
    evenPoint3.point = "420, -1050";
    }
    else
    {
    oddRotate.CenterY = "0";
    oddRotate.CenterX = "0";
    oddRotate.angle = "0";
    oddPoint1.point = "420, 570";
    oddPoint2.point = "420, 0"
    oddTranslate.X = "0";
    oddPoint3.point = "0, -1050"
    evenPoint1.point = "420, 570";
    evenPoint2.point = "420, 570";
    evenPoint3.point = "420, -1050";
    }
}

NavigationManager.prototype.evenPageMouseDown = function(sender, eventArgs)
{   //document.getElementById("searchBox").value = ("spread: " + this.spreadNumber);
    if((this.nextOddPage - 1) == 0 && this.bookDetails.backCoverImage == true)
    {
        //alert(this.bookDetails.backCoverImage == true);
         this.updatePoints(true, this.nextOddPage);
        this.plugIn.content.findName("openBookTimeline").begin();
        this.nextOddPage = 3;
        this.spreadNumber = ((this.nextOddPage - 1) / 2);
        
        this.disablePageTurn();
    }
    else
    {
        if (this.nextOddPage < this.maxNumPages)
        {//document.getElementById("searchBox").value =(this.nextOddPage);
            //alert("next odd page : " + this.nextOddPage + " sender name: " + sender.name);
            sender.captureMouse();
            this.trackMovement = true;
                switch(this.draggingValue)
                {
                case 0:
                    var _currDelta = ((eventArgs.getPosition(null).x + document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).x + document.body.scrollLeft;
                break
                case 90:
                    var _currDelta = ((eventArgs.getPosition(null).y + document.body.scrollTop) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).y + document.body.scrollTop;
                break
                case 180:
                    var _currDelta = ((eventArgs.getPosition(null).x - document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).X - document.body.scrollLeft;
                break
                case 270:
                    var _currDelta = ((eventArgs.getPosition(null).y + document.body.scrollTop) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).y + document.body.scrollTop;
                break
                default:
                    var _currDelta = ((eventArgs.getPosition(null).x + document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).x + document.body.scrollLeft;
                }
            // if user clicked on a page that has fully turned
            if ("page0"+getTwoDigitInt(this.nextOddPage -1) != sender.name)
            {
                if (this.nextOddPage < this.maxNumPages)
                {
                    this.beginPageAnimation("hideFold");
                }
                else
                {
                    this.onAnimationComplete("hideFold");
                }
            }
            else
            {
                this.pageAnimationType = "none";
            }
        }
        else
        {
            alert("Last page reached, can't turn forward");
            this.enablePageTurn();
            sender.releaseMouseCapture();
        }
    }
}   

NavigationManager.prototype.evenPageMouseUp = function(sender, eventArgs)
{
  sender.releaseMouseCapture();
  this.trackMovement = false;

  // if we are far enough to the left, finish the turn
  if (this.currX1 < 600)
  {
    this.beginPageAnimation("finishTurn");
    this.disablePageTurn();
  }
  // otherwise, go back to the folded position
  else
  {
    this.beginPageAnimation("showFold");
    this.disablePageTurn();
  }
}

NavigationManager.prototype.evenPageMouseMove = function(sender, eventArgs)
{
  // if we have an animation pending, don't animate
  if ((this.trackMovement) && (this.pageAnimationType == "none"))
  {
    switch(this.draggingValue)
                {
                case 0:
                    var _currDelta = ((eventArgs.getPosition(null).x + document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).x + document.body.scrollLeft;
                break
                case 90:
                    var _currDelta = ((eventArgs.getPosition(null).y + document.body.scrollTop) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).y + document.body.scrollTop;
                break
                case 180:
                    var _currDelta = ((eventArgs.getPosition(null).x - document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).X - document.body.scrollLeft;
                break
                case 270:
                    var _currDelta = ((eventArgs.getPosition(null).y + document.body.scrollTop) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).y + document.body.scrollTop;
                break
                default:
                    var _currDelta = ((eventArgs.getPosition(null).x + document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).x + document.body.scrollLeft;
                }
    if(this.bookDetails.rightToLeft == false)
    {
        if(this.draggingValue == 180 || this.draggingValue == 270)
            {
                this.currX1 = Math.min(880, Math.max(460, this.currX1 - _currDelta));
            }
            else
            {
                this.currX1 = Math.min(880, Math.max(460, this.currX1 + _currDelta));
            }
    }
    else
        this.currX1 = Math.min(880, Math.max(460, this.currX1 - _currDelta));
    this.updateScene(this.nextOddPage, this.currX1 - 460, false);
  }
  // if we are tracking movement but in the middle of an animation, update mouse position
  else if (this.trackMovement)
  {
    switch(this.draggingValue)
                {
                case 0:
                    var _currDelta = ((eventArgs.getPosition(null).x + document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).x + document.body.scrollLeft;
                break
                case 90:
                    var _currDelta = ((eventArgs.getPosition(null).y + document.body.scrollTop) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).y + document.body.scrollTop;
                break
                case 180:
                    var _currDelta = ((eventArgs.getPosition(null).x - document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).X - document.body.scrollLeft;
                break
                case 270:
                    var _currDelta = ((eventArgs.getPosition(null).y + document.body.scrollTop) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).y + document.body.scrollTop;
                break
                default:
                    var _currDelta = ((eventArgs.getPosition(null).x + document.body.scrollLeft) - this.previousMouseMovePosition)*1.05;
                    this.previousMouseMovePosition = eventArgs.getPosition(null).x + document.body.scrollLeft;
                }
  }
}

NavigationManager.prototype.enablePageTurn = function()
{
    this.plugIn.content.findName("oddPageCanvas")["isHitTestVisible"] = true;
    this.plugIn.content.findName("evenPageCanvas")["isHitTestVisible"] = true;
}

NavigationManager.prototype.disablePageTurn = function()
{
    this.plugIn.content.findName("oddPageCanvas")["isHitTestVisible"] = false;
    this.plugIn.content.findName("evenPageCanvas")["isHitTestVisible"] = false;
}

NavigationManager.prototype.calulatePageDraggingProperties = function(rotation, ticker)
{
    var value;
    var InitialPosition;
    InitialPosition = (((rotation / 90) - (ticker)) * (90));
    //alert(InitialPosition + " " + rotation + " " + ticker + " " + ((ticker % 4)));
    switch(InitialPosition)
    {
        case (0):
            this.draggingValue = ((ticker % 4) * 90);
        break
        case (90):
            this.draggingValue = (((ticker % 4) + 1) * 90);
        break
        case (180):
            this.draggingValue = (((ticker % 4) + 2) * 90);
        break
        case (270):
            this.draggingValue = (((ticker % 4) + 3) * 90);
        break
        default:
            this.draggingValue = ((ticker % 4) * 90);
    }
}


