// clsMain.cs
// Copyright (C) 2008 Karel Jordaan karel_jordaan@yahoo.com.au
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using System.Globalization;
using FileScope;
using FileScope.Gnutella2;
using GUIControlLogic;
using GUIControlLogic.Error;
using GUIControlLogic.Persistence;
using ZANZI.ctrl;
using TabControl=Crownwood.Magic.Controls.TabControl;
using TabPage=Crownwood.Magic.Controls.TabPage;

namespace ZANZIClient
{
    /// <summary>
    /// clsMain is the main cls for this application.
    /// </summary>
    public class clsMain : frmMain
    {
        #region Variable declaration 

        private const string LOG_FILE = "ZANZIClient_ErrorLog.txt";
        private readonly ZANZILib.clsMain clsEngen;
        private readonly clsSystemLog objSystemLog;
        private clsChat clsc;
        internal clsSettings clss; //= new GUIControlLogic.Persistence.clsSettings();
        //until cronwood can hide a tabpage the clsTransfer will be global
        internal clsTransfer clst;
        private IContainer components;
        private TabPage connectionpage = new TabPage();
        private TabPage discoverypage = new TabPage();
        private TabPage hostpage = new TabPage();
        //private GUIControlLogic.Persistence.clsHistory clsh;
        private int m_FileNewCount;

        // Create a new page with the appropriate control for display, title text and image
        private  StatusBarPanel sbConnStatus;
        private StatusBarPanel sbSwarmSize;
        private StatusBarPanel snNetworkStat;
        private TabPage systempage = new TabPage();
        internal TabControl tabControl1;
        protected ToolTip toolTip1;
        private TabPage transferpage = new TabPage();

        //private clsTreeView m_clsTreeView ;

        #endregion

        #region Menu handlers

        private void mnuFileNewSearch_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;

                m_FileNewCount += 1;

                frmSearch frm = new frmSearch(this, m_FileNewCount);
                AddTabPage(frm, 5);
                MakeTabVisable();

                Cursor = Cursors.Default;
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        private void mnuViewHistory_Click(object sender, EventArgs e)
        {
        }

