//public WebBrowserEvents(InternetExplorer _intExp, ref SWATWikiGenerator _write)
		//{
		//    _writer = _write;

		//    Random rand = new Random();
		//    browserId = rand.Next(100000);

		//    if (_intExp == null)
		//    {
		//        manualNavigation = true;
		//        ie = new InternetExplorerClass();
		//        //add a property for identification
		//        ie.PutProperty("ID", browserId);
		//        ie.Visible = true;
		//        object url = "about:blank";
		//        object nullObj = String.Empty;
		//        ConnectWindow(ie);
		//        ie.Navigate2(ref url, ref nullObj, ref nullObj, ref nullObj, ref nullObj);
		//        HTMLEvents.lastBrowser = browserId;
		//    }
		//    else
		//    {
		//        manualNavigation = false;
		//        ie = _intExp;
		//        ie.PutProperty("ID", browserId);
		//        ConnectWindow(ie);
		//    }


		//}


		public WebBrowserEvents(String flag, SHDocVw.InternetExplorer _intExp, ref SWATWikiGenerator _write)
		{
			_writer = _write;
			Random rand = new Random();
			browserId = rand.Next(100000);

			if (flag.Equals("AttachToWindow"))
			{
				init = false;
				manualNavigation = false;
				ie = _intExp;
				ie.PutProperty("ID", browserId);
				ConnectWindow(ie);
				//Debug.WriteLine((((HTMLDocument)ie.Document).frames).length);
				try
				{
					FramesCollection frames = (((HTMLDocument)ie.Document).frames);

					for (int i = 0; i < frames.length; i++)
					{
						object refIndex = i;
						try
						{
							HTMLDocument doc = (HTMLDocument)((HTMLWindow2Class)frames.item(ref refIndex)).document;
							if (doc.Equals((HTMLDocument)ie.Document)) continue;
							HTMLEvents aEvent = new HTMLEvents(_writer, doc, ie);
							_eventsList.Add(aEvent);
						}
						catch (UnauthorizedAccessException) { }
					}
				}
				catch (Exception) { }
				_eventsList.Insert(0, new HTMLEvents(_writer, ie.Document, ie));
				checkAttach();
			}
			else if (flag.Equals("NewWindow"))
			{
				manualNavigation = true;
				ie = new InternetExplorerClass();
				//add a property for identification
				ie.PutProperty("ID", browserId);
				ie.Visible = true;
				object url = "about:blank";
				object nullObj = String.Empty;
				ConnectWindow(ie);
				ie.Navigate2(ref url, ref nullObj, ref nullObj, ref nullObj, ref nullObj);
				HTMLEvents.lastBrowser = browserId;
			}
			else if (flag.Equals("PopUp"))
			{
				manualNavigation = false;
				ie = _intExp;
				ie.PutProperty("ID", browserId);
				ConnectWindow(ie);
			}
			else if (flag.Equals("AttachToModalWindow"))
			{
				IntPtr dialogHwnd = IntPtr.Zero;
				//int indexCount = NativeMethods.GetWindowWithSubstring("content.html", 0, 0, ref dialogHwnd);
				dialogHwnd = NativeMethods.GetForegroundWindow();


				if (dialogHwnd != IntPtr.Zero)
				{
					// This block of code SHOULD take a window handle and return an IHTMLDocument object to dialogDoc
					// Then it will assign that object to _doc. It follows an example in the MSDN library and WatiN uses it as well.
					// If it performs correctly, then we should be attached to the Dialog.
					dialogHwnd = NativeMethods.GetChildWindowHwnd(dialogHwnd, "Internet Explorer_Server");
					//dialogHwnd = NativeMethods.GetForegroundWindow();

					Int32 result = 0;
					Int32 dialog;
					Int32 message;

					message = NativeMethods.RegisterWindowMessage("WM_HTML_GETOBJECT");
					NativeMethods.SendMessageTimeout(dialogHwnd, message, 0, 0, NativeMethods.SMTO_ABORTIFHUNG, 1000, ref result);

					//NativeMethods.SendMessage(dialogHwnd, NativeMethods.WM_CLOSE, 0, 0);

					IHTMLDocument2 dialogDoc = null;
					System.Guid dialogID = typeof(mshtml.HTMLDocument).GUID;
					dialog = NativeMethods.ObjectFromLresult(result, ref dialogID, 0, ref dialogDoc);

					//HTMLFrameBase dialogDoc = null;
					//System.Guid dialogID = typeof(mshtml.HTMLFrameBase).GUID;
					//dialog = NativeMethods.ObjectFromLresult(result, ref dialogID, 0, ref dialogDoc);

					//dialogDoc.title = "Hello?";

					//IHTMLDialog dialogDoc = null;
					//System.Guid dialogID = typeof(mshtml.HTMLDialog).GUID;
					//dialog = NativeMethods.ObjectFromLresult(result, ref dialogID, 0, ref dialogDoc);




					//IHTMLWindow2 dialogDoc = null;
					//System.Guid dialogID = typeof(IHTMLWindow2).GUID;
					//dialog = NativeMethods.ObjectFromLresult(result, ref dialogID, 0, ref dialogDoc);

					//dialogDoc.close();

					//Console.WriteLine("URL: " + dialogDoc.close + "\nTitle: " + dialogDoc.title);

					//ie = (InternetExplorer)dialogDoc;
					//ie.PutProperty("ID", browserId);
					//ConnectWindow(ie);

					//doc = (HTMLDocument)dialogDoc;



				}
			}
			_browserList.Add(this);

		}
		/**
		 * Document has completed.
		 **/
		private void ie_DocumentComplete(object pDisp, ref object URL)
		{
			if (((String)URL).Equals("about:blank")) manualNavigation = true;
			//Debug.WriteLine("Document Complete");
			if (HTMLEvents.AssertElement) return;

			//Document complete begin reset timer
			timer.Interval = 3000;
			timer.Start();

			Boolean initSet = false;
			//Top level document?
			if (((HTMLDocument)(((IWebBrowser2)pDisp).Document)).Equals((HTMLDocument)ie.Document))
			{
				//return if not first time processing
				if (!init) return;
				init = false;
				initSet = true;
			}
			HTMLEvents currentElement = new HTMLEvents(_writer, ((IWebBrowser2)pDisp).Document, ie);
			//_eventsList.Add(temp, currentElement);  
			//if (!init) _eventsList.Add(currentElement);
			int id = (int)(((IWebBrowser2)pDisp).GetProperty("ID"));
			if (!initSet) _eventsList.Add(currentElement);
			else _eventsList.Insert(0, currentElement);

		}