﻿

RadarThrob.prototype.radius = 0;
RadarThrob.prototype.xamlObject = null;

RadarThrob.prototype.redScaleTransform = null;
RadarThrob.prototype.blueScaleTransform = null;
RadarThrob.prototype.yellowScaleTransform = null;
RadarThrob.prototype.greenScaleTransform = null;
RadarThrob.prototype.whiteScaleTransform = null;

RadarThrob.prototype.redCanvas = null;
RadarThrob.prototype.blueCanvas = null;
RadarThrob.prototype.yellowCanvas = null;
RadarThrob.prototype.greenCanvas = null;
RadarThrob.prototype.whiteCanvas = null;

function RadarThrob()
{
}

RadarThrob.prototype.Build = function()
{
    var t = '';
    var color = '#ffff0000';
    
    var stop1 = 0.2;
    var stop2 = 0.8;
    var stop3 = 0.8;
    
    t += '<Canvas Canvas.Left="200" Canvas.Top="200" Visibility="Visible" Opacity="1" IsHitTestVisible="false">';
        t += '<Canvas Canvas.Left="-10" Canvas.Top="-10">';
            t += '<Ellipse Height="20" Width="20">';
                    t += '<Ellipse.Fill>';
				        t += '<RadialGradientBrush RadiusX="0.5" RadiusY="0.512345" Center="0.5,0.5" GradientOrigin="0.5,0.5">';
					        t += '<RadialGradientBrush.GradientStops>';
						        t += '<GradientStop Color="#00000000" Offset="0"/>';
						        t += '<GradientStop Color="#00000000" Offset="' + stop1 + '"/>';
						        t += '<GradientStop Color="' + color + '" Offset="' + stop3 + '"/>';
						        t += '<GradientStop Color="#00000000" Offset="1"/>';
					        t += '</RadialGradientBrush.GradientStops>';
					        t += '<RadialGradientBrush.RelativeTransform>';
						        t += '<TransformGroup>';
                                    t += '<ScaleTransform CenterX="0.5" CenterY="0.5"  ScaleX="1.0" ScaleY="1.0" />';
                                    t += '<RotateTransform CenterX="100" CenterY="100"  Angle="0" />';
						        t += '</TransformGroup>';
					        t += '</RadialGradientBrush.RelativeTransform>';
				        t += '</RadialGradientBrush>';
			        t += '</Ellipse.Fill>'; 
			        t += '<Ellipse.RenderTransform>';
				        t += '<TransformGroup>';
                            t += '<ScaleTransform CenterX="10" CenterY="10"  ScaleX="1.0" ScaleY="1.0" />';
                            t += '<RotateTransform CenterX="100" CenterY="100"  Angle="0" />';
				        t += '</TransformGroup>';
			        t += '</Ellipse.RenderTransform>';
			                
            t += '</Ellipse>';           
        t += '</Canvas>';
        
        color = "#ffffff00";
        
        t += '<Canvas Canvas.Left="-8" Canvas.Top="-8" IsHitTestVisible="false">';
            t += '<Ellipse Height="16" Width="16">';
                    t += '<Ellipse.Fill>';
				        t += '<RadialGradientBrush RadiusX="0.5" RadiusY="0.512345" Center="0.5,0.5" GradientOrigin="0.5,0.5">';
					        t += '<RadialGradientBrush.GradientStops>';
						        t += '<GradientStop Color="#00000000" Offset="0"/>';
						        t += '<GradientStop Color="#00000000" Offset="' + stop1 + '"/>';
						        t += '<GradientStop Color="' + color + '" Offset="' + stop3 + '"/>';
						        t += '<GradientStop Color="#00000000" Offset="1"/>';
					        t += '</RadialGradientBrush.GradientStops>';
					        t += '<RadialGradientBrush.RelativeTransform>';
						        t += '<TransformGroup>';
                                    t += '<ScaleTransform CenterX="0.5" CenterY="0.5"  ScaleX="1.0" ScaleY="1.0" />';
                                    t += '<RotateTransform CenterX="100" CenterY="100"  Angle="0" />';
						        t += '</TransformGroup>';
					        t += '</RadialGradientBrush.RelativeTransform>';
				        t += '</RadialGradientBrush>';
			        t += '</Ellipse.Fill>'; 
			        t += '<Ellipse.RenderTransform>';
				        t += '<TransformGroup>';
                            t += '<ScaleTransform CenterX="8" CenterY="8"  ScaleX="1.0" ScaleY="1.0" />';
                            t += '<RotateTransform CenterX="100" CenterY="100"  Angle="0" />';
				        t += '</TransformGroup>';
			        t += '</Ellipse.RenderTransform>';
			                
            t += '</Ellipse>';           
        t += '</Canvas>';
        
        color = '#ff0000ff';
        
        t += '<Canvas Canvas.Left="-7" Canvas.Top="-7" IsHitTestVisible="false">';
            t += '<Ellipse Height="14" Width="14">';
                    t += '<Ellipse.Fill>';
				        t += '<RadialGradientBrush RadiusX="0.5" RadiusY="0.512345" Center="0.5,0.5" GradientOrigin="0.5,0.5">';
					        t += '<RadialGradientBrush.GradientStops>';
						        t += '<GradientStop Color="#00000000" Offset="0"/>';
						        t += '<GradientStop Color="#00000000" Offset="' + stop1 + '"/>';
						        t += '<GradientStop Color="' + color + '" Offset="' + stop3 + '"/>';
						        t += '<GradientStop Color="#00000000" Offset="1"/>';
					        t += '</RadialGradientBrush.GradientStops>';
					        t += '<RadialGradientBrush.RelativeTransform>';
						        t += '<TransformGroup>';
                                    t += '<ScaleTransform CenterX="0.5" CenterY="0.5"  ScaleX="1.0" ScaleY="1.0" />';
                                    t += '<RotateTransform CenterX="100" CenterY="100"  Angle="0" />';
						        t += '</TransformGroup>';
					        t += '</RadialGradientBrush.RelativeTransform>';
				        t += '</RadialGradientBrush>';
			        t += '</Ellipse.Fill>'; 
			        t += '<Ellipse.RenderTransform>';
				        t += '<TransformGroup>';
                            t += '<ScaleTransform CenterX="7" CenterY="7"  ScaleX="1.0" ScaleY="1.0" />';
                            t += '<RotateTransform CenterX="100" CenterY="100"  Angle="0" />';
				        t += '</TransformGroup>';
			        t += '</Ellipse.RenderTransform>';
			                
            t += '</Ellipse>';           
        t += '</Canvas>';
        
        color = '#ff00ff00';
        
        t += '<Canvas Canvas.Left="-5" Canvas.Top="-5" IsHitTestVisible="false">';
            t += '<Ellipse Height="10" Width="10">';
                    t += '<Ellipse.Fill>';
				        t += '<RadialGradientBrush RadiusX="0.5" RadiusY="0.512345" Center="0.5,0.5" GradientOrigin="0.5,0.5">';
					        t += '<RadialGradientBrush.GradientStops>';
						        t += '<GradientStop Color="#00000000" Offset="0"/>';
						        t += '<GradientStop Color="#00000000" Offset="' + stop1 + '"/>';
						        t += '<GradientStop Color="' + color + '" Offset="' + stop3 + '"/>';
						        t += '<GradientStop Color="#00000000" Offset="1"/>';
					        t += '</RadialGradientBrush.GradientStops>';
					        t += '<RadialGradientBrush.RelativeTransform>';
						        t += '<TransformGroup>';
                                    t += '<ScaleTransform CenterX="0.5" CenterY="0.5"  ScaleX="1.0" ScaleY="1.0" />';
                                    t += '<RotateTransform CenterX="100" CenterY="100"  Angle="0" />';
						        t += '</TransformGroup>';
					        t += '</RadialGradientBrush.RelativeTransform>';
				        t += '</RadialGradientBrush>';
			        t += '</Ellipse.Fill>'; 
			        t += '<Ellipse.RenderTransform>';
				        t += '<TransformGroup>';
                            t += '<ScaleTransform CenterX="5" CenterY="5"  ScaleX="1.0" ScaleY="1.0" />';
                            t += '<RotateTransform CenterX="100" CenterY="100"  Angle="0" />';
				        t += '</TransformGroup>';
			        t += '</Ellipse.RenderTransform>';            
            t += '</Ellipse>';           
        t += '</Canvas>';
        
 
        color = '#ff3fc2f6';
        
        t += '<Canvas Canvas.Left="-4" Canvas.Top="-1" IsHitTestVisible="false">';
            t += '<Ellipse Width="8" Height="2" >';
                    t += '<Ellipse.Fill>';
				        t += '<RadialGradientBrush RadiusX="0.5" RadiusY="0.512345" Center="0.5,0.5" GradientOrigin="0.5,0.5">';
					        t += '<RadialGradientBrush.GradientStops>';
						        t += '<GradientStop Color="#00000000" Offset="0"/>';
						        t += '<GradientStop Color="#00000000" Offset="' + stop1 + '"/>';
						        t += '<GradientStop Color="' + color + '" Offset="' + stop3 + '"/>';
						        t += '<GradientStop Color="#00000000" Offset="1"/>';
					        t += '</RadialGradientBrush.GradientStops>';
					        t += '<RadialGradientBrush.RelativeTransform>';
						        t += '<TransformGroup>';
                                    t += '<ScaleTransform CenterX="0.5" CenterY="0.5"  ScaleX="1.0" ScaleY="1.0" />';
                                    t += '<RotateTransform CenterX="100" CenterY="100"  Angle="0" />';
						        t += '</TransformGroup>';
					        t += '</RadialGradientBrush.RelativeTransform>';
				        t += '</RadialGradientBrush>';
			        t += '</Ellipse.Fill>'; 
			        t += '<Ellipse.RenderTransform>';
				        t += '<TransformGroup>';
                            t += '<ScaleTransform CenterX="4" CenterY="1"  ScaleX="1.0" ScaleY="1.0" />';
                            t += '<RotateTransform CenterX="100" CenterY="100"  Angle="0" />';
				        t += '</TransformGroup>';
			        t += '</Ellipse.RenderTransform>';            
            t += '</Ellipse>';           
        t += '</Canvas>'; 
 
        
    t += '</Canvas>';
            
    var rootCanvas = plugin.content.createFromXaml(t);
    this.xamlObject = rootCanvas;

    //alert(" it's " + this.xamlObject.children.getItem(2));//["ScaleX"]);    
    this.redCanvas = this.xamlObject.children.getItem(0);
    this.yellowCanvas = this.xamlObject.children.getItem(1);
    this.blueCanvas = this.xamlObject.children.getItem(2);
    this.greenCanvas = this.xamlObject.children.getItem(3);
 
    this.whiteCanvas = this.xamlObject.children.getItem(4);
    
    this.redScaleTransform = this.xamlObject.children.getItem(0).children.getItem(0).RenderTransform.children.getItem(0);
    this.yellowScaleTransform = this.xamlObject.children.getItem(1).children.getItem(0).RenderTransform.children.getItem(0);
    this.blueScaleTransform = this.xamlObject.children.getItem(2).children.getItem(0).RenderTransform.children.getItem(0);
    this.greenScaleTransform = this.xamlObject.children.getItem(3).children.getItem(0).RenderTransform.children.getItem(0);
    this.whiteScaleTransform = this.xamlObject.children.getItem(4).children.getItem(0).RenderTransform.children.getItem(0);

}

