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;
using Microsoft.Phone.Controls;

namespace SL_Orientation3
{
    public partial class MainPage : PhoneApplicationPage
    {
        //Instanz des Accelerometers anlegen
        Microsoft.Devices.Sensors.Accelerometer g_myAccelerometer = new Microsoft.Devices.Sensors.Accelerometer();
        //Zustand der Rotation
        bool g_boRotated = false;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            //ReadingChanged Event
            g_myAccelerometer.ReadingChanged += new EventHandler<Microsoft.Devices.Sensors.AccelerometerReadingEventArgs>(g_myAccelerometer_ReadingChanged);
            //Accelerometer starten
            g_myAccelerometer.Start();
        }

        void g_myAccelerometer_ReadingChanged(object sender, Microsoft.Devices.Sensors.AccelerometerReadingEventArgs e)
        {
            //Wenn Y positiv wird ist das Gerät gedreht.
            //In diesem Fall ist ein Wert von 0.6 der Schwellwert für die Rotation
            if (e.Y > 0.6f)
            {
                if (!g_boRotated)
                {
                    g_boRotated = true;
                    //Dispatcher.BeginInvoke verwenden, da dieses Event ausserhalb des UI Threads läuft
                    Dispatcher.BeginInvoke(() =>
                        {
                            PortraitUp.Stop();
                            PortraitDown.Begin();
                        }
                    );
                }

            }
            else
            {
                if (g_boRotated)
                {
                    g_boRotated = false;
                    //Dispatcher.BeginInvoke verwenden, da dieses Event ausserhalb des UI Threads läuft
                    Dispatcher.BeginInvoke(() =>
                    {
                        PortraitDown.Stop();
                        PortraitUp.Begin();
                    }
                    );
                }
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            webBrowser1.Navigate(new Uri("http://www.bing.com/search?q=" + textBox1.Text));
        }

        private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {
            webBrowser1.Navigate(new Uri("http://www.bing.com/search?q=Windows Phone 7"));
        }
    }
}