///////////////////////////////////////////////////////////////////////////////
//
//  inkManager.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 state of the ink annotation system
InkManager = function(plugIn, navigationManager) {
    this.plugIn = plugIn;
    // need the navigation manager to obtain the number of the current ink canvas to draw on.
    this.navigationManager = navigationManager;
    
    this.mouseCaptureCanvas = plugIn.content.findname("mouseCaptureCanvas");
    
    this.inkingMode = false;
    this.newStroke = null;
    this.newStroke2 = null;
    this.inkThickness = 4;
    this.inkColor = "white";
    this.inkColorContrast = "black";
    
    // Register eventhandlers
    //target.addEventListener("mouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter));
    this.mouseCaptureCanvas.addEventListener("mouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));
    this.mouseCaptureCanvas.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
    this.mouseCaptureCanvas.addEventListener("mouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp));
    this.mouseCaptureCanvas.addEventListener("mouseMove", Silverlight.createDelegate(this, this.handleMouseMove));
}

InkManager.prototype.toggleInkMode = function()
{
    this.inkingMode = !this.inkingMode;
    if (this.inkingMode)
    {
        this.mouseCaptureCanvas.isHitTestVisible = true;
    }
    else
    {
        this.mouseCaptureCanvas.isHitTestVisible = false;
    }
}

InkManager.prototype.clearInk = function()
{
  if (this.navigationManager.nextOddPage > 1)
  {
    var inkPresenter1 = this.plugIn.content.findName("page" + getTwoDigitInt(this.navigationManager.nextOddPage-2) + "ip");
    inkPresenter1.Strokes.Clear();
  }

  var inkPresenter2 = this.plugIn.content.findName("page" + getTwoDigitInt(this.navigationManager.nextOddPage-1) + "ip");
  inkPresenter2.Strokes.Clear();
}

InkManager.prototype.handleMouseLeave = function(sender, args)
{
    this.newStroke = null;   // Stop inking the current stroke if the mouse/stylus leaves the control area
    this.newStroke2 = null;
}

InkManager.prototype.handleMouseDown = function(sender, args)
{
  if (this.inkingMode)
  {
    sender.CaptureMouse();
    var inkPresenter = this.plugIn.content.findName("page" + getTwoDigitInt(this.navigationManager.nextOddPage-2) + "ip");
    if (inkPresenter != null)
    { 
      this.newStroke = this.plugIn.content.createFromXAML("<Stroke/>");
      inkPresenter.Strokes.Add(this.newStroke);
      this.newStroke.DrawingAttributes.Width = this.inkThickness;
      this.newStroke.DrawingAttributes.Height = this.inkThickness;
      this.newStroke.DrawingAttributes.Color = this.inkColor;
      this.newStroke.DrawingAttributes.OutlineColor = this.inkColorContrast;
      this.newStroke.StylusPoints.AddStylusPoints(args.GetStylusPoints(inkPresenter));
    }

    // Inking Page 2 (copy to other page so you can ink crossing pages)
    var inkPresenter2 = this.plugIn.content.findName("page" + getTwoDigitInt(this.navigationManager.nextOddPage-1) + "ip");
    if (inkPresenter2 != null)
    {
      this.newStroke2 = this.plugIn.content.createFromXAML("<Stroke/>");
      inkPresenter2.Strokes.Add(this.newStroke2);
      this.newStroke2.DrawingAttributes.Width = this.inkThickness;
      this.newStroke2.DrawingAttributes.Height = this.inkThickness;
      this.newStroke2.DrawingAttributes.Color = this.inkColor;
      this.newStroke2.DrawingAttributes.OutlineColor = this.inkColorContrast;
      this.newStroke2.StylusPoints.AddStylusPoints(args.GetStylusPoints(inkPresenter2));
    }
  }
}

InkManager.prototype.handleMouseUp = function(sender, args)
{
    this.newStroke = null;
    this.newStroke2 = null;
}

InkManager.prototype.handleMouseMove = function(sender, args)
{
  if (this.inkingMode)
  {
    if (this.newStroke != null)
    {
      var inkPresenter = this.plugIn.content.findName("page" + getTwoDigitInt(this.navigationManager.nextOddPage-2) + "ip");
      this.newStroke.StylusPoints.AddStylusPoints(args.GetStylusPoints(inkPresenter));
    }
     
    // Inking Page 2
    if (this.newStroke2 != null)
    {
      var inkPresenter2 = this.plugIn.content.findName("page" + getTwoDigitInt(this.navigationManager.nextOddPage-1) + "ip");
      if (inkPresenter2 != null)
      {
        this.newStroke2.StylusPoints.AddStylusPoints(args.GetStylusPoints(inkPresenter2));
      }
    }
  }
}