RadarThrob.prototype.OnClicked = function(sender, e)
{
    var throb = this.Throb(1);
    
    //dd("throb = " + throb);
    throb.Play();
}

RadarThrob.prototype.Throb = function(dur)
{
    var maxOpac = 0.4;
    
    var growRedX = new Animation("growRedX", this.redScaleTransform, "ScaleX", 1, 10.4, 0.3);
    var growRedY = new Animation("growRedY", this.redScaleTransform, "ScaleY", 1, 10, 0.3);
    var opacRed = new Animation("opacRed", this.redCanvas, "Opacity", 0, maxOpac, 0.3);  

    var growYellowX = new Animation("growYellowX", this.yellowScaleTransform, "ScaleX", 1, 19.3, 0.3);
    var growYellowY = new Animation("growYellowY", this.yellowScaleTransform, "ScaleY", 1, 19, 0.3);
    var opacYellow = new Animation("opacYellow", this.yellowCanvas, "Opacity", 0, maxOpac, 0.3);  
    
    var growBlueX = new Animation("growBlueX", this.blueScaleTransform, "ScaleX", 1, 6, 0.3);
    var growBlueY = new Animation("growBlueY", this.blueScaleTransform, "ScaleY", 1, 7, 0.3);
    var opacBlue = new Animation("opacBlue", this.blueCanvas, "Opacity", 0, maxOpac, 0.3);  

    var growGreenX = new Animation("growGreenX", this.greenScaleTransform, "ScaleX", 1, 14.2, 0.3);
    var growGreenY = new Animation("growGreenY", this.greenScaleTransform, "ScaleY", 1, 15, 0.3);
    var opacGreen = new Animation("opacGreen", this.greenCanvas, "Opacity", 0, maxOpac, 0.3);  
    
    var compRed = new CompoundAnimation();
    compRed.baseAnimation.duration = dur;
    opacRed.duration = compRed.baseAnimation.duration + 0.3 + 0;//Math.random() * 1.85;
    compRed.Add(growRedX);
    compRed.Add(growRedY);
    
    compRed.baseAnimation.easeFunction = function(frac) { return (1 - Math.cos(1 * Math.PI * frac))/2;}
    opacRed.easeFunction = function(frac) { var ff = Math.sqrt(frac); return (1 - Math.cos(4 * Math.PI * ff))/2;}
    compRed.baseAnimation.OnPlayAnimation = opacRed;
    
    var compBlue = new CompoundAnimation();
    compBlue.baseAnimation.duration = dur;
    opacBlue.duration = compBlue.baseAnimation.duration + 0.5 + 0;//Math.random() * 1.65;
    compBlue.Add(growBlueX);
    compBlue.Add(growBlueY);
    
    compBlue.baseAnimation.easeFunction = function(frac) { return (1 - Math.cos(1 * Math.PI * frac))/2;}
    opacBlue.easeFunction = function(frac) { var ff = Math.sqrt(frac); return (1 - Math.cos(4 * Math.PI * ff))/2;}
    compBlue.baseAnimation.OnPlayAnimation = opacBlue; 
      
    var compYellow = new CompoundAnimation();
    compYellow.baseAnimation.duration = dur;
    opacYellow.duration = compYellow.baseAnimation.duration + 0.5 + 0;//Math.random() * 1.15;
    compYellow.Add(growYellowX);
    compYellow.Add(growYellowY);
    
    compYellow.baseAnimation.easeFunction = function(frac) { return (1 - Math.cos(1 * Math.PI * frac))/2;}
    opacYellow.easeFunction = function(frac) { var ff = Math.sqrt(frac); return (1 - Math.cos(4 * Math.PI * ff))/2;}
    compYellow.baseAnimation.OnPlayAnimation = opacYellow;
    
    var compGreen = new CompoundAnimation();
    compGreen.baseAnimation.duration = dur;
    opacGreen.duration = compGreen.baseAnimation.duration + 1.0 + 0;//Math.random() * 1.0;
    compGreen.Add(growGreenX);
    compGreen.Add(growGreenY);
    
    compGreen.baseAnimation.easeFunction = function(frac) { return (1 - Math.cos(1 * Math.PI * frac))/2;}
    opacGreen.easeFunction = function(frac) { var ff = Math.sqrt(frac); return (1 - Math.cos(4 * Math.PI * ff))/2;}
    compGreen.baseAnimation.OnPlayAnimation = opacGreen;         
    
//    var compAll = new CompoundAnimation();         
//    compAll.baseAnimation.duration = dur;
//    compAll.Add(compRed);     
//    compAll.Add(compBlue);
//    compAll.Add(compYellow);

    var dlgX = new Animation("dlgX", dialog.xamlObject, "Canvas.Left", dialog.xamlObject["Canvas.Left"],
         dialog.xamlObject["Canvas.Left"] + 10,
             1.2);
    dlgX.easeFunction = function(frac) { var ff = frac*frac*frac; return (1 - Math.cos(2 * ff * Math.PI))/2; }
    
    
    var anim = new Animation("trigger", null, "none", 0, 0, opacGreen.duration);
    
    if (dur != -1)
    {
        anim.AddPlayAnimation(compRed);
        anim.AddPlayAnimation(compYellow);
        anim.AddPlayAnimation(compBlue);
        anim.AddPlayAnimation(compGreen);
        anim.AddPlayAnimation(dlgX);
    }
    
    return anim;
}

