/// <summary>
 /// Checks if the Tab is currently in the TabControl if so returns true otherwise returns false.
 /// </summary>
 /// <param name="tab"></param>
 /// <returns></returns>
 public Boolean TabAlreadyOpen(STabItem tab)
 {
     if (tabNames[tab.Name] != null)
     {
         return true;
     }
     else
     {
         return false;
     }
 }
 public static void OpenTab(ISTabView tab)
 {
     STabItem newTab = new STabItem(tab.TabTitle(), tab.TabIcon());
     newTab.Name = "Tab" + ((UserControl)tab).Name;
     newTab.TabContent = (UserControl)tab;
     if (!mainTabControl.TabAlreadyOpen(newTab))
     {
         mainTabControl.AddTab(newTab);
     }
     else
     {
         mainTabControl.SelectTab(newTab);
     }
 }
 /// <summary>
 /// Static method used for adding new tabs to the Mainform tabcontroller.
 /// </summary>
 /// <param name="control"></param>
 /// <param name="tabIcon"></param>
 /// <param name="title"></param>
 public static void OpenTab(UserControl control, Image tabIcon, String title)
 {
     STabItem newTab = new STabItem(title, tabIcon);
     newTab.Name = "Tab" + control.Name;
     newTab.TabContent = control;
     if (!mainTabControl.TabAlreadyOpen(newTab))
     {
         mainTabControl.AddTab(newTab);
     }
     else
     {
         mainTabControl.SelectTab(newTab);
     }
 }
        /// <summary>
        /// Will Add a new tab to the controller, if tab exists the control will select the tab.
        /// </summary>
        /// <param name="tab"></param>
        public void AddTab(STabItem tab)
        {
            //Check if tab already exists.
            if (!TabAlreadyOpen(tab))
            {
                //Add tab Name and object to Hashtables
                tabs.Add(tabs.Count, tab);
                tabNames.Add(tab.Name, tabs.Count-1);

                //Addes the Tabs Icon to the Top of the Control and associates it with event handlers.
                tab.IconClicked += TabItemClicked;
                tab.IconClosing += TabItemClosing;
                TabHeaders.Children.Add(tab);

                //If there is already a tab selected we just let the SelectTab function handle the rest.
                if (SelectedTab > -1)
                {
                    SelectTab(tab);
                }
                else
                {
                    //Tell the tab it is selected.
                    ((STabItem)TabHeaders.Children[TabHeaders.Children.Count - 1]).IsSelected = true;

                    //Update internal selection tracker
                    SelectedTab = TabHeaders.Children.Count - 1;

                    //Add content to content panel
                    TabContent.Children.Add(tab.TabContent);
                }
            }
            else
            {
                //Select the existing Tab
                SelectTab(tab);
            }

        }
        /// <summary>
        /// This Selects the Tab that matches the name of the STabItem object passed. This will skip if tab does not exist to prevent expection.
        /// </summary>
        /// <param name="tab"></param>
        public void SelectTab(STabItem tab)
        {
            if (tabNames[tab.Name] != null)
            {
                //Get the Tabs position
                STabItem tabItem = (STabItem)tabs[tabNames[tab.Name]];
                int index = TabHeaders.Children.IndexOf(tabItem);

                //Deselect the old tab
                ((STabItem)TabHeaders.Children[SelectedTab]).IsSelected = false;

                //Configure the Content animation
                DoubleAnimation fadeOut = new DoubleAnimation();
                fadeOut.From = 1;
                fadeOut.To = 0;
                fadeOut.Duration = new Duration(new TimeSpan(500000));

                //Attach Event handler
                fadeOut.Completed += PanelFadedOut;

                //Begin animation
                TabContent.BeginAnimation(Grid.OpacityProperty, fadeOut);

                //Change the Internal SelectedTab postion and select the passed tab.
                SelectedTab = index;
                ((STabItem)TabHeaders.Children[SelectedTab]).IsSelected = true;

                if (tab.TabContent is ISTabView)
                {
                    ((ISTabView)tab.TabContent).TabIsGainingFocus();
                }
            }
            
        }
        /// <summary>
        /// Removes Tab object from the TabControl
        /// </summary>
        /// <param name="tab"></param>
        public void RemoveTab(STabItem tab)
        {
            //Check if tab exists
            if(TabAlreadyOpen(tab))
            {
                //Make sure to not close all tabs
                if (tabs.Count > 1)
                {
                    //Get the index of the tab to close
                    int index = TabHeaders.Children.IndexOf(tab);
                    //Check if tab is currently selected
                    if (index == SelectedTab)
                    {
                        //If tab is first in selection move selection to next
                        if (SelectedTab == 0)
                        {
                            SelectTab((STabItem)TabHeaders.Children[SelectedTab + 1]);
                        }
                        //Select Previous Tab
                        else
                        {
                            SelectTab((STabItem)TabHeaders.Children[SelectedTab - 1]);
                        }
                    }
                    else
                    {
                        if (SelectedTab > index)
                        {
                            SelectedTab--;
                        }
                    }
                    //Remove tab in Hashtables
                    tabNames.Remove(tab.Name);
                    tabs.Remove(index);

                    Object[] keys = new Object[tabs.Keys.Count];
                    tabs.Keys.CopyTo(keys, 0);
                    Array.Sort(keys);
                    
                    foreach (Object key in keys)
                    {
                        int keyValue = Int32.Parse(key.ToString());
                        if (keyValue > index)
                        {
                            tabs.Add(keyValue - 1, tabs[keyValue]);
                            tabs.Remove(keyValue);
                            tabNames[((STabItem)tabs[keyValue - 1]).Name] = keyValue - 1;
                        }
                    }
                    //Remove Tab Icon
                    TabHeaders.Children.Remove((STabItem)TabHeaders.Children[index]);
                }
            }
        }