public ApplicationCanvas()
        {
            //var logo = new JSCSolutionsNETImage();
            //logo.AttachTo(this);
            var text = new TextBox
            {
                AcceptsReturn = true,
                BorderThickness = new Thickness(0),
                Background = Brushes.Transparent,
                Foreground = Brushes.Blue,
            };
            text.AttachTo(this);


            r.Fill = Brushes.Yellow;
            r.Opacity = 0.3;
            r.AttachTo(this);
            r.MoveTo(8, 8);

            this.SizeChanged += (s, e) =>
            {
                r.SizeTo(this.Width - 16, this.Height - 16);

                //logo.MoveTo(
                //    this.Width - logo.Width,
                //    this.Height - logo.Height
                //);
            };

            r.MouseEnter +=
                (s, e) =>
                {
                    text.Show();
                };

            r.MouseLeave +=
                (s, e) =>
                {
                    text.Hide();
                };


            r.MouseMove +=
                (s, e) =>
                {
                    var p = e.GetPosition(this);

                    text.Text = "jsc-solutions.net\n" + new { p.X, p.Y };

                    text.MoveTo(p.X + 32, p.Y);
                };
        }
        private void CreateContent(int sandcount, int cubecount, bool rotor)
        {
            var t = new TextBox
            {
                FontSize = 10,
                Text = "powered by jsc",
                BorderThickness = new Thickness(0),
                Foreground = 0xffffffff.ToSolidColorBrush(),
                Background = Brushes.Transparent,
                IsReadOnly = true,
                Width = Width
            }.MoveTo(8, 8).AttachTo(InfoContent);

            if (rotor)
                t.MoveTo(8, 32);

            var a = new AffineMesh();


            var _17 = new Avalon.Images._17().Source;
            var _17g = new Avalon.Images._17g().Source;
            var _18 = new Avalon.Images._18().Source;
            var _18g = new Avalon.Images._18g().Source;




            for (int cubex = -cubecount; cubex < cubecount; cubex++)
            {


                AddCube(a, _18, _18g, new AffinePoint(-1 + cubex * 4, -1, 0));
                AddCube(a, _18, _18g, new AffinePoint(-1 + cubex * 4, 1, 0));

                AddCube(a, _18, _18g, new AffinePoint(1 + cubex * 4, -1, 0));
                AddCube(a, _18, _18g, new AffinePoint(1 + cubex * 4, 1, 0));

                AddCube(a, _17, _17g, new AffinePoint(-1 + cubex * 4, -1, 1));
                AddCube(a, _17, _17g, new AffinePoint(-1 + cubex * 4, 1, 1));

                AddCube(a, _17, _17g, new AffinePoint(1 + cubex * 4, -1, 1));
                AddCube(a, _17, _17g, new AffinePoint(1 + cubex * 4, 1, 1));


            }


            var top = default(AffineMesh);

            if (rotor)
                top = AddCube(a, _18, _18g, new AffinePoint(0, 0, 2));

            var topdef = top;

            if (!rotor)
                for (int ix = -sandcount; ix <= sandcount; ix++)
                    for (int iy = -sandcount; iy <= sandcount; iy++)
                    {
                        AddCubeFace(a,
                            new Avalon.Images.sandv().Source,
                            new Avalon.Images.sandv().Source,
                            new AffinePoint(-100 + ix * 200, -100, -100 + iy * 200),
                            new AffinePoint(100 + ix * 200, -100, -100 + iy * 200),
                            new AffinePoint(-100 + ix * 200, -100, 100 + iy * 200),
                            new AffinePoint(100 + ix * 200, -100, 100 + iy * 200)
                        );
                    }





            //z			a = a.ToZoom(0.5);


            //a = a.ToZoom(0.8);
            //a = a.ToZoom(1.2);

            var _a = a;
            var Rotation = new AffineRotation
            {
                XY = (180 + 22).DegreesToRadians(),
                YZ = -22.DegreesToRadians(),
                XZ = 45.DegreesToRadians()
            };

            var MouseOffset0 = 0.0;
            var MouseOffset1 = 0.0;
            var MouseOffset2 = 0.0;

            var MouseMode = 0;

            this.MouseLeftButtonUp +=
                delegate
                {
                    MouseMode++;
                };

            this.MouseMove +=
                (sender, args) =>
                {

                    var pp = args.GetPosition(this);

                    if ((MouseMode % 4) == 1)
                    {
                        MouseOffset1 = pp.X;
                        Rotation = new AffineRotation
                        {
                            XZ = Rotation.XZ,
                            YZ = Rotation.YZ,

                            XY = 0.01 * (pp.X - MouseOffset0) * 2,

                        };
                    }

                    if ((MouseMode % 4) == 2)
                    {
                        MouseOffset2 = pp.X;
                        Rotation = new AffineRotation
                        {
                            XY = Rotation.XY,
                            YZ = Rotation.YZ,


                            XZ = 0.01 * (pp.X - MouseOffset1) * 2,

                        };
                    }

                    if ((MouseMode % 4) == 3)
                    {
                        MouseOffset0 = pp.X;
                        Rotation = new AffineRotation
                        {
                            XY = Rotation.XY,
                            XZ = Rotation.XZ,

                            YZ = 0.01 * (pp.X - MouseOffset2) * 2,

                        };
                    }

                };


            Action<int> nextframe = null;

            var sw2 = new Stopwatch();
            sw2.Start();

            nextframe =
                c =>
                {

                    sw2.Stop();

                    var sw = new Stopwatch();

                    sw.Start();

                    if (top != null)
                    {
                        a.Meshes.Remove(top);

                        top = topdef.ToTranslation(
                            new AffinePoint(0, -200 * 3, 0)
                        ).ToRotation(
                            new AffineRotation { XZ = 0.01 * c }
                        ).ToTranslation(
                            new AffinePoint(0, 200 * 3, 0)
                        );

                        a.Meshes.Add(top);
                    }



                    // rotate floor

                    if (rotor)
                    {
                        _a = a.ToZoom(0.5).ToRotation(Rotation);
                        Show(_a);
                    }
                    else
                        if (c == 1)
                        {
                            _a = a.ToZoom(0.5).ToRotation(Rotation);
                            Show(_a);
                        }

                    sw.Stop();

                    t.Text = new
                    {
                        rotor,
                        ShowCounter,
                        XY = Rotation.XY.RadiansToDegrees() % 360,
                        YZ = Rotation.YZ.RadiansToDegrees() % 360,
                        XZ = Rotation.XZ.RadiansToDegrees() % 360,
                        Renderer = sw.ElapsedMilliseconds + "ms",
                        Other = sw2.ElapsedMilliseconds + "ms"
                    }.ToString();

                    sw2 = new Stopwatch();
                    //sw2.Reset();
                    sw2.Start();
                    1.AtDelay(() => nextframe(c + 1));

                    //this.UpdateLayout();
                }
            ;

            1.AtDelay(() => nextframe(0));
        }
        public OrcasAvalonApplicationCanvas()
        {
            Width = DefaultWidth;
            Height = DefaultHeight;
            //Background = 0xffc0c0c0.ToSolidColorBrush();
            Background = Brushes.Black;

            var InfoOverlay = new Canvas
            {
                Width = DefaultWidth,
                Height = DefaultHeight,
            };


            var InfoOverlayShadow = new Rectangle
            {
                Width = DefaultWidth,
                Height = DefaultHeight,
                Fill = Brushes.Black
            }.AttachTo(InfoOverlay);

            var InfoOverlayShadowOpacity = InfoOverlay.ToAnimatedOpacity();
            InfoOverlayShadowOpacity.Opacity = 1;

            var InfoOverlayText = new TextBox
            {
                AcceptsReturn = true,
                IsReadOnly = true,
                Background = Brushes.Transparent,
                BorderThickness = new Thickness(0),
                Width = DefaultWidth,
                Height = 180,
                TextAlignment = TextAlignment.Center,
                FontSize = 30,
                Foreground = Brushes.White,
                Text = "The winner is\n the first player\n to get an unbroken row\n of five stones",
                FontFamily = new FontFamily("Verdana")

            }.MoveTo(0, (DefaultHeight - 180) / 2).AttachTo(InfoOverlay);



            var TouchOverlay = new Canvas
            {
                Width = DefaultWidth,
                Height = DefaultHeight,
                Background = Brushes.Yellow,
                Opacity = 0,
            };



            var Tiles = new Tile[Intersections * Intersections];

            #region WhereUnderAttack
            Func<int, int, IEnumerable<Tile>> WhereUnderAttack =
                (length, value) =>
                    Tiles.Where(
                        k =>
                        {
                            if (k.Value != 0)
                                return false;


                            return k.IsUnderAttack(value, length);
                        }
                    );
            #endregion

            #region AI
            Action AI =
                delegate
                {
                    // defensive rule:

                    var AttackWith4 = WhereUnderAttack(4, -1).FirstOrDefault();
                    if (AttackWith4 != null)
                    {
                        Console.WriteLine("AttackWith4");
                        AttackWith4.Value = -1;
                        return;
                    }

                    var AttackedBy4 = WhereUnderAttack(4, 1).FirstOrDefault();
                    if (AttackedBy4 != null)
                    {
                        Console.WriteLine("AttackedBy4");
                        AttackedBy4.Value = -1;
                        return;
                    }

                    var AttackWith3 = WhereUnderAttack(3, -1).FirstOrDefault();
                    if (AttackWith3 != null)
                    {
                        Console.WriteLine("AttackWith3");
                        AttackWith3.Value = -1;
                        return;
                    }

                    var AttackedBy3 = WhereUnderAttack(3, 1).FirstOrDefault();
                    if (AttackedBy3 != null)
                    {
                        Console.WriteLine("AttackedBy3");
                        AttackedBy3.Value = -1;
                        return;
                    }

                    var AttackWith2 = WhereUnderAttack(2, -1).FirstOrDefault();
                    if (AttackWith2 != null)
                    {
                        Console.WriteLine("AttackWith2");
                        AttackWith2.Value = -1;
                        return;
                    }

                    var AttackedBy2 = WhereUnderAttack(2, 1).FirstOrDefault();
                    if (AttackedBy2 != null)
                    {
                        Console.WriteLine("AttackedBy2");
                        AttackedBy2.Value = -1;
                        return;
                    }

                    var AttackedBy1 = WhereUnderAttack(1, 1).FirstOrDefault();
                    if (AttackedBy1 != null)
                    {
                        Console.WriteLine("AttackedBy1");
                        AttackedBy1.Value = -1;
                        return;
                    }



                    Console.WriteLine("Random");
                    Tiles.Where(k => k.Value == 0).Random().Value = -1;
                };
            #endregion

            var ResetOverlay = new Rectangle
            {
                Fill = Brushes.Yellow,
                Width = DefaultWidth,
                Height = DefaultHeight,
                Cursor = Cursors.Hand,
            };

            //9000.AtDelay(
            //    delegate
            //    {
            //        ResetOverlay.Orphanize();
            //        InfoOverlayShadowOpacity.Opacity = 0;
            //    }
            //);

            ResetOverlay.MouseLeftButtonUp +=
                delegate
                {
                    Tiles.ForEach(k => k.Value = 0);
                    ResetOverlay.Orphanize();
                    InfoOverlayShadowOpacity.Opacity = 0;
                };

            Action<Tile> Tiles_WithEvents =
                t =>
                {
                    #region add 2d awareness
                    t.ByOffset =
                        (ox, oy) =>
                        {
                            var x = t.X + ox;
                            var y = t.Y + oy;

                            if (x < 0)
                                return null;
                            if (y < 0)
                                return null;
                            if (x >= Intersections)
                                return null;
                            if (y >= Intersections)
                                return null;
                            return Tiles[x + y * Intersections];
                        };
                    #endregion

                    t.TouchOverlay.MouseLeftButtonUp +=
                        delegate
                        {
                            if (t.Value != 0)
                                return;

                            t.Value = 1;

                            if (AtClick != null)
                                AtClick();

                            // did we win?
                            if (Tiles.Any(
                                k =>
                                {
                                    if (k.Value != 1)
                                        return false;

                                    return k.IsUnderAttack(1, 4);
                                }
                            ))
                            {
                                InfoOverlayShadowOpacity.Opacity = 0.8;
                                InfoOverlayText.Text = "You won!\n\n:)";
                                ResetOverlay.AttachTo(TouchOverlay);


                                if (AtWin != null)
                                    AtWin();

                                return;
                            }

                            t.TouchOverlay.Hide();
                            100.AtDelay(
                                delegate
                                {
                                    AI();
                                    t.TouchOverlay.Show();


                                    if (Tiles.Any(
                                        k =>
                                        {
                                            if (k.Value != -1)
                                                return false;

                                            return k.IsUnderAttack(-1, 4);
                                        }
                                    ))
                                    {
                                        InfoOverlayShadowOpacity.Opacity = 0.8;
                                        InfoOverlayText.Text = "You lost!\n\n:(";
                                        ResetOverlay.AttachTo(TouchOverlay);


                                        if (AtLoss != null)
                                            AtLoss();

                                        return;
                                    }
                                }
                            );
                        };


                };

            var TileContainer = new Canvas().AttachTo(this);

            #region build board
            for (int x = 0; x < Intersections; x++)
                for (int y = 0; y < Intersections; y++)
                {

                    var t = new Tile(x, y);



                    t.Container.AttachTo(TileContainer);
                    t.TouchOverlay.AttachTo(TouchOverlay);

                    Tiles[x + y * Intersections] = t;
                    Tiles_WithEvents(t);
                }
            #endregion

            #region add logo
            var img = new com.abstractatech.gomoku.Avalon.Images.jsc
            {
            }.MoveTo(DefaultWidth - 96, DefaultHeight - 96).AttachTo(TileContainer);
            #endregion

            ResetOverlay.AttachTo(TouchOverlay);
            InfoOverlay.AttachTo(this);
            TouchOverlay.AttachTo(this);

            this.SizeChanged +=
                delegate
                {
                    TileContainer.MoveTo(
                        (this.Width - DefaultWidth) /2,
                        (this.Height - DefaultHeight) /2
                        );
                    TouchOverlay.MoveTo(
                     (this.Width - DefaultWidth) / 2,
                     (this.Height - DefaultHeight) / 2
                     );

                    ResetOverlay.SizeTo(this.Width, this.Height);
                    InfoOverlay.SizeTo(this.Width, this.Height);
                    InfoOverlayShadow.SizeTo(this.Width, this.Height);
                    //TouchOverlay.SizeTo(this.Width, this.Height);

                    InfoOverlayText.MoveTo(0, (this.Height - 180) / 2);
                    InfoOverlayText.SizeTo(this.Width, 180);


                };
        }
        public ApplicationCanvas()
        {
            {
                var r = new Rectangle();
                r.Fill = Brushes.Black;
                r.AttachTo(this);
                r.MoveTo(0, 0);
                r.Opacity = 0.9;
                this.SizeChanged += (s, e) => r.SizeTo(this.Width, this.Height / 2);
            }

            {
                var r = new Rectangle();
                r.Fill = Brushes.Black;
                r.AttachTo(this);

                this.SizeChanged += (s, e) => r.MoveTo(0, this.Height / 2).SizeTo(this.Width, this.Height / 2);
            }

            var VocabularyLines = Vocabulary.Trim().Split('\n');

            var v = VocabularyLines.Select(
                k =>
                {
                    var verbs = k.Split(':');

                    return new { A = verbs[0].Trim(), B = verbs[1].Trim() };
                }
            ).Randomize().AsCyclicEnumerator();


            var az = 0.5;
            var ax = 0.0;

            var ABCanvas = new Canvas().AttachTo(this);

            var A = new TextBox
            {
                BorderThickness = new Thickness(0),
                Background = Brushes.Transparent,
                TextAlignment = System.Windows.TextAlignment.Center,
                Foreground = Brushes.White,
                Text = "suur ettevõte",
                IsReadOnly = true,
                FontSize = 70
            };

            A.AttachTo(ABCanvas);

            var B = new TextBox
            {
                BorderThickness = new Thickness(0),
                Background = Brushes.Transparent,
                TextAlignment = System.Windows.TextAlignment.Center,
                Foreground = Brushes.White,
                Text = "large-scale enterprise",
                IsReadOnly = true,
                FontSize = 70
            };

            var MoveNextDisabled = false;
            Action MoveNext = delegate
            {
                if (MoveNextDisabled)
                    return;

                MoveNextDisabled = true;
                v.MoveNext();
                A.Text = v.Current.A;
                B.Text = v.Current.B;

                600.AtDelay(() => MoveNextDisabled = false);
            };

            MoveNext();

            B.AttachTo(ABCanvas);
            B.MoveTo(0, 64);

            Action Update =
                delegate
                {
          

                    if (Math.Abs(ax) > 0.4)
                        MoveNext();

                    az = Math.Min(1, az);
                    az = Math.Max(0, az);

                    var max = this.Height / 6;
                    var min = this.Height / 3;

                    // az = 1 is 0 degrees
                    // az = 0 is 90 degrees

                    az = 1 - az;

                    az -= 0.05;
                    az *= 10;

                    az = 1 - az;

                    az = Math.Min(1, az);
                    az = Math.Max(0, az);

                    //Console.WriteLine(new { az });

                    A.Opacity = Math.Pow(az, 10);
                    var bz = 1 - az;

                    B.Opacity = Math.Pow(bz, 10);

                    A.MoveTo(0, min + az * max - 16).SizeTo(this.Width, 100);
                    B.MoveTo(0, min + (1 - az) * max - 16).SizeTo(this.Width, 100);

                    ABCanvas.MoveTo(this.Width * ax * 0.1, 0);

                };

            this.SizeChanged +=
                delegate
                {
                    Update();
                };

            this.MouseMove += (sender, args) =>
            {
                var p = args.GetPosition(this);

                az = (p.Y / this.Height);
                ax = -1 * ((p.X / this.Width) - 0.5);

                Update();
            };

            this.TouchMove += (sender, args) =>
            {
                var p = args.GetTouchPoint(this).Position;
                az = (p.Y / this.Height);
                ax = -1 * ((p.X / this.Width) - 0.5);

                Update();
            };


            this.Accelerate = (x, y, z) =>
            {
                az = z;
                ax = x;
                Update();
            };
        }
        static void Main(string[] args)
        {
            var c = new JSCSolutionsNETCarouselCanvas
            {
                CloseOnClick = false
            };

            c.HideSattelites();


            //c.Container.Effect = new DropShadowEffect();
            //c.Container.BitmapEffect = new DropShadowBitmapEffect();

            //.MoveTo(0, ImageCarouselCanvas.DefaultHeight - 96).SizeTo(ImageCarouselCanvas.DefaultWidth, 96);

            var cc = new Canvas();


            // http://cloudstore.blogspot.com/2008/05/creating-custom-window-style.html
            var wcam = new Window();

            wcam.Background  = Brushes.Transparent;
            wcam.WindowStyle = WindowStyle.None;
            wcam.ResizeMode  = ResizeMode.NoResize;
            wcam.SizeTo(200, 200);
            wcam.AllowsTransparency = true;
            //wcam.Opacity = 0.5;
            wcam.ShowInTaskbar = false;
            wcam.Cursor        = Cursors.Hand;
            wcam.Focusable     = false;
            wcam.Topmost       = true;

            var w = cc.ToWindow();


            w.SizeToContent = SizeToContent.Manual;
            //w.SizeTo(400, 400);
            //w.ToTransparentWindow();


            // http://blog.joachim.at/?p=39
            // http://blogs.msdn.com/changov/archive/2009/01/19/webbrowser-control-on-transparent-wpf-window.aspx
            // http://blogs.interknowlogy.com/johnbowen/archive/2007/06/20/20458.aspx
            w.AllowsTransparency = true;
            w.WindowStyle        = System.Windows.WindowStyle.None;
            w.Focusable          = false;

            //w.Background = new SolidColorBrush(Color.FromArgb(0x20, 0, 0, 0));
            w.Background            = Brushes.Transparent;
            w.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            w.Topmost = true;
            //w.ShowInTaskbar = false;

            var winfoc = new Canvas();



            var winfo = winfoc.ToWindow();

            winfo.AllowsTransparency = true;
            winfo.ShowInTaskbar      = false;
            winfo.WindowStyle        = WindowStyle.None;
            //winfo.Background = Brushes.Transparent;
            winfo.Background = Brushes.Red;
            winfo.Opacity    = 0.3;

            winfo.ResizeMode = ResizeMode.NoResize;

            winfo.SizeToContent = SizeToContent.Manual;
            winfo.Topmost       = true;
            // http://www.squidoo.com/youtubehd



            var NextInputModeEnabled        = false;
            var NextInputModeKeyDownEnabled = false;

            Action <Key> NextInputModeKeyDown = delegate { };

            var CommandKeysEnabled = false;

            #region TopicText
            var TopicText = new System.Windows.Controls.TextBox
            {
                //IsReadOnly = true,
                Background      = Brushes.Transparent,
                BorderThickness = new System.Windows.Thickness(0),
                Foreground      = Brushes.White,
                Effect          = new DropShadowEffect(),
                Text            = "JSC C# Foo Bar",
                //TextDecorations = TextDecorations.Underline,
                FontFamily    = new FontFamily("Verdana"),
                FontSize      = 24,
                TextAlignment = System.Windows.TextAlignment.Right
            };
            #endregion


            #region KeyDown
            InterceptKeys.KeyDown +=
                key =>
            {
                if (key == Key.LeftShift)
                {
                    //w.Background = new SolidColorBrush(Color.FromArgb(2, 0, 0, 0));
                    //w.MakeInteractive(true);
                    NextInputModeEnabled = true;
                }
                else
                {
                    if (NextInputModeEnabled || NextInputModeKeyDownEnabled)
                    {
                        NextInputModeKeyDownEnabled = false;
                        NextInputModeKeyDown(key);
                    }

                    NextInputModeEnabled = false;
                }
            };
            #endregion

            #region KeyUp
            InterceptKeys.KeyUp +=
                key =>
            {
                if (key == Key.CapsLock)
                {
                    CommandKeysEnabled   = !CommandKeysEnabled;
                    TopicText.IsReadOnly = CommandKeysEnabled;
                    TopicText.Select(0, 0);
                }

                if (key == Key.LeftShift)
                {
                    NextInputModeKeyDownEnabled = false;
                }
                else
                {
                }

                NextInputModeEnabled = false;
            };
            #endregion


            var s = 7;

            var ThumbnailSize           = 0.4;
            var CaptionBackgroundHeight = 24;

            #region UpdateChildren
            Action UpdateChildren =
                delegate
            {
                if (w.ActualWidth == 0)
                {
                    return;
                }

                var ss  = s;
                var ss2 = 0;


                Console.WriteLine(
                    new { w.Left, w.Top });

                winfo.MoveTo(w.Left, w.Top).SizeTo(w.ActualWidth, w.ActualHeight);

                if (ThumbnailSize == 1)
                {
                    wcam.Background = Brushes.Black;
                    ss = 0;

                    var qw = w.ActualWidth - ss * 2;
                    var qh = w.ActualHeight - ss * 2;

                    // no status bars or menues please :)

                    wcam.MoveTo(
                        w.Left + ss + ss2,
                        w.Top + (w.ActualHeight - qh * ThumbnailSize - ss) - ss2
                        ).SizeTo(
                        qw * ThumbnailSize,
                        qh * ThumbnailSize
                        );
                }
                else
                {
                    wcam.Background = Brushes.Transparent;

                    //if (w.WindowState == WindowState.Maximized)
                    //{
                    //    ss2 = s;
                    //}

                    var qw = w.ActualWidth - ss * 2;
                    var qh = w.ActualHeight - ss * 2;



                    wcam.MoveTo(w.Left + ss + ss2, w.Top + (w.ActualHeight - qh * ThumbnailSize - ss) - ss2).SizeTo(qw * ThumbnailSize, qh * ThumbnailSize);
                }
            };
            #endregion


            w.LocationChanged +=
                delegate
            {
                UpdateChildren();
            };



            var Borders = Enumerable.Range(1, s * 2).Reverse().Select(
                Width =>
                new
            {
                Width = Width * 2,
                Left  = new Rectangle {
                    Fill = Brushes.Black, Opacity = 0.06
                }.MoveTo(0, 0).AttachTo(winfoc),
                Right = new Rectangle {
                    Fill = Brushes.Black, Opacity = 0.06
                }.MoveTo(0, 0).AttachTo(winfoc),
                Bottom = new Rectangle {
                    Fill = Brushes.Black, Opacity = 0.03
                }.MoveTo(0, 0).AttachTo(winfoc),
                Top = new Rectangle {
                    Fill = Brushes.Black, Opacity = 0.11
                }.MoveTo(0, 0).AttachTo(winfoc)
            }
                ).ToArray();

            var CaptionBackgroundOverlay = new Rectangle
            {
                Fill    = Brushes.Black,
                Opacity = 0.02,
            }.AttachTo(cc);

            var CaptionSysMenuOverlay = new Rectangle
            {
                Fill    = Brushes.Black,
                Opacity = 0.02,
            }.AttachTo(cc).SizeTo(CaptionBackgroundHeight * 4, CaptionBackgroundHeight);

            var ExtraBorderTop = new Rectangle
            {
                Fill    = Brushes.Black,
                Opacity = 0.0,
            }.AttachTo(winfoc);

            var ExtraBorderBottom = new Rectangle
            {
                Fill    = Brushes.Black,
                Opacity = 0.0,
            }.AttachTo(winfoc);

            var CaptionClose = new TextBox
            {
                Foreground      = Brushes.Red,
                FontFamily      = new FontFamily("Webdings"),
                Text            = "r",
                Background      = Brushes.Transparent,
                BorderThickness = new Thickness(0),
                TextAlignment   = System.Windows.TextAlignment.Center,
                Opacity         = 0.5
            }.AttachTo(winfoc);


            var CaptionText = new System.Windows.Controls.TextBox
            {
                IsReadOnly      = true,
                Background      = Brushes.Transparent,
                BorderThickness = new System.Windows.Thickness(0),
                Foreground      = Brushes.White,
                Effect          = new System.Windows.Media.Effects.DropShadowEffect(),
                Text            = "jsc-solutions.net",
                //TextDecorations = TextDecorations.Underline,
                FontFamily    = new FontFamily("Verdana"),
                FontSize      = 16,
                TextAlignment = System.Windows.TextAlignment.Right
            }
            .AttachTo(winfoc);

            TopicText.AttachTo(winfoc);

            #region SetCaption
            Action <string> SetCaption =
                text =>
            {
                if (string.IsNullOrEmpty(text))
                {
                    CaptionText.Text = "jsc-solutions.net";
                }
                else
                {
                    CaptionText.Text = text + " | jsc-solutions.net";
                }
            };
            #endregion



            c.AttachContainerTo(winfoc);

            var ExtraBorderSize = 0.10;


            var Intro = new PromotionBrandIntro.ApplicationCanvas().AttachTo(winfoc);
            Intro.Opacity = 0;

            #region SizeChanged
            Action SizeChanged =
                delegate
            {
                Intro.SizeTo(w.ActualWidth, w.ActualHeight);
                //ink.SizeTo(w.ActualWidth, w.ActualHeight - CaptionBackgroundHeight);

                var CaptionWidth = 200;

                CaptionBackgroundOverlay.MoveTo(w.ActualWidth - CaptionWidth, 0).SizeTo(CaptionWidth, CaptionBackgroundHeight);


                ExtraBorderTop.MoveTo(0, 0).SizeTo(w.ActualWidth, w.ActualHeight * ExtraBorderSize);
                ExtraBorderBottom.MoveTo(0, w.ActualHeight * (1 - ExtraBorderSize)).SizeTo(w.ActualWidth, w.ActualHeight * ExtraBorderSize);

                TopicText.MoveTo(
                    0,
                    w.ActualHeight - 48
                    ).SizeTo(w.ActualWidth - 48, 48);


                if (c != null)
                {
                    c.MoveContainerTo(-200 + 42, -200 + 38);
                }
                Borders.WithEach(k => k.Left.MoveTo(0, 0).SizeTo(k.Width, w.ActualHeight));
                Borders.WithEach(k => k.Right.MoveTo(w.ActualWidth - k.Width, 0).SizeTo(k.Width, w.ActualHeight));
                Borders.WithEach(k => k.Bottom.MoveTo(0, w.ActualHeight - k.Width).SizeTo(w.ActualWidth, k.Width));
                Borders.WithEach(k => k.Top.MoveTo(0, 0).SizeTo(w.ActualWidth, k.Width));
                CaptionText.MoveTo(0, 2).SizeTo(w.ActualWidth - CaptionBackgroundHeight, 32);
                CaptionClose.MoveTo(w.ActualWidth - CaptionBackgroundHeight, s).SizeTo(CaptionBackgroundHeight - s, CaptionBackgroundHeight - s);

                UpdateChildren();
            };
            #endregion

            w.SizeChanged +=
                delegate
            {
                SizeChanged();
            };

            w.StateChanged +=
                delegate
            {
                if (w.WindowState == WindowState.Maximized)
                {
                    w.WindowState = WindowState.Normal;
                }

                SizeChanged();
            };



            #region GetWindows
            Func <IEnumerable <Internal.Window> > GetWindows =
                delegate
            {
                var windows = new List <Internal.Window>();
                Internal.EnumWindows(
                    (IntPtr hwnd, int lParam) =>
                {
                    if (new WindowInteropHelper(wcam).Handle != hwnd &&
                        (Internal.GetWindowLongA(hwnd, Internal.GWL_STYLE) & Internal.TARGETWINDOW) == Internal.TARGETWINDOW
                        )
                    {
                        StringBuilder sb = new StringBuilder(100);
                        Internal.GetWindowText(hwnd, sb, sb.Capacity);

                        windows.Add(
                            new Internal.Window
                        {
                            Handle = hwnd,
                            Title  = sb.ToString()
                        }
                            );
                    }

                    return(true);        //continue enumeration
                }
                    , 0);

                return(windows.OrderBy(k => k.Title));
            };
            #endregion

            var ResetThumbnailSkip = 0;

            Func <Internal.Window> GetCurrentThumbnail = () => GetWindows().AsCyclicEnumerable().Skip(ResetThumbnailSkip).First();

            #region AnimationCompleted
            Intro.AnimationCompleted +=
                delegate
            {
                if (c == null)
                {
                    c = new JSCSolutionsNETCarouselCanvas
                    {
                        CloseOnClick = false
                    }.AttachContainerTo(winfoc);
                    c.HideSattelites();

                    CaptionClose.Show();
                    SizeChanged();
                }
            };
            #endregion


            wcam.SourceInitialized +=
                delegate
            {
                {
                    wcam.MakeInteractive(false);

                    UpdateChildren();
                }



                var thumb = IntPtr.Zero;
                ResetThumbnailSkip = GetWindows().Where(
                    k =>
                    k.Title.Contains("Chrome") ||
                    k.Title.Contains("Studio") ||
                    k.Title.Contains("Minefield")

                    ).TakeWhile(k => k.Handle != Internal.GetForegroundWindow()).Count();


                #region ResetThumbnail
                Action ResetThumbnail =
                    delegate
                {
                    GetCurrentThumbnail().With(
                        shadow =>
                    {
                        //t.Text = shadow.Title;

                        if (thumb != IntPtr.Zero)
                        {
                            Internal.DwmUnregisterThumbnail(thumb);
                        }

                        int i = Internal.DwmRegisterThumbnail(
                            new WindowInteropHelper(wcam).Handle, shadow.Handle, out thumb);

                        #region UpdateThumbnail
                        Action UpdateThumbnail =
                            delegate
                        {
                            if (thumb != IntPtr.Zero)
                            {
                                Internal.PSIZE size;
                                Internal.DwmQueryThumbnailSourceSize(thumb, out size);

                                Internal.DWM_THUMBNAIL_PROPERTIES props = new Internal.DWM_THUMBNAIL_PROPERTIES();

                                props.fVisible              = true;
                                props.dwFlags               = Internal.DWM_TNP_VISIBLE | Internal.DWM_TNP_RECTDESTINATION | Internal.DWM_TNP_OPACITY | Internal.DWM_TNP_SOURCECLIENTAREAONLY;
                                props.opacity               = (byte)((byte)(0x7F) + (byte)((0x80) * (ThumbnailSize)));
                                props.rcDestination         = new Internal.Rect(0, 0, (int)wcam.ActualWidth, (int)wcam.ActualHeight);
                                props.fSourceClientAreaOnly = true;

                                if (size.x < wcam.ActualWidth)
                                {
                                    props.rcDestination.Right = props.rcDestination.Left + size.x;

                                    props.rcDestination.Left  += ((int)wcam.ActualWidth - size.x) / 2;
                                    props.rcDestination.Right += ((int)wcam.ActualWidth - size.x) / 2;
                                }

                                if (size.y < wcam.ActualHeight)
                                {
                                    props.rcDestination.Bottom = props.rcDestination.Top + size.y;

                                    props.rcDestination.Top    += ((int)wcam.ActualHeight - size.y) / 2;
                                    props.rcDestination.Bottom += ((int)wcam.ActualHeight - size.y) / 2;
                                }



                                Internal.DwmUpdateThumbnailProperties(thumb, ref props);
                            }
                        };
                        #endregion


                        (1000 / 15).AtInterval(UpdateThumbnail);

                        wcam.SizeChanged += delegate { UpdateThumbnail(); };
                    }
                        );
                };
                #endregion



                ResetThumbnail();

                NextInputModeKeyDown +=
                    key =>
                {
                    if (key == Key.RightShift)
                    {
                        NextInputModeKeyDownEnabled = true;

                        if (c != null)
                        {
                            CaptionClose.Hide();
                            c.AtClose +=
                                delegate
                            {
                                c.OrphanizeContainer();
                            };
                            c.Close();
                            c = null;
                        }

                        Intro.Background      = Brushes.Transparent;
                        Intro.Overlay.Opacity = 1;
                        Intro.FadeIn(
                            delegate
                        {
                            2000.AtDelay(Intro.PrepareAnimation());
                        }
                            );
                    }

                    if (!CommandKeysEnabled)
                    {
                        if (key == Key.F2)
                        {
                            w.Activate();

                            TopicText.Focusable = true;
                            TopicText.Focus();
                            TopicText.SelectAll();
                        }
                        return;
                    }

                    if (key == Key.Right)
                    {
                        if (w.IsActive)
                        {
                            GetCurrentThumbnail().Activate();
                        }
                        else
                        {
                            NextInputModeKeyDownEnabled = true;
                            ResetThumbnailSkip          = GetWindows().TakeWhile(k => k.Handle != Internal.GetForegroundWindow()).Count();
                            ResetThumbnail();
                        }
                    }

                    if (key == Key.Up)
                    {
                        NextInputModeKeyDownEnabled = true;

                        if (ThumbnailSize < 0.3)
                        {
                            ThumbnailSize = 0.3;
                        }
                        else if (ThumbnailSize < 0.5)
                        {
                            ThumbnailSize = 0.5;
                        }
                        else if (ThumbnailSize < 1)
                        {
                            ThumbnailSize = 1;
                        }
                        else
                        {
                            if (c != null)
                            {
                                CaptionClose.Hide();
                                c.AtClose +=
                                    delegate
                                {
                                    c.OrphanizeContainer();
                                };
                                c.Close();
                                c = null;
                            }
                        }


                        UpdateChildren();
                    }
                    if (key == Key.Down)
                    {
                        NextInputModeKeyDownEnabled = true;
                        if (c == null)
                        {
                            c = new JSCSolutionsNETCarouselCanvas
                            {
                                CloseOnClick = false
                            }.AttachContainerTo(winfoc);
                            CaptionClose.Show();
                        }
                        else if (ThumbnailSize > 0.5)
                        {
                            ThumbnailSize = 0.5;
                        }
                        else if (ThumbnailSize > 0.3)
                        {
                            ThumbnailSize = 0.3;
                        }
                        else if (ThumbnailSize == 0.3)
                        {
                            ThumbnailSize = 0;
                        }

                        SizeChanged();
                    }



                    if (key == Key.Left)
                    {
                        if (w.IsActive)
                        {
                            NextInputModeKeyDownEnabled = true;
                            if (ExtraBorderTop.Opacity == 1)
                            {
                                ExtraBorderTop.Opacity    = 0;
                                ExtraBorderBottom.Opacity = 0;
                            }
                            else
                            {
                                ExtraBorderTop.Opacity    = 1;
                                ExtraBorderBottom.Opacity = 1;
                            }
                        }
                    }
                };
            };

            winfo.SourceInitialized +=
                delegate
            {
                winfo.MakeInteractive(false);
            };

            w.SourceInitialized +=
                delegate
            {
                // http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/61e93dca-e24c-4953-9719-22ce3f705353
                Matrix m  = PresentationSource.FromVisual(w).CompositionTarget.TransformToDevice;
                double dx = m.M11;
                double dy = m.M22;


                w.SizeTo(1280 / dx, 768 / dy);
                w.MoveTo(8, 0);
                SizeChanged();

                wcam.Owner  = w;
                winfo.Owner = w;
                wcam.Show();
                winfo.Show();

                HwndSource hwndSource = (HwndSource)HwndSource.FromVisual(w);
                hwndSource.AddHook(
                    (IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handeled) =>
                {
                    if (msg == 0x0084)         // WM_NCHITTEST
                    {
                        //http://agsmith.wordpress.com/2008/09/16/hit-testing-in-wpf/

                        var p = new
                        {
                            cx = w.ActualWidth - (Internal.LOWORD(lParam) - w.Left),
                            cy = w.ActualHeight - (Internal.HIWORD(lParam) - w.Top),
                            x  = Internal.LOWORD(lParam) - w.Left,
                            y  = Internal.HIWORD(lParam) - w.Top
                        };


                        CaptionText.Text = p.ToString();

                        handeled = true;

                        return((IntPtr)HitTestValues.HTCAPTION);        // HTCAPTION


                        if (false)
                        {
                            if (p.x < s)
                            {
                                if (p.y < s)
                                {
                                    return((IntPtr)HitTestValues.HTTOPLEFT);        // HTCAPTION
                                }
                            }
                            if (p.x < CaptionBackgroundHeight)
                            {
                                if (p.y < CaptionBackgroundHeight)
                                {
                                    return((IntPtr)HitTestValues.HTSYSMENU);        // HTCAPTION
                                }
                            }
                            if (p.cx < CaptionBackgroundHeight)
                            {
                                if (p.y < CaptionBackgroundHeight)
                                {
                                    return((IntPtr)HitTestValues.HTCLOSE);        // HTCAPTION
                                }
                            }
                            if (p.cx < s)
                            {
                                if (p.cy < s)
                                {
                                    return((IntPtr)HitTestValues.HTBOTTOMRIGHT);        // HTCAPTION
                                }
                            }
                            if (p.cx < s)
                            {
                                if (p.y < s)
                                {
                                    return((IntPtr)HitTestValues.HTTOPRIGHT);        // HTCAPTION
                                }
                            }
                            if (p.x < s)
                            {
                                if (p.cy < s)
                                {
                                    return((IntPtr)HitTestValues.HTBOTTOMLEFT);        // HTCAPTION
                                }
                            }
                            if (p.x < s)
                            {
                                return((IntPtr)HitTestValues.HTLEFT);        // HTCAPTION
                            }
                            if (p.y < s)
                            {
                                return((IntPtr)HitTestValues.HTTOP);        // HTCAPTION
                            }
                            if (p.cx < s)
                            {
                                return((IntPtr)HitTestValues.HTRIGHT);        // HTCAPTION
                            }
                            if (p.cy < s)
                            {
                                return((IntPtr)HitTestValues.HTBOTTOM);        // HTCAPTION
                            }
                            if (p.y < CaptionBackgroundHeight)
                            {
                                return((IntPtr)HitTestValues.HTCAPTION);        // HTCAPTION
                            }
                            return((IntPtr)HitTestValues.HTTRANSPARENT);        // HTCAPTION
                        }
                    }
                    return(IntPtr.Zero);
                }

                    );
            };



            InterceptKeys.InternalMain(
                Rehook =>
            {
                w.Deactivated +=
                    delegate
                {
                    TopicText.Focusable = false;
                };
                w.Activated +=
                    delegate
                {
                    //TopicText.Focus();
                    Rehook();
                };

                w.ShowDialog();
            }
                );
        }
        static void Main(string[] args)
        {
            var c = new JSCSolutionsNETCarouselCanvas
                {
                    CloseOnClick = false
                };

            c.HideSattelites();


            //c.Container.Effect = new DropShadowEffect();
            //c.Container.BitmapEffect = new DropShadowBitmapEffect();

            //.MoveTo(0, ImageCarouselCanvas.DefaultHeight - 96).SizeTo(ImageCarouselCanvas.DefaultWidth, 96);

            var cc = new Canvas();


            // http://cloudstore.blogspot.com/2008/05/creating-custom-window-style.html
            var wcam = new Window();
            wcam.Background = Brushes.Transparent;
            wcam.WindowStyle = WindowStyle.None;
            wcam.ResizeMode = ResizeMode.NoResize;
            wcam.SizeTo(200, 200);
            wcam.AllowsTransparency = true;
            //wcam.Opacity = 0.5;
            wcam.ShowInTaskbar = false;
            wcam.Cursor = Cursors.Hand;
            wcam.Focusable = false;
            wcam.Topmost = true;

            var w = cc.ToWindow();


            w.SizeToContent = SizeToContent.Manual;
            //w.SizeTo(400, 400);
            //w.ToTransparentWindow();


            // http://blog.joachim.at/?p=39
            // http://blogs.msdn.com/changov/archive/2009/01/19/webbrowser-control-on-transparent-wpf-window.aspx
            // http://blogs.interknowlogy.com/johnbowen/archive/2007/06/20/20458.aspx
            w.AllowsTransparency = true;
            w.WindowStyle = System.Windows.WindowStyle.None;
            w.Focusable = false;

            //w.Background = new SolidColorBrush(Color.FromArgb(0x20, 0, 0, 0));
            w.Background = Brushes.Transparent;
            w.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            w.Topmost = true;
            //w.ShowInTaskbar = false;

            var winfoc = new Canvas();



            var winfo = winfoc.ToWindow();

            winfo.AllowsTransparency = true;
            winfo.ShowInTaskbar = false;
            winfo.WindowStyle = WindowStyle.None;
            //winfo.Background = Brushes.Transparent;
            winfo.Background = Brushes.Red;
            winfo.Opacity = 0.3;

            winfo.ResizeMode = ResizeMode.NoResize;

            winfo.SizeToContent = SizeToContent.Manual;
            winfo.Topmost = true;
            // http://www.squidoo.com/youtubehd









            var NextInputModeEnabled = false;
            var NextInputModeKeyDownEnabled = false;

            Action<Key> NextInputModeKeyDown = delegate { };

            var CommandKeysEnabled = false;

            #region TopicText
            var TopicText = new System.Windows.Controls.TextBox
            {
                //IsReadOnly = true,
                Background = Brushes.Transparent,
                BorderThickness = new System.Windows.Thickness(0),
                Foreground = Brushes.White,
                Effect = new DropShadowEffect(),
                Text = "JSC C# Foo Bar",
                //TextDecorations = TextDecorations.Underline,
                FontFamily = new FontFamily("Verdana"),
                FontSize = 24,
                TextAlignment = System.Windows.TextAlignment.Right
            };
            #endregion


            #region KeyDown
            InterceptKeys.KeyDown +=
                key =>
                {
                    if (key == Key.LeftShift)
                    {
                        //w.Background = new SolidColorBrush(Color.FromArgb(2, 0, 0, 0));
                        //w.MakeInteractive(true);
                        NextInputModeEnabled = true;
                    }
                    else
                    {

                        if (NextInputModeEnabled || NextInputModeKeyDownEnabled)
                        {

                            NextInputModeKeyDownEnabled = false;
                            NextInputModeKeyDown(key);

                        }

                        NextInputModeEnabled = false;
                    }
                };
            #endregion

            #region KeyUp
            InterceptKeys.KeyUp +=
                key =>
                {
                    if (key == Key.CapsLock)
                    {
                        CommandKeysEnabled = !CommandKeysEnabled;
                        TopicText.IsReadOnly = CommandKeysEnabled;
                        TopicText.Select(0, 0);
                    }

                    if (key == Key.LeftShift)
                    {
                        NextInputModeKeyDownEnabled = false;
                    }
                    else
                    {

                    }

                    NextInputModeEnabled = false;
                };
            #endregion


            var s = 7;

            var ThumbnailSize = 0.4;
            var CaptionBackgroundHeight = 24;

            #region UpdateChildren
            Action UpdateChildren =
                delegate
                {
                    if (w.ActualWidth == 0)
                        return;

                    var ss = s;
                    var ss2 = 0;


                    Console.WriteLine(
                        new { w.Left, w.Top });

                    winfo.MoveTo(w.Left, w.Top).SizeTo(w.ActualWidth, w.ActualHeight);

                    if (ThumbnailSize == 1)
                    {
                        wcam.Background = Brushes.Black;
                        ss = 0;

                        var qw = w.ActualWidth - ss * 2;
                        var qh = w.ActualHeight - ss * 2;

                        // no status bars or menues please :)

                        wcam.MoveTo(
                            w.Left + ss + ss2,
                            w.Top + (w.ActualHeight - qh * ThumbnailSize - ss) - ss2
                        ).SizeTo(
                            qw * ThumbnailSize,
                            qh * ThumbnailSize
                        );

                    }
                    else
                    {
                        wcam.Background = Brushes.Transparent;

                        //if (w.WindowState == WindowState.Maximized)
                        //{
                        //    ss2 = s;
                        //}

                        var qw = w.ActualWidth - ss * 2;
                        var qh = w.ActualHeight - ss * 2;



                        wcam.MoveTo(w.Left + ss + ss2, w.Top + (w.ActualHeight - qh * ThumbnailSize - ss) - ss2).SizeTo(qw * ThumbnailSize, qh * ThumbnailSize);

                    }


                };
            #endregion


            w.LocationChanged +=
                delegate
                {
                    UpdateChildren();
                };



            var Borders = Enumerable.Range(1, s * 2).Reverse().Select(
                Width =>
                new
                {
                    Width = Width * 2,
                    Left = new Rectangle { Fill = Brushes.Black, Opacity = 0.06 }.MoveTo(0, 0).AttachTo(winfoc),
                    Right = new Rectangle { Fill = Brushes.Black, Opacity = 0.06 }.MoveTo(0, 0).AttachTo(winfoc),
                    Bottom = new Rectangle { Fill = Brushes.Black, Opacity = 0.03 }.MoveTo(0, 0).AttachTo(winfoc),
                    Top = new Rectangle { Fill = Brushes.Black, Opacity = 0.11 }.MoveTo(0, 0).AttachTo(winfoc)
                }
            ).ToArray();

            var CaptionBackgroundOverlay = new Rectangle
            {
                Fill = Brushes.Black,
                Opacity = 0.02,
            }.AttachTo(cc);

            var CaptionSysMenuOverlay = new Rectangle
            {
                Fill = Brushes.Black,
                Opacity = 0.02,
            }.AttachTo(cc).SizeTo(CaptionBackgroundHeight * 4, CaptionBackgroundHeight);

            var ExtraBorderTop = new Rectangle
            {
                Fill = Brushes.Black,
                Opacity = 0.0,
            }.AttachTo(winfoc);

            var ExtraBorderBottom = new Rectangle
            {
                Fill = Brushes.Black,
                Opacity = 0.0,
            }.AttachTo(winfoc);

            var CaptionClose = new TextBox
            {
                Foreground = Brushes.Red,
                FontFamily = new FontFamily("Webdings"),
                Text = "r",
                Background = Brushes.Transparent,
                BorderThickness = new Thickness(0),
                TextAlignment = System.Windows.TextAlignment.Center,
                Opacity = 0.5
            }.AttachTo(winfoc);


            var CaptionText = new System.Windows.Controls.TextBox
            {
                IsReadOnly = true,
                Background = Brushes.Transparent,
                BorderThickness = new System.Windows.Thickness(0),
                Foreground = Brushes.White,
                Effect = new System.Windows.Media.Effects.DropShadowEffect(),
                Text = "jsc-solutions.net",
                //TextDecorations = TextDecorations.Underline,
                FontFamily = new FontFamily("Verdana"),
                FontSize = 16,
                TextAlignment = System.Windows.TextAlignment.Right
            }
            .AttachTo(winfoc);

            TopicText.AttachTo(winfoc);

            #region SetCaption
            Action<string> SetCaption =
                text =>
                {
                    if (string.IsNullOrEmpty(text))
                        CaptionText.Text = "jsc-solutions.net";
                    else
                        CaptionText.Text = text + " | jsc-solutions.net";

                };
            #endregion




            c.AttachContainerTo(winfoc);

            var ExtraBorderSize = 0.10;


            var Intro = new PromotionBrandIntro.ApplicationCanvas().AttachTo(winfoc);
            Intro.Opacity = 0;

            #region SizeChanged
            Action SizeChanged =
                delegate
                {
                    Intro.SizeTo(w.ActualWidth, w.ActualHeight);
                    //ink.SizeTo(w.ActualWidth, w.ActualHeight - CaptionBackgroundHeight);

                    var CaptionWidth = 200;

                    CaptionBackgroundOverlay.MoveTo(w.ActualWidth - CaptionWidth, 0).SizeTo(CaptionWidth, CaptionBackgroundHeight);


                    ExtraBorderTop.MoveTo(0, 0).SizeTo(w.ActualWidth, w.ActualHeight * ExtraBorderSize);
                    ExtraBorderBottom.MoveTo(0, w.ActualHeight * (1 - ExtraBorderSize)).SizeTo(w.ActualWidth, w.ActualHeight * ExtraBorderSize);

                    TopicText.MoveTo(
                        0,
                        w.ActualHeight - 48
                    ).SizeTo(w.ActualWidth - 48, 48);

                   
                    if (c != null)
                        c.MoveContainerTo(-200 + 42, -200 + 38);
                    Borders.WithEach(k => k.Left.MoveTo(0, 0).SizeTo(k.Width, w.ActualHeight));
                    Borders.WithEach(k => k.Right.MoveTo(w.ActualWidth - k.Width, 0).SizeTo(k.Width, w.ActualHeight));
                    Borders.WithEach(k => k.Bottom.MoveTo(0, w.ActualHeight - k.Width).SizeTo(w.ActualWidth, k.Width));
                    Borders.WithEach(k => k.Top.MoveTo(0, 0).SizeTo(w.ActualWidth, k.Width));
                    CaptionText.MoveTo(0, 2).SizeTo(w.ActualWidth - CaptionBackgroundHeight, 32);
                    CaptionClose.MoveTo(w.ActualWidth - CaptionBackgroundHeight, s).SizeTo(CaptionBackgroundHeight - s, CaptionBackgroundHeight - s);

                    UpdateChildren();
                };
            #endregion

            w.SizeChanged +=
                delegate
                {
                    SizeChanged();
                };

            w.StateChanged +=
                delegate
                {
                    if (w.WindowState == WindowState.Maximized)
                        w.WindowState = WindowState.Normal;

                    SizeChanged();
                };



            #region GetWindows
            Func<IEnumerable<Internal.Window>> GetWindows =
                delegate
                {
                    var windows = new List<Internal.Window>();
                    Internal.EnumWindows(
                        (IntPtr hwnd, int lParam) =>
                        {
                            if (new WindowInteropHelper(wcam).Handle != hwnd
                                && (Internal.GetWindowLongA(hwnd, Internal.GWL_STYLE) & Internal.TARGETWINDOW) == Internal.TARGETWINDOW
                                )
                            {
                                StringBuilder sb = new StringBuilder(100);
                                Internal.GetWindowText(hwnd, sb, sb.Capacity);

                                windows.Add(
                                    new Internal.Window
                                    {
                                        Handle = hwnd,
                                        Title = sb.ToString()
                                    }
                                );
                            }

                            return true; //continue enumeration
                        }
                        , 0);

                    return windows.OrderBy(k => k.Title);
                };
            #endregion

            var ResetThumbnailSkip = 0;

            Func<Internal.Window> GetCurrentThumbnail = () => GetWindows().AsCyclicEnumerable().Skip(ResetThumbnailSkip).First();

            #region AnimationCompleted
            Intro.AnimationCompleted +=
                delegate
                {
                    if (c == null)
                    {
                        c = new JSCSolutionsNETCarouselCanvas
                        {
                            CloseOnClick = false
                        }.AttachContainerTo(winfoc);
                        c.HideSattelites();

                        CaptionClose.Show();
                        SizeChanged();
                    }
                };
            #endregion


            wcam.SourceInitialized +=
                delegate
                {
                    {
                        wcam.MakeInteractive(false);

                        UpdateChildren();
                    }



                    var thumb = IntPtr.Zero;
                    ResetThumbnailSkip = GetWindows().Where(
                        k =>
                            k.Title.Contains("Chrome")
                            || k.Title.Contains("Studio")
                            || k.Title.Contains("Minefield")

                            ).TakeWhile(k => k.Handle != Internal.GetForegroundWindow()).Count();


                    #region ResetThumbnail
                    Action ResetThumbnail =
                        delegate
                        {
                            GetCurrentThumbnail().With(
                                shadow =>
                                {
                                    //t.Text = shadow.Title;

                                    if (thumb != IntPtr.Zero)
                                        Internal.DwmUnregisterThumbnail(thumb);

                                    int i = Internal.DwmRegisterThumbnail(
                                        new WindowInteropHelper(wcam).Handle, shadow.Handle, out thumb);

                                    #region UpdateThumbnail
                                    Action UpdateThumbnail =
                                        delegate
                                        {
                                            if (thumb != IntPtr.Zero)
                                            {
                                                Internal.PSIZE size;
                                                Internal.DwmQueryThumbnailSourceSize(thumb, out size);

                                                Internal.DWM_THUMBNAIL_PROPERTIES props = new Internal.DWM_THUMBNAIL_PROPERTIES();

                                                props.fVisible = true;
                                                props.dwFlags = Internal.DWM_TNP_VISIBLE | Internal.DWM_TNP_RECTDESTINATION | Internal.DWM_TNP_OPACITY | Internal.DWM_TNP_SOURCECLIENTAREAONLY;
                                                props.opacity = (byte)((byte)(0x7F) + (byte)((0x80) * (ThumbnailSize)));
                                                props.rcDestination = new Internal.Rect(0, 0, (int)wcam.ActualWidth, (int)wcam.ActualHeight);
                                                props.fSourceClientAreaOnly = true;

                                                if (size.x < wcam.ActualWidth)
                                                {
                                                    props.rcDestination.Right = props.rcDestination.Left + size.x;

                                                    props.rcDestination.Left += ((int)wcam.ActualWidth - size.x) / 2;
                                                    props.rcDestination.Right += ((int)wcam.ActualWidth - size.x) / 2;
                                                }

                                                if (size.y < wcam.ActualHeight)
                                                {

                                                    props.rcDestination.Bottom = props.rcDestination.Top + size.y;

                                                    props.rcDestination.Top += ((int)wcam.ActualHeight - size.y) / 2;
                                                    props.rcDestination.Bottom += ((int)wcam.ActualHeight - size.y) / 2;

                                                }



                                                Internal.DwmUpdateThumbnailProperties(thumb, ref props);
                                            }
                                        };
                                    #endregion


                                    (1000 / 15).AtInterval(UpdateThumbnail);

                                    wcam.SizeChanged += delegate { UpdateThumbnail(); };
                                }
                            );
                        };
                    #endregion



                    ResetThumbnail();

                    NextInputModeKeyDown +=
                        key =>
                        {
                            if (key == Key.RightShift)
                            {
                                NextInputModeKeyDownEnabled = true;

                                if (c != null)
                                {
                                    CaptionClose.Hide();
                                    c.AtClose +=
                                        delegate
                                        {
                                            c.OrphanizeContainer();
                                        };
                                    c.Close();
                                    c = null;
                                }

                                Intro.Background = Brushes.Transparent;
                                Intro.Overlay.Opacity = 1;
                                Intro.FadeIn(
                                    delegate
                                    {


                                        2000.AtDelay(Intro.PrepareAnimation());
                                    }
                                );
                            }

                            if (!CommandKeysEnabled)
                            {
                                if (key == Key.F2)
                                {
                                    w.Activate();

                                    TopicText.Focusable = true;
                                    TopicText.Focus();
                                    TopicText.SelectAll();
                                }
                                return;
                            }

                            if (key == Key.Right)
                            {
                                if (w.IsActive)
                                {
                                    GetCurrentThumbnail().Activate();
                                }
                                else
                                {
                                    NextInputModeKeyDownEnabled = true;
                                    ResetThumbnailSkip = GetWindows().TakeWhile(k => k.Handle != Internal.GetForegroundWindow()).Count();
                                    ResetThumbnail();
                                }
                            }

                            if (key == Key.Up)
                            {
                                NextInputModeKeyDownEnabled = true;

                                if (ThumbnailSize < 0.3)
                                    ThumbnailSize = 0.3;
                                else if (ThumbnailSize < 0.5)
                                    ThumbnailSize = 0.5;
                                else if (ThumbnailSize < 1)
                                    ThumbnailSize = 1;
                                else
                                {
                                    if (c != null)
                                    {
                                        CaptionClose.Hide();
                                        c.AtClose +=
                                            delegate
                                            {
                                                c.OrphanizeContainer();
                                            };
                                        c.Close();
                                        c = null;
                                    }

                                }


                                UpdateChildren();

                            }
                            if (key == Key.Down)
                            {
                                NextInputModeKeyDownEnabled = true;
                                if (c == null)
                                {
                                    c = new JSCSolutionsNETCarouselCanvas
                                    {
                                        CloseOnClick = false
                                    }.AttachContainerTo(winfoc);
                                    CaptionClose.Show();
                                }
                                else if (ThumbnailSize > 0.5)
                                    ThumbnailSize = 0.5;
                                else if (ThumbnailSize > 0.3)
                                    ThumbnailSize = 0.3;
                                else if (ThumbnailSize == 0.3)
                                    ThumbnailSize = 0;

                                SizeChanged();
                            }



                            if (key == Key.Left)
                            {
                                if (w.IsActive)
                                {
                                    NextInputModeKeyDownEnabled = true;
                                    if (ExtraBorderTop.Opacity == 1)
                                    {
                                        ExtraBorderTop.Opacity = 0;
                                        ExtraBorderBottom.Opacity = 0;
                                    }
                                    else
                                    {
                                        ExtraBorderTop.Opacity = 1;
                                        ExtraBorderBottom.Opacity = 1;
                                    }
                                }
                            }

                        };


                };

            winfo.SourceInitialized +=
                delegate
                {
                    winfo.MakeInteractive(false);
                };

            w.SourceInitialized +=
                delegate
                {

                    // http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/61e93dca-e24c-4953-9719-22ce3f705353
                    Matrix m = PresentationSource.FromVisual(w).CompositionTarget.TransformToDevice;
                    double dx = m.M11;
                    double dy = m.M22;


                    w.SizeTo(1280 / dx, 768 /dy);
                    w.MoveTo(8, 0);
                    SizeChanged();

                    wcam.Owner = w;
                    winfo.Owner = w;
                    wcam.Show();
                    winfo.Show();

                    HwndSource hwndSource = (HwndSource)HwndSource.FromVisual(w);
                    hwndSource.AddHook(
                        (IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handeled) =>
                        {
                            if (msg == 0x0084) // WM_NCHITTEST
                            {
                                //http://agsmith.wordpress.com/2008/09/16/hit-testing-in-wpf/

                                var p = new
                                {
                                    cx = w.ActualWidth - (Internal.LOWORD(lParam) - w.Left),
                                    cy = w.ActualHeight - (Internal.HIWORD(lParam) - w.Top),
                                    x = Internal.LOWORD(lParam) - w.Left,
                                    y = Internal.HIWORD(lParam) - w.Top
                                };


                                CaptionText.Text = p.ToString();

                                handeled = true;

                                return (IntPtr)HitTestValues.HTCAPTION; // HTCAPTION


                                if (false)
                                {
                                    if (p.x < s)
                                        if (p.y < s)
                                            return (IntPtr)HitTestValues.HTTOPLEFT; // HTCAPTION

                                    if (p.x < CaptionBackgroundHeight)
                                        if (p.y < CaptionBackgroundHeight)
                                            return (IntPtr)HitTestValues.HTSYSMENU; // HTCAPTION


                                    if (p.cx < CaptionBackgroundHeight)
                                        if (p.y < CaptionBackgroundHeight)
                                            return (IntPtr)HitTestValues.HTCLOSE; // HTCAPTION


                                    if (p.cx < s)
                                        if (p.cy < s)
                                            return (IntPtr)HitTestValues.HTBOTTOMRIGHT; // HTCAPTION

                                    if (p.cx < s)
                                        if (p.y < s)
                                            return (IntPtr)HitTestValues.HTTOPRIGHT; // HTCAPTION

                                    if (p.x < s)
                                        if (p.cy < s)
                                            return (IntPtr)HitTestValues.HTBOTTOMLEFT; // HTCAPTION

                                    if (p.x < s)
                                        return (IntPtr)HitTestValues.HTLEFT; // HTCAPTION

                                    if (p.y < s)
                                        return (IntPtr)HitTestValues.HTTOP; // HTCAPTION


                                    if (p.cx < s)
                                        return (IntPtr)HitTestValues.HTRIGHT; // HTCAPTION

                                    if (p.cy < s)
                                        return (IntPtr)HitTestValues.HTBOTTOM; // HTCAPTION

                                    if (p.y < CaptionBackgroundHeight)
                                        return (IntPtr)HitTestValues.HTCAPTION; // HTCAPTION

                                    return (IntPtr)HitTestValues.HTTRANSPARENT; // HTCAPTION
                                }

                            }
                            return IntPtr.Zero;
                        }

                   );
                };



            InterceptKeys.InternalMain(
                Rehook =>
                {
                    w.Deactivated +=
                        delegate
                        {
                            TopicText.Focusable = false;
                        };
                    w.Activated +=
                        delegate
                        {


                            //TopicText.Focus();
                            Rehook();
                        };

                    w.ShowDialog();
                }
            );

        }
        public ApplicationCanvasXTransparent()
        {
            var Borders = Enumerable.Range(1, 12).Reverse().Select(
                Width =>
                {
                    var Left = new Rectangle { Fill = Brushes.Black, Opacity = 0.06 }.MoveTo(0, 0).AttachTo(this);
                    var Right = new Rectangle { Fill = Brushes.Black, Opacity = 0.06 }.MoveTo(0, 0).AttachTo(this);
                    var Bottom = new Rectangle { Fill = Brushes.Black, Opacity = 0.09 }.MoveTo(0, 0).AttachTo(this);
                    var Top = new Rectangle { Fill = Brushes.Black, Opacity = 0.11 }.MoveTo(0, 0).AttachTo(this);


                    return new
                    {
                        Width = Width * 1.5,
                        Left,
                        Right,
                        Bottom,
                        Top
                    };
                }
            ).ToArray();




            CreateLogoAnimation();

            #region CaptionTextShadow
            var CaptionTextShadow = new System.Windows.Controls.TextBox
            {
                IsReadOnly = true,
                Background = Brushes.Transparent,
                BorderThickness = new System.Windows.Thickness(0),
                Foreground = Brushes.Black,
                Text = "jsc-solutions.net",
                //TextDecorations = TextDecorations.Underline,
                FontFamily = new FontFamily("Verdana"),
                FontSize = 16,
                TextAlignment = System.Windows.TextAlignment.Right
            }
            .AttachTo(this);
            #endregion

            #region CaptionText
            var CaptionText = new System.Windows.Controls.TextBox
            {
                IsReadOnly = true,
                Background = Brushes.Transparent,
                BorderThickness = new System.Windows.Thickness(0),
                Foreground = Brushes.White,
                Text = "jsc-solutions.net",
                //TextDecorations = TextDecorations.Underline,
                FontFamily = new FontFamily("Verdana"),
                FontSize = 16,
                TextAlignment = System.Windows.TextAlignment.Right
            }
            .AttachTo(this);
            #endregion


            #region TopicTextShadow
            TopicTextShadow = new System.Windows.Controls.TextBox
            {
                //IsReadOnly = true,
                Background = Brushes.Transparent,
                BorderThickness = new System.Windows.Thickness(0),
                Foreground = Brushes.Black,
                Text = "JSC C# Foo Bar",
                //TextDecorations = TextDecorations.Underline,
                FontFamily = new FontFamily("Verdana"),
                FontSize = 24,
                TextAlignment = System.Windows.TextAlignment.Right
            }
            .AttachTo(this);
            #endregion

            #region TopicText
            TopicText = new System.Windows.Controls.TextBox
            {
                //IsReadOnly = true,
                Background = Brushes.Transparent,
                BorderThickness = new System.Windows.Thickness(0),
                Foreground = Brushes.White,
                Text = "JSC C# Foo Bar",
                //TextDecorations = TextDecorations.Underline,
                FontFamily = new FontFamily("Verdana"),
                FontSize = 24,
                TextAlignment = System.Windows.TextAlignment.Right
            }
            .AttachTo(this);
            #endregion

            Action SizeChanged =
              delegate
              {

                  var w = new { ActualWidth = this.Width, ActualHeight = this.Height };

                  Borders.WithEach(k => { k.Left.MoveTo(0, 0).SizeTo(k.Width, w.ActualHeight); });
                  Borders.WithEach(k => { k.Right.MoveTo(w.ActualWidth - k.Width, 0).SizeTo(k.Width, w.ActualHeight); });
                  Borders.WithEach(k => { k.Bottom.MoveTo(0, w.ActualHeight - k.Width).SizeTo(w.ActualWidth, k.Width); });
                  Borders.WithEach(k => { k.Top.MoveTo(0, 0).SizeTo(w.ActualWidth, k.Width); });

                  CaptionText.MoveTo(0, 6).SizeTo(this.Width - 6, 24);
                  CaptionTextShadow.MoveTo(0 + 1, 6 + 1).SizeTo(this.Width - 6, 24);

                  TopicText.MoveTo(0, this.Height - 38).SizeTo(this.Width - 6, 38);
                  TopicTextShadow.MoveTo(2, this.Height - 38 + 2).SizeTo(this.Width - 6, 38);
              };

            this.SizeChanged +=
                delegate
                {
                    SizeChanged();
                };

            this.SizeTo(200, 200);
        }
		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();
						};
					}



				}
			);
		}
        public ApplicationCanvas()
        {
            var r0shadow = new Rectangle();
            r0shadow.Opacity = 0.5;
            r0shadow.Fill = Brushes.Black;
            r0shadow.AttachTo(this);

            var r1shadow = new Rectangle();
            r1shadow.Opacity = 0.5;
            r1shadow.Fill = Brushes.Black;
            r1shadow.AttachTo(this);


            var rcontent = new Canvas().AttachTo(this);


            var r0 = new Rectangle();


            r0.Opacity = 0.8;
            r0.Fill = Brushes.DarkGreen;
            r0.AttachTo(rcontent);

            var r1 = new Rectangle();


            r1.Opacity = 0.8;
            r1.Fill = Brushes.DarkGreen;
            r1.AttachTo(rcontent);
            r1.MoveTo(0, 96);



            r0.SizeTo(500, 96 - 16);
            r1.SizeTo(500, 230);
            r0shadow.SizeTo(500, 96 - 16);
            r1shadow.SizeTo(500, 230);

            var t = new TextBox();

            t.IsReadOnly = true;

            //            type: System.Windows.Controls.TextBlock
            //method: Void set_Foreground(System.Windows.Media.Brush)

            t.Foreground = Brushes.Yellow;
            t.AttachTo(rcontent);
            t.AcceptsReturn = true;
            t.Text = "You have found a shop.\nSee something you like?";
            t.Background = Brushes.Transparent;
            t.BorderThickness = new Thickness(0);
            t.FontSize = 24;
            t.MoveTo(8, 8);


            t2 = new TextBox();

            t2.IsReadOnly = true;

            //            type: System.Windows.Controls.TextBlock
            //method: Void set_Foreground(System.Windows.Media.Brush)

            t2.Foreground = Brushes.Yellow;
            t2.AttachTo(rcontent);
            t2.AcceptsReturn = true;
            t2.Text = "! First we will log you in to FACEBOOK and then to PAYPAL, takes a few...";
            t2.Background = Brushes.Transparent;
            t2.BorderThickness = new Thickness(0);
            t2.MoveTo(8, 290);

            t2o = t2.ToAnimatedOpacity();

            t2o.Opacity = 0.2;

            bg_ammo = new Rectangle();


            bg_ammo.Fill = Brushes.Yellow;
            bg_ammo.AttachTo(rcontent);
            bg_ammo.SizeTo(400, 48);
            bg_ammo.MoveTo(50, 128 + 32 - 8);

            var ammo = new Avalon.Images.avatars_ammo();

            ammo.AttachTo(rcontent);
            ammo.MoveTo(32 + 32, 128 + 32);



            overlay_ammo.Fill = Brushes.Yellow;
            overlay_ammo.Opacity = 0;
            overlay_ammo.AttachTo(rcontent);
            overlay_ammo.SizeTo(400, 48);
            overlay_ammo.MoveTo(50, 128 + 32 - 8);

            var bg_ammo_opacity = bg_ammo.ToAnimatedOpacity();

            overlay_ammo.Cursor = Cursors.Hand;
            bg_ammo_opacity.Opacity = 0.5;
            overlay_ammo.MouseEnter +=
                delegate
                {
                    bg_ammo_opacity.Opacity = 1.0;
                };

            overlay_ammo.MouseLeave +=
                delegate
                {
                    bg_ammo_opacity.Opacity = 0.5;
                };
            overlay_ammo.MouseLeftButtonUp +=
             delegate
             {
                 t2o.Opacity = 1;

                 if (BuyAmmo != null)
                     BuyAmmo();
             };














            bg_shotgun.Opacity = 0.5;
            bg_shotgun.Fill = Brushes.Yellow;
            bg_shotgun.AttachTo(rcontent);
            bg_shotgun.SizeTo(400, 48);
            bg_shotgun.MoveTo(50, 128 + 64 + 32);

            var shotgun = new Avalon.Images.avatars_shotgun();

            shotgun.AttachTo(rcontent);
            shotgun.MoveTo(32 + 32, 128 + 64 + 32 + 8);



            var overlay_shotgun = new Rectangle();


            overlay_shotgun.Opacity = 0;
            overlay_shotgun.Fill = Brushes.Yellow;
            overlay_shotgun.AttachTo(rcontent);
            overlay_shotgun.SizeTo(400, 48);
            overlay_shotgun.MoveTo(50, 128 + 64 + 32);

            var bg_shotgun_opacity = bg_shotgun.ToAnimatedOpacity();

            overlay_shotgun.Cursor = Cursors.Hand;
            bg_shotgun_opacity.Opacity = 0.5;
            overlay_shotgun.MouseEnter +=
                delegate
                {
                    bg_shotgun_opacity.Opacity = 1.0;
                };

            overlay_shotgun.MouseLeave +=
                delegate
                {
                    bg_shotgun_opacity.Opacity = 0.5;
                };

            overlay_shotgun.MouseLeftButtonUp +=
                delegate
                {
                    t2o.Opacity = 1;

                    if (BuyShotgun != null)
                        BuyShotgun();
                };

            this.SizeChanged += (s, e) =>
            {



                r0shadow.MoveTo((this.Width - 500) / 2 + 4, (this.Height - 400) / 2 + 4);
                r1shadow.MoveTo((this.Width - 500) / 2 + 4, (this.Height - 400) / 2 + 4 + 96);
                rcontent.MoveTo((this.Width - 500) / 2, (this.Height - 400) / 2);

            };

            var rclose_shadow = new Rectangle();


            //rclose.Opacity = 0.8;
            rclose_shadow.Fill = Brushes.Black;
            rclose_shadow.AttachTo(rcontent);
            rclose_shadow.Opacity = 0.3;

            rclose_shadow.SizeTo(32, 32);

            var rclose = new Rectangle();


            rclose.Fill = Brushes.Red;
            rclose.AttachTo(rcontent);
            rclose.Cursor = Cursors.Hand;

            rclose.SizeTo(32, 32);

            var rclosemagin = -8;
            rclose.MoveTo(500 - 32 - rclosemagin, rclosemagin);
            rclose_shadow.MoveTo(500 - 32 - rclosemagin + 4, rclosemagin + 4);


            rclose.MouseLeftButtonUp +=
                delegate
                {
                    if (Close != null)
                        Close();
                };
        }