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

var iNextPage = 1;    // Reference to the next 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 trackMovement;

function startDrag (sender, eventArgs)
{
  trackMovement=true;
}
function startDragBack (sender, eventArgs)
{
    iNextPage -= 2;
    action = "";
    posX = 880;
    hideFold();
    trackMovement=true;
}

function stopDrag (sender, eventArgs)
{
    trackMovement=false;
    posX = dragX;
    if ((dragX-880) < -280)
    {
      action = "animFinish";
      window.clearInterval(dragTimer);
      dragTimer = window.setInterval(getMouseXY, 5);
    }
    else
    {
      showDrag();
      action = "animBack";
      window.clearInterval(dragTimer);
      dragTimer = window.setInterval(getMouseXY, 5);
    }
    trackMovement=true;
}

function showFold ()
{
  if (iNextPage < maxNumPages)
  {
    showDrag();
    if (iNextPage > 1)
    {
      wpf.findName("pageBack").opacity = "0.01";
    }
    else
    {
      wpf.findName("pageBack").opacity = "0";
    }
    posX = 880;
    action = "anim";
    window.clearInterval(dragTimer);
    dragTimer = window.setInterval(getMouseXY, 5);
    trackMovement=true;
  }
  else
  {
    hideDrag();
  }
}

function hideFold ()
{
  if ((iNextPage+2) < maxNumPages)
  {
    var pageMask = wpf.findName("page"+(iNextPage+2));
    pageMask.Points = "0,0 0,0 0,0 0,0";
    var pageMask = wpf.findName("page"+(iNextPage+3));
    pageMask.Points = "0,0 0,0 0,0 0,0";
    var e = wpf.findName("shadowBehind");
    e.Points = "0,0 0,0 0,0 0,0";
    var e = wpf.findName("shadow_fold");
    e.Points = "0,0 0,0 0,0 0,0";
  }
}

function hideDrag()
{
  wpf.findName("pageCorner").opacity = 0;
}

function showDrag()
{
  wpf.findName("pageCorner").opacity = 0.01;
}

function getMouseXY(sender, args)
{
  if (trackMovement)
  {
    var x1 = 0;
    if (action == "anim")
    {
      posX -= 5;
      x1 = posX;
      if (posX <= 840) 
      {
        action = "";
        window.clearInterval(dragTimer);
        trackMovement = false;
      }
      else
      {
        window.clearInterval(dragTimer);
        dragTimer = window.setInterval(getMouseXY, 5);
      }
    }
    else if (action == "animFinish") 
    {
      posX -= 30;
      x1 = posX;
      if (x1 <= 460) 
      {
        finished = true;
        x1 = 460;
        posX = x1;
        action = "";
        window.clearInterval(dragTimer);
        trackMovement=false;
      }
    }
    else if (action == "animBack") 
    {
      posX += 30;
      x1 = posX;
      if (x1 >= 840)
      {
        x1 = 840;
        posX = x1;
        action = "";
        window.clearInterval(dragTimer);
        trackMovement=false;
      }
    }
    else
    {
      var tempX = 0
      offsetX = ((document.body.clientWidth-900)/2);
      tempX = args.X + document.body.scrollLeft;
      if (tempX < 0) { tempX = 0; }
      if (tempX < 460) tempX = 460;
      if (tempX > 880) tempX = 880;
      x1 = tempX;
    }
    holdX = x1;
    updatePage();
  }
}


