using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using ZedGraph;
using System.Globalization;
using System.Net;
using FTD2XX_NET;
using System.Threading;
using System.Resources;




namespace teplomer
{
    

    public partial class Form1 : Form
    {
        int[] terms = null;
        double jalpha, ja, jb, jc, jd, je,frac;
        int nph = 2;
        int index_hled = 0;

        int daay, moonth, yeaar;
        DateTime prvni_1, prvni_2, druhy_1, druhy_2, treti_1, treti_2, ctvrty_1, ctvrty_2;
        string[] teplota, cas, tlak, datum, vlhkost,vetr2, rosny, vetr_max, srazky_max;

        FileStream fs;
        StreamWriter m_streamWriter;

        private System.Windows.Forms.NotifyIcon notifyIcon1;

        
        byte byte1X, byte2X, byte3X, byte4X, byte5X, byte6X, byte7X, byte8X, byte9X, byte10X;
        Bitmap image1, image2, image3, image4, image5;
        private Graphics graX, graY, graZ, graD, graA;
        Font drawFont = new Font("Arial", 13);
        Font drawFont1 = new Font("Arial", 11);
        SolidBrush drawBrush2 = new SolidBrush(Color.Blue);
        SolidBrush drawBrush1 = new SolidBrush(Color.Black);
        SolidBrush drawBrush = new SolidBrush(Color.Red);
        double[] arrteplota, arrcas, arrhod, arrmin, arrtlak, arrvlhkost, arrrosny, arrvetr, arrsrazky;
        int[] arrrok, arrden, arrmesic, arrdatum;

        double vlh, tepl, tlakk, vetr, twc, smer, ros, srazky;
        Bitmap barometr = new Bitmap("obrazky/barometr.bmp");
        Bitmap rucka = new Bitmap("obrazky/rucka.bmp");
        Bitmap ruka = new Bitmap("obrazky/ruka.bmp");
        Bitmap kompas = new Bitmap("obrazky/kompas.bmp");
        Bitmap polojasno = new Bitmap("obrazky/polojasno.png");
        Bitmap dest = new Bitmap("obrazky/dest.png");
        Bitmap slunecno = new Bitmap("obrazky/slunecno.png");
        Bitmap zamraceno = new Bitmap("obrazky/zamraceno.png");
        Bitmap noc = new Bitmap("obrazky/night.png");
        Bitmap snih = new Bitmap("obrazky/snow.png");
        int pocet_hodnot = 0;
        int hour1,min1,sec1;
        DateTime vychod,zapad;

        //-------------------zapadvychod
        decimal N, N1, N2, N3;
        decimal month, year, day;
        double lngHour, longitude;
        double t1, t, M1, M, L, L1, RA, RA1;
        double Lquadrant = 90, RAquadrant = -90;
        double Lquadrant1 = 90, RAquadrant1 = -90;
        double sinDec, cosDec, cosH, cosH1, sinDec1, cosDec1;
        double zenith = 90.83, latitude;
        double H, H1, T, T1, UT, UT1;
        double localT, localT1, localOffset;
        //-------------------------------

        int sirka = 883;
        int vyska = 778;

        byte high, low;

        public Form1()
        {

            InitializeComponent();
            init_PCB();
            P1paint();
            P2paint();
            grafy_load();
          
        }

        void hlavicka()   //hlavicka tabulky dat
        {
            DataGridViewTextBoxColumn casColumn = new DataGridViewTextBoxColumn();
            casColumn.DataPropertyName = "cas";
            casColumn.HeaderText = "Datum:";

            DataGridViewTextBoxColumn datumColumn = new DataGridViewTextBoxColumn();
            datumColumn.DataPropertyName = "datum";
            datumColumn.HeaderText = "Čas:";

            DataGridViewTextBoxColumn teplotaColumn = new DataGridViewTextBoxColumn();
            teplotaColumn.DataPropertyName = "Teplota";
            teplotaColumn.HeaderText = "Teplota: [°C]";

            DataGridViewTextBoxColumn rosnyColumn = new DataGridViewTextBoxColumn();
            rosnyColumn.DataPropertyName = "Rosny";
            rosnyColumn.HeaderText = "Rosný bod: [°C]";

            DataGridViewTextBoxColumn vlhkostColumn = new DataGridViewTextBoxColumn();
            vlhkostColumn.DataPropertyName = "Vlhkost";
            vlhkostColumn.HeaderText = "Vlhkost: [%]";

            DataGridViewTextBoxColumn tlakColumn = new DataGridViewTextBoxColumn();
            tlakColumn.DataPropertyName = "Tlak";
            tlakColumn.HeaderText = "Tlak: [hPa]";

            dg1.Columns.Add(casColumn);
            dg1.Columns.Add(datumColumn);
            dg1.Columns.Add(teplotaColumn);
            dg1.Columns.Add(rosnyColumn);
            dg1.Columns.Add(vlhkostColumn);
            dg1.Columns.Add(tlakColumn);
            
        }

