/// <summary>
        /// Refresh the search visualisation
        /// </summary>
        /// <param name="refreshSearchVisualisation">The search visualisation to refresh</param>
        public void RefreshSearchVisualisation(SearchVisualisation refreshSearchVisualisation)
        {
            bool found = false;

            for (int i = 0; i < _items.Children.Count && !found; i++)
            {
                ToggleButton b = _items.Children[i] as ToggleButton;

                SearchVisualisation searchVisualisation = b.Tag as SearchVisualisation;

                if (searchVisualisation != null)
                {
                    if (searchVisualisation.SearchResultSet.SearchDescription.Id == refreshSearchVisualisation.SearchResultSet.SearchDescription.Id)
                    {
                        found = true;

                        Grid g = b.Content as Grid;

                        if (g != null)
                        {
                            Image image = g.Children[0] as Image;

                            if (image != null)
                            {
                                image.Source = new System.Windows.Media.Imaging.BitmapImage(VisualisationFactory.GetSmallImage(searchVisualisation.Visualisation));
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Add a new search visualisation
        /// </summary>
        /// <param name="searchVisualisation">The search visualisation to add</param>
        public void AddSearchVisualisation(SearchVisualisation searchVisualisation)
        {
            ToggleButton button = CreateTabButton(searchVisualisation);

            _items.Children.Add(button);

            SelectNewItem(button);

            _currentIndex = _items.Children.IndexOf(button);

            CalculateButtonVisibility(true);
        }
        /// <summary>
        /// Localise the control
        /// </summary>
        public void Localise()
        {
            for (int i = 0; i < _items.Children.Count; i++)
            {
                ToggleButton b = _items.Children[i] as ToggleButton;

                SearchVisualisation searchVisualisation = b.Tag as SearchVisualisation;

                if (searchVisualisation != null)
                {
                    searchVisualisation.Localise();
                }
            }
        }
        /// <summary>
        /// Remove a search visualisation from the list
        /// </summary>
        /// <param name="searchVisualisation">The search visualisation to remove</param>
        public void RemoveSearchVisualisation(SearchVisualisation searchVisualisation)
        {
            int found = -1;

            for (int i = 0; i < _items.Children.Count && found == -1; i++)
            {
                ToggleButton button = _items.Children[i] as ToggleButton;

                if (button.Tag == searchVisualisation)
                {
                    found = i;
                    _items.Children.RemoveAt(i);
                    if (i <= _currentIndex)
                    {
                        _currentIndex--;
                    }
                }
            }

            if (found != -1)
            {
                int newIndex = _currentIndex;

                if (newIndex < 0)
                {
                    newIndex = 0;
                }

                if (ItemSelected != null)
                {
                    if (newIndex < _items.Children.Count)
                    {
                        ToggleButton b = _items.Children[newIndex] as ToggleButton;

                        SelectNewItem(b);
                    }
                    else
                    {
                        ItemSelected(null);
                    }
                }
            }

            CalculateButtonVisibility(true);
        }
        /// <summary>
        /// Find the search visualisation
        /// </summary>
        /// <param name="id">The id of the item to find</param>
        public SearchVisualisation FindSearchVisualisation(Guid id)
        {
            SearchVisualisation found = null;

            for (int i = 0; i < _items.Children.Count && found == null; i++)
            {
                ToggleButton b = _items.Children[i] as ToggleButton;

                SearchVisualisation searchVisualisation = b.Tag as SearchVisualisation;

                if (searchVisualisation != null)
                {
                    if (searchVisualisation.SearchResultSet.SearchDescription.Id == id)
                    {
                        found = searchVisualisation;
                    }
                }
            }

            return(found);
        }
        /// <summary>
        /// Select the search visualisation
        /// </summary>
        /// <param name="id">The id of the search visualisation to select</param>
        /// <returns>True if an item was selected</returns>
        public bool SelectSearchVisualisation(Guid id)
        {
            bool found = false;

            for (int i = 0; i < _items.Children.Count && !found; i++)
            {
                ToggleButton b = _items.Children[i] as ToggleButton;

                SearchVisualisation searchVisualisation = b.Tag as SearchVisualisation;

                if (searchVisualisation != null)
                {
                    if (searchVisualisation.SearchResultSet.SearchDescription.Id == id)
                    {
                        found = true;
                        SelectNewItem(b);
                    }
                }
            }

            return(found);
        }
        private ToggleButton CreateTabButton(SearchVisualisation searchVisualisation)
        {
            ToggleButton button = new ToggleButton();

            Grid g = new Grid();

            ColumnDefinition cd0 = new ColumnDefinition();

            cd0.Width = new GridLength(6);

            ColumnDefinition cd1 = new ColumnDefinition();

            cd1.Width = new GridLength(20);

            ColumnDefinition cd2 = new ColumnDefinition();

            cd2.Width = new GridLength(6);

            ColumnDefinition cd3 = new ColumnDefinition();

            cd3.Width = new GridLength(80);

            ColumnDefinition cd4 = new ColumnDefinition();

            cd4.Width = new GridLength(6);

            ColumnDefinition cd5 = new ColumnDefinition();

            cd5.Width = new GridLength(20);

            ColumnDefinition cd6 = new ColumnDefinition();

            cd6.Width = new GridLength(20);

            ColumnDefinition cd7 = new ColumnDefinition();

            cd7.Width = new GridLength(6);

            g.ColumnDefinitions.Add(cd0);
            g.ColumnDefinitions.Add(cd1);
            g.ColumnDefinitions.Add(cd2);
            g.ColumnDefinitions.Add(cd3);
            g.ColumnDefinitions.Add(cd4);
            g.ColumnDefinitions.Add(cd5);
            g.ColumnDefinitions.Add(cd6);
            g.ColumnDefinitions.Add(cd7);

            Image image = new Image();

            image.Width  = 20;
            image.Height = 20;
            image.Source = new System.Windows.Media.Imaging.BitmapImage(VisualisationFactory.GetSmallImage(searchVisualisation.Visualisation));
            Grid.SetColumn(image, 1);
            g.Children.Add(image);

            TextBlock tb = new TextBlock();

            tb.Text         = searchVisualisation.SearchResultSet.SearchDescription.Query;
            tb.TextWrapping = TextWrapping.NoWrap;
#if !SILVERLIGHT
            tb.TextTrimming = TextTrimming.CharacterEllipsis;
#endif
            tb.TextAlignment       = TextAlignment.Left;
            tb.VerticalAlignment   = VerticalAlignment.Center;
            tb.HorizontalAlignment = HorizontalAlignment.Left;
            Grid.SetColumn(tb, 3);
            g.Children.Add(tb);

            Button favouritesAddButton = new Button();
            favouritesAddButton.Click += new RoutedEventHandler(favouritesAddButton_Click);
            favouritesAddButton.Width  = 20;
            favouritesAddButton.Height = 20;
            favouritesAddButton.Tag    = searchVisualisation;

            Image imageFavouritesAdd = new Image();
            imageFavouritesAdd.Source  = new BitmapImage(new Uri("Resources/FavouritesAdd16.png", UriKind.Relative));
            imageFavouritesAdd.Stretch = Stretch.None;

            favouritesAddButton.Content = imageFavouritesAdd;

            Grid.SetColumn(favouritesAddButton, 5);
            g.Children.Add(favouritesAddButton);

            TextBlock tbClose = new TextBlock();
            tbClose.Text = "X";
            tbClose.VerticalAlignment = VerticalAlignment.Top;

            Button closeButton = new Button();
            closeButton.Margin  = new Thickness(0, 0, 0, 0);
            closeButton.Click  += new RoutedEventHandler(closeButton_Click);
            closeButton.Content = tbClose;
            closeButton.Tag     = searchVisualisation;
            closeButton.Width   = 20;
            closeButton.Height  = 20;

            Grid.SetColumn(closeButton, 6);
            g.Children.Add(closeButton);

            g.Tag    = searchVisualisation;
            g.Width  = 164;
            g.Height = 32;

            button.Content = g;
            button.Tag     = searchVisualisation;
            button.Height  = 32;
            button.Width   = 164;

            button.Click += new RoutedEventHandler(button_Click);

            return(button);
        }