function updatePage()
{
  if (iNextPage>maxNumPages)
    return true;
    
  var strLog = "";
    
  var x1 = holdX;
  var e = wpf.findName("pageCorner");
  e["Canvas.Left"] = x1-880;
  
  var e = wpf.findName("line");
  var e2 = wpf.findName("cover");
  
  var page_width = e2.Width;
  var page_height = e2.Height + 45;
  var angle = ((45*(x1-880)/page_width)-45);
  e.X1 = x1;
  var x2 = x1+(Math.cos(angle*Math.PI/180)*page_height);
  e.X2 = x2;
  
  strLog += "angle=" +angle+ "width="+page_width+"x1=" + x1 + "; x2=" + x2 + "<br/>";
  
  var y1 = e2.Height + e2["Canvas.Top"];
  var angle2 = ((45*(x1-880)/page_width))-45;
  var y2 = y1+(Math.sin(angle*Math.PI/180)*(page_height));
  var y3 = (Math.tan(angle*Math.PI/180)*Math.abs(x1-880))+570;
  var y4 = (Math.tan(angle*Math.PI/180)*Math.abs(x1-880))+690;
  e.Y2 = y2;
  
  // ----------------------------------------
  // Mask of the Page 02
  var pageMask = wpf.findName("page"+(iNextPage+1));
  strLog += "MASK OF PAGE "+(iNextPage+1)+ ":";
  if (x2 <= 880)
  {
    var xCorr = Math.abs(((x1-(565/Math.tan(angle*Math.PI/180))))-880);
    pageMask.Points = "0,0 420,570 "+(x1-460)+",570 "+(420-xCorr)+",0 420,0 420,570";
    strLog += " Points1=" + "0,0 420,570 "+(x1-460)+",570 "+(420-xCorr)+",0 420,0 420,570" + "<br/>" ;
  }
  else 
  {
    if (y2-30 < 0)
    {
      pageMask.Points = "0,0 420,570 "+(x1-460)+",570 "+(x2-460)+",0 420,570";
      strLog += " Points2=" + "0,0 420,570 "+(x1-460)+",570 "+(x2-460)+",0 420,570" + "<br/>" ;
    }
    else
    {
      pageMask.Points = "0,0 420,570 "+(x1-460)+",570 "+(x2-460)+","+(y2-30)+" 420,570";
      strLog += " Points2=" + "0,0 420,570 "+(x1-460)+",570 "+(x2-460)+","+(y2-30)+" 420,570" + "<br/>" ;
    }
  }
  
  // ----------------------------------------
  // Rotates and moves Page 01
  var transPage01 = wpf.findName("translatePage"+iNextPage);
  transPage01.X = x1+((angle+45)*9.34);
  transPage01.Y = 30;
  var rotPage01 = wpf.findName("rotatePage"+iNextPage);
  rotPage01.Angle = angle*2-180;
  rotPage01.CenterX = ((angle+45)*-9.35);
  // Mask of Page 01
  var pageMask = wpf.findName("page"+iNextPage);
  strLog += "PAGE "+(iNextPage)+ ":";
  if ((x1-880) >= -222)
  {
    pageMask.Points = "0,0 420,0 0,0 0,570 "+Math.abs(x1-880)+",570 0,"+y3;
    strLog += " Points=" + "0,0 420,0 0,0 0,570 "+Math.abs(x1-880)+",570 0,"+y3 + "<br/>" ;
  }
  else
  {
    pageMask.Points = "0,0 420,0 0,0 0,570 "+Math.abs(x1-880)+",570 0,0 "+(880-(x1-(570/Math.tan(angle*Math.PI/180))))+",0 "+Math.abs(x1-880)+",570";
    strLog += " Points=" + "0,0 420,0 0,0 0,570 "+Math.abs(x1-880)+",570 0,0 "+(880-(x1-(570/Math.tan(angle*Math.PI/180))))+",0 "+Math.abs(x1-880)+",570" + "<br/>" ;
  }
    
  // ----------------------------------------
  // Redraw the shadow behind
  var pageMask = wpf.findName("shadowBehind");
  if ((y3+30) < 28)
  {
    if ((y3+30) > 10)
    {
      pageMask.Points = "880,30 "+(x1-(570/Math.tan(angle*Math.PI/180)))+",30 "+(x1)+",600 "+(x1+30)+",600";
    }
    else
    {
      pageMask.Points = (x1-(570/Math.tan(angle*Math.PI/180))+10)+",30 "+(x1-(570/Math.tan(angle*Math.PI/180)))+",30 "+(x1)+",600 "+(x1+30)+",600";
    }      
  }
  else
  {
    if ((y3+30) < 570) 
    {
      pageMask.Points = "880,"+(y3+30)+" "+(x1+30)+",600 "+(x1)+",600";
    }
    else
    {
      pageMask.Points = "880,"+(y3+30)+" 880,600 "+(x1)+",600";
    }
  }
  
  // ----------------------------------------
  // Redraw the shadow behind
  // Shadow Page Fold
  var shadFold = wpf.findName("translateShadowFold01");
  shadFold.X = x1-20;
  var shadFold = wpf.findName("rotateShadowFold01");
  shadFold.Angle = 90+angle;
  var shadFoldMask = wpf.findName("shadow_fold");
  var y5 = 20-Math.sin((Math.abs(angle*2)-90)*Math.PI/180)*20;
  if ((x1-880) >= -222)
  {
    var h = (880-x1)/Math.cos(angle*Math.PI/180);
  }
  else
  {
    var xh = Math.abs(570/Math.tan(angle*Math.PI/180));
    var h = xh/Math.cos(angle*Math.PI/180);
  }
  var y6 = 650-h;
  if ((x1-880) > -22)
  {
    shadFoldMask.Points = "0,0 0,0 0,0 0,0";
  }
  else
  {
    if ((x1-880) >= -222)
    {
      var h2 = Math.abs(20*(Math.tan(angle*Math.PI/180)));
      shadFoldMask.Points = "0,0 20,0 0,0 0,650 0,0 0,"+(y6+h2)+" 20,"+(y6)+" 20,650 0,"+(650-y5)+" 0,0";
    }
    else
    {
      if (angle <= -69.6)
      {
        var h2 = (((180+(angle*2)))/2)/3;
        shadFoldMask.Points = "0,0 20,0 0,0 0,650 0,0 0,"+(y6-h2)+" 20,"+(y6)+" 20,650 0,"+(650-y5)+" 0,0";
      }
      else
      {
        var h2 = (((180+(angle*2)))/2)/3;
        var d6 = 68-Math.abs(angle);
        var p = 100-Math.abs(d6*100/1.6);
        var x6 = (100*p/100);
        var x7 = (43*p/100);
        shadFoldMask.Points = "0,0 20,0 0,0 0,650 0,0 0,"+(y6-h2+x6)+" "+(x7)+","+(y6-(x7/5))+" 20,"+(y6)+" 20,650 0,"+(650-y5)+" 0,0";
      }
    }
  }
  
  var shadowPage01 = wpf.findName("shadowPage01");
  if ((x1-880) < -404)
  {
    var delta = 420 - Math.abs(x1-880);
    var p = delta*100/16;
    var opc = p*0.6/100;
    shadFoldMask.opacity = opc;
    var opc = p*0.25/100;
    pageMask.opacity = opc;
    var opc = (100-p*2)*1/100;
    if (iNextPage == 1)
    {
      if (opc >= 0 && opc <= 100) shadowPage01.opacity = opc;
    }
  }
  else
  {
    shadFoldMask.opacity = 0.6;
    pageMask.opacity = 0.25;
    if (iNextPage == 1) 
    {
      shadowPage01.opacity = 0;
      wpf.findName("pageBack").opacity = "0";
    }
  }
    
  dragX = x1;
    
  if (finished)
  { 
    iNextPage += 2;
    showFold();
    finished = false;
  }
    
  return true;
}