        private void Form1_Load(object sender, EventArgs e)  //inicializace pri zapnuti aplikace
        {
            ToolTip tooltip1 = new ToolTip();
            tooltip1.UseAnimation = true;
            tooltip1.UseFading = true;
            tooltip1.IsBalloon = true;
            tooltip1.ToolTipTitle = "Nápověda";
            tooltip1.SetToolTip(bt_auto, "Najde RF USB modul a připojí se pokud \n je připojen k USB portu PC");
            tooltip1.SetToolTip(bt_pripojit, "Připojí se k vybranému portu a \n začne zobrazovat přijaté data");
            tooltip1.SetToolTip(cb_1, "Vyber datum, od kterého chceš vykrelit grafy");
            tooltip1.SetToolTip(cb_2, "Vyber datum, do kterého chceš vykrelit grafy");
            tooltip1.SetToolTip(cb_3, "Vyber datum, od kterého chceš najít rekordy počasí");
            tooltip1.SetToolTip(cb_4, "Vyber datum, do kterého chceš najít rekordy počasí");
            tooltip1.SetToolTip(bt_pocitej, "Vypočítá čas východu a západu slunce \n podle zadaných souřadnic");
            tooltip1.SetToolTip(bt_nacti2, "Načte data potřebné k vyhledávání rekordů počasí. \n Na výběr je ze dvou zdrojů. ");
            tooltip1.SetToolTip(bt_nacti, "Načte data do tabulky. Je na výběr ze dvou zdrojů \n Tyto data jsou potřeba pro vykreslování grafů a hledání rekordů. ");
            
            cb_1.DropDownStyle = ComboBoxStyle.DropDownList;
            cb_1.Items.Add("Datum od:");
            cb_1.SelectedIndex = 0;
            cb_1.Enabled = false;
            cb_2.DropDownStyle = ComboBoxStyle.DropDownList;
            cb_2.Items.Add("Datum do:");
            cb_2.SelectedIndex = 0;
            cb_2.Enabled = false;
            cb_3.DropDownStyle = ComboBoxStyle.DropDownList;
            cb_3.Items.Add("Datum od:");
            cb_3.SelectedIndex = 0;
            cb_3.Enabled = false;
            cb_4.DropDownStyle = ComboBoxStyle.DropDownList;
            cb_4.Items.Add("Datum do:");
            cb_4.SelectedIndex = 0;
            cb_4.Enabled = false;
            
            bt_search.Enabled = false;
            bt_uloz.Enabled = false;
            bt_uloz2.Enabled = false;
            bt_hledej.Enabled = false;
            bt_kresli.Enabled = false;
            bt_hledej_rekordy.Enabled = false;       
            bt_prev.Enabled = false;
            bt_next.Enabled = false;           
            cb_gmt.SelectedIndex = 13;
            cb_delka.SelectedIndex = 0;
            cb_sirka.SelectedIndex = 0;
            notifyIcon1.Visible = true;
            notifyIcon1.BalloonTipText = "Vítejte v meteostanici";
            notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
            notifyIcon1.BalloonTipTitle = "Meteostanice Vánek";
            notifyIcon1.ShowBalloonTip(500);

            nacti_souradnice();
            vychod_zapad(false);
            vych_zapad_za();
            label49.Text = GetMoonPhase();
            label89.Text = "Fáze: " + GetMoonPhase();
            doit(DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year);

            //---
            //inicializace tabulky---------------------------------

            DataGridViewCellStyle style = dg1.ColumnHeadersDefaultCellStyle;
            dg1.EditMode = DataGridViewEditMode.EditProgrammatically;
            dg1.BackgroundColor = Color.Honeydew;
            dg1.ColumnHeadersHeight = 200;
            dg1.RowHeadersWidth = 60;
            

            for (int cols = 0; cols < 8; cols++)
            {
                switch (cols)
                {
                    case 0:
                        dg1.Columns.Add("Header ", "Datum:");
                        break;
                    case 1:
                        dg1.Columns.Add("Header ", "Čas: ");
                        break;
                    case 2:
                        dg1.Columns.Add("Header ", "Teplota: [°C]");
                        break;
                    case 3:
                        dg1.Columns.Add("Header ", "Rosný bod: [°C]");
                        break;
                    case 4:
                        dg1.Columns.Add("Header ", "Vlhkost: [%]");
                        break;
                    case 5:
                        dg1.Columns.Add("Header ", "Tlak: [hPa]");
                        break;
                    case 6:
                        dg1.Columns.Add("Header ", "Srážky: [mm]");
                        break;
                    case 7:
                        dg1.Columns.Add("Header ", "Rychlost: [m/s]");
                        break;
                    default:
                        dg1.Columns.Add("Header ", "Header ");
                        break;
                }
            }
            //----------inicializace tabulky--------------------------------

            try
            {
                fs = new FileStream(@"c:\temp\mcb1.txt", FileMode.OpenOrCreate, FileAccess.Write);
                m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            }
            catch 
            {
                if (MessageBox.Show("neni vytvoren soubor C:/temp//mcb.txt \n laskavě ho vytvoř a zkus to znova \n\n aplikace se nyní vypne!!! ", "hlasneni", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK) { Close(); }
                else Close();
            }


            string[] allPorts;
            try
            {
                allPorts = SerialPort.GetPortNames();
                cb_port.Items.Clear();

                foreach (string port in allPorts)
                {
                    /* Do roletoveho menu pridame vsechny nalezene porty */
                    cb_port.Items.Add(port);
                }
                //cb_port.SelectedIndex = 0;
                cb_port.SelectedItem = "COM4";
            }
            catch (Exception)
            {
                /* V pripade nezdaru vypiseme do status baru chybove hlaseni */
                stat_label.Text = "Nebyl nalezen příslušný COM port...";
            }
        }

        private void init_PCB()   //inicializace teplomeru a tlakomeru
        {
            image1 = new Bitmap(80, 500);
            image2 = new Bitmap(100, 500);


            graX = Graphics.FromImage(image1);
            graY = Graphics.FromImage(image2);

            pictureBox1.Image = image1;
            pictureBox2.Image = image2;

            graX.Clear(SystemColors.Control);
            graY.Clear(SystemColors.Control);

            graX.DrawLine(new Pen(Color.Red), 50, 15, 50, 462);// x,y,x,y
            graY.DrawLine(new Pen(Color.Blue), 60, 15, 60, 462);// x,y,x,y
            graX.DrawLine(new Pen(Color.Red), 70, 15, 70, 462);//X
            graY.DrawLine(new Pen(Color.Blue), 80, 15, 80, 462);//X
            graX.DrawArc(new Pen(Color.Red), 50, 4, 20, 30, 0, -180);// x,y,sirka,vyska,startuhel,konec uhel
            graY.DrawArc(new Pen(Color.Blue), 60, 4, 20, 30, 0, -180);// x,y,sirka,vyska,startuhel,konec uhel
            graX.FillPie(drawBrush, 40, 459, 40, 40, -57, 294);// x,y,sirka,vyska,startuhel,konec uhel
            graY.FillPie(drawBrush2, 50, 459, 40, 40, -57, 294);// x,y,sirka,vyska,startuhel,konec uhel
            graX.FillRectangle(new SolidBrush(Color.Red), 50, 462, 20, 17); //kulicka
            graY.FillRectangle(new SolidBrush(Color.Blue), 60, 462, 20, 17); //kulicka


            for (int i = 0; i <= 90; i++)
            {
                // Draw lines every 1 degrees
                if ((i) % 1 == 0)
                {
                    graX.DrawLine(new Pen(Color.Blue), 40, 4 * (12 + i), 45, 4 * (12 + i));
                }

                if ((i) % 5 == 0)
                {
                    graX.DrawLine(new Pen(Color.Red), 38, 4 * (12 + i), 45, 4 * (12 + i));
                }
                if ((i - 30) % 10 == 0)
                {
                    graX.DrawLine(new Pen(Color.Black), 35, 4 * (12 + i), 45, 4 * (12 + i));
                    if (i < 30)
                    {
                        graX.DrawString((i - 30) + "°".ToString(), drawFont, drawBrush1, 0, 447 - 4 * (12 + i), new StringFormat());
                    }
                    else if (i == 30) { graX.DrawString((i - 30) + "°".ToString(), drawFont, drawBrush1, 15, 447 - 4 * (12 + i), new StringFormat()); }
                    else graX.DrawString((i - 30) + "°".ToString(), drawFont, drawBrush1, 5, 447 - 4 * (12 + i), new StringFormat());
                }
            }
            for (int i = 0; i <= 100; i++)
            {
                if ((i) % 1 == 0)
                {
                    graY.DrawLine(new Pen(Color.Blue), 50, 4 * (12 + i), 55, 4 * (12 + i));
                }
                if ((i) % 5 == 0)
                {
                    graY.DrawLine(new Pen(Color.Red), 48, 4 * (12 + i), 55, 4 * (12 + i));
                }
                if ((i) % 10 == 0)
                {

                    graY.DrawLine(new Pen(Color.Black), 45, 4 * (12 + i), 55, 4 * (12 + i));
                    if (i == 0) 
                    { 
                        graY.DrawString((i) + "%".ToString(), drawFont, drawBrush1, 15, 485 - 4 * (12 + i), new StringFormat()); 
                    }
                    else if (i == 100) { graY.DrawString((i) + "%".ToString(), drawFont, drawBrush1, 0, 485 - 4 * (12 + i), new StringFormat()); }
                    else graY.DrawString((i) + "%".ToString(), drawFont, drawBrush1, 5, 485 - 4 * (12 + i), new StringFormat());
                    //4 * (10 + i)
                }
            }

        }



        private void bt_pripojit_Click(object sender, EventArgs e)   //akce pri stisknuti tlacitka pripojit/odpojit
        {

            if (!serialPort1.IsOpen)  //kdyz neni pripojen
            {
                try
                {
                    /* Nastavime vybrany port ke komunikaci */
                    serialPort1.PortName = cb_port.Text;
                    /* Nastavime jeho rychlost */
                    serialPort1.BaudRate = 1200;
                    serialPort1.Open();
                    if (serialPort1.IsOpen)
                    {
                        /* Do status baru vypiseme soucasny stav */
                        stat_label.Text = "Připojeno na port " + cb_port.Text + " rychlostí " + serialPort1.BaudRate.ToString();

                        cb_port.Enabled = false;
                        label1.Text = "Stav: Připojeno";
                        timer2.Enabled = true;
                        bt_pripojit.Text = "Odpojit";
                        bt_auto.Enabled = false;
                    }

                }
                catch (Exception)
                {
                    /* V pripade nezdaru vypiseme chybove hlaseni*/
                    stat_label.Text = "Nepodařilo se otevřít " + cb_port.Text + " port...";
                }

            }
            else  //kdyz je pripojen
            {
                try
                {

                    /* Zavreme spojeni */
                    serialPort1.DiscardInBuffer();
                    serialPort1.Close();

                    /* Do status baru vypiseme soucasny stav */
                    if (!serialPort1.IsOpen)
                    {
                        stat_label.Text = "Odpojeno...";
                        cb_port.Enabled = true;
                        label1.Text = "Stav: Odpojeno";
                        timer2.Enabled = false;
                        bt_auto.Enabled = true;
                        bt_pripojit.Text = "Připojit";
                        bt_auto.Enabled = true;
                    }
                }
                catch (Exception)
                {
                    /* V pripade nezdaru vypiseme chybove hlaseni */
                    stat_label.Text = "Vyskytla se chyba při pokusu odpojení se od"
                          + cb_port.Text + " portu ...";
                }           
            }               
        }

        private void on_received(object sender, SerialDataReceivedEventArgs e)  //prijate data po seriove lince
        {
            
            try
            {
                byte1X = (byte)serialPort1.ReadByte(); //cte prvni byte X
                byte2X = (byte)serialPort1.ReadByte(); //cte druhy byte X
                              
                if ((byte1X == 0xAA) && (byte2X == 0xA5))
                //if (byte1X == 0xA5)
                {
                    byte3X = (byte)serialPort1.ReadByte();
                    byte4X = (byte)serialPort1.ReadByte();
                    byte5X = (byte)serialPort1.ReadByte();
                    byte6X = (byte)serialPort1.ReadByte();
                    byte7X = (byte)serialPort1.ReadByte();
                    byte8X = (byte)serialPort1.ReadByte();
                    byte9X = (byte)serialPort1.ReadByte();  //kontrolni soucet
                    byte10X = (byte)serialPort1.ReadByte(); //cte stop bit
                    serialPort1.DiscardInBuffer();                 
                }
                else return;
                if (byte10X != 0x5A) 
                { return; }
                this.Invoke(new EventHandler(ZpracDat));
            }
            catch { }
        }

        private void ZpracDat(object sender, EventArgs e)
        {

            byte[] retez = new byte[6];
                retez[0] = byte3X;
                retez[1] = byte4X;
                retez[2] = byte5X;
                retez[3] = byte6X;
                retez[4] = byte7X;
                retez[5] = byte8X;
                read_byte(retez);
        }

        private void grafy_load()   //inicializace grafu
        {

            GraphPane pane = ZGC1.GraphPane;
            pane.Title.Text = "Vývoj teploty tc [°C] a relativní vlhkosti r [%] za dané období";
            pane.XAxis.Title.Text = "dny, čas";
            pane.YAxis.Title.Text = "Teplota [°C]";
            pane.Y2Axis.Title.Text = "Vlhkost [%]";

            pane.XAxis.Type = AxisType.DateAsOrdinal;
            pane.XAxis.Scale.Format = "HH:mm \n dd-MM-yy";
            // pozadi grafu
            pane.Chart.Fill = new Fill(Color.FromArgb(255, 255, 245), Color.FromArgb(255, 255, 190), 90F);

            GraphPane pane2 = ZGC2.GraphPane;
            pane2.Title.Text = "Vývoj atmosférického tlaku P [hPa] a rosného bodu td [°C] za dané období";
            pane2.XAxis.Title.Text = "dny, čas";
            pane2.YAxis.Title.Text = "Rosný bod [°C]";
            pane2.Y2Axis.Title.Text = "Tlak [hPa]";

            pane2.XAxis.Type = AxisType.DateAsOrdinal;
            pane2.XAxis.Scale.Format = "HH:mm \n dd-MM-yy";
            // pozadi grafu
            pane2.Chart.Fill = new Fill(Color.FromArgb(255, 255, 245), Color.FromArgb(255, 255, 190), 90F);

        }

        private void bt_kresli_Click(object sender, EventArgs e)  //vykresleni grafu
        {
            this.Cursor = Cursors.WaitCursor;
            GraphPane pane = ZGC1.GraphPane;
            ZGC1.GraphPane.CurveList.Clear();


            for (int i = 0; i < pocet_hodnot; i++)
            {
                int hodiny = (int)arrhod[i];
                int minuty = (int)arrmin[i];
                int den = (int)arrden[i];
                int mesic = (int)arrmesic[i];
                int rok = (int)arrrok[i];
                DateTime MyDate = new DateTime(rok, mesic, den, hodiny, minuty, 0);
                arrcas[i] = MyDate.ToOADate();
            }

            if (rb1.Checked == true)
            {
                pane = ZGC1.GraphPane;
                pane.Title.Text = "Vývoj teploty tc [°C] a relativní vlhkosti r [%] za dané období";
                pane.XAxis.Title.Text = "dny, čas";
                pane.YAxis.Title.Text = "Teplota [°C]";
                pane.Y2Axis.Title.Text = "Vlhkost [%]";

                pane.XAxis.Type = AxisType.DateAsOrdinal;
                pane.XAxis.Scale.Format = "HH:mm \n dd-MM-yy";
                // pozadi grafu
                pane.Chart.Fill = new Fill(Color.FromArgb(255, 255, 245), Color.FromArgb(255, 255, 190), 90F);
                
                // Generace legendy
                LineItem myCurve = pane.AddCurve("Teplota", arrcas, arrteplota, Color.Red, SymbolType.None);
                myCurve.Symbol.Fill = new Fill(Color.Red);
                myCurve = pane.AddCurve("Vlhkost", arrcas, arrvlhkost, Color.Blue, SymbolType.None);
                myCurve.Symbol.Fill = new Fill(Color.Blue);
                myCurve.IsY2Axis = true;

                pane.XAxis.MajorGrid.IsVisible = true;
                pane.YAxis.Scale.FontSpec.FontColor = Color.Red;
                pane.YAxis.Title.FontSpec.FontColor = Color.Red;
                pane.YAxis.MajorTic.IsOpposite = false;
                pane.YAxis.MinorTic.IsOpposite = false;
                pane.YAxis.MajorGrid.IsZeroLine = true;
                pane.YAxis.Scale.Align = AlignP.Inside;

                pane.Y2Axis.IsVisible = true;
                pane.Y2Axis.Scale.FontSpec.FontColor = Color.Blue;
                pane.Y2Axis.Title.FontSpec.FontColor = Color.Blue;
                pane.Y2Axis.MajorTic.IsOpposite = false;
                pane.Y2Axis.MinorTic.IsOpposite = false;
                pane.Y2Axis.MajorGrid.IsVisible = true;
                pane.Y2Axis.Scale.Align = AlignP.Inside;

                ZGC1.AxisChange();
                ZGC1.Refresh();
                

            }
            else
            {
                pane = ZGC1.GraphPane;
                pane.Title.Text = "Vývoj síly větru za dané období";
                pane.XAxis.Title.Text = "dny, čas";
                pane.YAxis.Title.Text = "vítr [m/s]";
                pane.Y2Axis.Title.Text = "";

                pane.XAxis.Type = AxisType.DateAsOrdinal;
                pane.XAxis.Scale.Format = "HH:mm \n dd-MM-yy";
                // pozadi grafu
                pane.Chart.Fill = new Fill(Color.FromArgb(255, 255, 245), Color.FromArgb(255, 255, 190), 90F);
                
                // Generace legendy
                LineItem myCurve = pane.AddCurve("Vítr", arrcas, arrvetr, Color.Red, SymbolType.None);
                myCurve.Symbol.Fill = new Fill(Color.Red);
                //myCurve = pane.AddCurve("Vlhkost", arrcas, arrvlhkost, Color.Blue, SymbolType.None);
                //myCurve.Symbol.Fill = new Fill(Color.Blue);
                //myCurve.IsY2Axis = true;

                pane.XAxis.MajorGrid.IsVisible = true;
                pane.YAxis.Scale.FontSpec.FontColor = Color.Red;
                pane.YAxis.Title.FontSpec.FontColor = Color.Red;
                pane.YAxis.MajorTic.IsOpposite = false;
                pane.YAxis.MinorTic.IsOpposite = false;
                pane.YAxis.MajorGrid.IsZeroLine = true;
                pane.YAxis.Scale.Align = AlignP.Inside;

                pane.Y2Axis.IsVisible = false;
                /*pane.Y2Axis.IsVisible = true;
                pane.Y2Axis.Scale.FontSpec.FontColor = Color.Blue;
                pane.Y2Axis.Title.FontSpec.FontColor = Color.Blue;
                pane.Y2Axis.MajorTic.IsOpposite = false;
                pane.Y2Axis.MinorTic.IsOpposite = false;
                pane.Y2Axis.MajorGrid.IsVisible = true;
                pane.Y2Axis.Scale.Align = AlignP.Inside;*/

                ZGC1.AxisChange();
                ZGC1.Refresh();
                
            }

                //---
                GraphPane pane2 = ZGC2.GraphPane;
                ZGC2.GraphPane.CurveList.Clear();

                for (int i = 0; i < pocet_hodnot; i++)
                {
                    int hodiny = (int)arrhod[i];
                    int minuty = (int)arrmin[i];
                    int den = (int)arrden[i];
                    int mesic = (int)arrmesic[i];
                    int rok = (int)arrrok[i];
                    DateTime MyDate = new DateTime(rok, mesic, den, hodiny, minuty, 0);
                    arrcas[i] = MyDate.ToOADate();
                }

                // Generace legendy
                LineItem myCurve1 = pane2.AddCurve("Rosný bod", arrcas, arrrosny, Color.Red, SymbolType.None);
                myCurve1.Symbol.Fill = new Fill(Color.Red);
                myCurve1 = pane2.AddCurve("Tlak", arrcas, arrtlak, Color.Blue, SymbolType.None);
                myCurve1.Symbol.Fill = new Fill(Color.Blue);
                myCurve1.IsY2Axis = true;

                pane2.XAxis.MajorGrid.IsVisible = true;
                pane2.YAxis.Scale.FontSpec.FontColor = Color.Red;
                pane2.YAxis.Title.FontSpec.FontColor = Color.Red;
                pane2.YAxis.MajorTic.IsOpposite = false;
                pane2.YAxis.MinorTic.IsOpposite = false;
                pane2.YAxis.MajorGrid.IsZeroLine = true;
                pane2.YAxis.Scale.Align = AlignP.Inside;
                pane2.Y2Axis.IsVisible = true;
                pane2.Y2Axis.Scale.FontSpec.FontColor = Color.Blue;
                pane2.Y2Axis.Title.FontSpec.FontColor = Color.Blue;
                pane2.Y2Axis.MajorTic.IsOpposite = false;
                pane2.Y2Axis.MinorTic.IsOpposite = false;
                pane2.Y2Axis.MajorGrid.IsVisible = true;
                pane2.Y2Axis.Scale.Align = AlignP.Inside;
                ZGC2.AxisChange();
                ZGC2.Refresh();
                //---

                this.Cursor = Cursors.Arrow;
            
        }

        private void bt_nacti_Click2(object sender, EventArgs e)
        {

            this.Cursor = Cursors.AppStarting;

            int kam = 0;
            cb_1.Items.Clear();
            cb_2.Items.Clear();
            cb_3.Items.Clear();
            cb_4.Items.Clear();

            if (rb_url2.Checked) { kam = 0; }
            if (rb_file2.Checked) { kam = 1; }

            

            nacti_txt(kam);
            if (dg1.Rows.Count != 1)  //jen kdyz je neco v tabulce
            {
                cb_1.Items.Add(dg1.Rows[0].Cells[0].Value.ToString());
                cb_2.Items.Add(dg1.Rows[0].Cells[0].Value.ToString());
                cb_3.Items.Add(dg1.Rows[0].Cells[0].Value.ToString());
                cb_4.Items.Add(dg1.Rows[0].Cells[0].Value.ToString());
                for (int i = 1; i < dg1.RowCount - 1; i++)
                {
                    if ((dg1.Rows[i].Cells[0].Value.ToString()) != (dg1.Rows[i - 1].Cells[0].Value.ToString()))
                    {
                        cb_1.Items.Add(dg1.Rows[i].Cells[0].Value.ToString());
                        cb_2.Items.Add(dg1.Rows[i].Cells[0].Value.ToString());
                        cb_3.Items.Add(dg1.Rows[i].Cells[0].Value.ToString());
                        cb_4.Items.Add(dg1.Rows[i].Cells[0].Value.ToString());
                    }
                }

                cb_1.SelectedIndex = cb_1.Items.Count - 1;
                cb_2.SelectedIndex = cb_2.Items.Count - 1;
                cb_3.SelectedIndex = cb_3.Items.Count - 1;
                cb_4.SelectedIndex = cb_4.Items.Count - 1;

                bt_search.Enabled = true;
                bt_uloz.Enabled = true;
                bt_uloz2.Enabled = true;
                bt_hledej.Enabled = true;
                bt_hledej_rekordy.Enabled = true;
                bt_hledej_rekordy.Enabled = true;
                cb_1.Enabled = true;
                cb_2.Enabled = true;
                cb_3.Enabled = true;
                cb_4.Enabled = true;


            }
            else
            {
                bt_search.Enabled = false;
                bt_uloz.Enabled = false;
                bt_uloz2.Enabled = false;
                bt_hledej.Enabled = false;
                bt_kresli.Enabled = false;
                bt_hledej_rekordy.Enabled = false;
                bt_hledej_rekordy.Enabled = false;
                cb_1.DropDownStyle = ComboBoxStyle.DropDownList;
                cb_1.Items.Add("Datum od:");
                cb_1.SelectedIndex = 0;
                cb_1.Enabled = false;
                cb_2.DropDownStyle = ComboBoxStyle.DropDownList;
                cb_2.Items.Add("Datum do:");
                cb_2.SelectedIndex = 0;
                cb_2.Enabled = false;
                cb_3.DropDownStyle = ComboBoxStyle.DropDownList;
                cb_3.Items.Add("Datum od:");
                cb_3.SelectedIndex = 0;
                cb_3.Enabled = false;
                cb_4.DropDownStyle = ComboBoxStyle.DropDownList;
                cb_4.Items.Add("Datum do:");
                cb_4.SelectedIndex = 0;
                cb_4.Enabled = false;

            }
            this.Cursor = Cursors.Arrow;
        }

     
        private void bt_nacti_Click(object sender, EventArgs e)  //nacitani dat do tabulky
        {
            this.Cursor = Cursors.WaitCursor;
            int kam = 0;
            cb_1.Items.Clear();
            cb_2.Items.Clear();
            cb_3.Items.Clear();
            cb_4.Items.Clear();

            if (rb_url.Checked) { kam = 0; }
            if (rb_file.Checked) { kam = 1; }

            nacti_txt(kam);
            dg1.ClearSelection();

            if (dg1.Rows.Count != 1)  //jen kdyz je neco v tabulce
            {                
                cb_1.Items.Add(dg1.Rows[0].Cells[0].Value.ToString());
                cb_2.Items.Add(dg1.Rows[0].Cells[0].Value.ToString());
                cb_3.Items.Add(dg1.Rows[0].Cells[0].Value.ToString());
                cb_4.Items.Add(dg1.Rows[0].Cells[0].Value.ToString());
                for (int i = 1; i < dg1.RowCount - 1; i++)
                {
                    if ((dg1.Rows[i].Cells[0].Value.ToString()) != (dg1.Rows[i - 1].Cells[0].Value.ToString()))
                    {
                        cb_1.Items.Add(dg1.Rows[i].Cells[0].Value.ToString());
                        cb_2.Items.Add(dg1.Rows[i].Cells[0].Value.ToString());
                        cb_3.Items.Add(dg1.Rows[i].Cells[0].Value.ToString());
                        cb_4.Items.Add(dg1.Rows[i].Cells[0].Value.ToString());
                    }
                }

                cb_1.SelectedIndex = cb_1.Items.Count - 1;
                cb_2.SelectedIndex = cb_2.Items.Count - 1;
                cb_3.SelectedIndex = cb_3.Items.Count - 1;
                cb_4.SelectedIndex = cb_4.Items.Count - 1;

                bt_search.Enabled = true;
                bt_uloz.Enabled = true;
                bt_uloz2.Enabled = true;
                bt_hledej.Enabled = true;
                bt_hledej_rekordy.Enabled = true;
                cb_1.Enabled = true;
                cb_2.Enabled = true;
                cb_3.Enabled = true;
                cb_4.Enabled = true;

            }
            else
            {
                bt_search.Enabled = false;
                bt_uloz.Enabled = false;
                bt_uloz2.Enabled = false;
                bt_hledej.Enabled = false;
                bt_kresli.Enabled = false;
                bt_hledej_rekordy.Enabled = false;
                cb_1.DropDownStyle = ComboBoxStyle.DropDownList;
                cb_1.Items.Add("Datum od:");
                cb_1.SelectedIndex = 0;
                cb_1.Enabled = false;
                cb_2.DropDownStyle = ComboBoxStyle.DropDownList;
                cb_2.Items.Add("Datum do:");
                cb_2.SelectedIndex = 0;
                cb_2.Enabled = false;
                cb_3.DropDownStyle = ComboBoxStyle.DropDownList;
                cb_3.Items.Add("Datum od:");
                cb_3.SelectedIndex = 0;
                cb_3.Enabled = false;
                cb_4.DropDownStyle = ComboBoxStyle.DropDownList;
                cb_4.Items.Add("Datum do:");
                cb_4.SelectedIndex = 0;
                cb_4.Enabled = false;

            }
            this.Cursor = Cursors.Arrow;
        }
            


        private void nacti_txt(int kam)   //volano z predchozi
        {
            
            dg1.Rows.Clear();
            if (kam == 0) {
                string filePath = tb_url.Text.ToString();

                try
                {
                    HttpWebRequest request = HttpWebRequest.Create(filePath) as HttpWebRequest;
                    request.Timeout = 5000; // cekani na pripojeni
                    request.Method = "HEAD"; 

                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;

                    int statusCode = (int)response.StatusCode;
                    if (statusCode >= 100 && statusCode < 400) //Good requests
                    {
                        
                    }
                    else if (statusCode >= 500 && statusCode <= 510) //Server Errors
                    {
                        MessageBox.Show("Neplataná adresa URl");
                    }
                }
                catch 
                {
                        MessageBox.Show("Problém s připojením na danou url: " + filePath.ToString());
                        return;
                }
                                 
                StreamReader sr = new StreamReader(WebRequest.Create(filePath).GetResponse().GetResponseStream());
                    string dataRow = ""; 
                    object[] row;
            
                    while ((dataRow = sr.ReadLine()) != null)
                    {
                        if (dataRow != "")
                        {
                            row = dataRow.Split(';'); 
                            dg1.Rows.Add(row); // Add the row.
                            
                        }
                    }
            }

            if (kam == 1)
            {
                
                OpenFileDialog openFile = new OpenFileDialog();
                if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.Cursor = Cursors.WaitCursor;
                    string filePath = openFile.FileName;
                    StreamReader sr = new StreamReader(filePath);
                    string dataRow = ""; 

                    object[] row;
                    while ((dataRow = sr.ReadLine()) != null)
                    {
                        if (dataRow != "")
                        {
                            row = dataRow.Split(';'); 
                            dg1.Rows.Add(row); 
                            
                        }
                    }
                }
            }
            

            int count2 = dg1.RowCount - 1;
            label6.Text = "Celkem položek: " + count2.ToString();

            if (dg1 != null)
            {
                for (int count = 0; (count <= (dg1.Rows.Count - 2)); count++)
                {
                    dg1.Rows[count].HeaderCell.Value = string.Format((count + 1).ToString());
                }
            }
        }

