using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

// Additional using statements.
using System.Windows.Media.Imaging;

namespace BasicSilverlight
{
    public partial class MainPage : UserControl
    {
        // Track whether the image is loaded.
        Boolean ImageOpened;

        public MainPage()
        {
            InitializeComponent();

            // The image can't be loaded yet.
            ImageOpened = false;
        }

        private void APOD_Select(object sender, RoutedEventArgs e)
        {
            if (ImageOpened)
            {
                // Set ImageOpened to false.
                ImageOpened = false;

                // Disable the buttons.
                rbAPOD1.IsEnabled = false;
                rbAPOD2.IsEnabled = false;
                rbAPOD3.IsEnabled = false;
                rbAPOD4.IsEnabled = false;
                rbAPOD5.IsEnabled = false;

                // Create a string to hold the base URL.
                String BaseURL = "http://apod.nasa.gov/apod/image/1108/";

                // Obtain access to the selected radio button.
                RadioButton SelectedButton = (RadioButton)e.OriginalSource;

                // Use the radio button name to choose an image.
                switch (SelectedButton.Name)
                {
                    case "rbAPOD1":
                        APOD_Image.Source = new BitmapImage(
                            new Uri(BaseURL + "sunpillar_stankiewicz_3888.jpg"));
                        break;
                    case "rbAPOD2":
                        APOD_Image.Source = new BitmapImage(
                            new Uri(BaseURL + "shapley1_eso_900.jpg"));
                        break;
                    case "rbAPOD3":
                        APOD_Image.Source = new BitmapImage(
                            new Uri(BaseURL + "per_110812_ladanyi_1000c.jpg"));
                        break;
                    case "rbAPOD4":
                        APOD_Image.Source = new BitmapImage(
                            new Uri(BaseURL + "NGC7331_crawford900c.jpg"));
                        break;
                    case "rbAPOD5":
                        APOD_Image.Source = new BitmapImage(
                            new Uri(BaseURL + "stpan_casado_900.jpg"));
                        break;
                }
            }
        }

        private void APOD_Image_ImageOpened(object sender, RoutedEventArgs e)
        {
            // When the image has finished loading, then set ImageLoaded true.
            ImageOpened = true;

            // Enable each of the buttons.
            rbAPOD1.IsEnabled = true;
            rbAPOD2.IsEnabled = true;
            rbAPOD3.IsEnabled = true;
            rbAPOD4.IsEnabled = true;
            rbAPOD5.IsEnabled = true;
        }
    }
}