function jumpToPage (page)
{
    previous =iNextPage;
    iNextPage = page;
    if (iNextPage == -1) {
        // If COVER
        for (var i=0; i+2<maxNumPages; i+=2)
            {
            var evenPage = wpf.findName("page"+(i+2));
            evenPage.Points = "0,0 420,570 420,570 420,570 420,570";
            var oddPage = wpf.findName("rotatePage"+(i+1));
            oddPage.Angle = "90";
            var oddPage = wpf.findName("translatePage"+(i+1));
            oddPage.X = "880";
            var oddPage = wpf.findName("page"+(i+1));
            oddPage.Points = "0,0 0,0 0,0";
        }
        iNextPage = 1;
    } else if (previous <= iNextPage) {
        // Clicked on a page ahead of the current one
        // Hide all the EVEN pages above the new SELECTED page
        if (iNextPage>0)
            {
            for (var i=0; i<iNextPage; i+=2)
                {
                var evenPage = wpf.findName("page"+(i+2));
                evenPage.Points = "0,0 420,0 420,570 0,570 0,0";
            }
        }
        for (var i=iNextPage; i>0; i-=2)
            {
            var oddPage = wpf.findName("rotatePage"+i);
            oddPage.Angle = "0";
            var oddPage = wpf.findName("translatePage"+i);
            oddPage.X = 40;
            var oddPage = wpf.findName("page"+i);
            oddPage.Points = "0,0 420,0 420,570 0,570 0,0";
        }
        iNextPage += 2;
        showShadowLeft();
    } else {
        if (iNextPage>0)
            {
            for (var i=Math.min(previous, maxNumPages-1); i>iNextPage; i-=2)
                {
                var evenPage = wpf.findName("page"+(i+1));
                evenPage.Points = "0,0 0,0 0,0";
            }
        }
        for (var i=Math.min(previous, maxNumPages-1); i>iNextPage; i-=2)
            {
            var oddPage = wpf.findName("rotatePage"+i);
            oddPage.Angle, "90";
            var oddPage = wpf.findName("translatePage"+i);
            oddPage.X = "880";
            var oddPage = wpf.findName("page"+i);
            oddPage.Points = "0,0 0,0 0,0";
        }
        
        iNextPage += 2;
        showShadowLeft();
    }
    hideFold();
    if ((iNextPage) < maxNumPages) showFold();
    }

function showShadowLeft ()
{
    var e = wpf.findName("shadowPage01");
    e.opacity = "1";
}
