        private void bt_uloz_Click(object sender, EventArgs e)   //ulozeni dat jako .txt
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "text file|*.txt";
            saveFileDialog1.Title = "Save as text File";
            saveFileDialog1.ShowDialog();

            if (saveFileDialog1.FileName != "")
            {
                TextWriter sw = new StreamWriter(saveFileDialog1.FileName);
                int rowcount = dg1.Rows.Count;
                for (int i = 0; i < rowcount - 1; i++)
                {
                    sw.WriteLine(dg1.Rows[i].Cells[0].Value.ToString() + ";" +
                                dg1.Rows[i].Cells[1].Value.ToString() + ";" +
                                dg1.Rows[i].Cells[2].Value.ToString() + ";" +
                                dg1.Rows[i].Cells[3].Value.ToString() + ";" +
                                dg1.Rows[i].Cells[4].Value.ToString() + ";" +
                                dg1.Rows[i].Cells[5].Value.ToString() + ";" +
                                dg1.Rows[i].Cells[6].Value.ToString() + ";" +
                                dg1.Rows[i].Cells[7].Value.ToString());
                }
                sw.Close(); 
                MessageBox.Show("Data byly úspěšně uloženy");
            }
        }



        private void konecToolStripMenuItem_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            Close();
        }


        private void P1paint()   //vykresli tlak a tendenci do picturebox
        {
            image3 = new Bitmap(250, 247);
            graZ = Graphics.FromImage(image3);
            pictureBox3.Image = image3;

            image5 = new Bitmap(200, 200);
            graA = Graphics.FromImage(image5);
            pictureBox5.Image = image5;

            if ((hour1 > 18) || (hour1 < 5)) { graA.DrawImage(noc, 0, 0, (float)(noc.Width), (float)(noc.Height)); }
            else
            {

                if (tlakk > 1020) { graA.DrawImage(slunecno, 0, 0, (float)(slunecno.Width), (float)(slunecno.Height)); }
                if (tlakk < 1012) {
                    if (tepl < 0) graA.DrawImage(snih, 0, 0, (float)(snih.Width), (float)(snih.Height));
                    else graA.DrawImage(dest, 0, 0, (float)(dest.Width), (float)(dest.Height));
                }
                if ((tlakk >= 1012) && (tlakk < 1015)) { graA.DrawImage(zamraceno, 0, 0, (float)(zamraceno.Width), (float)(zamraceno.Height)); }
                if ((tlakk >= 1015) && (tlakk <= 1020)) { graA.DrawImage(polojasno, 0, 0, (float)(polojasno.Width), (float)(polojasno.Height)); }
            }



            double RollAngle = (2.75 * tlakk - (88 * 2.75)) * Math.PI / 180;

            Point ptBoule = new Point(0, -20);
            Point ptRotation = new Point(125, 125);

            float scale = 1F;

            barometr.MakeTransparent(Color.White);
            rucka.MakeTransparent(Color.Yellow);
            graZ.DrawImage(barometr, 0, 0, (float)(barometr.Width * scale), (float)(barometr.Height * scale));

            RotateAndTranslate(rucka, RollAngle, 0, ptBoule, (int)(4 * 0), ptRotation, 1);


        }

        private void P2paint()  //vykresli smer vetru
        {

            image4 = new Bitmap(248, 251);
            graD = Graphics.FromImage(image4);
            pictureBox4.Image = image4;

            double RollAngle1 = (45 * smer) * Math.PI / 180;

            Point ptBoule1 = new Point(0, 0);
            Point ptRotation1 = new Point(123, 124);

            float scale1 = 1;
            kompas.MakeTransparent(Color.Yellow);
            ruka.MakeTransparent(Color.Yellow);
            graD.DrawImage(kompas, 0, 0, (float)(kompas.Width * scale1), (float)(kompas.Height * scale1));
            RotateAndTranslate1(ruka, RollAngle1, 0, ptBoule1, (int)(4 * 0), ptRotation1, 1);

        }

        protected void RotateAndTranslate(Image img, Double alphaRot, Double alphaTrs, Point ptImg, int deltaPx, Point ptRot, float scaleFactor)  //rotace sipky tlakomeru
        {
            double beta = 0;
            double d = 0;
            float deltaXRot = 0;
            float deltaYRot = 0;
            float deltaXTrs = 0;
            float deltaYTrs = 0;

            // Rotace
            if (ptImg != ptRot)
            {

                if (ptRot.X != 0)
                {
                    beta = Math.Atan((double)ptRot.Y / (double)ptRot.X);
                }

                d = Math.Sqrt((ptRot.X * ptRot.X) + (ptRot.Y * ptRot.Y));
                // offset
                deltaXRot = (float)(d * (Math.Cos(alphaRot - beta) - Math.Cos(alphaRot) * Math.Cos(alphaRot + beta) - Math.Sin(alphaRot) * Math.Sin(alphaRot + beta)));
                deltaYRot = (float)(d * (Math.Sin(beta - alphaRot) + Math.Sin(alphaRot) * Math.Cos(alphaRot + beta) - Math.Cos(alphaRot) * Math.Sin(alphaRot + beta)));
            }

            // Translace
            deltaXTrs = (float)(deltaPx * (Math.Sin(alphaTrs)));
            deltaYTrs = (float)(-deltaPx * (-Math.Cos(alphaTrs)));
            graZ.RotateTransform((float)(alphaRot * 180 / Math.PI));

            // vykresli obrazek
            graZ.DrawImage(img, (ptImg.X + deltaXRot + deltaXTrs) * scaleFactor, (ptImg.Y + deltaYRot + deltaYTrs) * scaleFactor, img.Width * scaleFactor, img.Height * scaleFactor);
            graZ.RotateTransform((float)(-alphaRot * 180 / Math.PI));
        }

        protected void RotateAndTranslate1(Image img, Double alphaRot, Double alphaTrs, Point ptImg, int deltaPx, Point ptRot, float scaleFactor)  //rotace sipky smeru vetru
        {
            double beta = 0;
            double d = 0;
            float deltaXRot = 0;
            float deltaYRot = 0;
            float deltaXTrs = 0;
            float deltaYTrs = 0;


            if (ptImg != ptRot)
            {
                if (ptRot.X != 0)
                {
                    beta = Math.Atan((double)ptRot.Y / (double)ptRot.X);
                }

                d = Math.Sqrt((ptRot.X * ptRot.X) + (ptRot.Y * ptRot.Y));
                deltaXRot = (float)(d * (Math.Cos(alphaRot - beta) - Math.Cos(alphaRot) * Math.Cos(alphaRot + beta) - Math.Sin(alphaRot) * Math.Sin(alphaRot + beta)));
                deltaYRot = (float)(d * (Math.Sin(beta - alphaRot) + Math.Sin(alphaRot) * Math.Cos(alphaRot + beta) - Math.Cos(alphaRot) * Math.Sin(alphaRot + beta)));
            }

            deltaXTrs = (float)(deltaPx * (Math.Sin(alphaTrs)));
            deltaYTrs = (float)(-deltaPx * (-Math.Cos(alphaTrs)));
            graD.RotateTransform((float)(alphaRot * 180 / Math.PI));

            graD.DrawImage(img, (ptImg.X + deltaXRot + deltaXTrs) * scaleFactor, (ptImg.Y + deltaYRot + deltaYTrs) * scaleFactor, img.Width * scaleFactor, img.Height * scaleFactor);

            graD.RotateTransform((float)(-alphaRot * 180 / Math.PI));
        }

        void read_byte(byte[] neco)  //provadi zpetny menchester a identifikaci veliciny a vykresleni hodnot
        {
            int cc;
            cc = byte9X;

            bool znamenko = false;
            Int16 cislo;
            int i, x = 0, n = 1, j, k;
            int data_1;
            int data_2 = 0;
            double ki = 0;
            int crc = 0 ;
            char[] vysledek = new char[10];

            for (k = 0; k < 3; k++)
            {
                for (j = 0; j < 2; j++)
                {
                    data_1 = neco[j + (2 * k)];
                    for (i = 0; i < 4; i++)
                    {
                        data_1 = data_1 & 0xc0;
                        if (data_1 == 0x80) data_2 = data_2 + 1;   //jestlize je jedna tak 10
                        else if (data_1 == 0x40) data_2 = data_2 + 0;
                        else { crc = 1; break; /*data_2 = data_2 + 0;*/ }
                        if ((i == 3) && (j == 1)) x = 1;
                        data_2 = data_2 << (1 - x);		//posune vytvareny retez o dva bity 
                        data_1 = neco[j + (2 * k)] << (n * 2);
                        n++;
                    }
                    if (crc == 1) break;

                    x = 0;
                    n = 1;

                }
                if (crc == 1) break;
                
                vysledek[k] = Convert.ToChar(data_2);
                data_2 = 0;
            }
            if (crc == 1) return;

            
            high = (byte)vysledek[1];
            low = (byte)vysledek[2];

           // osetreni chyb
            int crcc = ((high / 2) + (low / 2))+1;
            
            if (crcc != byte9X) 
            {
                return; 
            }
           //  osetreni chyb

            cislo = (Int16)(high << 8);
            cislo = (Int16)(cislo + low);

            if (cislo < 0) {  cislo = (Int16)(cislo * -1); znamenko = true; }
            
            vysledek[1] = (char)(cislo % 100000 / 10000 + 48);
            vysledek[2] = (char)(cislo % 10000 / 1000 + 48);
            vysledek[3] = (char)(cislo % 1000 / 100 + 48);
            vysledek[4] = (char)(cislo % 100 / 10 + 48);
            vysledek[5] = (char)(cislo % 10 + 48);
            vysledek[6] = '\0';

            switch (vysledek[0])
            {

                case 'T':  //teplota
                    {
                        if (znamenko == true) { vysledek[0] = '-'; znamenko = false; }
                        else vysledek[0] = ' ';
                        vysledek[1] = vysledek[2];
                        vysledek[2] = vysledek[3];
                        vysledek[3] = '.';
                        vysledek[6] = '0';
                        vysledek[7] = '0'; 
                        vysledek[8] = '0';
                                                                     
                        string pokus2 = new string(vysledek);
                        
                        try
                        {
                            tepl = double.Parse(pokus2, CultureInfo.InvariantCulture);
                        }
                        catch (OverflowException)
                        { }

                        vysledek[6] = ' ';
                        vysledek[7] = '°';
                        vysledek[8] = 'C';
                        vysledek[9] = '\n';
                        if (vysledek[1] == '0') { vysledek[1] = ' '; }
                        pokus2 = new string(vysledek);
                        label2.Text = pokus2.ToString();                   
                    }

                    break;

                case 'R':  //vlhkost
                    {
                        vysledek[0] = ' ';
                        vysledek[1] = vysledek[2];
                        vysledek[2] = vysledek[3];
                        vysledek[3] = '.';
                        vysledek[6] = ' ';
                        vysledek[7] = '%';
                        vysledek[8] = '\0';
                        if (vysledek[1] == '0') { vysledek[1] = ' '; }
                        string pokus = new string(vysledek);
                        label3.Text = (pokus).ToString();
                        vysledek[6] = '\0'; vysledek[7] = '\0';

                        pokus = new string(vysledek);
                        try
                        {
                            vlh = double.Parse(pokus, CultureInfo.InvariantCulture);
                        }
                        catch (OverflowException)
                        { }
                        init_PCB();
                        graX.FillRectangle(new SolidBrush(Color.Red), 52, 289 - (int)tepl * 4, 17, 173 + 4 * (int)tepl); //x,y,sirka vyska
                        graY.FillRectangle(new SolidBrush(Color.Blue), 62, 449 - (int)vlh * 4, 17, 15 + 4 * (int)vlh); //x,y,sirka vysk

                        //---------------rosny bod------------------------------//
                        char[] jjjj = new char[8];
                        ki = (Math.Log10(vlh) - 2) / 0.4343 + (17.62 * tepl) / (243.12 + tepl);
                        double dw = 243.12 * ki / (17.62 - ki);
                        dw = dw * 100;
                        int sen = (int)dw;
                        jjjj[0] = ' ';
                        if (sen < 0) { sen = Math.Abs(sen); jjjj[0] = '-'; }
                        jjjj[1] = (char)(sen % 10000 / 1000 + 48);
                        jjjj[2] = (char)(sen % 1000 / 100 + 48);
                        jjjj[3] = '.';
                        jjjj[4] = (char)(sen % 100 / 10 + 48);
                        jjjj[5] = (char)(sen % 10 + 48);
                        jjjj[6] = ' ';
                        jjjj[7] = '°';
                        if ((jjjj[0] == '-') && (jjjj[1] == '0')) { jjjj[0] = ' '; jjjj[1] = '-'; }
                        if ((jjjj[0] != '-') && (jjjj[1] == '0')) { jjjj[0] = ' '; jjjj[1] = ' '; }
                        string pokus7 = new string(jjjj);
                        label5.Text = (pokus7).ToString() + "C";

                        jjjj[6] = '\0';
                        jjjj[7] = '\0';
                        pokus = new string(jjjj);
                        try
                        {
                            ros = double.Parse(pokus, CultureInfo.InvariantCulture);
                        }
                        catch (OverflowException)
                        { }
                    }
                    break;

                case 'P':   //tlak
                    {
                        vysledek[0] = vysledek[1];
                        vysledek[1] = vysledek[2];
                        vysledek[2] = vysledek[3];
                        vysledek[3] = vysledek[4];
                        vysledek[4] = '.';
                        vysledek[6] = ' ';
                        vysledek[7] = 'h';
                        vysledek[8] = 'P';
                        vysledek[9] = 'a';

                        string pokus3 = new string(vysledek);
                        label4.Text = (pokus3).ToString();

                        vysledek[6] = ' '; vysledek[7] = ' '; vysledek[8] = ' '; vysledek[9] = ' ';
                        pokus3 = new string(vysledek);

                        try
                        {
                            tlakk = double.Parse(pokus3, CultureInfo.InvariantCulture);
                        }
                        catch (OverflowException)
                        { }
                        P1paint();
                    }
                    break;

                case 'V': //rychlost vetru
                    {
                        vysledek[0] = vysledek[2];
                        vysledek[1] = vysledek[3];
                        vysledek[2] = vysledek[4];
                        vysledek[3] = '.';
                        vysledek[4] = vysledek[5];
                        vysledek[5] = '\0';
                        if (vysledek[0] == '0' && vysledek[1] == '0') { vysledek[0] = ' '; vysledek[1] = ' '; }
                        else { vysledek[0] = ' '; }


                        //-----------------wind chill------------------//
                        char[] chill = new char[9];
                        string pokus6 = new string(vysledek);
                        pokus6 = new string(vysledek);
                        try
                        {
                            vetr = double.Parse(pokus6, CultureInfo.InvariantCulture);
                            vetr = vetr * 3.6;
                        }
                        catch (OverflowException)
                        { }
                        if (vetr == 0)
                        {
                            label10.Text = "N/A";
                        }
                        else if (tepl <= 10)
                        {                           
                            twc = 13.13 + 0.6215 * tepl - 11.37 * (Math.Pow(vetr, 0.16)) + 0.3965 * tepl * (Math.Pow(vetr, 0.16));
                            twc = twc * 100;
                            int sen = (int)twc;
                            chill[0] = ' ';
                            if (sen < 0) { sen = Math.Abs(sen); chill[0] = '-'; }
                            chill[1] = (char)(sen % 10000 / 1000 + 48);
                            chill[2] = (char)(sen % 1000 / 100 + 48);
                            chill[3] = '.';
                            chill[4] = (char)(sen % 100 / 10 + 48);
                            chill[5] = (char)(sen % 10 + 48);
                            chill[6] = ' ';
                            chill[7] = '°';
                            chill[8] = 'C';
                            if ((chill[0] == '-') && (chill[1] == '0')) { chill[0] = ' '; chill[1] = '-'; }
                            if ((chill[0] != '-') && (chill[1] == '0')) { chill[0] = ' '; chill[1] = ' '; }
                            string pokus8 = new string(chill);
                            label10.Text = (pokus8).ToString();
                        }
                        else
                        {
                            double pp = tepl * 100;
                            chill[0] = (char)(pp % 10000 / 1000 + 48);
                            chill[1] = (char)(pp % 1000 / 100 + 48);
                            chill[2] = '.';
                            chill[3] = (char)(pp % 100 / 10 + 48);
                            chill[4] = (char)(pp % 10 + 48);
                            chill[5] = ' ';
                            chill[6] = '°';
                            chill[7] = 'C';
                            string po = new string(chill);
                            label10.Text = po.ToString();
                        }

                        vysledek[5] = ' ';
                        vysledek[6] = 'm';
                        vysledek[7] = '/';
                        vysledek[8] = 's';
                        string pokus4 = new string(vysledek);
                        label9.Text = (pokus4).ToString();

                        double p = vetr * 100;
                        chill[0] = (char)(p % 100000 / 10000 + 48);
                        chill[1] = (char)(p % 10000 / 1000 + 48);
                        chill[2] = (char)(p % 1000 / 100 + 48);
                        chill[3] = '.';
                        chill[4] = (char)(p % 100 / 10 + 48);
                        chill[5] = (char)(p % 10 + 48);
                        chill[6] = ' ';
                        chill[7] = 'k';
                        chill[8] = 'm';

                        if ((chill[0] == '0') && (chill[1] =='0')) 
                        {
                            chill[0] = chill[2];
                            chill[1] = chill[3];
                            chill[2] = chill[4];
                            chill[3] = chill[5];
                            chill[4] = chill[6];
                            chill[5] = chill[7];
                            chill[6] = chill[8];
                            chill[7] = '/';
                            chill[8] = 'h';
                            string pop = new string(chill);
                            label103.Text = pop.ToString();
                        }
                        else if (chill[0] == '0') 
                        {
                            chill[0] = chill[1];
                            chill[1] = chill[2];
                            chill[2] = chill[3];
                            chill[3] = chill[4];
                            chill[4] = chill[5];
                            chill[5] = chill[6];
                            chill[6] = chill[7];
                            chill[7] = 'm';
                            chill[8] = '/';
                            string pop = new string(chill);
                            label103.Text = pop.ToString() + "h";
                        }

                        


                    }
                    break;

                case 'S': //smer vetru
                    {
                        vysledek[0] = ' ';
                        smer = high;
                        P2paint();

                        switch (high)
                        {
                            case 0: label102.Text = "Severní 360°";
                                break;
                            case 1: label102.Text = "Severovýchodní 45°";
                                break;
                            case 2: label102.Text = "Východní 90°";
                                break;
                            case 3: label102.Text = "Jihovýchodní 135°";
                                break;
                            case 4: label102.Text = "Jižní 180°";
                                break;
                            case 5: label102.Text = "Jihozápadní 225°";
                                break;
                            case 6: label102.Text = "Západní 270°";
                                break;
                            case 7: label102.Text = "Severozápadní 315°";
                                break;
                            default:
                                break;
                        }
                    }
                    break;

                case 'D':   //srazky
                    {

                        vysledek[0] = vysledek[2];
                        vysledek[1] = vysledek[3];
                        vysledek[2] = vysledek[4];
                        vysledek[3] = '.';
                        vysledek[4] = vysledek[5];
                        vysledek[5] = '\0';
                        vysledek[6] = '\0';
                        if (vysledek[0] == '0' && vysledek[1] == '0') { vysledek[0] = ' '; vysledek[1] = ' '; }
                        else if (vysledek[0] == '0') { vysledek[0] = ' '; }

                        string pokus7 = new string(vysledek);
                        pokus7 = new string(vysledek);
                        try
                        {
                            srazky = double.Parse(pokus7, CultureInfo.InvariantCulture);
                        }
                        catch
                        { }
                        vysledek[5] = ' ';
                        vysledek[6] = 'm';
                        vysledek[7] = 'm';
                        string pokus6 = new string(vysledek);
                        label13.Text = (pokus6).ToString();

                    }
                    break;

                case 'A': // teplota max
                    {
                        if (znamenko == true) { vysledek[0] = '-'; znamenko = false; }
                        else vysledek[0] = ' ';
                        vysledek[1] = vysledek[2];
                        vysledek[2] = vysledek[3];
                        vysledek[3] = '.';
                        vysledek[6] = ' ';
                        vysledek[7] = '°';
                        vysledek[8] = 'C';
                        vysledek[9] = '\0';
                        if (vysledek[1] == '0') { vysledek[1] = ' '; }

                        string pokus9 = new string(vysledek);
                        label7.Text = (pokus9).ToString();
                    }
                    break;

                case 'B': // teplota min
                    {
                        if (znamenko == true) { vysledek[0] = '-'; znamenko = false; }
                        else vysledek[0] = ' ';
                        vysledek[1] = vysledek[2];
                        vysledek[2] = vysledek[3];
                        vysledek[3] = '.';
                        vysledek[6] = ' ';
                        vysledek[7] = '°';
                        vysledek[8] = 'C';
                        vysledek[9] = '\0';
                        if (vysledek[1] == '0') { vysledek[1] = ' '; }

                        string pokus7 = new string(vysledek);
                        label8.Text = (pokus7).ToString();
                    }
                    break;

                case 'G': //vlhkost max
                    {
                        vysledek[0] = ' ';
                        vysledek[1] = vysledek[2];
                        vysledek[2] = vysledek[3];
                        vysledek[3] = '.';
                        vysledek[6] = ' ';
                        vysledek[7] = '%';
                        vysledek[8] = '\0';
                        if (vysledek[1] == '0') { vysledek[1] = ' '; }
                        string pokus8 = new string(vysledek);
                        label16.Text = (pokus8).ToString();
                    }
                    break;

                case 'H': //vlhkost min
                    {
                        vysledek[0] = ' ';
                        vysledek[1] = vysledek[2];
                        vysledek[2] = vysledek[3];
                        vysledek[3] = '.';
                        vysledek[6] = ' ';
                        vysledek[7] = '%';
                        vysledek[8] = '\0';
                        if (vysledek[1] == '0') { vysledek[1] = ' '; }
                        string pokus8 = new string(vysledek);
                        label17.Text = (pokus8).ToString();
                    }
                    break;

                case 'K': // tlak max
                    {
                        vysledek[0] = ' ';
                        vysledek[6] = vysledek[5];
                        vysledek[5] = '.';
                        vysledek[7] = ' ';
                        vysledek[8] = 'h';
                        vysledek[9] = 'P';

                        string pokus8 = new string(vysledek);
                        label21.Text = (pokus8).ToString() + "a";
                    }
                    break;

                case 'L': //tlak min
                    {
                        vysledek[0] = ' ';
                        vysledek[6] = vysledek[5];
                        vysledek[5] = '.';
                        vysledek[7] = ' ';
                        vysledek[8] = 'h';
                        vysledek[9] = 'P';

                        string pokus8 = new string(vysledek);
                        label20.Text = (pokus8).ToString() + "a";
                    }
                    break;

                case 'M': //vetr max
                    {
                        vysledek[0] = vysledek[2];
                        vysledek[1] = vysledek[3];
                        vysledek[2] = vysledek[4];
                        vysledek[3] = '.';
                        vysledek[4] = vysledek[5];
                        vysledek[5] = ' ';
                        vysledek[6] = 'm';
                        vysledek[7] = '/';
                        vysledek[8] = 's';
                        if (vysledek[0] == '0' && vysledek[1] == '0') { vysledek[0] = ' '; vysledek[1] = ' '; }
                        else { vysledek[0] = ' '; }
                        string pokus8 = new string(vysledek);
                        label23.Text = (pokus8).ToString();
                    }
                    break;

                case 'N': // teplota max cas
                    {
                        vysledek[0] = (char)(high % 100 / 10 + 48);
                        vysledek[1] = (char)(high % 10 + 48);
                        vysledek[2] = ':';
                        vysledek[3] = (char)(low % 100 / 10 + 48);
                        vysledek[4] = (char)(low % 10 + 48);
                        vysledek[5] = '\0';
                        string pokus8 = new string(vysledek);
                        label24.Text = "v " + (pokus8).ToString();
                    }
                    break;

                case 'O':  //teplota min cas
                    {
                        vysledek[0] = (char)(high % 100 / 10 + 48);
                        vysledek[1] = (char)(high % 10 + 48);
                        vysledek[2] = ':';
                        vysledek[3] = (char)(low % 100 / 10 + 48);
                        vysledek[4] = (char)(low % 10 + 48);
                        vysledek[5] = '\0';

                        string pokus8 = new string(vysledek);
                        label25.Text = "v " + (pokus8).ToString();
                    }
                    break;

                case 'E': //vlhkost max cas
                    {
                        vysledek[0] = (char)(high % 100 / 10 + 48);
                        vysledek[1] = (char)(high % 10 + 48);
                        vysledek[2] = ':';
                        vysledek[3] = (char)(low % 100 / 10 + 48);
                        vysledek[4] = (char)(low % 10 + 48);
                        vysledek[5] = '\0';

                        string pokus8 = new string(vysledek);
                        label26.Text = "v " + (pokus8).ToString();
                    }
                    break;

                case 'F': // vlhkost min cas
                    {
                        vysledek[0] = (char)(high % 100 / 10 + 48);
                        vysledek[1] = (char)(high % 10 + 48);
                        vysledek[2] = ':';
                        vysledek[3] = (char)(low % 100 / 10 + 48);
                        vysledek[4] = (char)(low % 10 + 48);
                        vysledek[5] = '\0';

                        string pokus8 = new string(vysledek);
                        label27.Text = "v " + (pokus8).ToString();
                    }
                    break;

                case 'I': //tlak max cas
                    {
                        vysledek[0] = (char)(high % 100 / 10 + 48);
                        vysledek[1] = (char)(high % 10 + 48);
                        vysledek[2] = ':';
                        vysledek[3] = (char)(low % 100 / 10 + 48);
                        vysledek[4] = (char)(low % 10 + 48);
                        vysledek[5] = '\0';

                        string pokus8 = new string(vysledek);
                        label28.Text = "v " + (pokus8).ToString();
                    }
                    break;

                case 'J': //tlak min cas
                    {
                        vysledek[0] = (char)(high % 100 / 10 + 48);
                        vysledek[1] = (char)(high % 10 + 48);
                        vysledek[2] = ':';
                        vysledek[3] = (char)(low % 100 / 10 + 48);
                        vysledek[4] = (char)(low % 10 + 48);
                        vysledek[5] = '\0';

                        string pokus8 = new string(vysledek);
                        label29.Text = "v " + (pokus8).ToString();
                    }
                    break;

                case 'Z': //srazky rok
                    {
                        vysledek[0] = vysledek[2];
                        vysledek[1] = vysledek[3];
                        vysledek[2] = vysledek[4];
                        vysledek[3] = '.';
                        vysledek[4] = vysledek[5];
                        vysledek[5] = ' ';
                        vysledek[6] = 'm';
                        vysledek[7] = 'm';
                        if (vysledek[0] == '0' && vysledek[1] == '0') { vysledek[0] = ' '; vysledek[1] = ' '; }
                        else if(vysledek[0] =='0') { vysledek[0] =' '; }

                        string pomoc = new string(vysledek);
                        label91.Text = pomoc.ToString();

                    }
                    break;

                case 'C': // vetr max cas
                    {
                        vysledek[0] = (char)(high % 100 / 10 + 48);
                        vysledek[1] = (char)(high % 10 + 48);
                        vysledek[2] = ':';
                        vysledek[3] = (char)(low % 100 / 10 + 48);
                        vysledek[4] = (char)(low % 10 + 48);
                        vysledek[5] = '\0';
                        string pokus11 = new string(vysledek);
                        label101.Text = "v " + (pokus11).ToString();
                    }
                    break;
         
                default: break;
            }          
        }

        private void casovac(object sender, EventArgs e)  //Timer1 - hodiny ve stavovem panelu, na prelomu dne vypocte cas vychodu a zapadu
        {
            string time = DateTime.Now.ToString("hh\\:mm:ss tt"); 
            string date = DateTime.Now.ToString("dd\\/MM\\/yyyy");
            
            hour1 = DateTime.Now.Hour;
            min1 = DateTime.Now.Minute;
            sec1 = DateTime.Now.Second;
            if ((hour1 == 0) && (min1 == 0) && (sec1 == 1))  //vypocte novy cas V/Z
            { 
                vychod_zapad(false);
                label49.Text = GetMoonPhase();
                doit(DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year);
            
            }  

            statlabel2.Text = time;
            statlabel3.Text = date;
        }

        private void zapis_soubor(object sender, EventArgs e) //Timer2 - zapis namerenych dat kazdych 15minut
        {
            m_streamWriter.WriteLine("{0};{1};{2};{3};{4};{5};{6};{7}", DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString(), tepl, ros, vlh, tlakk, vetr, srazky);
            m_streamWriter.Flush();
        }

        private void bt_search_Click(object sender, EventArgs e) //vyhledavani dat z tabulky
        {
            dg1.ClearSelection();
            List<int> list = new List<int>();
            string search = tb_hledat.Text;
            bool test = false;
            int pocet = 0;

            for (int i = 0; i < dg1.Rows.Count - 1; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (dg1.Rows[i].Cells[j].Value.ToString() == search)
                    {
                        dg1.Rows[i].Cells[j].Selected = true;
                        list.Add(i); //prida najity index
                        //dg1.FirstDisplayedScrollingRowIndex = i;
                        test = true;
                        pocet++;
                        //break;
                    }
                }
            }
            terms = list.ToArray();

            if (test == true)
            {
                dg1.FirstDisplayedScrollingRowIndex = terms[0];
                label92.Text = "Zobrazen: 1 z " + terms.Length.ToString() + " vyhledaných";
                MessageBox.Show("nalezeno " + pocet + " záznamů", "Hlášení", MessageBoxButtons.OK, MessageBoxIcon.Information);
                bt_next.Enabled = true;
            }
            else MessageBox.Show("nenalezeno", "Hlášení", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void bt_hledej_Click(object sender, EventArgs e)  //hleda data pro grafy
        {
            dg1.ClearSelection();
            this.Cursor = Cursors.WaitCursor;
            string od = cb_1.SelectedItem.ToString();
            string doo = cb_2.SelectedItem.ToString();
            string searchod = od;
            string searchdo = doo;
            int min = 0, max = 0;

            if (cb_1.SelectedIndex > cb_2.SelectedIndex) {
                MessageBox.Show("Chyba: \n Nelze hledat datum od > do !!", "Hlášení ", MessageBoxButtons.OK, MessageBoxIcon.Error); this.Cursor = Cursors.Arrow; return;
            }


            for (int i = 0; i < dg1.Rows.Count - 1; i++)
            {
                if (dg1.Rows[i].Cells[0].Value.ToString() == searchod)
                {
                    min = i;
                    
                    //dg1.Rows[i].Selected = true;
                    break;
                }
            }
            for (int i = 0; i < dg1.Rows.Count - 1; i++)
            {
                if (dg1.Rows[i].Cells[0].Value.ToString() == searchdo)
                {
                    max = i + 1;
                    //dg1.Rows[i].Selected = true;
                    //break;
                }
            }
            for (int a = min; a < max; a++)
            {
                dg1.Rows[a].Selected = true;
            }
            //MessageBox.Show("nalezeno " + (max - min) + " záznamů", "Hlášení", MessageBoxButtons.OK, MessageBoxIcon.Information);

            dg1.FirstDisplayedScrollingRowIndex = min;  //posunuti
            pocet_hodnot = max - min;


            //----
            teplota = null; cas = null; tlak = null; datum = null; vlhkost = null; rosny = null; vetr2 = null;
            //pocet_hodnot = (int)numericUpDown1.Value;

            int je = 0;
            datum = new string[max - min];
            cas = new string[max - min];
            teplota = new string[max - min];
            vlhkost = new string[max - min];
            tlak = new string[max - min];
            rosny = new string[max - min];
            vetr2 = new string[max - min];

            for (int i = min; i < max; i++)
            {
                datum[je] = dg1.Rows[i].Cells[0].Value.ToString();
                cas[je] = dg1.Rows[i].Cells[1].Value.ToString();
                teplota[je] = dg1.Rows[i].Cells[2].Value.ToString();
                rosny[je] = dg1.Rows[i].Cells[3].Value.ToString();
                vlhkost[je] = dg1.Rows[i].Cells[4].Value.ToString();
                tlak[je] = dg1.Rows[i].Cells[5].Value.ToString();
                vetr2[je] = dg1.Rows[i].Cells[7].Value.ToString();
                je++;
   
            }
          
            arrteplota = new double[teplota.Length];
            arrhod = new double[teplota.Length];
            arrmin = new double[teplota.Length];
            arrcas = new double[teplota.Length];
            arrtlak = new double[tlak.Length];
            arrvlhkost = new double[vlhkost.Length];
            arrrosny = new double[rosny.Length];
            arrdatum = new int[datum.Length];
            arrrok = new int[teplota.Length];
            arrden = new int[teplota.Length];
            arrmesic = new int[teplota.Length];
            arrvetr = new double[teplota.Length];

            //NumberStyles styles;
            //styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |NumberStyles.Float;

            for (int i = 0; i < teplota.Length; i++)
            {
                arrteplota[i] = double.Parse(teplota[i], CultureInfo.CreateSpecificCulture("en-EN"));
                arrtlak[i] = double.Parse(tlak[i], CultureInfo.CreateSpecificCulture("en-EN"));
                arrvlhkost[i] = double.Parse(vlhkost[i], CultureInfo.CreateSpecificCulture("en-EN"));
                arrrosny[i] = double.Parse(rosny[i], CultureInfo.CreateSpecificCulture("en-EN"));
                arrvetr[i] = double.Parse(vetr2[i], CultureInfo.CreateSpecificCulture("en-EN"));

                DateTime dt = Convert.ToDateTime(cas[i]);
                arrhod[i] = dt.Hour;
                arrmin[i] = dt.Minute;

                DateTime dt1 = Convert.ToDateTime(datum[i]);
                arrrok[i] = dt1.Year;
                arrmesic[i] = dt1.Month;
                arrden[i] = dt1.Day;

                //arrcas[i] = double.Parse(arrString2[i], CultureInfo.CurrentCulture);

            }
            bt_kresli.Enabled = true;
            this.Cursor = Cursors.Arrow;
            MessageBox.Show("nalezeno " + (max - min) + " záznamů", "Hlášení", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }


        private void vychod_zapad(bool stav)  //algoritmus vychodu a zapadu slunce
        {

            if (stav == true)
            {
                DateTime dateTime = dateTimePicker1.Value;
                day = dateTime.Day;
                year = dateTime.Year;
                month = dateTime.Month;
            }
            else {
                 day = DateTime.Now.Day;
                 year = DateTime.Now.Year;
                 month = DateTime.Now.Month;
            }
        
            
            localOffset = cb_gmt.SelectedIndex - 12;
            if (cb_sirka.SelectedIndex == 1) { latitude = (-1 * (Double.Parse(stupne_sirka.Text)) + ((Double.Parse(minuty_sirka.Text) / 60)) + ((Double.Parse(sekundy_sirka.Text) / 3600))); }
            else latitude = (Double.Parse(stupne_sirka.Text)) + ((Double.Parse(minuty_sirka.Text) / 60)) + ((Double.Parse(sekundy_sirka.Text) / 3600));
            if (cb_delka.SelectedIndex == 1) { longitude = (-1 * (Double.Parse(stupne_delka.Text)) + ((Double.Parse(minuty_delka.Text) / 60)) + ((Double.Parse(sekundy_delka.Text) / 3600))); }
            else longitude = (Double.Parse(stupne_delka.Text)) + ((Double.Parse(minuty_delka.Text) / 60) + ((Double.Parse(sekundy_delka.Text) / 3600)));


            N1 = Math.Floor(275 * month / 9);
            N2 = Math.Floor((month + 9) / 12);
            N3 = (1 + Math.Floor((year - 4 * Math.Floor(year / 4) + 2) / 3));
            N = N1 - (N2 * N3) + day - 30;

            lngHour = longitude / 15;

            
            t = (double)N + ((6 - lngHour) / 24);
            
            t1 = (double)N + ((18 - lngHour) / 24);

            M = (0.9856 * t) - 3.289;
            M1 = (0.9856 * t1) - 3.289;


            L = M + (1.916 * Math.Sin(DegreeToRadian(M))) + (0.020 * Math.Sin(2 * DegreeToRadian(M))) + 282.634;
            L = L - 360;
            L1 = M1 + (1.916 * Math.Sin(DegreeToRadian(M1))) + (0.020 * Math.Sin(2 * DegreeToRadian(M1))) + 282.634;
            L1 = L1 - 360;

            RA = Math.Atan((0.91764 * Math.Tan(DegreeToRadian(L))));
            RA = RadianToDegree(RA);

            RA1 = Math.Atan((0.91764 * Math.Tan(DegreeToRadian(L1))));
            RA1 = RadianToDegree(RA1);

            Lquadrant = (Math.Floor(L / 90)) * 90;
            RAquadrant = (Math.Floor(RA / 90)) * 90;

            Lquadrant1 = (Math.Floor(L1 / 90)) * 90;
            RAquadrant1 = (Math.Floor(RA1 / 90)) * 90;

            RA = RA + (Lquadrant - RAquadrant);
            RA = RA / 15;

            RA1 = RA1 + (Lquadrant1 - RAquadrant1);
            RA1 = RA1 / 15;

            sinDec = 0.39782 * Math.Sin(DegreeToRadian(L));
            cosDec = Math.Cos(Math.Asin(sinDec));

            sinDec1 = 0.39782 * Math.Sin(DegreeToRadian(L1));
            cosDec1 = Math.Cos(Math.Asin(sinDec1));

            cosH = (Math.Cos(DegreeToRadian(zenith)) - (sinDec * Math.Sin(DegreeToRadian(latitude)))) / (cosDec * Math.Cos(DegreeToRadian(latitude)));
            cosH1 = (Math.Cos(DegreeToRadian(zenith)) - (sinDec1 * Math.Sin(DegreeToRadian(latitude)))) / (cosDec1 * Math.Cos(DegreeToRadian(latitude)));


            
            H = 360 - RadianToDegree(Math.Acos(cosH));

            H1 = RadianToDegree(Math.Acos(cosH1));

            H = H / 15;
            H1 = H1 / 15;
            T = H + RA - (0.06571 * t) - 6.622;
            T1 = H1 + RA1 - (0.06571 * t1) - 6.622;
            UT = T - lngHour;
            UT1 = T1 - lngHour;

            localT = UT + localOffset;
            if (localT < 0) { localT = localT + 24; }
            localT1 = UT1 + localOffset;
            localT1 = 24 + localT1;
            if (localT1 > 24) { localT1 = localT1 - 24; }

            int hourr = (int)localT;
            int min = (int)((localT - hourr) * 60);
            vychod = new System.DateTime(2001, 5, 16, hourr, min, 0);
            label41.Text = vychod.ToString("hh\\:mm tt");
            vychod = new System.DateTime(2001, 5, 16, hourr+1, min, 0);
            label71.Text = vychod.ToString("hh\\:mm tt");
            

            hourr = (int)localT1;
            min = (int)((localT1 - hourr) * 60);
            zapad = new System.DateTime(2001, 5, 16, hourr, min, 0);
            label40.Text = zapad.ToString("hh\\:mm tt");
            zapad = new System.DateTime(2001, 5, 16, hourr+1, min, 0);
            label72.Text = zapad.ToString("hh\\:mm tt");
            
            if (stav == false)
            {
                label42.Text = vychod.ToString("hh\\:mm tt");
                label43.Text = zapad.ToString("hh\\:mm tt");
                string date = DateTime.Now.ToString("dd\\/MM\\/yyyy");
                label44.Text = "dne: " + date;
            }
        }

        private void bt_pocitej_Click(object sender, EventArgs e)  //prepocet zapadu a vychodu
        {
            vychod_zapad(true);
            label49.Text = GetMoonPhase();

            vychod_zapad(false);
            vych_zapad_za();

            uloz_souradnice();
            

        }

        private double DegreeToRadian(double angle)
        {
            return Math.PI * angle / 180.0;
        }

        private double RadianToDegree(double angle)
        {
            return angle * (180.0 / Math.PI);
        }

        private void dm1_SelectedItemChanged(object sender, EventArgs e)
        {
            bt_kresli.Enabled = false;
            
        }

        private void dm2_SelectedItemChanged(object sender, EventArgs e)
        {
            bt_kresli.Enabled = false;
            
        }

        private void prepocitej(object sender, EventArgs e)
        {
            vych_zapad_za();
        }

        private void vych_zapad_za() {

            DateTime now = DateTime.Now;
            int Difference = (now.TimeOfDay - vychod.TimeOfDay).Hours;
            int Difference2 = (now.TimeOfDay - vychod.TimeOfDay).Minutes;
            if ((Difference2 >= 0) && (Difference >= 0)) 
            {
                if (Difference == 0) { label45.Text = "před: " + Difference2.ToString() + " min"; }
                else
                label45.Text = "před: " + Difference.ToString() + " hod " + Difference2.ToString() + " min"; 
                }

            else { label45.Text = "za: " + (-1*Difference).ToString() + " hod " + (-1*Difference2).ToString() + " min"; }

            Difference = (now.TimeOfDay - zapad.TimeOfDay).Hours;
            Difference2 = (now.TimeOfDay - zapad.TimeOfDay).Minutes;
            if ((Difference2 >= 0)&&(Difference >= 0)) 
                { 
                if (Difference == 0) { label46.Text = "před: " + Difference2.ToString() + " min"; }
                else
                label46.Text = "před: " + Difference.ToString() + " hod " + Difference2.ToString() + " min";
                }

            else { label46.Text = "za: " + (-1 * Difference).ToString() + " hod " + (-1 * Difference2).ToString() + " min"; }
            
        
        }

        private void bt_auto_Click(object sender, EventArgs e) //automaticke hledani USB-RF prijmace
        {
            string tata, dada;
            FTDI f = new FTDI();
            FTD2XX_NET.FTDI.FT_STATUS s;
            s = f.OpenByIndex(0);
            f.GetDescription(out dada);
            f.GetCOMPort(out tata);
            DialogResult result;

            

            if (dada == "Meteo vanek")
            {
                result = MessageBox.Show("Na portu: " + tata + " detekován: " + dada + "\n Chcete se připojit?", "Co chcete udělat?",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);

                f.Close();

                if (result == DialogResult.Yes)
                {
                    if (dada == "Meteo vanek")
                    {
                        serialPort1.PortName = tata;
                        serialPort1.BaudRate = 1200;
                        serialPort1.Open();
                    }
                    if (serialPort1.IsOpen)
                    {
                        /* Do status baru vypiseme soucasny stav */
                        stat_label.Text = "Připojeno na port " + serialPort1.PortName + " rychlostí " + serialPort1.BaudRate;

                        /* Vypneme/zapneme prvky v okne */
                        cb_port.Enabled = false;
                        label1.Text = "Stav: Připojeno";
                        timer2.Enabled = true;
                        bt_auto.Enabled = false;
                        bt_pripojit.Text = "Odpojit";
                    }
                }
                if (result == DialogResult.No)
                {
                }
            }
            else { MessageBox.Show("RF modul nenalezen","Error",MessageBoxButtons.OK,MessageBoxIcon.Error); }

            
            //----FTDI
        }

        private void dg1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void on_resize(object sender, EventArgs e) //zmena velikosti okna, neni dokonceno
        {
            int roz_sirka = (this.Size.Width - 883);
            int roz_vyska = (this.Size.Height - 778);
            tabControl1.Width = 843 + roz_sirka;
            tabControl1.Height = 710 + roz_vyska;

            //--- grafy
            ZGC1.Width = 801 + roz_sirka;
            ZGC1.Height = (((29400 / (778 -170 )) * (roz_vyska))/100) + 294;
            ZGC2.Height = ZGC1.Height;
            ZGC2.Width = 801 + roz_sirka;
            ZGC2.Top = ZGC1.Height + ZGC1.Location.Y + 5;
            //--- /grafy

            groupBox1.Width = (((15000 / 883) * (roz_sirka)) / 100) + 150;

            groupBox2.Width = (((13100 / 883) * (roz_sirka)) / 100) + 131;
            groupBox2.Left = groupBox1.Width + 10;

            groupBox3.Width = (((15200 / 883) * (roz_sirka)) / 100) + 152;
            groupBox3.Left = groupBox1.Width + groupBox2.Width + 15;

            groupBox5.Width = (((12700 / 883) * (roz_sirka)) / 100) + 127;
            groupBox5.Left = groupBox1.Width + groupBox2.Width + groupBox3.Width + 20;

            groupBox9.Width = (((12700 / 883) * (roz_sirka)) / 100) + 127;
            groupBox9.Left = groupBox1.Width + groupBox2.Width + groupBox3.Width + 20;

            groupBox4.Width = (((23600 / 883) * (roz_sirka)) / 100) + 236;
            groupBox4.Left = groupBox1.Width + groupBox2.Width + groupBox3.Width + groupBox5.Width + 25;


            label47.Text = (843 + (this.Size.Width - 883)).ToString();

            //---- tabulka
            dg1.Width = (((66800 / (883 - 100)) * (roz_sirka)) / 100) + 713;
            dg1.Height = (((64200 / (778 - 130)) * (roz_vyska )) / 100) + 571;

            //--- /tabulka




        }

        private void closed(object sender, FormClosedEventArgs e)
        {
            //while (prijimani) { }
            //serialPort1.DiscardInBuffer();
            serialPort1.Close();
        }



        private void bt_hledej_rekordy_Click(object sender, EventArgs e)  //hledani rekordu pocasi z tabulky
        {
            //-----
            //PB1.Value = 60;

            /*Thread t = new Thread(delegate() { pocitej(); });
            t.Start();
            t.Join(); */
            this.Cursor = Cursors.WaitCursor;
            pocitej();
        }    


        private void pocitej()  //algoritmus hledani maxim a minim
        {
            string od = cb_3.SelectedItem.ToString();
            string doo = cb_4.SelectedItem.ToString();
            string searchod = od;
            string searchdo = doo;
            int min = 0, max = 0;

            if (cb_3.SelectedIndex > cb_4.SelectedIndex)
            {
                MessageBox.Show("Chyba: \n Nelze hledat datum od > do !!", "Hlášení ", MessageBoxButtons.OK, MessageBoxIcon.Error); this.Cursor = Cursors.Arrow; return;
            }


            //bt_kresli.Enabled = true;


            for (int i = 0; i < dg1.Rows.Count - 1; i++)
            {
                if (dg1.Rows[i].Cells[0].Value.ToString() == searchod)
                {
                    min = i;
                    //dg1.Rows[i].Selected = true;
                    break;
                }
            }
            for (int i = 0; i < dg1.Rows.Count - 1; i++)
            {
                if (dg1.Rows[i].Cells[0].Value.ToString() == searchdo)
                {
                    max = i + 1;
                    //dg1.Rows[i].Selected = true;
                    //break;
                }
            }
            for (int a = min; a < max; a++)
            {
                dg1.Rows[a].Selected = true;
            }
            //MessageBox.Show("nalezeno " + (max - min) + " záznamů", "Hlášení", MessageBoxButtons.OK, MessageBoxIcon.Information);

            pocet_hodnot = max - min;


            //----
            teplota = null; cas = null; tlak = null; datum = null; vlhkost = null; rosny = null;

            int je = 0;
            datum = new string[max - min];
            cas = new string[max - min];
            teplota = new string[max - min];
            vlhkost = new string[max - min];
            tlak = new string[max - min];
            vetr_max = new string[max - min];
            srazky_max = new string[max - min];



            for (int i = min; i < max; i++)
            {

                datum[je] = dg1.Rows[i].Cells[0].Value.ToString();
                cas[je] = dg1.Rows[i].Cells[1].Value.ToString();
                teplota[je] = dg1.Rows[i].Cells[2].Value.ToString();

                vlhkost[je] = dg1.Rows[i].Cells[4].Value.ToString();
                tlak[je] = dg1.Rows[i].Cells[5].Value.ToString();
                srazky_max[je] = dg1.Rows[i].Cells[6].Value.ToString();
                vetr_max[je] = dg1.Rows[i].Cells[7].Value.ToString();
                
                je++;
            }

            arrteplota = new double[teplota.Length];
            arrtlak = new double[tlak.Length];
            arrvlhkost = new double[vlhkost.Length];
            arrvetr = new double[vetr_max.Length];
            arrsrazky = new double[srazky_max.Length];


            for (int i = 0; i < teplota.Length; i++)
            {
                arrteplota[i] = double.Parse(teplota[i], CultureInfo.CreateSpecificCulture("en-EN"));
                arrtlak[i] = double.Parse(tlak[i], CultureInfo.CreateSpecificCulture("en-EN"));
                arrvlhkost[i] = double.Parse(vlhkost[i], CultureInfo.CreateSpecificCulture("en-EN"));
                arrvetr[i] = double.Parse(vetr_max[i], CultureInfo.CreateSpecificCulture("en-EN"));
                arrsrazky[i] = double.Parse(srazky_max[i], CultureInfo.CreateSpecificCulture("en-EN"));
            }

            MessageBox.Show("nalezeno " + (max - min) + " záznamů", "Hlášení", MessageBoxButtons.OK, MessageBoxIcon.Information);

            double rek_max = 0;
            double rek_min = 0;
            int cas_max = 0;
            int cas_min = 0;
            //string dat_max = " ";
            //string dat_min = " ";

            min_max_index(arrteplota, out rek_max, out rek_min, out cas_max, out cas_min);
            label54.Text = rek_max.ToString() + " °C";
            label53.Text = rek_min.ToString() + " °C";
            label55.Text = "v " + cas[cas_max];
            label56.Text = "v " + cas[cas_min];
            label69.Text = "dne: " + datum[cas_max];
            label77.Text = "dne: " + datum[cas_min];

            min_max_index(arrvlhkost, out rek_max, out rek_min, out cas_max, out cas_min);
            label58.Text = rek_max.ToString() + " %";
            label57.Text = rek_min.ToString() + " %";
            label63.Text = "v " + cas[cas_max];
            label60.Text = "v " + cas[cas_min];
            label75.Text = "dne: " + datum[cas_max];
            label78.Text = "dne: " + datum[cas_min];

            min_max_index(arrtlak, out rek_max, out rek_min, out cas_max, out cas_min);
            label68.Text = rek_max.ToString() + " hPa";
            label64.Text = rek_min.ToString() + " hPa";
            label67.Text = "v " + cas[cas_max];
            label65.Text = "v " + cas[cas_min];
            label76.Text = "dne: " + datum[cas_max];
            label79.Text = "dne: " + datum[cas_min];

            min_max_index(arrvetr, out rek_max, out rek_min, out cas_max, out cas_min);
            label96.Text = rek_max.ToString() + " m/s";
            label100.Text = "v " + cas[cas_max];
            label94.Text = "dne: " + datum[cas_max];

            min_max_index(arrsrazky, out rek_max, out rek_min, out cas_max, out cas_min);
            label95.Text = rek_max.ToString() + " mm";
            label98.Text = "v " + cas[cas_max];
            label93.Text = "dne: " + datum[cas_max];

            this.Cursor = Cursors.Arrow;
        }
        

        public void min_max_index(double[] pole, out double maxima, out double minima,out int index_max, out int index_min) 
        {
            maxima = -100;
            minima = 2000;
            index_max = 0;
            index_min = 0;

            for (int i = 0; i < teplota.Length; i++)
            {
                if (pole[i] > maxima)
                {
                    maxima = pole[i];
                    index_max = i;
                }
                else if (pole[i] <= minima)
                {
                    minima = pole[i];
                    index_min = i;
                }
            }
        }

        public static string GetMoonPhase()  //vypocet fazi mesice
        {
            int year = DateTime.Now.Year;
            int month = DateTime.Now.Month;
            int day = DateTime.Now.Day;
            double c, e, jd;
            int b;
            if (month < 3)
            {
                year--;
                month += 12;
            }
            month++;
            c = 365.25 * year;
            e = 30.6 * month;
            jd = (c + e + day - 694039.09) / 29.5305882;
            b = (int)jd;
            jd -= b;
            b = (int)Math.Round(jd * 8);
            if (b >= 8) b = 0;
            switch (b)
            {
                case 0:
                    return "nov";
                case 1:
                    return "dorůstající";
                case 2:
                    return "první čtvrť";
                case 3:
                    return "dorůstá";
                case 4:
                    return "úplněk";
                case 5:
                    return "couvá";
                case 6:
                    return "Poslední čtvrť";
                case 7:
                    return "ubývající";
                default:
                    return "Error";
            }
        }

        private void hledat_click(object sender, EventArgs e)
        {
            tb_hledat.Text = "";
        }

        private void doit(int thisday,int thismonth, int thisyear)
        {
            DateTime theDate;
            double n = DateTime.Now.Minute;
            double timzon;
            double j1,i1,i2,i3,secs;
            int cycle;

            timzon = 1 / 24.0;
            n = Math.Floor(12.37 * (thisyear - 1900 + ((1.0 * thismonth - 0.5) / 12.0)));
            j1 = julday(thismonth, thisday, thisyear);
            flmoon(n, 2);

            n += Math.Floor(1.0 * (j1 - jd) / 28.0); cycle = 1;

            for (var iii = 1; iii <= 8; iii++)
            {              
                flmoon(n, nph);
                frac = 24.0 * (frac + timzon);
                if (frac < 0)
                {
                    --jd;
                    frac += 24.0;
                }

                if (frac > 12.0)
                {
                    ++jd;
                    frac -= 12.0;
                }
                else

                    frac += 12.0;
                i1 = Math.Floor(frac);
                secs = 3600.0 * (frac - i1);
                i2 = Math.Floor(secs / 60.0);
                i3 = Math.Floor(secs - 60 * i2 + 0.5);
                caldat(jd);

                theDate = new DateTime(yeaar,moonth,daay,(int)i1, (int)i2, (int)i3);

                if (nph == 2)
                {

                    if (cycle == 1)
                    {
                        {   //pokud je mladsi nez 7dni, tak ho ponecha
                            label81.Text = /*weekday[theDate.Day] +*/ theDate.Day.ToString() + "/" + theDate.Month.ToString() + "/" + theDate.Year.ToString() + "\n     " + theDate.ToShortTimeString();
                            prvni_1 = theDate;
                        }
                    }
                    else
                    {
                     //   {
                            label85.Text = /*weekday[theDate.Day] +*/ theDate.Day.ToString() + "/" + theDate.Month.ToString() + "/" + theDate.Year.ToString() + "\n     " + theDate.ToShortTimeString();
                            prvni_2 = theDate;
                     //   }
                    }
                }
                else if (nph == 3)
                {

                    if (cycle == 1)
                    {
                        label82.Text = /*weekday[theDate.Day] +*/ theDate.Day.ToString() + "/" + theDate.Month.ToString() + "/" + theDate.Year.ToString() + "\n     " + theDate.ToShortTimeString();
                        druhy_1 = theDate;

                    }
                    else
                    {
                        label86.Text =/*weekday[theDate.Day] +*/ theDate.Day.ToString() + "/" + theDate.Month.ToString() + "/" + theDate.Year.ToString() + "\n     " + theDate.ToShortTimeString();
                        druhy_2 = theDate;
                    }
                }
                else if (nph == 0)
                {

                    if (cycle == 1)
                    {                        
                            label83.Text = /*weekday[theDate.Day] +*/ theDate.Day.ToString() + "/" + theDate.Month.ToString() + "/" + theDate.Year.ToString() + "\n     " + theDate.ToShortTimeString();
                            treti_1 = theDate;
                    }

                    else
                    {
                            label87.Text = /*weekday[theDate.Day] +*/ theDate.Day.ToString() + "/" + theDate.Month.ToString() + "/" + theDate.Year.ToString() + "\n     " + theDate.ToShortTimeString();
                            treti_2 = theDate;
                    }
                }
                else if (nph == 1)
                {
                    if (cycle == 1)
                    {
                        label84.Text = /*weekday[theDate.Day] +*/ theDate.Day.ToString() + "/" + theDate.Month.ToString() + "/" + theDate.Year.ToString() + "\n     " + theDate.ToShortTimeString();
                        ctvrty_1 = theDate;
                    }
                    else
                    {
                        label88.Text = /*weekday[theDate.Day] +*/ theDate.Day.ToString() + "/" + theDate.Month.ToString() + "/" + theDate.Year.ToString() + "\n     " + theDate.ToShortTimeString();
                        ctvrty_2 = theDate;
                    }
                }

                if (nph == 3)
                {
                    nph = 0;
                    ++n;
                }
                else

                    ++nph;

                if (nph == 2)
                {
                    ++cycle;
                }
            }


        }

       double julday(int mm,int id, int iyyy) //prepocet roku na juliansky
       { 
           double jy,jm;
           double j1,j2,j3,j4,j5,j6,jul;

           if (iyyy == 0)
           {
               return 0;
           }
           else
           {

               if (iyyy < 0) { iyyy += 1; }
               if (mm > 2)
               {
                   jy = iyyy;
                   jm = 1.0 * mm + 1;
               }
               else
               {
                   jy = iyyy - 1;
                   jm = 1.0 * mm + 13;
               }

               j1 = 1.0 * Math.Floor(365.25 * jy);
               j2 = 1.0 * j1 + Math.Floor(30.6001 * jm);
               j3 = 1.0 * j2 + 1.0 * id;
               j4 = 1.0 * j3 + 1720995;

               if (id + 31 * (mm + 12 * iyyy) >= (15 + 31 * (10 + 12 * 1582)))
               {
                   ja = 1.0 * Math.Floor(0.01 * jy);
                   jb = 1.0 * Math.Floor(0.25 * ja);
                   j5 = 1.0 * j4 + 2;
                   j6 = 1.0 * j5 - ja;
                   jul = 1.0 * j6 + jb;
               }
               else
               {
                   jul = j4;
               }
               return jul;
           }
}

        void flmoon(double n, double nph) 
        {
            double c,t,t2;
            double RAD, am,asi,xtra,i;


	        RAD = 3.14159265/180.0;
	        c = n + nph / 4.0;
	        t = c / 1236.85;
	        t2 = t * t;
	        asi = 359.2242 + 29.105356 * c;
	        am = 306.0253 + 385.816918 * c + 0.010730 * t2;
	        jd = 2415020 + 28 * n + 7 * nph;
	        xtra = 0.75933 + 1.53058868 * c + ((0.0001178) - (0.000000155) * t) * t2;
	        if (nph == 0 || nph == 2)
            {
		        xtra += (0.1734 - 0.000393 * t) * Math.Sin(RAD * asi)- 0.4068 * Math.Sin(RAD * am);
            }
	        else if (nph == 1 || nph == 3)
            {
		        xtra += (0.1721 - 0.0004 * t) * Math.Sin(RAD * asi) - 0.6280 * Math.Sin(RAD * am);
            }
	        else
            {
            }
	        i = (xtra > 0.0 ? Math.Floor(xtra) :  Math.Ceiling(xtra - 1.0));
	        jd += i;
	        frac = xtra - i;

        }

        
        void caldat(double julian)
        {
            

	        julian = 0 + julian;
	        if (julian >= 2299161) 
            {
		        jalpha = Math.Floor((1.0 * (julian - 1867216)-0.25)/36524.25);
		        ja = 1 * julian + 1 + jalpha - Math.Floor(0.25 * jalpha);
	        } else 
            {
		        ja = julian;
	        }

	        jb = ja + 1524;
	        jc = Math.Floor(6680.0 + ((jb - 2439870)-122.1)/365.25);
	        jd = 365 * jc + Math.Floor(0.25 * jc);
	        je = Math.Floor((jb - jd)/ 30.6001);
	        daay = (int)(jb - jd - Math.Floor(30.6001 * je));
            moonth = (int)(je - 1);
	        if (moonth > 12) { moonth -= 12; }
            yeaar = (int)(jc - 4715);
	        if (moonth > 2) { --yeaar; }
	        if (yeaar <= 0) { --yeaar; }
        }

        private void uloz_souradnice ()  //ukladani udajuu do zdroju aplikace
        { 
            IResourceWriter writer = new ResourceWriter("myResources.resources");
            writer.AddResource("stupne_sirka", stupne_sirka.Text);
            writer.AddResource("minuty_sirka", minuty_sirka.Text);
            writer.AddResource("sekundy_sirka", sekundy_sirka.Text);
            writer.AddResource("stupne_delka", stupne_delka.Text);
            writer.AddResource("minuty_delka", minuty_delka.Text);
            writer.AddResource("sekundy_delka", sekundy_delka.Text);
            writer.AddResource("delka", cb_delka.SelectedIndex.ToString());
            writer.AddResource("sirka", cb_sirka.SelectedIndex.ToString());
            writer.AddResource("gmt", cb_gmt.SelectedIndex.ToString());
            writer.AddResource("url", tb_url.Text.ToString());
            writer.Close();
        }

        private void nacti_souradnice()  //nacitani zdroju
        {
            IResourceReader reader = new ResourceReader("myResources.resources");
            IDictionaryEnumerator en = reader.GetEnumerator();

            while (en.MoveNext())
            {
                switch (en.Key.ToString())
                {
                    case "stupne_sirka":
                        int stup_sirka = int.Parse(en.Value.ToString());
                        stupne_sirka.Text = stup_sirka.ToString();
                        break;
                    case "minuty_sirka":
                        int min_sirka = int.Parse(en.Value.ToString());
                        minuty_sirka.Text = min_sirka.ToString();
                        break;
                    case "sekundy_sirka":
                        int sek_sirka = int.Parse(en.Value.ToString());
                        sekundy_sirka.Text = sek_sirka.ToString();
                        break;
                    case "stupne_delka":
                        int stup_delka = int.Parse(en.Value.ToString());
                        stupne_delka.Text = stup_delka.ToString();
                        break;
                    case "minuty_delka":
                        int min_delka = int.Parse(en.Value.ToString());
                        minuty_delka.Text = min_delka.ToString();
                        break;
                    case "sekundy_delka":
                        int sek_delka = int.Parse(en.Value.ToString());
                        sekundy_delka.Text = sek_delka.ToString();
                        break;
                    case "sirka":
                        int sir = int.Parse(en.Value.ToString());
                        cb_sirka.SelectedIndex = sir;
                        break;
                    case "delka":
                        int delk = int.Parse(en.Value.ToString());
                        cb_delka.SelectedIndex = delk;
                        break;
                    case "gmt":
                        int gmt = int.Parse(en.Value.ToString());
                        cb_gmt.SelectedIndex = gmt;
                        break;
                    case "url":
                        tb_url.Text = en.Value.ToString();
                        tb_url2.Text = en.Value.ToString();
                        break;
                    default:
                        // error
                        break;
                }
            }
            reader.Close();
        }

        private void bt_prev_Click(object sender, EventArgs e) //ukaze predesly nalezeny zaznam v tabulce
        {
            if (index_hled > 0)
            {
                index_hled--;
                dg1.FirstDisplayedScrollingRowIndex = terms[index_hled];
                label92.Text = "Zobrazen: "+(index_hled+1).ToString()+" z " + terms.Length.ToString() + " vyhledaných";
                bt_next.Enabled = true;
            }
            else 
            {
                bt_prev.Enabled = false;
            }
            if (index_hled == 0)
            {
                bt_prev.Enabled = false;
            }
            
        }

        private void bt_next_Click(object sender, EventArgs e) //ukaze dalsi nalezeny zaznam v tabulce
        {
            
            if (index_hled < terms.Length - 1)
            {
                index_hled++;
                dg1.FirstDisplayedScrollingRowIndex = terms[index_hled];
                label92.Text = "Zobrazen: " + (index_hled + 1).ToString() + " z " + terms.Length.ToString() + " vyhledaných";
                bt_prev.Enabled = true;
            }
            else 
            {
                bt_next.Enabled = false;
            }
            if (index_hled == terms.Length - 1) 
            {
                bt_next.Enabled = false;
            }
        }

        private void text_tb_url_change(object sender, EventArgs e)  //zmena url dat, ulozi do resources
        {
            uloz_souradnice();
            tb_url2.Text = tb_url.Text;
        }

        private void text_tb_url2_change(object sender, EventArgs e) //zmena url dat, ulozi do resources
        {
            uloz_souradnice();
            tb_url.Text = tb_url2.Text;
        }

        private void click(object sender, EventArgs e)  // ukaze bublinu u ikonky meteostanice
        {
            notifyIcon1.BalloonTipText = "Teplota " + tepl + "°C\nVlhkost " + vlh + "%\nTlak " + tlakk + "hPa";
            notifyIcon1.BalloonTipTitle = "Meteostanice Vánek";
            notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
            notifyIcon1.ShowBalloonTip(500);
            notifyIcon1.Visible = true;
        }

        
    }
}