RadarThrob.prototype.sparking = false;

RadarThrob.prototype.IsSparking = function()
{
    return this.sparking;
}

RadarThrob.prototype.SetupSpark = function(val)
{
    this.xamlObject["Opacity"] = val ? 1 : 0;
    this.xamlObject["Visibility"] = val ? "Visible" : "Collapsed";
    this.redCanvas["Opacity"] = !val ? 1: 0;
    this.blueCanvas["Opacity"] = !val ? 1: 0;
    this.yellowCanvas["Opacity"] = !val ? 1: 0;
    this.greenCanvas["Opacity"] = !val ? 1: 0;
    this.whiteCanvas["Opacity"] = !val ? 1: 0;
}

RadarThrob.prototype.sparkAnim = null;

RadarThrob.prototype.Spark = function(sender, dur)
{
    this.xamlObject["Canvas.Left"] = sender["Canvas.Left"] + sender.children.getItem(0)["Width"]/2;
    this.xamlObject["Canvas.Top"] = sender["Canvas.Top"] + sender.children.getItem(0)["Height"]/2;

    var maxOpac = 0.2;
    
    var growRedX = new Animation("growWhiteX", this.whiteScaleTransform, "ScaleX", 1, 60, 0.3);
    var growRedY = new Animation("growWhiteY", this.whiteScaleTransform, "ScaleY", 1, 60, 0.3);
    var opacRed = new Animation("opacWhite", this.whiteCanvas, "Opacity", 0, maxOpac, 0.3);  

    var compRed = new CompoundAnimation();
    compRed.baseAnimation.duration = dur;
    opacRed.duration = compRed.baseAnimation.duration + 0.3 + 0;//Math.random() * 1.85;
    compRed.Add(growRedX);
    compRed.Add(growRedY);
    
    compRed.baseAnimation.easeFunction = function(frac) { return (1 - Math.cos(1 * Math.PI * frac))/2;}
    opacRed.easeFunction = function(frac) { var ff = Math.sqrt(frac); return (1 - Math.cos(4 * Math.PI * ff))/2;}
    compRed.baseAnimation.OnPlayAnimation = opacRed;
    
    //var anim = new Animation("trigger", null, "none", 0, 0, opacGreen.duration);
    //anim.AddPlayAnimation(compRed);
    compRed.baseAnimation.OnPlayScript = Silverlight.createDelegate(this, this.OnStart);//function() { mainWindow.throb.SetupSpark(true); mainWindow.throb.sparking = true; };
    compRed.baseAnimation.OnStopScript = Silverlight.createDelegate(this, this.OnStop);//function() { mainWindow.throb.SetupSpark(false); mainWindow.throb.sparking = false; };
    
    this.sparkAnim = compRed.baseAnimation;
    
    return compRed;
}

RadarThrob.prototype.OnStart = function()
{
    this.SetupSpark(true);
    this.sparking = true;
}

RadarThrob.prototype.OnStop = function()
{
    this.SetupSpark(false);
    this.sparking = false;
}