/// <summary>Raises the <see cref="CreateWindow"/> event.</summary>
        /// <param name="e">The data for the event.</param>
        protected virtual void OnCreateWindow(GeckoCreateWindowEventArgs e)
        {
            var evnt = (EventHandler <GeckoCreateWindowEventArgs>)Events[CreateWindowEvent];

            if (evnt != null)
            {
                evnt(this, e);
            }
        }
		/// <summary>Raises the <see cref="CreateWindow"/> event.</summary>
		/// <param name="e">The data for the event.</param>
		protected virtual void OnCreateWindow( GeckoCreateWindowEventArgs e )
		{
			var evnt = ( EventHandler<GeckoCreateWindowEventArgs> ) Events[ CreateWindowEvent ];
			if ( evnt != null ) evnt( this, e );
		}
            public nsIWebBrowserChrome CreateChromeWindow(nsIWebBrowserChrome parent, uint chromeFlags)
            {
                // for chrome windows, we can use the AppShellService to create the window using some built-in xulrunner code
                GeckoWindowFlags flags = (GeckoWindowFlags)chromeFlags;
                if ((flags & GeckoWindowFlags.OpenAsChrome) != 0)
                {
                    // obtain the services we need
                    // nsIAppShellService appShellService = Xpcom.GetService<nsIAppShellService>("@mozilla.org/appshell/appShellService;1");

                    // create the child window
                    nsIXULWindow xulChild = AppShellService.CreateTopLevelWindow(null, null, chromeFlags, -1, -1);

                    // this little gem allows the GeckoWebBrowser to be properly activated when it gains the focus again
                    if (parent is GeckoWebBrowser && (flags & GeckoWindowFlags.OpenAsDialog) != 0)
                    {
                        EventHandler gotFocus = null;
                        gotFocus = delegate(object sender, EventArgs e)
                        {
                            var geckoWebBrowser = (GeckoWebBrowser)sender;
                            geckoWebBrowser.GotFocus -= gotFocus;

                            if (geckoWebBrowser.WebBrowserFocus != null)
                                geckoWebBrowser.WebBrowserFocus.Activate();
                        };
                        (parent as GeckoWebBrowser).GotFocus += gotFocus;
                    }

                    // return the chrome
                    return Xpcom.QueryInterface<nsIWebBrowserChrome>(xulChild);
                }

                GeckoWebBrowser browser = parent as GeckoWebBrowser;
                if (browser != null)
                {
                    GeckoCreateWindowEventArgs e = new GeckoCreateWindowEventArgs((GeckoWindowFlags)chromeFlags);
                    browser.OnCreateWindow(e);

                    if (e.WebBrowser != null)
                    {
                        // set flags
                        ((nsIWebBrowserChrome)e.WebBrowser).SetChromeFlagsAttribute(chromeFlags);
                        return e.WebBrowser;
                    }

                    //nsIAppShellService appShellService = Xpcom.GetService<nsIAppShellService>(Contracts.AppShellService);
                    nsIXULWindow xulChild = AppShellService.CreateTopLevelWindow(null, null, chromeFlags, -1, -1);
                    return Xpcom.QueryInterface<nsIWebBrowserChrome>(xulChild);
                }
                return null;
            }
        void _browser_CreateWindow(object sender, GeckoCreateWindowEventArgs e)
        {
            //Console.WriteLine(e);
            if (!string.IsNullOrWhiteSpace( e.Uri))
            {
                string browserUrl = _browser.Url.GetLeftPart(UriPartial.Path);
                string browserRoot = browserUrl.Substring(0, browserUrl.LastIndexOf('/'));
                if (e.Uri.StartsWith(browserRoot) && false)
                {
                    //then we're opening a local file
                }
                else
                {

                    var uri = e.Uri == "https://www.paypal.com/cgi-bin/webscr" ?
                        "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=DXBL3RF9PNXJU" :   //donate button was clicked. then we change the URL from a form submit
                        e.Uri;

                    //Create a temporary file to open in the browser. This is hackish, but gives us a ton more control
                    //of how the page loads, in a platform and browser-agnostic way. Without this, we'd have a hard time
                    //opening to anchors.
                    string template =
            @"<html>
            <head>
            <meta http-equiv='refresh' content='0;url={0}' />
            </head>
            </html>
            ";
                    string tempFileContents = string.Format(template, uri);
                    string tempPath = IOPath.Combine(IOPath.GetTempPath(), IOPath.GetRandomFileName());
                    string browseTempFile = IOPath.Combine(tempPath, "temp.html");

                    Directory.CreateDirectory(tempPath);

                    using (var f = File.OpenWrite(browseTempFile))
                    using (var sw = new StreamWriter(f))
                    {
                        sw.Write(tempFileContents);
                        _tempPathsCreated.Add(tempPath);
                    }

                    ProcessStartInfo info = new ProcessStartInfo()
                    {
                        UseShellExecute = true,
                        FileName = "file:///" + new FileInfo(browseTempFile).FullName
                    };

                    var proc = new Process()
                    {
                        StartInfo = info
                    };

                    proc.Start();

                }

                e.Cancel = true;
                Console.WriteLine(e.Uri);
            }
        }