public void Step4_ShowMatrix(
            AeroNavigationBar.HistoryInfo History,
            LinkImage[] Source,
            ComparisionInfo[] Comparision,
            ComparisionValue[] Values)
        {
            History.AddFrame(
                delegate
            {
                var More = Comparision.Count(k => k.WaitingForUser && k.Value == null);

                this.Title.Text = "The Matrix. You have " + More + " image pairs to compare...";

                #region headers
                var o = Source.Select <LinkImage, Action>(
                    (k, i) =>
                {
                    k.SizeTo(0.15);
                    k.AttachContainerTo(this);
                    k.MoveContainerTo(60, 150 + i * 60);

                    var kx = new TextButtonControl
                    {
                        Text   = "#" + (1 + i),
                        Width  = 40,
                        Height = 32
                    };

                    kx.AttachContainerTo(this);
                    kx.MoveContainerTo(130, 160 + i * 60);
                    kx.Background.Fill    = Brushes.White;
                    kx.Background.Opacity = 0.3;

                    var ky = new TextButtonControl
                    {
                        Text   = "#" + (1 + i),
                        Width  = 40,
                        Height = 32
                    };

                    ky.AttachContainerTo(this);
                    ky.MoveContainerTo(200 + i * 60, 100);
                    ky.Background.Fill    = Brushes.White;
                    ky.Background.Opacity = 0.3;

                    var kxr = new Rectangle
                    {
                        Fill   = Brushes.Black,
                        Width  = Source.Length * 60 + 140,
                        Height = 1
                    };

                    kxr.AttachTo(this);
                    kxr.MoveTo(60, 200 + i * 60);


                    var kyr = new Rectangle
                    {
                        Fill   = Brushes.Black,
                        Height = Source.Length * 60 + 60,
                        Width  = 1
                    };

                    kyr.AttachTo(this);
                    kyr.MoveTo(250 + i * 60, 100);

                    return(delegate
                    {
                        k.OrphanizeContainer();
                        kx.OrphanizeContainer();
                        ky.OrphanizeContainer();
                        kxr.Orphanize();
                        kyr.Orphanize();
                    });
                }
                    ).ToArray();
                #endregion

                #region values

                var v = Comparision.Select <ComparisionInfo, Action>(
                    k =>
                {
                    var kt = new TextButtonControl
                    {
                        Text   = "",
                        Width  = 40,
                        Height = 32
                    };

                    kt.AttachContainerTo(this);
                    kt.MoveContainerTo(200 + k.X * 60, 160 + k.Y * 60);

                    kt.Background.Fill    = Brushes.White;
                    kt.Background.Opacity = 0.3;

                    if (k.Value == null)
                    {
                        if (k.WaitingForUser)
                        {
                            kt.Background.Fill    = Brushes.Yellow;
                            kt.Background.Opacity = 0.5;
                        }
                    }
                    else
                    {
                        kt.Text = k.Value.ToString();

                        if (k.Value.Value == 1)
                        {
                            kt.Background.Fill    = Brushes.Cyan;
                            kt.Background.Opacity = 0.5;
                        }
                    }

                    return(delegate
                    {
                        kt.OrphanizeContainer();
                    });
                }
                    ).ToArray();

                #endregion

                return(delegate
                {
                    this.Title.Text = "...";

                    o.ForEach(h => h());
                    v.ForEach(h => h());
                });
            }
                );
        }
        public void Step5_ShowMistakeMatrix(
            AeroNavigationBar.HistoryInfo History,
            LinkImage[] Source,
            ComparisionInfo[] Comparision,
            ComparisionValue[] Values)
        {
            History.AddFrame(
                delegate
            {
                var More = Comparision.Count(k => k.WaitingForUser && k.Value == null);



                #region headers
                var o = Source.Select <LinkImage, Action>(
                    (k, i) =>
                {
                    k.SizeTo(0.15);
                    k.AttachContainerTo(this);
                    k.MoveContainerTo(60, 150 + i * 60);

                    var kx = new TextButtonControl
                    {
                        Text   = "#" + (1 + i),
                        Width  = 40,
                        Height = 32
                    };

                    kx.AttachContainerTo(this);
                    kx.MoveContainerTo(130, 160 + i * 60);
                    kx.Background.Fill    = Brushes.White;
                    kx.Background.Opacity = 0.3;

                    var ky = new TextButtonControl
                    {
                        Text   = "#" + (1 + i),
                        Width  = 40,
                        Height = 32
                    };

                    ky.AttachContainerTo(this);
                    ky.MoveContainerTo(200 + i * 60, 100);
                    ky.Background.Fill    = Brushes.White;
                    ky.Background.Opacity = 0.3;

                    var kxr = new Rectangle
                    {
                        Fill   = Brushes.Black,
                        Width  = Source.Length * 60 + 140,
                        Height = 1
                    };

                    kxr.AttachTo(this);
                    kxr.MoveTo(60, 200 + i * 60);


                    var kyr = new Rectangle
                    {
                        Fill   = Brushes.Black,
                        Height = Source.Length * 60 + 60,
                        Width  = 1
                    };

                    kyr.AttachTo(this);
                    kyr.MoveTo(250 + i * 60, 100);

                    return(delegate
                    {
                        k.OrphanizeContainer();
                        kx.OrphanizeContainer();
                        ky.OrphanizeContainer();
                        kxr.Orphanize();
                        kyr.Orphanize();
                    });
                }
                    ).ToArray();
                #endregion

                #region values

                var Mistakes = Comparision.Where(q => q.WaitingForUser).ToArray(
                    q =>
                {
                    var x_cells =
                        Comparision.Where(k => k.X == q.Y).OrderBy(k => k.Y).ToArray();

                    var x_product =
                        x_cells.Product(k => k.GetCurrentValue());

                    var y_cells =
                        Comparision.Where(k => k.Y == q.X).OrderBy(k => k.X).ToArray();

                    var y_product =
                        y_cells.Product(k => k.GetCurrentValue());


                    var z = Math.Pow(q.GetCurrentValue(), Source.Length);

                    return(new { q, Mistake = 1.0 / Math.Pow(x_product * y_product * z, 1.0 / (Source.Length - 2)) });

                    /*
                     *
                     * 1/POWER(PRODUCT(R4C2:R9C2)*PRODUCT(R9C2:R9C7)*POWER(R[-46]C;veerge);1/(veerge-2))
                     * 1/POWER(x_product*y_product*POWER(R[-46]C;veerge);1/(veerge-2))
                     * 1/POWER(x_product*y_product*z;1/(veerge-2))
                     *
                     */
                }
                    ).OrderBy(
                    ContextMistakes =>
                {
                    var Mistakes_Max = ContextMistakes.Max(k => k.Mistake);
                    var Mistakes_Min = ContextMistakes.Min(k => k.Mistake);

                    var Mistake_Value = Mistakes_Min;

                    if (Mistakes_Max * Mistakes_Min > 1.0)
                    {
                        Mistake_Value = Mistakes_Max;
                    }

                    return(ContextMistakes.First(k => k.Mistake == Mistake_Value));
                }
                    ).ToArray();

                var Gradient = Colors.Red.ToGradient(Colors.Blue, Mistakes.Length).ToArray();



                Title.Text = "Biggest mistake was made at " + Mistakes.First().q.ToVersusString() + ". Click on a cell to recompare.";


                var v = Mistakes.Select(
                    (k, k_index) =>
                {
                    var kt = new TextButtonControl
                    {
                        Text   = "",
                        Width  = 60 - 4,
                        Height = 32
                    };

                    kt.AttachContainerTo(this);
                    kt.MoveContainerTo(192 + k.q.X * 60, 160 + k.q.Y * 60);

                    kt.Background.Fill = new SolidColorBrush(Gradient[k_index]);


                    kt.Text = k.Mistake.ToString();

                    kt.Click +=
                        delegate
                    {
                        var NewComparision = Comparision.ToArray(
                            oo =>
                        {
                            var n = new ComparisionInfo
                            {
                                WaitingForUser = oo.WaitingForUser,
                                Value          = oo.Value,
                                X = oo.X,
                                Y = oo.Y
                            };

                            if (oo == k.q)
                            {
                                n.Value = null;
                            }



                            return(n);
                        }
                            );


                        Step3_Compare(History, Source, NewComparision, Values);
                    };

                    return(new Action(
                               delegate
                    {
                        kt.OrphanizeContainer();
                    }
                               ));
                }
                    ).ToArray();

                #endregion

                return(delegate
                {
                    this.Title.Text = "...";

                    o.ForEach(h => h());
                    v.ForEach(h => h());
                });
            }
                );
        }
        public void Step3_Compare(
            AeroNavigationBar.HistoryInfo History,
            LinkImage[] Source,
            ComparisionInfo[] Comparision,
            ComparisionValue[] Values)
        {
            History.AddFrame(
                delegate
            {
                var Current = Comparision.Where(k => k.WaitingForUser && k.Value == null).FirstOrDefault();

                var MatrixButton = new TextButtonControl
                {
                    Text   = ">> Show the matrix",
                    Width  = 400,
                    Height = 40,
                }.AttachContainerTo(this).MoveContainerTo(350, 100 + 40 * 10);

                MatrixButton.Content.FontSize = 20;

                var MatrixButton_bg = MatrixButton.Background.ToAnimatedOpacity();


                MatrixButton.Background.Fill = Brushes.White;
                MatrixButton_bg.Opacity      = 0;

                MatrixButton.Overlay.MouseEnter +=
                    delegate { MatrixButton_bg.Opacity = 1; };


                MatrixButton.Overlay.MouseLeave +=
                    delegate { MatrixButton_bg.Opacity = 0; };


                MatrixButton.Click +=
                    delegate
                {
                    Step4_ShowMatrix(History, Source, Comparision, Values);
                };

                if (Current == null)
                {
                    this.Title.Text = "You are done!";

                    var MistakeMatrixButton = new TextButtonControl
                    {
                        Text   = ">> Show the mistake matrix",
                        Width  = 400,
                        Height = 40,
                    }.AttachContainerTo(this).MoveContainerTo(350, 100 + 40 * 9);

                    MistakeMatrixButton.Content.FontSize = 20;

                    var MistakeMatrixButton_bg = MistakeMatrixButton.Background.ToAnimatedOpacity();


                    MistakeMatrixButton.Background.Fill = Brushes.White;
                    MistakeMatrixButton_bg.Opacity      = 0;

                    MistakeMatrixButton.Overlay.MouseEnter +=
                        delegate { MistakeMatrixButton_bg.Opacity = 1; };


                    MistakeMatrixButton.Overlay.MouseLeave +=
                        delegate { MistakeMatrixButton_bg.Opacity = 0; };


                    MistakeMatrixButton.Click +=
                        delegate
                    {
                        Step5_ShowMistakeMatrix(History, Source, Comparision, Values);
                    };

                    var RestartButton = new TextButtonControl
                    {
                        Text   = ">> Restart",
                        Width  = 400,
                        Height = 40,
                    }.AttachContainerTo(this).MoveContainerTo(350, 100 + 40 * 11);

                    RestartButton.Content.FontSize = 20;

                    var RestartButton_bg = RestartButton.Background.ToAnimatedOpacity();


                    RestartButton.Background.Fill = Brushes.White;
                    RestartButton_bg.Opacity      = 0;

                    RestartButton.Overlay.MouseEnter +=
                        delegate { RestartButton_bg.Opacity = 1; };


                    RestartButton.Overlay.MouseLeave +=
                        delegate { RestartButton_bg.Opacity = 0; };


                    RestartButton.Click +=
                        delegate
                    {
                        Step1_ChooseImageSet(History, DefaultDataSets);
                    };


                    // step 1 - each row gets a geomean and is seen as a new column
                    var GeomeanColumn = Enumerable.Range(0, Source.Length).ToArray(
                        i => Comparision.Where(k => k.Y == i).Geomean(k => k.GetCurrentValue())
                        );

                    // step 2 - geomean gets a sum
                    var GeomeanColumnSum = GeomeanColumn.Sum();

                    // step 3 - each column gets a sum
                    //var SumRow = Enumerable.Range(0, Source.Length).ToArray(
                    //    i => Comparision.Where(k => k.X == i).Sum(k => k.GetCurrentValue())
                    //);

                    // step 4 - calculate the weights for each row
                    var GeomeanWeightColumn = GeomeanColumn.ToArray(k => k / GeomeanColumnSum);

                    // step 5 - calculate max selfvalue
                    //var MaxSelfValue = SumRow.MatrixMultiplication(GeomeanWeightColumn);


                    var Sorted = GeomeanWeightColumn.
                                 Select((weight, i) => new { weight = 1 - weight, i, Source = Source[i] }).
                                 OrderBy(k => k.weight).Select((k, i) => new { k.weight, i, k.Source }).ToArray();

                    var DisposeSorted = new List <Action>();


                    foreach (var v in Sorted)
                    {
                        var k = v;

                        var zoom = (0.5 + v.weight * 0.5) / 2.0;

                        Console.WriteLine(new { v.i, zoom, v.weight }.ToString());

                        v.Source.ClickEnabled = false;
                        v.Source.SizeTo(zoom);

                        var k_x = 500 + Convert.ToInt32(-30 * v.i * zoom) * v.i;
                        var k_y = 100 + Convert.ToInt32(70 * v.i * zoom);

                        v.Source.MoveContainerTo(k_x, k_y);
                        v.Source.AttachContainerTo(this);


                        var k_Text = new TextBox
                        {
                            Background      = Brushes.Black,
                            Width           = 60,
                            Height          = 22,
                            Foreground      = Brushes.Yellow,
                            BorderThickness = new Thickness(0),
                            Text            = "" + v.weight,
                            IsReadOnly      = true
                        };


                        bool MouseEnterDisabled      = false;
                        MouseEventHandler MouseEnter =
                            delegate
                        {
                            // cannot remove event from MouseEnter yet
                            if (MouseEnterDisabled)
                            {
                                return;
                            }

                            k.Source.BringContainerToFront();
                            k_Text.BringToFront();
                        };

                        k.Source.Overlay.MouseEnter += MouseEnter;

                        k_Text.MoveTo(k_x - 30, k_y - 11).AttachTo(this);

                        DisposeSorted.Add(
                            delegate
                        {
                            k.Source.OrphanizeContainer();

                            k_Text.Orphanize();

                            MouseEnterDisabled = true;
                        }
                            );
                    }

                    MatrixButton.BringContainerToFront();
                    MistakeMatrixButton.BringContainerToFront();


                    return(delegate
                    {
                        this.Title.Text = "...";

                        DisposeSorted.ToArray().ForEach(h => h());


                        MatrixButton.OrphanizeContainer();
                        MistakeMatrixButton.OrphanizeContainer();
                        RestartButton.OrphanizeContainer();
                    });
                }
                else
                {
                    var More = Comparision.Count(k => k.WaitingForUser && k.Value == null);

                    this.Title.Text = "Compare images #" + (1 + Current.X) + " above and #" + (1 + Current.Y) + " below. You have " + More + " image pairs to compare...";


                    var X = Source[Current.X];
                    var Y = Source[Current.Y];

                    X.ClickEnabled = false;
                    X.SizeTo(0.5).MoveContainerTo(100, 100).AttachContainerTo(this);

                    Y.ClickEnabled = false;
                    Y.SizeTo(0.5).MoveContainerTo(100, 300).AttachContainerTo(this);

                    #region Options
                    var Options = Values.Select(
                        (Value, Index) =>
                    {
                        var o7 = new TextButtonControl
                        {
                            Text   = "above is " + Value.Name + " than below (" + Value.ToString() + ")",
                            Width  = 400,
                            Height = 40,
                        }.AttachContainerTo(this).MoveContainerTo(350, 100 + 40 * Index);

                        o7.Content.FontSize = 20;

                        var o7bg = o7.Background.ToAnimatedOpacity();


                        o7.Background.Fill = Brushes.White;
                        o7bg.Opacity       = 0;

                        o7.Overlay.MouseEnter +=
                            delegate { o7bg.Opacity = 1; };


                        o7.Overlay.MouseLeave +=
                            delegate { o7bg.Opacity = 0; };


                        o7.Click +=
                            delegate
                        {
                            var NewComparision = Comparision.ToArray(
                                o =>
                            {
                                var n = new ComparisionInfo
                                {
                                    WaitingForUser = o.WaitingForUser,
                                    Value          = o.Value,
                                    X = o.X,
                                    Y = o.Y
                                };

                                if (o == Current)
                                {
                                    n.Value = Value;
                                }

                                if (o.Y == Current.X)
                                {
                                    if (o.X == Current.Y)
                                    {
                                        if (Value.InverseOf != null)
                                        {
                                            n.Value = Value.InverseOf;
                                        }
                                        else
                                        {
                                            var Inverse = Values.SingleOrDefault(k => k.InverseOf == Value);

                                            if (Inverse == null)
                                            {
                                                n.Value = Value;
                                            }
                                            else
                                            {
                                                n.Value = Inverse;
                                            }
                                        }
                                    }
                                }

                                return(n);
                            }
                                );

                            Step3_Compare(History, Source, NewComparision, Values);
                        };

                        return(o7);
                    }
                        ).ToArray();
                    #endregion

                    return(delegate
                    {
                        this.Title.Text = "...";

                        X.OrphanizeContainer();
                        Y.OrphanizeContainer();

                        Options.ForEach(k => k.OrphanizeContainer());
                        MatrixButton.OrphanizeContainer();
                    });
                }
            }
                );
        }