﻿/*
Silverlight: Convert Ink to Xaml
http://blogs.msdn.com/webnext/archive/2007/06/01/silverlight-convert-ink-to-xaml.aspx
*/
function loadStrokes(xmlNode)
{
/// <summary>
/// Returns a stroke collection loaded from the passed in XML node.  
/// NOTE: xmlNode must have a child <SC> node for this routine to succeed.
/// </summary>
/// <remarks>http://www.tabletpcpost.com/search/js/SearchTIP.js</remarks>
  var loadedStrokes = silverlight.content.CreateFromXaml('<StrokeCollection/>');
      
  var strokes = xmlNode.selectNodes("./sc/s");
  alert ('strokes: ' + strokes);
  if (strokes != null)
  {
    // for each stroke, get the stylus points
    for (var j = 0; j < strokes.length; j++)
    {
      var s = strokes(j);
      var sps = s.selectNodes("./sp");

      var newS = silverlight.content.CreateFromXaml('<Stroke/>');
     
      var c = s.getAttribute("c");
      var oc = s.getAttribute("oc");
      var w = s.getAttribute("w");
      var h = s.getAttribute("h");
      var o = s.getAttribute("o");
      
      if (c != null) newS.TipOpacity = o;
      if (w != null) newS.DrawingAttributes.Width = w;
      if (h != null) newS.DrawingAttributes.Height = h;
      if (c != null) newS.DrawingAttributes.Color = c;
      if (oc != null) newS.DrawingAttributes.OutlineColor = oc;
      
      var newSPC = silverlight.content.CreateFromXaml('<StylusPointCollection/>');
      //alert("newS=" + newS + " newSPC=" + newSPC);

      //for each stylusPoint, get x, y, p
      for (var k = 0; k < sps.length; k++)
      {
        var stylusPoint = sps(k);
        var x = stylusPoint.getAttribute("x");
        var y = stylusPoint.getAttribute("y");
        var p = stylusPoint.getAttribute("p");
        var newSP = silverlight.content.CreateFromXaml('<StylusPoint/>');
        //alert("newSP=" + newSP);
        newSP.X = x;
        newSP.Y = y;
        if (p != null) newSP.PressureFactor = p;
        //alert("newSP x=" + newSP.X + " y=" + newSP.Y);
        newSPC.Add(newSP);
      }
      
      newS.StylusPoints.Add(newSPC);
      
      loadedStrokes.Add(newS);
    }
  }
  return loadedStrokes;
}

function saveStrokesXYOnly(strokeCollection, lat, lon, zoon)
{
/// <summary>Returns a string that is the XML for the persisted strokes.</summary>
/// <remarks>http://www.tabletpcpost.com/search/js/SearchTIP.js</remarks>
  var inkXML = "<sc>";
    
  if (strokeCollection != null)
  {
    //var inkXML = "<sc " +  + ">";
    var strokeCount = strokeCollection.Count;

    for (i=0; i<strokeCount; i++)
    {
      var s = strokeCollection.GetItem(i);
      inkXML += saveStrokeXYOnly(s);
    }
  }
  inkXML += "</sc>";
  return inkXML;
}

function saveStrokeXYOnly(s)
{
/// <summary>
/// Build an xml string with XY coordinates only in a condensed fashion
/// to make the remote reco as fast as feasible.
/// </summary>
/// <remarks>http://www.tabletpcpost.com/search/js/SearchTIP.js</remarks>
  var inkXML = "<s>";

  var stylusPointCount = s.StylusPoints.Count;

  for (j=0; j<stylusPointCount; j++)
  {
    var sp = s.StylusPoints.GetItem(j);
    inkXML += "<sp x='" + sp.X + "' y='" + sp.Y + "' />";
  }
  inkXML += "</s>";
  return inkXML;
}
