public ApplicationCanvas()
		{
			Width = DefaultWidth;
			Height = DefaultHeight;

            //this.ClipToBounds = true;

			Colors.Blue.ToGradient(Colors.Red, DefaultHeight / 4).Select(
				(c, i) =>
					new Rectangle
					{
						Fill = new SolidColorBrush(c),
						Width = DefaultWidth,
						Height = 4,
					}.MoveTo(0, i * 4).AttachTo(this)
			).ToArray();



			//var help = new Image
			//{
			//    Source = (KnownAssets.Path.Assets + "/help.png").ToSource()
			//}.AttachTo(this);

			//help.Opacity = 0;

			var img = new Avalon.Images.jsc().MoveTo(DefaultWidth - 128, DefaultHeight - 128).AttachTo(this);

			var t = new TextBox
			{
				FontSize = 10,
				Text = "powered by jsc",
				BorderThickness = new Thickness(0),
				Foreground = 0xffffffff.ToSolidColorBrush(),
				Background = Brushes.Transparent,
				IsReadOnly = true,
				Width = DefaultWidth
			}.MoveTo(8, 8).AttachTo(this);

            this.WriteStatus = o => t.Text = o.ToString();

			var t2 = new TextBox
			{
				FontSize = 10,
				AcceptsReturn = true,
				Text = @"
ie and opera are not supported
firefox reports after transform
chrome reports before transform
we cannot rely on mouse position currently
shall wait for improvements like touch API
",
				BorderThickness = new Thickness(0),
				Foreground = 0xffffffff.ToSolidColorBrush(),
				Background = Brushes.Transparent,
				IsReadOnly = true,
				Width = DefaultWidth,
				Height = 128,
			}.MoveTo(8, 32).AttachTo(this);

			//help.Opacity = 1;
			img.Opacity = 0.5;

			t.MouseEnter +=
				delegate
				{
					//help.Opacity = 0.5;

					img.Opacity = 1;
					t.Foreground = 0xffffff00.ToSolidColorBrush();
				};

			t.MouseLeave +=
				delegate
				{
					//help.Opacity = 1;

					img.Opacity = 0.5;
					t.Foreground = 0xffffffff.ToSolidColorBrush();
				};


			var sand = new Avalon.Images.sand()
			{
				Width = 100,
				Height = 100,
			}.AttachTo(this);

			// structs are not that good for translations...
			// we might be able to skip them it seems
			sand.RenderTransform = new MatrixTransform(1.2, 0.5, 0.2, 1, 100, 100);

			sand.Cursor = Cursors.Hand;

			// jsc/mxmlc cannot handle property name "this" :)

			sand.Opacity = 0.7;

			sand.MouseMove +=
				(sender, args) =>
				{
					var p1 = args.GetPosition(sand);
					var p2 = args.GetPosition(this);

					t.Text = new
					{
						sand = new { p1.X, p1.Y },
						that = new { p2.X, p2.Y }
					}.ToString();

				};


			


			var tri = new Avalon.Images._17()
			{
				Width = 100,
				Height = 100,
			}.AttachTo(this);


            var trig = new Avalon.Images._17g()
			{
				Width = 100,
				Height = 100,
			}.AttachTo(this);

            var tri2 = new Avalon.Images._17()
			{
				Width = 100,
				Height = 100,
			}.AttachTo(this);


            var trig2 = new Avalon.Images._17g()
			{
				Width = 100,
				Height = 100,
			}.AttachTo(this);


			// cursor position calculations are not ready
			// for transofrmed elements.
			// we will provide a floor for those events...
			var shadow = new Rectangle
			{
				Width = DefaultWidth,
				Height = DefaultHeight,

				Fill = Brushes.Black,
			}.AttachTo(this);

			var shadowa = shadow.ToAnimatedOpacity();

			shadowa.Opacity = 0;

			Func<Brush, int, int, Movable> m =
				(Color, X, Y) =>
				{
					var m0 = new Movable
					{
						Context = this,
						Color = Color
					};

					m0.MoveTo(X, Y);

					return m0;
				};

			var m1 = m(Brushes.Green, 250, 50);
			var m2 = m(Brushes.Red, 250 + 100, 50);
			var m3 = m(Brushes.Blue, 250, 50 + 100);
			var m4 = m(Brushes.Yellow, 250 + 100, 50 + 100);

			var m5 = m(Brushes.Green, 250 + 200, 50);
			var m6 = m(Brushes.Blue, 250 + 200, 50 + 100);

            Movables = new[] { m1, m2, m3, m4, m5, m6};


			Action Update =
				delegate
				{
					

					tri.RenderTransform = new AffineTransform
					{
						Left = 0,
						Top = 0,
						Width = 100,
						Height = 100,

						X1 = m2.X,
						Y1 = m2.Y,

						X2 = m3.X,
						Y2 = m3.Y,

						X3 = m1.X,
						Y3 = m1.Y,
						
					};


					trig.RenderTransform = new AffineTransform
					{
						Left = 0,
						Top = 0,
						Width = 100,
						Height = 100,

						X1 = m3.X,
						Y1 = m3.Y,

						X2 = m2.X,
						Y2 = m2.Y,

						X3 = m4.X,
						Y3 = m4.Y,

					};

					tri2.RenderTransform = new AffineTransform
					{
						Left = 0,
						Top = 0,
						Width = 100,
						Height = 100,

						X1 = m5.X,
						Y1 = m5.Y,

						X2 = m4.X,
						Y2 = m4.Y,

						X3 = m2.X,
						Y3 = m2.Y,

					};


					trig2.RenderTransform = new AffineTransform
					{
						Left = 0,
						Top = 0,
						Width = 100,
						Height = 100,

						X1 = m4.X,
						Y1 = m4.Y,

						X2 = m5.X,
						Y2 = m5.Y,

						X3 = m6.X,
						Y3 = m6.Y,

					};
				};

			foreach (var k in this.Movables)
			{
				k.Container.MouseLeftButtonDown += delegate { shadowa.Opacity = 0.2; };
				k.Container.MouseLeftButtonUp += delegate { shadowa.Opacity = 0; };
				k.Changed += Update;
			}

			Update();

            
		}
        public AvalonBetrisCanvas()
        {
            Width = DefaultWidth;
            Height = DefaultHeight;

            this.ClipToBounds = true;

            Colors.Blue.ToGradient(Colors.White, DefaultHeight / 4).Select(
                (c, i) =>
                    new Rectangle
                    {
                        Fill = new SolidColorBrush(c),
                        Width = DefaultWidth,
                        Height = 4,
                    }.MoveTo(0, i * 4).AttachTo(this)
            ).ToArray();

            var t = new TextBox
            {
                FontSize = 10,
                Text = "powered by jsc",
                BorderThickness = new Thickness(0),
                Foreground = 0xffffffff.ToSolidColorBrush(),
                Background = Brushes.Transparent,
                IsReadOnly = true,
                Width = DefaultWidth
            }.MoveTo(8, 8).AttachTo(this);

            var t2 = new TextBox
            {
                FontSize = 10,
                AcceptsReturn = true,
                Text = @"
            BETRIS
            ",
                BorderThickness = new Thickness(0),
                Foreground = 0xffffffff.ToSolidColorBrush(),
                Background = Brushes.Transparent,
                IsReadOnly = true,
                Width = DefaultWidth,
                Height = 128,
            }.MoveTo(8, 32).AttachTo(this);

            var bg = new Image
            {
                Source = ("assets/AvalonBetris/building_block_tetris.png").ToSource()
            }.MoveTo(0, 0).AttachTo(this);

            var img = new Image
            {
                Source = ("assets/AvalonBetris/jsc.png").ToSource()
            }.MoveTo(DefaultWidth - 128, DefaultHeight - 128).AttachTo(this);

            bg.MouseLeftButtonUp +=
                (sender, args) =>
                {
                    var p = args.GetPosition(this);
                    t2.AppendTextLine(new { p.X, p.Y }.ToString());
                };

            //help.Opacity = 1;
            img.Opacity = 0.5;

            var aw = 49;
            var ah = 49;

            var tri = new Image
            {
                Width = aw,
                Height = ah,
                Source = "assets/AvalonBetris/_19a.png".ToSource()
            }.AttachTo(this);

            var trig = new Image
            {
                Width = aw,
                Height = ah,
                Source = "assets/AvalonBetris/_19b.png".ToSource()
            }.AttachTo(this);

            var tri2 = new Image
            {
                Width = aw,
                Height = ah,
                Source = "assets/AvalonBetris/_18a.png".ToSource()
            }.AttachTo(this);

            var trig2 = new Image
            {
                Width = aw,
                Height = ah,
                Source = "assets/AvalonBetris/_18b.png".ToSource()
            }.AttachTo(this);

            // cursor position calculations are not ready
            // for transofrmed elements.
            // we will provide a floor for those events...
            var shadow = new Rectangle
            {
                Width = DefaultWidth,
                Height = DefaultHeight,

                Fill = Brushes.Black,
            }.AttachTo(this);

            var shadowa = shadow.ToAnimatedOpacity();

            shadowa.Opacity = 0;

            Func<Brush, int, int, Movable> m =
                (Color, X, Y) =>
                {
                    var m0 = new Movable
                    {
                        Context = this,
                        Color = Color
                    };

                    m0.MoveTo(X, Y);

                    return m0;
                };

            var m1 = m(Brushes.Green, 197, 123);
            var m2 = m(Brushes.Red, 224, 110);
            var m3 = m(Brushes.Blue, 197, 161);
            var m4 = m(Brushes.Yellow, 222, 159);

            var m5 = m(Brushes.Green, 265, 118);
            var m6 = m(Brushes.Blue, 265, 166);

            Action Update =
                delegate
                {

                    tri.RenderTransform = new AffineTransform
                    {
                        Left = 0,
                        Top = 0,
                        Width = aw,
                        Height = ah,

                        X1 = m2.X,
                        Y1 = m2.Y,

                        X2 = m3.X,
                        Y2 = m3.Y,

                        X3 = m1.X,
                        Y3 = m1.Y,

                    };

                    trig.RenderTransform = new AffineTransform
                    {
                        Left = 0,
                        Top = 0,
                        Width = aw,
                        Height = ah,

                        X1 = m3.X,
                        Y1 = m3.Y,

                        X2 = m2.X,
                        Y2 = m2.Y,

                        X3 = m4.X,
                        Y3 = m4.Y,

                    };

                    tri2.RenderTransform = new AffineTransform
                    {
                        Left = 0,
                        Top = 0,
                        Width = aw,
                        Height = ah,

                        X1 = m5.X,
                        Y1 = m5.Y,

                        X2 = m4.X,
                        Y2 = m4.Y,

                        X3 = m2.X,
                        Y3 = m2.Y,

                    };

                    trig2.RenderTransform = new AffineTransform
                    {
                        Left = 0,
                        Top = 0,
                        Width = aw,
                        Height = ah,

                        X1 = m4.X,
                        Y1 = m4.Y,

                        X2 = m5.X,
                        Y2 = m5.Y,

                        X3 = m6.X,
                        Y3 = m6.Y,

                    };
                };

            foreach (var k in new[] { m1, m2, m3, m4, m5, m6 })
            {
                k.Container.MouseLeftButtonDown += delegate { shadowa.Opacity = 0.2; };
                k.Container.MouseLeftButtonUp += delegate { shadowa.Opacity = 0; };
                k.Changed += Update;
            }

            Update();
        }
		public InteractiveSketchupWarehouseCanvas()
		{
			Width = DefaultWidth;
			Height = DefaultHeight;
			Background = Brushes.White;

			this.ClipToBounds = true;

			//Colors.Cyan.ToGradient(Colors.White, DefaultHeight / 4).Select(
			//    (c, i) =>
			//        new Rectangle
			//        {
			//            Fill = new SolidColorBrush(c),
			//            Width = DefaultWidth,
			//            Height = 4,
			//        }.MoveTo(0, i * 4).AttachTo(this)
			//).ToArray();




			var img = new Image
			{
				Width = 400,
				Height = 300
			};

			img.AttachTo(this);

			new TextBox
			{
				Foreground = Brushes.Red,
				Width = DefaultWidth,
				BorderThickness = new Thickness(0),
				Background = Brushes.Transparent,
				IsReadOnly = true,
				Height = 32,
				Text = "Flash version will only work in web context!"
			}.AttachTo(this);

			var imagenum = 34;

			var src_uri = ApplyFrame(imagenum);



			img.Cursor = Cursors.Hand;

			var xoverlay = new Rectangle
			{
				Width = DefaultWidth,
				Height = DefaultHeight,
				Fill = Brushes.Black,
				Opacity = 0,
				Cursor = Cursors.Hand,
			}.AttachTo(this);


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

			var bga = bg.ToAnimatedOpacity();

			bg.Opacity = 0.5;

			var c = new ScriptCoreLib.Shared.Avalon.Carousel.SimpleCarouselControl(DefaultWidth, DefaultHeight);

			var options = new[]
			{
				"58eca9d1551fde5a3b70edf326d07e86",
				"6e006174683f7372de48c10a5a3895c4",
				"5ff285283e8ec610434fdb742d7e0cea",
				"8d4d82e8807c3823bd4169514fca71f2",
				"bad472a435d3fab41992a70eb6b3a2a6",
				"3a4a2c47e8d8fdbb7302a5e36ce363de",
				"982a7381b7c5f4d36dce312d2cfb61e8",
				"84bafa682ff9c15b665aa76cf50df2c3",
				"9533d2d07d433ac072f17dbd3c237580",
				"d7222464c02cca18e92340be97470aea",
				"67a2216e4253e3f75eb112e8bc6bbc53",
				"5f310d576a8180e2ae504fdbdb5a7e96",
				"d106aaeaa51217cc465e08d496c0420f",
				"d2f505aca92ee65fbdb8512730b99253"
			};

			var position = 0.0;


			var a = c.Container.ToAnimatedOpacity();
			
			a.Opacity = 1;
		
			foreach (var mid_ in options.Randomize())
			{
				var mid = mid_;
				position += 0.6;
				c.AddEntry(
					new ScriptCoreLib.Shared.Avalon.Carousel.SimpleCarouselControl.EntryInfo
					{
						Position = position,
						Source = new Sketchup { mid = mid },
						Click =
							delegate
							{
								if (this.Current.mid == mid)
								{
									Uri u = new Sketchup { mid = Current.mid, IsPreview = true };

									u.NavigateTo();
									return;
								}

								this.Current.mid = mid;

								c.Overlay.Hide();
								a.Opacity = 0;

							
										Update(img, imagenum);
								

								1500.AtDelay(
									delegate
									{
										bga.Opacity = 0;
									}
								);

							}
					}
				);


			}

			xoverlay.MouseLeftButtonUp +=
				delegate
				{
					if (a.Opacity == 0)
					{
						c.Overlay.Show();
						a.Opacity = 1;
						bga.Opacity = 0.7;
					}
					else
					{
						a.Opacity = 0;
						bga.Opacity = 0;
						c.Overlay.Hide();
					}
				};

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

					var p = args.GetPosition(this);

					var z = (p.Y / DefaultHeight) * 0.7;

					img.MoveTo((DefaultWidth - DefaultWidth * z) / 2, (DefaultHeight - DefaultHeight * z) / 2);
					img.Width = DefaultWidth * z;
					img.Height = DefaultHeight * z;

					if (a.Opacity < 0.5)
					{
						imagenum = Convert.ToInt32((p.X / DefaultWidth + 0.5) * 35) % 36;

						img.Source = ApplyFrame(imagenum);
					}
				};


			c.AttachContainerTo(this);
			c.Overlay.AttachTo(this);
			c.Show();
		}
        public OrcasAvalonApplicationCanvas()
        {
            Width = DefaultWidth;
            Height = DefaultHeight;

            this.ClipToBounds = true;

            var x = GameReferenceExtensions.Default;

            var Content = new Canvas
            {
                Width = DefaultWidth,
                Height = DefaultHeight,
            }.AttachTo(this);

            int LogoSize = 32;

            new Image
            {
                Width = LogoSize,
                Height = LogoSize,
                Source = ("assets/Bulldog.Promotion/jsc.png").ToSource()
            }.MoveTo(DefaultWidth - LogoSize, DefaultHeight - LogoSize).AttachTo(this);

            var TouchOverlayContainer = new Canvas
            {
                Width = DefaultWidth,
                Height = DefaultHeight,
            }.AttachTo(this);

            var wc = new WebClient();

            var List = new List<Entry>();

            #region load
            wc.DownloadStringCompleted +=
                (sender, args) =>
                {

                    var a = GameReferenceExtensions.Partial;

                    a.ForEach(k => k.Source = k.Image.Substring(GameReferenceExtensions.Host.Length + 1));

                    if (args.Error == null)
                        a = a.FromYAML(args.Result);

                    this.Dispatcher.Invoke(
                        (Action)
                        delegate
                        {
                            var i = 0;
                            foreach (var k_ in a)
                            {
                                var k = k_;

                                var Container = new Canvas
                                {
                                    Width = GameReferenceExtensions.Width,
                                    Height = GameReferenceExtensions.Height,
                                }.AttachTo(Content).MoveTo(0, i * GameReferenceExtensions.Height);

                                var Image = new Image
                                {
                                    Width = GameReferenceExtensions.Width,
                                    Height = GameReferenceExtensions.Height,

                                    Stretch = Stretch.Fill
                                }.AttachTo(Container);

                                var Shadow = new Rectangle
                                {
                                    Fill = Brushes.Black,
                                    Opacity = 0.5,
                                    Width = GameReferenceExtensions.Width - 2 * 2,
                                    Height = GameReferenceExtensions.Height / 3,

                                }.AttachTo(Container).MoveTo(2, GameReferenceExtensions.Height / 3);

                                var ShadowBottom = new Rectangle
                                {
                                    Fill = Brushes.Black,
                                    Width = GameReferenceExtensions.Width - 2 * 2,
                                    Height = GameReferenceExtensions.Height / 3 / 2,
                                }.AttachTo(Container).MoveTo(2, GameReferenceExtensions.Height / 3 + GameReferenceExtensions.Height / 3 / 2);

                                var AnimatedShadow = Shadow.ToAnimatedOpacity();
                                AnimatedShadow.Opacity = 0;

                                var AnimatedShadowBottom = ShadowBottom.ToAnimatedOpacity();
                                AnimatedShadowBottom.Opacity = 0;

                                var Text = new TextBox
                                {
                                    BorderThickness = new Thickness(0),
                                    Width = GameReferenceExtensions.Width * 2,
                                    Background = Brushes.Transparent,
                                    Foreground = Brushes.White,
                                    IsReadOnly = true,
                                    Text = k.Title,
                                    TextAlignment = TextAlignment.Center,
                                    FontSize = 10,
                                    FontFamily = new FontFamily("Verdana")
                                }.AttachTo(Container).MoveTo(-GameReferenceExtensions.Width / 2, 8 + GameReferenceExtensions.Height / 3);
                                Text.Hide();

                                var AnimatedImage = Container.ToAnimatedOpacity();

                                AnimatedImage.Opacity = 0;

                                ShowImageWhenReady(k, Image, Text, AnimatedImage);

                                var TouchOverlay = new Rectangle
                                {
                                    Fill = Brushes.White,
                                    Width = GameReferenceExtensions.Width,
                                    Height = GameReferenceExtensions.Height,
                                    Cursor = Cursors.Hand,
                                    Opacity = 0
                                }.AttachTo(TouchOverlayContainer).MoveTo(0, i * GameReferenceExtensions.Height);

                                TouchOverlay.MouseLeftButtonUp +=
                                    delegate
                                    {
                                        new Uri(k.Link).NavigateTo();
                                    };

                                List.Add(new Entry
                                {
                                    Shadow = AnimatedShadow,
                                    ShadowBottom = AnimatedShadowBottom,
                                    Text = Text,
                                    Canvas = Container,
                                    Image = Image,
                                    TouchOverlay = TouchOverlay
                                });

                                i++;
                            }

                            StartAnimation(List);
                        }
                    );
                };
            #endregion

            wc.DownloadStringAsync(new Uri(GameReferenceExtensions.Host + GameReferenceExtensions.Path));
        }
        public ApplicationCanvas()
        {

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

            Container720 = new Canvas().AttachTo(Container720X);
            //Container720A = Container720.ToAnimatedOpacity();

            i = new Avalon.Images.Promotion3D_controller_720p();
            i.AttachTo(Container720);
            iA = i.ToAnimatedOpacity();
            iA.Opacity = 0.8;

            var ShadowOverlay = new Rectangle { Fill = Brushes.Black };
            var ShadowOverlayA = ShadowOverlay.ToAnimatedOpacity();


            ShadowOverlay.AttachTo(Container720);
            ShadowOverlay.SizeTo(1280, 720);

            ii = new Avalon.Images.Promotion3D_controller_android_720();
            ii.AttachTo(Container720);
            var iiA = ii.ToAnimatedOpacity();


            var Title = new Avalon.Images.Title();
            Title.AttachTo(Container720X);





            this.SizeChanged += (s, e) =>
            {
                Container720X.MoveTo(
                        (this.Width - 1280) / 2,
                    (this.Height - 720) / 2
                );


                Console.WriteLine(new { this.Width, this.Height });


            };

            enter = new Avalon.Images.start
            {
                Cursor = Cursors.Hand
            }.AttachTo(this);

            entero = enter.ToAnimatedOpacity();


            this.SizeChanged += (s, e) => enter.MoveTo(
                (this.Width - 128) * 0.8 + 64,
                (this.Height - 128) / 2 + 32);

            #region Black
            {
                Rectangle r = new Rectangle();
                r.Fill = Brushes.Black;
                r.AttachTo(this);
                r.MoveTo(0, 0);

                this.SizeChanged += (s, e) =>
                {
                    r.Width = this.Width;
                    r.Height = ((this.Height - 720) / 2).Max(0);
                };
            }

            {
                Rectangle r = new Rectangle();
                r.Fill = Brushes.Black;
                r.AttachTo(this);
                r.MoveTo(0, 0);

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

                    //                NotImplementedException
                    //at ScriptCoreLib.ActionScript.BCLImplementation.System.Windows::__UIElement/InternalGetHeight_100663344()
                    //at ScriptCoreLib.ActionScript.BCLImplementation.System.Windows::__UIElement/VirtualGetHeight_100663342()
                    //at ScriptCoreLib.ActionScript.BCLImplementation.System.Windows::__FrameworkElement/get Height()

                    var Height = ((this.Height - 720) / 2).Max(0);

                    r.Height = Height;
                    r.MoveTo(0, this.Height - Height);
                };
            }
            #endregion

            var hotzone = new Rectangle { Fill = Brushes.Green, Opacity = 0 }.AttachTo(Container720X);

            hotzone.Cursor = Cursors.Hand;

            hotzone.MoveTo(1280 / 3, 720 * 2 / 3);
            hotzone.SizeTo(1280 / 3, 720 / 3);


            hotzone.MouseEnter +=
                delegate
                {
                    ShadowOverlayA.Opacity = 0.4;
                    iiA.Opacity = 1.0;
                    entero.Opacity = 0.2;

                    //Container720A.Opacity = 1;
                };


            hotzone.MouseLeave +=
                delegate
                {
                    ShadowOverlayA.Opacity = 0;
                    iiA.Opacity = 0;
                    entero.Opacity = 0.8;

                    //Container720A.Opacity = VideoPlayingOpacity;
                };


            hotzone.MouseLeftButtonUp +=
                delegate
                {
                    new Uri("http://young-beach-4377.herokuapp.com/android").NavigateTo();
                };

            #region enter
            enter.MouseEnter +=
                delegate
                {
                    entero.Opacity = 1;
                    ShadowOverlayA.Opacity = 0.4;

                    //Container720A.Opacity = 1;
                };


            enter.MouseLeave +=
             delegate
             {
                 entero.Opacity = 0.8;
                 ShadowOverlayA.Opacity = 0;

                 // got video?
                 //Container720A.Opacity = VideoPlayingOpacity;
                 //Container720A.Opacity = 1.0;
             };
            #endregion



            ShadowOverlayA.Opacity = 0;
            iiA.Opacity = 0;
            entero.Opacity = 0.8;

        }
        public ApplicationCanvas()
        {
            c = new Base.ApplicationCanvas();

            c.Selection.GlassArea.Orphanize();
            c.Selection.Orphanize();



            c.AttachTo(this);
            c.MoveTo(8, 8);

            this.SizeChanged += (s, e) => c.SizeTo(this.Width - 16.0, this.Height - 16.0);

            r.Fill = Brushes.Red;
            r.Opacity = 0;

            r.AttachTo(this);
            r.MoveTo(8, 8);

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

            Rectangle h = null;
            AnimatedOpacity<Rectangle> hOpacity = null;


            Action<Action<double, double>> GetPosition = null;
            var Windows = new List<Base.ApplicationCanvas.WindowInfo>();

         
            #region GetSnapLocation
            Action<Func<UIElement, Point>, Action<bool, double, double, double, double>> GetSnapLocation =
                (e_GetPosition, SetLocation) =>
                {
                    var p = e_GetPosition(r);

                    var x = 0.0;
                    var y = 0.0;

                    GetPosition((_x, _y) => { x = _x; y = _y; });

                    var cx = p.X - x;
                    var cy = p.Y - y;

                    if (cx < 0)
                    {
                        x += cx;
                        cx = -cx;
                    }

                    if (cy < 0)
                    {
                        y += cy;
                        cy = -cy;
                    }

                    var Snap = 16;
                    var SnapMode = false;

                    Enumerable.FirstOrDefault(
                        from k in Windows
                        let dx0 = Math.Abs(k.WindowLocation.Left - x)
                        where dx0 < Snap
                        orderby dx0
                        select k
                    ).With(
                     ax =>
                     {
                         SnapMode = true;
                         cx += x - ax.WindowLocation.Left;
                         x = ax.WindowLocation.Left;
                     }
                   );


                    Enumerable.FirstOrDefault(
                         from k in Windows
                         let dx0 = Math.Abs(k.WindowLocation.Top - y)
                         where dx0 < Snap
                         orderby dx0
                         select k
                     ).With(
                      ax =>
                      {
                          SnapMode = true;
                          cy += y - ax.WindowLocation.Top;
                          y = ax.WindowLocation.Top;
                      }
                    );


                    SetLocation(SnapMode, x, y, cx, cy);
                };
            #endregion

            #region MouseLeftButtonDown
            r.MouseLeftButtonDown +=
               (s, e) =>
               {

                   h = new Rectangle
                   {
                       Fill = Brushes.Black,
                   };
                   hOpacity = h.ToAnimatedOpacity();
                   hOpacity.Opacity = 0.3;

                   var p = e.GetPosition(r);

                   //Console.WriteLine("MouseLeftButtonDown " + new { p.X, p.Y });

                   GetPosition = y => y(p.X, p.Y);


                   h.AttachTo(c).MoveTo(p).SizeTo(0, 0);

                   c.Selection.Orphanize();

                   c.Selection.WindowLocation.Left = p.X;
                   c.Selection.WindowLocation.Top = p.Y;
                   c.Selection.WindowLocation.Width = 0;
                   c.Selection.WindowLocation.Height = 0;
                   c.Selection.WindowLocationChanged();
                   c.Selection.Attach();


                   Windows.WithEach(k => k.GlassOpacity.Opacity = 0);
               };
            #endregion



            #region MouseMove
            r.MouseMove +=
               (s, e) =>
               {
                   if (GetPosition != null)
                   {



                       GetSnapLocation(e.GetPosition,
                           (SnapMode, x, y, cx, cy) =>
                           {
                               //Console.WriteLine("MouseMove " + new { x, y, cx, cy });

                               if (SnapMode)
                                   hOpacity.Opacity = 0.9;
                               else
                                   hOpacity.Opacity = 0.3;

                               c.Selection.WindowLocation.Left = x;
                               c.Selection.WindowLocation.Top = y;
                               c.Selection.WindowLocation.Width = cx;
                               c.Selection.WindowLocation.Height = cy;
                               c.Selection.WindowLocationChanged();

                               h.MoveTo(x, y).SizeTo(cx, cy);
                           }
                       );
                   }
               };
            #endregion


            #region MouseLeftButtonUp
            r.MouseLeftButtonUp +=
                 (s, e) =>
                 {
                     //Console.WriteLine("MouseLeftButtonUp");

                     if (GetPosition == null)
                         return;


                     GetSnapLocation(e.GetPosition,
                        (SnapMode, x, y, cx, cy) =>
                        {
                            //Console.WriteLine("MouseLeftButtonUp " + new { x, y, cx, cy });

                            Windows.WithEach(k => k.GlassOpacity.Opacity = 1);

                            h.Orphanize();
                            c.Selection.Orphanize();

                            if (cx > 32)
                                if (cy > 32)
                                    c.CreateWindow(
                                        new Base.ApplicationCanvas.Position
                                        {
                                            Left = x,
                                            Top = y,
                                            Width = cx,
                                            Height = cy
                                        },

                                       Windows.Add
                                     );


                        }
                    );



                     GetPosition = null;


                 };
            #endregion



            #region TouchDown
            r.TouchDown +=
               (s, e) =>
               {
                   
                   h = new Rectangle
                   {
                       Fill = Brushes.Black,
                   };
                   hOpacity = h.ToAnimatedOpacity();
                   hOpacity.Opacity = 0.3;

                   var p = e.GetTouchPoint(r).Position;

                   //Console.WriteLine("MouseLeftButtonDown " + new { p.X, p.Y });

                   GetPosition = y => y(p.X, p.Y);


                   h.AttachTo(c).MoveTo(p).SizeTo(0, 0);

                   c.Selection.Orphanize();

                   c.Selection.WindowLocation.Left = p.X;
                   c.Selection.WindowLocation.Top = p.Y;
                   c.Selection.WindowLocation.Width = 0;
                   c.Selection.WindowLocation.Height = 0;
                   c.Selection.WindowLocationChanged();
                   c.Selection.Attach();


                   Windows.WithEach(k => k.GlassOpacity.Opacity = 0);
               };
            #endregion



            #region MouseMove
            r.TouchMove +=
               (s, e) =>
               {
                   if (GetPosition != null)
                   {

                       Func<UIElement, Point> e_GetPosition = x => e.GetTouchPoint(x).Position;

                       GetSnapLocation(e_GetPosition,
                           (SnapMode, x, y, cx, cy) =>
                           {
                               //Console.WriteLine("MouseMove " + new { x, y, cx, cy });

                               if (SnapMode)
                                   hOpacity.Opacity = 0.9;
                               else
                                   hOpacity.Opacity = 0.3;

                               c.Selection.WindowLocation.Left = x;
                               c.Selection.WindowLocation.Top = y;
                               c.Selection.WindowLocation.Width = cx;
                               c.Selection.WindowLocation.Height = cy;
                               c.Selection.WindowLocationChanged();

                               h.MoveTo(x, y).SizeTo(cx, cy);
                           }
                       );
                   }
               };
            #endregion


            #region TouchUp
            r.TouchUp +=
                 (s, e) =>
                 {
                     //Console.WriteLine("MouseLeftButtonUp");

                     if (GetPosition == null)
                         return;

                     Func<UIElement, Point> e_GetPosition = x => e.GetTouchPoint(x).Position;

                     GetSnapLocation(e_GetPosition,
                        (SnapMode, x, y, cx, cy) =>
                        {
                            //Console.WriteLine("MouseLeftButtonUp " + new { x, y, cx, cy });

                            Windows.WithEach(k => k.GlassOpacity.Opacity = 1);

                            h.Orphanize();
                            c.Selection.Orphanize();

                            if (cx > 32)
                                if (cy > 32)
                                    c.CreateWindow(
                                        new Base.ApplicationCanvas.Position
                                        {
                                            Left = x,
                                            Top = y,
                                            Width = cx,
                                            Height = cy
                                        },

                                       Windows.Add
                                     );


                        }
                    );



                     GetPosition = null;


                 };
            #endregion

        }
        void InitializeContent()
        {
            Colors.Blue.ToGradient(Colors.Red, Convert.ToInt32( Height) / 4).Select(
            (c, i) =>
                new Rectangle
                {
                    Fill = new SolidColorBrush(c),
                    Width = Width,
                    Height = 4,
                }.MoveTo(0, i * 4).AttachTo(this)
        ).ToArray();



            //var help = new Image
            //{
            //    Source = (KnownAssets.Path.Assets + "/help.png").ToSource()
            //}.AttachTo(this);

            //help.Opacity = 0;

            var img = new Image
            {
                Source = new Avalon.Images.jsc().Source
            }.MoveTo(Width - 128, Height - 128).AttachTo(this);

            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(this);



            //help.Opacity = 1;
            img.Opacity = 0.5;








            // cursor position calculations are not ready
            // for transofrmed elements.
            // we will provide a floor for those events...
            var shadow = new Rectangle
            {
                Width = Width,
                Height = Height,

                Fill = Brushes.Black,
            }.AttachTo(this);

            var shadowa = shadow.ToAnimatedOpacity();

            shadowa.Opacity = 0;



            var a = new AffineMesh();

            Func<double, double, double, Brush, AffinePoint> a_Add =
                (X, Y, Z, Fill) =>
                {
                    var p = new AffinePoint { Z = Z, X = X, Y = Y };
                    var h = new Rectangle { Fill = Fill, Width = 4, Height = 4 }.AttachTo(this);
                    //var ht = new TextBox { }.AttachTo(this);

                    var historysize = 64;
                    var history = new Queue<Rectangle>();

                    Action<AffinePoint> p_Update =
                        pp =>
                        {
                            if (history.Count > historysize)
                            {
                                history.Dequeue();
                            }

                            // -100 == 0.5
                            // 0 == 1
                            // +100 == 2

                            //var zoom = (pp.Z) / 1000;

                            var zoom = 0.5;

                            var pp_X = Width / 2 - 4 + pp.X * zoom;
                            var pp_Y = Height / 2 - 4 + pp.Y * zoom;
                            // Z is ignored here
                            // but could be used for sorting

                            h.MoveTo(pp_X, pp_Y);

                            //var hh = new Rectangle { Fill = Fill, Width = 4, Height = 4, Opacity = 1 }.MoveTo(pp_X, pp_Y).AttachTo(this);
                            //history.Enqueue(hh);

                            //foreach (var k in history.Select((c, i) => new { c, i }))
                            //{
                            //    k.c.Opacity = ((double)k.i / historysize) * 0.6;
                            //}

                            //ht.MoveTo(
                            //    DefaultWidth / 2 + pp.X - 4,
                            //    DefaultHeight / 2 + pp.Y + 4
                            //);

                            //ht.Text = new { pp.X, pp.Y, pp.Z }.ToString();
                        };

                    p.Tag = p_Update;

                    a.Add(p);

                    p_Update(p);

                    return p;
                };


            Enumerable.Range(0, 10).Select(X => a_Add(X * 10, 0, 0, Brushes.Red)).ToArray();
            Enumerable.Range(0, 10).Select(Y => a_Add(0, Y * 10, 0, Brushes.Blue)).ToArray();
            Enumerable.Range(0, 10).Select(Z => a_Add(0, 0, Z * 10, Brushes.GreenYellow)).ToArray();

            Enumerable.Range(0, 10).Select(X => a_Add(X * 10 + 100, 0, 0, Brushes.Red)).ToArray();
            Enumerable.Range(0, 10).Select(Y => a_Add(100, Y * 10, 0, Brushes.Blue)).ToArray();
            Enumerable.Range(0, 10).Select(Z => a_Add(100, 0, Z * 10, Brushes.GreenYellow)).ToArray();


            Enumerable.Range(0, 100).Select(X => a_Add(X * 10 + 200, 0, 0, Brushes.Red)).ToArray();
            Enumerable.Range(0, 10).Select(Y => a_Add(200, Y * 10, 0, Brushes.Blue)).ToArray();
            Enumerable.Range(0, 10).Select(Z => a_Add(200, 0, Z * 10, Brushes.GreenYellow)).ToArray();

            {
                var radius = 100;
                foreach (var i in Enumerable.Range(0, 90).Select(aa => (aa * 4).DegreesToRadians()))
                {
                    a_Add(Math.Cos(i) * radius, Math.Sin(i) * radius, 0, Brushes.GreenYellow);
                    a_Add(Math.Cos(i) * radius, 0, Math.Sin(i) * radius, Brushes.BlueViolet);
                    a_Add(0, Math.Cos(i) * radius, Math.Sin(i) * radius, Brushes.Magenta);
                }
            }

            {
                var radius = 200;
                foreach (var i in Enumerable.Range(0, 90).Select(aa => (aa * 4).DegreesToRadians()))
                {
                    a_Add(Math.Cos(i) * radius, Math.Sin(i) * radius, 0, Brushes.GreenYellow);
                    a_Add(Math.Cos(i) * radius, 0, Math.Sin(i) * radius, Brushes.BlueViolet);
                    a_Add(0, Math.Cos(i) * radius, Math.Sin(i) * radius, Brushes.Magenta);
                }
            }

            {
                var radius = 500;
                foreach (var i in Enumerable.Range(0, 90).Select(aa => (aa * 4).DegreesToRadians()))
                {
                    a_Add(Math.Cos(i) * radius, Math.Sin(i) * radius, 0, Brushes.GreenYellow);
                    a_Add(Math.Cos(i) * radius, 0, Math.Sin(i) * radius, Brushes.BlueViolet);
                    a_Add(0, Math.Cos(i) * radius, Math.Sin(i) * radius, Brushes.Magenta);
                }
            }

            a_Add(200, 0, 0, Brushes.BlueViolet);
            a_Add(0, 200, 0, Brushes.Yellow);
            a_Add(0, 0, 200, Brushes.Red);

            (1000 / 50).AtIntervalWithCounter(
                c =>
                {
                    // rotate floor
                    var _a = a.ToRotation(
                        new AffineRotation
                        {
                            XY = 0.01,
                            YZ = 0.02,
                            XZ = 0.03
                        }
                    );

                    //var _a = a.ToRotation(0, 0.01);

                    a = _a;

                    //var _a = a.ToRotation(c * 0.01, c * 0.005);
                    //var _a = a.ToRotation(c * 0.01, 0);

                    foreach (var p in _a.Points)
                    {
                        ((Action<AffinePoint>)p.Tag)(p);
                    }
                }
            );
        }
        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();
                };
        }