LoadingVisual = function(parent, text, font, left, top) {
  this.$parent = parent;
  this.create(text, font, left, top);
}

LoadingVisual.prototype.$f = function(name) {
  return this.$root.findName(name);
}

LoadingVisual.prototype.$cfx = function(xaml) {
  return this.$parent.getHost().content.createFromXaml(xaml, true);
}

LoadingVisual.prototype.create = function(text, font, left, top) {
  this.$root = this.$cfx(this.$xaml);
  this.$parent.children.add(this.$root);
  this.$root["Canvas.Left"] = left;
  this.$root["Canvas.Top"] = top;

  this.$spin = this.$f("spin");
  this.$loading = this.$f("loading");

  if (font) {
    this.$loading.setFontSource(font);
  }
  this.$loading.text = text;
}
  
LoadingVisual.prototype.start = function() {
  this.$spin.begin();
  this.$root.visibility = "Visible";
}

LoadingVisual.prototype.stop = function() {
  this.$root.visibility = "Collapsed";
  this.$spin.stop();
}

LoadingVisual.prototype.$xaml = '\
<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="wait" Visibility="Collapsed">\
<Canvas.RenderTransform>\
  <ScaleTransform x:Name="scale" ScaleX="0.5" ScaleY="0.5"/>\
</Canvas.RenderTransform>\
<Ellipse Fill="#00FFFFFF" StrokeThickness="8" Width="32" Height="32">\
  <Ellipse.Resources>\
    <Storyboard x:Name="spin" Storyboard.TargetName="rt" Storyboard.TargetProperty="Angle">\
      <DoubleAnimation From="0" To="360" BeginTime="0:0:0" Duration="0:0:0.45" RepeatBehavior="Forever"/>\
    </Storyboard>\
  </Ellipse.Resources>\
  <Ellipse.Stroke>\
    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">\
      <GradientStop Color="#E5000000" Offset="0"/>\
      <GradientStop Color="#00FFFFFF" Offset="0.87"/>\
    </LinearGradientBrush>\
  </Ellipse.Stroke>\
  <Ellipse.RenderTransform>\
    <TransformGroup>\
      <RotateTransform x:Name="rt" Angle="20" CenterX="16" CenterY="16"/>\
    </TransformGroup>\
  </Ellipse.RenderTransform>\
</Ellipse>\
<TextBlock x:Name="loading" Canvas.Top="2" Canvas.Left="44" Foreground="#666666" FontFamily="Tahoma" FontSize="22">Loading...</TextBlock>\
</Canvas>';