        private void mnuToolsLanguage_Click(object sender, EventArgs e)
        {
            try
            {
                clsLanguage frm = new clsLanguage(this);
                //throw(new clsExeption ());
                frm.Show();
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }


        private void mnuToolsOptions_Click(object sender, EventArgs e)
        {
            try
            {
                frmOptions frm = new frmOptions(this);
                //throw(new clsExeption ());
                frm.Show();
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        private void mnuWindowCascade_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void mnuWindowTileHorizontal_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void mnuWindowTileVertical_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }


        private void mnuHelpAbout_Click_1(object sender, EventArgs e)
        {
            frmAbout frm = new frmAbout(this);
            frm.Show();
        }

        private void mnuToolsChat_Click(object sender, EventArgs e)
        {
//			//ZANZIClient.frmSearch frm = (ZANZIClient.frmSearch)this.ActiveMdiChild;
//			ZANZIClient.frmSearch frm = (ZANZIClient.frmSearch) tabControl1.ActiveControl ;
//
//			//ZANZIClient.frmChat chatfrm = new frmChat(this,frm.lstCharacteristics,frm.m_ResultOneObj, frm.m_CurrentResultRecordGUID);
//			ZANZIClient.frmChat chatfrm = new frmChat(this,frm.m_ResultOneObj, frm.m_CurrentResultRecordGUID);
//			//chatfrm.Show();
//
//			AddTabPage(chatfrm,1);

            //zzoi 13 mrt 2009 commented for now
            //clsc.ChatIP();

            MessageBox.Show("Chat ot fucntional yet.");
        }

        private void mnuEditCopy_Click(object sender, EventArgs e)
        {
            // Determine the active child form.
            Form activeChild = ActiveMdiChild;

            // If there is an active child form, find the active control, which
            // in this example should be a RichTextBox.
            if (activeChild != null)
            {
                try
                {
                    TextBox theBox = (TextBox) activeChild.ActiveControl;
                    if (theBox != null)
                    {
                        // Put the selected text on the Clipboard.
                        Clipboard.SetDataObject(theBox.SelectedText);
                    }
                }
                catch
                {
                    MessageBox.Show("You need to select a TextBox.");
                }
            }
        }

        private void mnuEditPaste_Click(object sender, EventArgs e)
        {
            // Determine the active child form.
            Form activeChild = ActiveMdiChild;

            // If there is an active child form, find the active control, which
            // in this example should be a RichTextBox.
            if (activeChild != null)
            {
                try
                {
                    TextBox theBox = (TextBox) activeChild.ActiveControl;
                    if (theBox != null)
                    {
                        // Create a new instance of the DataObject interface.
                        IDataObject data = Clipboard.GetDataObject();
                        // If the data is text, then set the text of the 
                        // RichTextBox to the text in the clipboard.
                        if (data.GetDataPresent(DataFormats.Text))
                        {
                            theBox.SelectedText = data.GetData(DataFormats.Text).ToString();
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("You need to select a TextBox.");
                }
            }
        }

        private void mnuEditCut_Click(object sender, EventArgs e)
        {
            // Determine the active child form.
            Form activeChild = ActiveMdiChild;

            // If there is an active child form, find the active control, which
            // in this example should be a RichTextBox.
            if (activeChild != null)
            {
                try
                {
                    TextBox theBox = (TextBox) activeChild.ActiveControl;
                    if (theBox != null)
                    {
                        // Put the selected text on the Clipboard.
                        Clipboard.SetDataObject(theBox.SelectedText);
                        theBox.Text = "";
                    }
                }
                catch
                {
                    MessageBox.Show("You need to select a TextBox.");
                }
            }
        }

        private void mnuFileOpen_Click(object sender, EventArgs e)
        {
            //zzoi 9 mrt 2009 default at present engen already started
            // no discovery bazooka yet
            // therefore disable

            //the engen may not be running
            clsEngen.StartEngen();
            StartStop.Start();
        }

        private void mnuFileSave_Click(object sender, EventArgs e)
        {
            ////ctrlHosts. mnuHostAdd_Click
            ////call the tab and constructor that will show the dlg for new host
            //clsHosts cls = new clsHosts();
            //ctrlHosts ctrl = null;
            ////if not extst
            //if (!tabControl1.TabPages.Contains(hostpage))
            //{
            //    ctrl = cls.GetHostsCtrl();

            //    //zzoi 2June2008 add to reset culture event
            //    CultureChanged += ctrl.ResetCulture;

            //    hostpage = AddTabPage(ctrl, 0);
            //    MakeTabVisable();
            //}
            //else
            //{
            //    hostpage.Selected = true;
            //}

            mnuVeiwHostCache_Click(null, null);

            //add the host to the host list
            ((ctrlHosts)hostpage.Control).mnuHostAdd_Click(null, null);
            //ctrl.mnuHostAdd_Click(null, null);

            //connect to the host
            //ctrl.mnuHostConnect_Click(null, null);
            ((ctrlHosts)hostpage.Control).mnuHostConnect_Click(null, null);
        }

        private void mnuToolsUpdate_Click_1(object sender, EventArgs e)
        {
        }

        private void mnuViewSystemLog_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            //if not extst
            if (!tabControl1.TabPages.Contains(systempage))
            {
                ctrlLog ctrl = objSystemLog.GetSystemLogCtrl;
                systempage = AddTabPage(ctrl, 2);

                MakeTabVisable();
            }
            else
            {
                //zzoi 9mrt 2009 show tab
                systempage.Visible = true;
                systempage.Selected = true;
            }
            Cursor = Cursors.Default;
        }

        internal void mnuViewTransfers_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            //if not extst
            if (!tabControl1.TabPages.Contains(transferpage))
            {
                ctrlTransfer ctrl = new ctrlTransfer(this);

                //zzoi 2June2008 add to reset culture event
                CultureChanged += ctrl.ResetCulture;


                transferpage = AddTabPage(ctrl, 4);
                //throw(new clsExeption ());
                //frm.Show();

                MakeTabVisable();
            }
            else
            {
                //zzoi 9mrt 2009 show tab
                transferpage.Visible = true;
                transferpage.Selected = true;
            }

            Cursor = Cursors.Default;
        }

        private void mnuViewDiscovery_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                //if not extst
                if (!tabControl1.TabPages.Contains(discoverypage))
                {
                    clsDiscovery cls = new clsDiscovery();
                    ctrlDiscovery ctrl = cls.GetDiscoveryCtrl();

                    //zzoi 2June2008 add to reset culture event
                    CultureChanged += ctrl.ResetCulture;

                    discoverypage = AddTabPage(ctrl, 6);
                    MakeTabVisable();
                }
                else
                {
                    //zzoi 9mrt 2009 show tab
                    discoverypage.Visible = true;
                    discoverypage.Selected = true;
                }
                Cursor = Cursors.Default;
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        internal void mnuVeiwHostCache_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                //if not extst
                if (!tabControl1.TabPages.Contains(hostpage))
                {
                    clsHosts cls = new clsHosts();
                    ctrlHosts ctrl = cls.GetHostsCtrl();

                    //zzoi 2June2008 add to reset culture event
                    CultureChanged += ctrl.ResetCulture;

                    hostpage = AddTabPage(ctrl, 0);
                    MakeTabVisable();
                }
                else
                {
                    //zzoi 9mrt 2009 show tab
                    hostpage.Visible = true;
                    hostpage.Selected = true;
                }
                Cursor = Cursors.Default;
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }


        private void mnuViewNeighbours_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                //if not extst
                if (!tabControl1.TabPages.Contains(connectionpage))
                {
                    clsCnn cls = new clsCnn();
                    ctrlCnn ctrl = cls.GetCnnCtrl();

                    //zzoi 2June2008 add to reset culture event
                    CultureChanged += ctrl.ResetCulture;

                    connectionpage = AddTabPage(ctrl, 7);
                    MakeTabVisable();
                }
                else
                {
                    //zzoi 9mrt 2009 show tab
                    connectionpage.Visible=true;
                    connectionpage.Selected = true;
                }
                Cursor = Cursors.Default;
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        private void mnuToolsUpdate_Click(Object sender, EventArgs e)
        {
            try
            {
                //use process.startinfo to exec external app for update
                //CVUpdate.clsMain clsUpdate = new CVUpdate.clsMain();
                //clsUpdate.DoUpdate();

                //zzoi 13 mrt 2009 
                MessageBox.Show("Update not fucntional yet.");
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        private void mnuHelpAbout_Click(Object sender, EventArgs e)
        {
            try
            {
                frmAbout frm = new frmAbout(this);
                frm.Show();
                //throw(new clsExeption ("test"));
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        #endregion

        //zzoi 2June 2008 reset event to change language text

        #region Delegates

        public delegate void ResetCultureDelegate();

        //\\zzoi 9 mrt 2009 update the status bar on this thread
        public delegate void updateStatusBar(string statustext);

        #endregion

// delegate declaration
        public static event ResetCultureDelegate CultureChanged;

        private void mnuFileClose_Click(object sender, EventArgs e)
        {
            //StartStop.Stop();
            clsEngen.StopEngen();
        }

        private void mnuFileExit_Click_1(object sender, EventArgs e)
        {
            //show message to wait
            Cursor = Cursors.WaitCursor;
            //clsEngen.StopEngen();
            //exit appllication


            try
            {
                //zzoi 3 june 2008 shutdown the p2p engen
                //set the hourglass
                Cursor = Cursors.WaitCursor;
                //clsEngen.StopEngen();


                //Auto save settings
                //clsh.SaveHistory(this); 
                //clsh.Dispose();

                //User Settings
                clss.SaveMDIFormLocationSettings();
                clss.Dispose();

                #region P2P engen closing 

                //main.search.SerializeSearches();
                //main.trayIcon.Visible = false;

                clsEngen.StopEngen();
                clss.SaveFileScopeSettings();

                #endregion

                Application.Exit();
            }
            catch (Exception ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        #region Custom Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support 
        /// modify the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(clsMain));
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.tabControl1 = new Crownwood.Magic.Controls.TabControl();
            this.sbConnStatus = new System.Windows.Forms.StatusBarPanel();
            this.sbSwarmSize = new System.Windows.Forms.StatusBarPanel();
            this.snNetworkStat = new System.Windows.Forms.StatusBarPanel();
            ((System.ComponentModel.ISupportInitialize)(this.sbConnStatus)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.sbSwarmSize)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.snNetworkStat)).BeginInit();
            this.SuspendLayout();
            // 
            // mainMenu1
            // 
            resources.ApplyResources(this.mainMenu1, "mainMenu1");
            // 
            // mnuFile
            // 
            resources.ApplyResources(this.mnuFile, "mnuFile");
            // 
            // mnuFileExit
            // 
            resources.ApplyResources(this.mnuFileExit, "mnuFileExit");
            this.mnuFileExit.Click += new System.EventHandler(this.mnuFileExit_Click);
            // 
            // mnuTools
            // 
            resources.ApplyResources(this.mnuTools, "mnuTools");
            // 
            // mnuToolsLanguage
            // 
            resources.ApplyResources(this.mnuToolsLanguage, "mnuToolsLanguage");
            this.mnuToolsLanguage.Click += new System.EventHandler(this.mnuToolsLanguage_Click);
            // 
            // mnuToolsUpdate
            // 
            resources.ApplyResources(this.mnuToolsUpdate, "mnuToolsUpdate");
            this.mnuToolsUpdate.Click += new System.EventHandler(this.mnuToolsUpdate_Click);
            // 
            // mnuHelp
            // 
            resources.ApplyResources(this.mnuHelp, "mnuHelp");
            // 
            // mnuHelpAbout
            // 
            resources.ApplyResources(this.mnuHelpAbout, "mnuHelpAbout");
            this.mnuHelpAbout.Click += new System.EventHandler(this.mnuHelpAbout_Click);
            // 
            // mnuHelpContents
            // 
            resources.ApplyResources(this.mnuHelpContents, "mnuHelpContents");
            this.mnuHelpContents.Click += new System.EventHandler(this.mnuHelpContents_Click);
            // 
            // statusBar1
            // 
            this.statusBar1.AccessibleDescription = null;
            this.statusBar1.AccessibleName = null;
            resources.ApplyResources(this.statusBar1, "statusBar1");
            this.statusBar1.BackgroundImage = null;
            this.statusBar1.Font = null;
            this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
            this.sbConnStatus,
            this.sbSwarmSize,
            this.snNetworkStat});
            this.toolTip1.SetToolTip(this.statusBar1, resources.GetString("statusBar1.ToolTip"));
            // 
            // mnuToolsOptions
            // 
            resources.ApplyResources(this.mnuToolsOptions, "mnuToolsOptions");
            this.mnuToolsOptions.Click += new System.EventHandler(this.mnuToolsOptions_Click);
            // 
            // mnuFileNew
            // 
            resources.ApplyResources(this.mnuFileNew, "mnuFileNew");
            // 
            // mnuFileNewSearch
            // 
            resources.ApplyResources(this.mnuFileNewSearch, "mnuFileNewSearch");
            this.mnuFileNewSearch.Click += new System.EventHandler(this.mnuFileNewSearch_Click);
            // 
            // mnuFileOpen
            // 
            resources.ApplyResources(this.mnuFileOpen, "mnuFileOpen");
            this.mnuFileOpen.Click += new System.EventHandler(this.mnuFileOpen_Click);
            // 
            // mnuFileClose
            // 
            resources.ApplyResources(this.mnuFileClose, "mnuFileClose");
            this.mnuFileClose.Click += new System.EventHandler(this.mnuFileClose_Click);
            // 
            // mnuFileSave
            // 
            resources.ApplyResources(this.mnuFileSave, "mnuFileSave");
            this.mnuFileSave.Click += new System.EventHandler(this.mnuFileSave_Click);
            // 
            // menuItem3
            // 
            resources.ApplyResources(this.menuItem3, "menuItem3");
            // 
            // menuItem5
            // 
            resources.ApplyResources(this.menuItem5, "menuItem5");
            // 
            // menuItem7
            // 
            resources.ApplyResources(this.menuItem7, "menuItem7");
            // 
            // menuItem8
            // 
            resources.ApplyResources(this.menuItem8, "menuItem8");
            // 
            // mnuEditCut
            // 
            resources.ApplyResources(this.mnuEditCut, "mnuEditCut");
            this.mnuEditCut.Click += new System.EventHandler(this.mnuEditCut_Click);
            // 
            // mnuEditCopy
            // 
            resources.ApplyResources(this.mnuEditCopy, "mnuEditCopy");
            this.mnuEditCopy.Click += new System.EventHandler(this.mnuEditCopy_Click);
            // 
            // mnuEditPaste
            // 
            resources.ApplyResources(this.mnuEditPaste, "mnuEditPaste");
            this.mnuEditPaste.Click += new System.EventHandler(this.mnuEditPaste_Click);
            // 
            // menuItem9
            // 
            resources.ApplyResources(this.menuItem9, "menuItem9");
            // 
            // mnuEditFind
            // 
            resources.ApplyResources(this.mnuEditFind, "mnuEditFind");
            this.mnuEditFind.Click += new System.EventHandler(this.mnuEditFind_Click);
            // 
            // mnuToolsChat
            // 
            resources.ApplyResources(this.mnuToolsChat, "mnuToolsChat");
            this.mnuToolsChat.Click += new System.EventHandler(this.mnuToolsChat_Click);
            // 
            // mnuToolsVoice
            // 
            resources.ApplyResources(this.mnuToolsVoice, "mnuToolsVoice");
            this.mnuToolsVoice.Click += new System.EventHandler(this.mnuToolsVoice_Click);
            // 
            // mnuToolsExchangeRates
            // 
            resources.ApplyResources(this.mnuToolsExchangeRates, "mnuToolsExchangeRates");
            this.mnuToolsExchangeRates.Click += new System.EventHandler(this.mnuToolsExchangeRates_Click);
            // 
            // mnuToolsCompare
            // 
            resources.ApplyResources(this.mnuToolsCompare, "mnuToolsCompare");
            // 
            // mnuEdit
            // 
            resources.ApplyResources(this.mnuEdit, "mnuEdit");
            // 
            // mnuView
            // 
            resources.ApplyResources(this.mnuView, "mnuView");
            // 
            // mnuViewSystemLog
            // 
            resources.ApplyResources(this.mnuViewSystemLog, "mnuViewSystemLog");
            this.mnuViewSystemLog.Click += new System.EventHandler(this.mnuViewSystemLog_Click);
            // 
            // mnuViewTransfers
            // 
            resources.ApplyResources(this.mnuViewTransfers, "mnuViewTransfers");
            this.mnuViewTransfers.Click += new System.EventHandler(this.mnuViewTransfers_Click);
            // 
            // mnuObject
            // 
            resources.ApplyResources(this.mnuObject, "mnuObject");
            // 
            // mnuObjectOpen
            // 
            resources.ApplyResources(this.mnuObjectOpen, "mnuObjectOpen");
            // 
            // mnuObjectClose
            // 
            resources.ApplyResources(this.mnuObjectClose, "mnuObjectClose");
            // 
            // mnuObjectSave
            // 
            resources.ApplyResources(this.mnuObjectSave, "mnuObjectSave");
            // 
            // mnuObjectSaveAs
            // 
            resources.ApplyResources(this.mnuObjectSaveAs, "mnuObjectSaveAs");
            // 
            // mnuObjectPrint
            // 
            resources.ApplyResources(this.mnuObjectPrint, "mnuObjectPrint");
            // 
            // mnuViewDiscovery
            // 
            resources.ApplyResources(this.mnuViewDiscovery, "mnuViewDiscovery");
            this.mnuViewDiscovery.Click += new System.EventHandler(this.mnuViewDiscovery_Click);
            // 
            // imageList1
            // 
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            this.imageList1.Images.SetKeyName(0, "");
            this.imageList1.Images.SetKeyName(1, "");
            this.imageList1.Images.SetKeyName(2, "");
            this.imageList1.Images.SetKeyName(3, "");
            this.imageList1.Images.SetKeyName(4, "");
            this.imageList1.Images.SetKeyName(5, "");
            this.imageList1.Images.SetKeyName(6, "");
            this.imageList1.Images.SetKeyName(7, "");
            // 
            // mnuVeiwHostCache
            // 
            resources.ApplyResources(this.mnuVeiwHostCache, "mnuVeiwHostCache");
            this.mnuVeiwHostCache.Click += new System.EventHandler(this.mnuVeiwHostCache_Click);
            // 
            // mnuViewNeighbours
            // 
            resources.ApplyResources(this.mnuViewNeighbours, "mnuViewNeighbours");
            this.mnuViewNeighbours.Click += new System.EventHandler(this.mnuViewNeighbours_Click);
            // 
            // tabControl1
            // 
            this.tabControl1.AccessibleDescription = null;
            this.tabControl1.AccessibleName = null;
            resources.ApplyResources(this.tabControl1, "tabControl1");
            this.tabControl1.Appearance = Crownwood.Magic.Controls.TabControl.VisualAppearance.MultiDocument;
            this.tabControl1.BackgroundImage = null;
            this.tabControl1.Font = null;
            this.tabControl1.HideTabsMode = Crownwood.Magic.Controls.TabControl.HideTabsModes.ShowAlways;
            this.tabControl1.IDEPixelBorder = false;
            this.tabControl1.Multiline = true;
            this.tabControl1.Name = "tabControl1";
            this.toolTip1.SetToolTip(this.tabControl1, resources.GetString("tabControl1.ToolTip"));
            this.tabControl1.ClosePressed += new System.EventHandler(this.tabControl1_ClosePressed);
            // 
            // sbConnStatus
            // 
            resources.ApplyResources(this.sbConnStatus, "sbConnStatus");
            this.sbConnStatus.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
            this.sbConnStatus.Icon = null;
            // 
            // sbSwarmSize
            // 
            resources.ApplyResources(this.sbSwarmSize, "sbSwarmSize");
            this.sbSwarmSize.Icon = null;
            // 
            // snNetworkStat
            // 
            resources.ApplyResources(this.snNetworkStat, "snNetworkStat");
            this.snNetworkStat.Icon = null;
            // 
            // clsMain
            // 
            this.AccessibleDescription = null;
            this.AccessibleName = null;
            resources.ApplyResources(this, "$this");
            this.BackColor = System.Drawing.SystemColors.Control;
            this.BackgroundImage = null;
            this.Controls.Add(this.tabControl1);
            this.Font = null;
            this.Icon = null;
            this.Name = "clsMain";
            this.toolTip1.SetToolTip(this, resources.GetString("$this.ToolTip"));
            this.Controls.SetChildIndex(this.statusBar1, 0);
            this.Controls.SetChildIndex(this.tabControl1, 0);
            ((System.ComponentModel.ISupportInitialize)(this.sbConnStatus)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.sbSwarmSize)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.snNetworkStat)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        #region Functions

        /// <summary>Main entry for clsMain</summary>
        /// <returns>Takes no variables and returns nothing</returns>
        public clsMain(Thread tr, ZANZILib.clsMain cls, clsSystemLog ctrl, clsSettings clsss)
        {
            try
            {
                //register for events
                GUIBridge.NetworkStatusChange += NetworkStatusChange;
                

                //add traces
                //Trace.Listeners.Add(new GUIControlLogic.Error.clsWebReportListener());
                //Trace.Listeners.Add(new GUIControlLogic.Error.clsTextWriterListener());
                //Email
                //GUIControlLogic.Error.clsEmailListener el = new GUIControlLogic.Error.clsEmailListener(ex);
                //Trace.Listeners.Add (el);

                //ref the settings class
                clss = clsss;



                objSystemLog = ctrl;


                clsEngen = cls;
                //User Settings
                clss.LoadMDIFormLocationSettings(this);

                //				clsh = new GUIControlLogic.Persistence.clsHistory  (clss.m_SerialOptionsSetting,this );
                //				clsh.LoadHistory();

                base.Closing += clsMain_Closing;
                
                
                InitializeComponent();


                tr.Abort();
                Application.DoEvents();

                //zzoi 28Feb 2009
                //set the culture to english default
                //for development purposes

#if DEBUG
                CultureInfo ci = new CultureInfo("en");
                Thread.CurrentThread.CurrentUICulture = ci;
                //CultureChanged();
                ResetCulture();

#endif

            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        /// <summary>
        /// This function resets the text after a culture change has been made with clsLanuage
        /// <returns>Takes no variables and returns nothing</returns>
        /// </summary>
        public void ResetCulture()
        {
            try
            {
                //zzoi 6 June 2008 added mnuitems for reset to other language
                //Text = resources.GetString("$this.Text");
                mnuEdit.Text = resources.GetString("mnuEdit.Text");
                mnuEditCopy.Text = resources.GetString("mnuEditCopy.Text");
                mnuEditCut.Text = resources.GetString("mnuEditCut.Text");
                mnuEditFind.Text = resources.GetString("mnuEditFind.Text");
                mnuEditPaste.Text = resources.GetString("mnuEdit.Text");

                mnuFile.Text = resources.GetString("mnuFile.Text");
                mnuFileClose.Text = resources.GetString("mnuFileClose.Text");
                mnuFileExit.Text = resources.GetString("mnuFileExit.Text");
                mnuFileNew.Text = resources.GetString("mnuFileNew.Text");
                mnuFileNewSearch.Text = resources.GetString("mnuFileNewSearch.Text");
                mnuFileOpen.Text = resources.GetString("mnuFileOpen.Text");
                mnuFileSave.Text = resources.GetString("mnuFileSave.Text");

                mnuHelp.Text = resources.GetString("mnuHelp.Text");
                mnuHelpAbout.Text = resources.GetString("mnuHelpAbout.Text");
                mnuHelpContents.Text = resources.GetString("mnuHelpContents.Text");

                mnuTools.Text = resources.GetString("mnuTools.Text");
                mnuToolsLanguage.Text = resources.GetString("mnuToolsLanguage.Text");
                //mnuToolsSound.Text = resources.GetString("mnuToolsSound.Text");
                mnuToolsUpdate.Text = resources.GetString("mnuToolsUpdate.Text");
                mnuToolsChat.Text = resources.GetString("mnuToolsChat.Text");
                mnuToolsCompare.Text = resources.GetString("mnuToolsCompare.Text");
                mnuToolsExchangeRates.Text = resources.GetString("mnuToolsExchangeRates.Text");
                mnuToolsLanguage.Text = resources.GetString("mnuToolsLanguage.Text");
                mnuToolsOptions.Text = resources.GetString("mnuToolsOptions.Text");
                mnuToolsVoice.Text = resources.GetString("mnuToolsVoice.Text");

                mnuView.Text = resources.GetString("mnuView.Text");
                mnuVeiwHostCache.Text = resources.GetString("mnuVeiwHostCache.Text");
                mnuViewDiscovery.Text = resources.GetString("mnuViewDiscovery.Text");
                mnuViewNeighbours.Text = resources.GetString("mnuViewNeighbours.Text");
                mnuViewSystemLog.Text = resources.GetString("mnuViewSystemLog.Text");
                mnuViewTransfers.Text = resources.GetString("mnuViewTransfers.Text");

                mnuObject.Text = resources.GetString("mnuObject.Text");
                mnuObjectClose.Text = resources.GetString("mnuObjectClose.Text");
                mnuObjectOpen.Text = resources.GetString("mnuObjectOpen.Text");
                mnuObjectPrint.Text = resources.GetString("mnuObjectPrint.Text");
                mnuObjectSave.Text = resources.GetString("mnuObjectSave.Text");
                mnuObjectSaveAs.Text = resources.GetString("mnuObjectSaveAs.Text");
                //mnuObject.Text = resources.GetString("mnuObject.Text");
                //mnuObject.Text = resources.GetString("mnuObject.Text");


                //Reset the culture of any open objects by raising a event to subscribers.
                if(CultureChanged != null)
                CultureChanged();

                //reset the tab caption for each tab
                for (int i = 0; i < tabControl1.TabPages.Count; i++)
                {
                    tabControl1.TabPages[i].Title = (tabControl1.TabPages[i].Control as Ictrl).Text;
                    tabControl1.TabPages[i].Refresh();
                }
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        private void MakeTabVisable()
        {
            if (! tabControl1.Visible)
            {
                tabControl1.Visible = true;
            }
        }

        public TabPage AddTabPage(Control frm, int imgidx)
        {
            // Create a new page with the appropriate control for display, title text and image
            TabPage page = new TabPage(frm.Text, frm, imageList1, imgidx);

            // Make this page become selected when added
            page.Selected = true;

            tabControl1.TabPages.Add(page);

            return page;
        }
        /// <summary>
        /// Getting Network status change from the p2p lib
        /// </summary>
        /// <param name="status">Status string sent from the engen</param>
        public void NetworkStatusChange(string status)
        {
            //sbConnStatus.Text = status;
            this.BeginInvoke(new clsMain.updateStatusBar(this.UpdateStatusBar), status);
        }

        public  void UpdateStatusBar(string newText)
        {
            sbConnStatus.Text = newText;
        }

        #endregion

        #region Events

        private void clsMain_Load(object sender, EventArgs e)
        {
            clsc = new clsChat(this);

            TopMost = true;
            Activate();
            Thread.Sleep(100);
            TopMost = false;

            //zzoi 9 June 2008 set the connection status
            sbConnStatus.Text = "Disconnected";

            //zzoi 28Feb 2009
            //set the culture to english default
            //for development purposes

#if DEBUG
            CultureInfo ci = new CultureInfo("en");
            Thread.CurrentThread.CurrentUICulture = ci;
            //CultureChanged();
            ResetCulture();

#endif
        }

        private void clsMain_Closing(object sender, CancelEventArgs e)
        {
            //Auto save settings
            //clsh.SaveHistory(this); 
            //clsh.Dispose();

            //User Settings
            clss.SaveMDIFormLocationSettings();
            clss.Dispose();

            #region P2P engen closing 

            //main.search.SerializeSearches();
            //main.trayIcon.Visible = false;

            clsEngen.StopEngen();
            clss.SaveFileScopeSettings();

            #endregion
        }

        protected void OnRemovePage(object sender, EventArgs e)
        {
            tabControl1.TabPages.Remove(tabControl1.SelectedTab);


            //zzoi remove the delegate subscription on close
            try
            {
                CultureChanged -= (tabControl1.SelectedTab as Ictrl).ResetCulture;
            }
            catch
            {
            }

            if (tabControl1.TabPages.Count == 0)
            {
                tabControl1.Visible = false;
            }
        }

        #endregion

        private void tabControl1_ClosePressed(object sender, EventArgs e)
        {

            //zzoi 9mrt 2009 just hide the tab page, not needed to remove
            //zzoi 9mrt 2009
            //TODO: search pages can be removed. delegate to sub class a flag/property if should hide or remove

            if ((tabControl1.SelectedTab.Control  as Ictrl).StaticControl == true)
            {

                tabControl1.TabPages.Remove(tabControl1.SelectedTab);

                
                //keep the reference and hide the ctrl
                //tabControl1.SelectedTab.Visible = false;

                //TabControl tab = (TabControl)sender;
                //tab.SelectedTab.Visible = false;

                //tab.Refresh();
                //tabControl1.SelectNextControl(sender,true,true,false,true);
                //tabControl1.Invalidate ();
            }
            else
            {
                //the search ctrl
                //remove the tab
                tabControl1.TabPages.Remove(tabControl1.SelectedTab);
            }

                            



        }

        private void mnuFileExit_Click(object sender, EventArgs e)
        {
            //show message to wait
            Cursor = Cursors.WaitCursor;
            //clsEngen.StopEngen();
            //exit appllication


            try
            {
                //zzoi 3 june 2008 shutdown the p2p engen
                //set the hourglass
                Cursor = Cursors.WaitCursor;
                //clsEngen.StopEngen();


                //Auto save settings
                //clsh.SaveHistory(this); 
                //clsh.Dispose();

                //User Settings
                clss.SaveMDIFormLocationSettings();
                clss.Dispose();

                #region P2P engen closing

                //main.search.SerializeSearches();
                //main.trayIcon.Visible = false;

                clsEngen.StopEngen();
                clss.SaveFileScopeSettings();

                #endregion

                Application.Exit();
            }
            catch (Exception ex)
            {
                new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        private void mnuToolsVoice_Click(object sender, EventArgs e)
        {
            //zzoi 13 mrt 2009 
            MessageBox.Show("Voice not fucntional yet.");
        }

        private void mnuToolsExchangeRates_Click(object sender, EventArgs e)
        {
            //zzoi 13 mrt 2009 
            MessageBox.Show("Exchange rates not fucntional yet.");
        }

        private void mnuEditFind_Click(object sender, EventArgs e)
        {
            //zzoi 13 mrt 2009 
            MessageBox.Show("Find not fucntional yet.");
        }

        private void mnuHelpContents_Click(object sender, EventArgs e)
        {
            //zzoi 13 mrt 2009 
            MessageBox.Show("Contents not fucntional yet.");
        }

    }
}