Imports Skybound.Gecko
Public Class Form2
    Public i As Integer
    Public t As String = 0
    Public able As String = 1

    Private Sub KryptonButton9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton9.Click
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(KryptonComboBox1.Text)
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Skybound.Gecko.Xpcom.Initialize(My.Application.Info.DirectoryPath + "/xulrunner/")
        createtab()
        loadhistory()
        loadbookmarks()
        KryptonGroup2.Hide()
        KryptonGroup1.Visible = False
        KryptonGroup2.Visible = False
        KryptonGroup3.Visible = False
        KryptonGroup4.Visible = False
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(My.Settings.Home)
        i = 0
    End Sub

    Private Sub KryptonComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles KryptonComboBox1.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(KryptonComboBox1.Text)
        End If
    End Sub

    Private Sub KryptonButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton2.Click
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(KryptonComboBox1.Text)
    End Sub

    Private Sub KryptonButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton3.Click
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoForward()
    End Sub

    Private Sub KryptonButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton4.Click
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(My.Settings.Home)
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
        KryptonComboBox1.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Url.AbsoluteUri
    End Sub

    Private Sub KryptonButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub KryptonButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton7.Click
        If KryptonGroup1.Visible = False Then
            KryptonGroup1.Visible = True
        Else
            KryptonGroup1.Visible = False
        End If
    End Sub

    Private Sub KryptonButton13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton13.Click
        If KryptonGroup3.Visible = True Then
            KryptonGroup3.Hide()
        Else
            KryptonGroup3.Show()
        End If
    End Sub

    Private Sub DownloadmanagerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub SetHomeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub KryptonContextMenu1_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs)

    End Sub

    Private Sub google_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles google.Click
        If KryptonGroup2.Visible = True Then
            KryptonGroup2.Hide()
        Else
            KryptonGroup2.Show()

        End If
    End Sub

    Private Sub PictureBox2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub yahoo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yahoo.Click
        waiting.Image = google.Image
        waiting.Tag = google.Tag
        google.Image = yahoo.Image
        google.Tag = yahoo.Tag
        yahoo.Image = waiting.Image
        yahoo.Tag = waiting.Tag
        KryptonGroup2.Hide()
    End Sub

    Private Sub bing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bing.Click
        waiting.Image = google.Image
        waiting.Tag = google.Tag
        google.Image = bing.Image
        google.Tag = bing.Tag
        bing.Image = waiting.Image
        bing.Tag = waiting.Tag
        KryptonGroup2.Hide()
    End Sub

    Private Sub ask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ask.Click
        waiting.Image = google.Image
        waiting.Tag = google.Tag
        google.Image = ask.Image
        google.Tag = ask.Tag
        ask.Image = waiting.Image
        ask.Tag = waiting.Tag
        KryptonGroup2.Hide()
    End Sub

    Private Sub youtube_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles youtube.Click
        waiting.Image = google.Image
        waiting.Tag = google.Tag
        google.Image = youtube.Image
        google.Tag = youtube.Tag
        youtube.Image = waiting.Image
        youtube.Tag = waiting.Tag
        KryptonGroup2.Hide()
    End Sub

    Private Sub KryptonLinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonLinkLabel1.LinkClicked
        My.Settings.Home = KryptonComboBox1.Text
    End Sub

    Private Sub KryptonButton10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton10.Click
        KryptonListBox1.Items.Add(KryptonComboBox1.Text)
        syncbookmarks()
    End Sub
    Private Sub KryptonLinkLabel3_LinkClicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonLinkLabel3.LinkClicked
        Dim Browse As New WebBrowser
        TabControl1.TabPages.Add(t, "Tab" + t)
        TabControl1.SelectTab(t)
        Browse.Name = "wb"
        Browse.Dock = DockStyle.Fill
        Browse.Visible = True
        TabControl1.SelectedTab.Controls.Add(Browse)
        AddHandler CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).DocumentCompleted, AddressOf wb_doc
        t = t + 1
    End Sub

    Private Sub KryptonLinkLabel4_LinkClicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonLinkLabel4.LinkClicked
        If t > 1 Then
            TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex)
            TabControl1.SelectTab(TabControl1.TabPages.Count - 1)
            t = t - 1
        End If
    End Sub

    Private Sub KryptonButton14_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles KryptonButton14.Click
        If google.Tag = "google" Then
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://www.google.com/#sclient=psy&hl=en&q=" + KryptonTextBox1.Text + "&aq=f&aqi=g4g-o1&aql=&oq=&gs_rfai=&pbx=1&fp=84f34ab5383c7ee9")
        ElseIf google.Tag = "yahoo" Then
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://search.yahoo.com/search;_ylt=AuaDb.H4ZTr.Lq6qlkNko1ebvZx4?p=" + KryptonTextBox1.Text)
        ElseIf google.Tag = "bing" Then
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://www.bing.com/search?q=" + KryptonTextBox1.Text)
        ElseIf google.Tag = "ask" Then
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://www.ask.com/web?qsrc=2990&o=0&l=dir&q=" + KryptonTextBox1.Text)
        ElseIf google.Tag = "youtube" Then
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://www.youtube.com/results?search_query=" & KryptonTextBox1.Text & "&aq=f")
        End If
    End Sub

    Private Sub KryptonButton11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton11.Click
        If KryptonListBox1.SelectedIndex >= 0 Then
            KryptonListBox1.Items.Remove(KryptonListBox1.SelectedItem)
            syncbookmarks()
        End If
    End Sub

    Private Sub KryptonButton15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton15.Click
        KryptonListBox2.Items.Clear()
        synchistory()
    End Sub
    Sub synchistory()
        On Error GoTo E
        Dim file As String = My.Application.Info.DirectoryPath + "/history.txt"
        Dim i As String = KryptonListBox2.Items.Count
        Dim f As String = 0
        If My.Computer.FileSystem.FileExists(My.Application.Info.DirectoryPath + "/history.txt") Then
            My.Computer.FileSystem.DeleteFile(file)
            Do Until f = i
                If f = i - 1 Then
                    KryptonListBox2.SelectedIndex = f
                    Dim write As New System.IO.StreamWriter(file, True)
                    write.Write(KryptonListBox2.SelectedItem)
                    write.Close()
                    f = f + 1
                Else
                    KryptonListBox2.SelectedIndex = f
                    Dim write As New System.IO.StreamWriter(file, True)
                    write.Write(KryptonListBox2.SelectedItem + ",")
                    write.Close()
                    f = f + 1
                End If
            Loop
        Else
            Do Until f = i
                If f = i - 1 Then
                    KryptonListBox2.SelectedIndex = f
                    Dim write As New System.IO.StreamWriter(file, True)
                    write.Write(KryptonListBox2.SelectedItem)
                    write.Close()
                    f = f + 1
                Else
                    KryptonListBox2.SelectedIndex = f
                    Dim write As New System.IO.StreamWriter(file, True)
                    write.Write(KryptonListBox2.SelectedItem + ",")
                    write.Close()
                    f = f + 1
E:
                End If
            Loop
        End If
    End Sub

    Private Sub KryptonButton16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton16.Click
        If KryptonListBox2.SelectedItem = "" Then
        Else
            KryptonListBox2.Items.Remove(KryptonListBox2.SelectedItem)
        End If
    End Sub
    Sub wb_doc()
        TabControl1.SelectedTab.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle
    End Sub
    Sub loadbookmarks()
        If My.Computer.FileSystem.FileExists(My.Application.Info.DirectoryPath + "/bookmarks.txt") Then
            Dim i As Integer
            Dim file As String = My.Application.Info.DirectoryPath + "/bookmarks.txt"
            Dim read As New System.IO.StreamReader(file)
            Dim line() As String
            Dim textbox As String

            textbox = read.ReadToEnd
            line = textbox.Split(",")
            For i = 0 To UBound(line)
                KryptonListBox1.Items.Add(line(i))
            Next i
        Else
        End If

    End Sub
    Sub syncbookmarks()
        Dim file As String = My.Application.Info.DirectoryPath + "/bookmarks.txt"
        Dim i As String = KryptonListBox1.Items.Count
        Dim f As String = 0
        If My.Computer.FileSystem.FileExists(My.Application.Info.DirectoryPath + "/bookmarks.txt") Then
            My.Computer.FileSystem.DeleteFile(file)
            Do Until f = i
                If f = i - 1 Then
                    KryptonListBox1.SelectedIndex = f
                    Dim write As New System.IO.StreamWriter(file, True)
                    write.Write(KryptonListBox1.SelectedItem)
                    write.Close()
                    f = f + 1
                Else
                    KryptonListBox1.SelectedIndex = f
                    Dim write As New System.IO.StreamWriter(file, True)
                    write.Write(KryptonListBox1.SelectedItem + ",")
                    write.Close()
                    f = f + 1
                End If
            Loop
        Else
            Do Until f = i
                If f = i - 1 Then
                    KryptonListBox1.SelectedIndex = f
                    Dim write As New System.IO.StreamWriter(file, True)
                    write.Write(KryptonListBox1.SelectedItem)
                    write.Close()
                    f = f + 1
                Else
                    KryptonListBox1.SelectedIndex = f
                    Dim write As New System.IO.StreamWriter(file, True)
                    write.Write(KryptonListBox1.SelectedItem + ",")
                    write.Close()
                    f = f + 1
                End If
            Loop
        End If
    End Sub
    Sub loadhistory()
        If My.Computer.FileSystem.FileExists(My.Application.Info.DirectoryPath + "/history.txt") Then
            Dim i As Integer
            Dim file As String = My.Application.Info.DirectoryPath + "/history.txt"
            Dim read As New System.IO.StreamReader(file)
            Dim line() As String
            Dim textbox As String

            textbox = read.ReadToEnd
            line = textbox.Split(",")
            For i = 0 To UBound(line)
                KryptonListBox2.Items.Add(line(i))
            Next i
        Else
        End If
    End Sub
    Sub createtab()

        Dim Browse As New WebBrowser
        TabControl1.TabPages.Add(t, "Tab" + t)
        TabControl1.SelectTab(t)
        Browse.Name = "wb"
        Browse.Dock = DockStyle.Fill
        Browse.Visible = True
        AddHandler Browse.Navigating, AddressOf Browse_Navigating
        AddHandler Browse.Navigated, AddressOf Browse_Navigated
        AddHandler Browse.ProgressChanged, AddressOf Browse_Progresschange
        AddHandler Browse.DocumentTitleChanged, AddressOf Browse_DTC
        TabControl1.SelectedTab.Controls.Add(Browse)
        t = t + 1

    End Sub
    Sub Browse_Navigated(ByVal sender As System.Object, ByVal e As System.EventArgs)
        TabControl1.SelectedTab.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Document.Title
        KryptonComboBox1.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Url.AbsoluteUri
    End Sub
    Private Sub Browse_Progresschange(ByVal sender As System.Object, ByVal e As Windows.Forms.WebBrowserProgressChangedEventArgs)
        On Error GoTo A
        Dim maxprog As Double = e.MaximumProgress
        Dim curprog As Double = e.CurrentProgress
        If maxprog > 0 Then
            If maxprog < 1 Then
                ToolStripProgressBar1.Value = (curprog / maxprog) * 100
            Else
A:
            End If
        End If
    End Sub
    Sub Browse_DTC()
        If KryptonComboBox1.Items.Contains(CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Url.AbsoluteUri) Then
        Else
            KryptonComboBox1.Items.Add(CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Url.AbsoluteUri)
        End If
        If KryptonListBox2.Items.Contains(CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Url.AbsoluteUri) Then
        Else
            KryptonListBox2.Items.Add(CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Url.AbsoluteUri)
        End If
    End Sub
    Sub Browse_Navigating(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ToolStripStatusLabel1.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).StatusText
    End Sub

    Private Sub KryptonButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton8.Click
        If KryptonGroup4.Visible = False Then
            KryptonGroup4.Visible = True
        Else
            KryptonGroup4.Visible = False
        End If
        synchistory()
    End Sub

    Private Sub KryptonLabel4_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)

    End Sub

    Private Sub KryptonPanel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles KryptonPanel1.Paint

    End Sub

    Private Sub KryptonLinkLabel5_LinkClicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonLinkLabel5.LinkClicked
        MsgBox("Welcome to the Trinity Project, an open-source, co-lab webbrowser project. This project was started by Brandonsoft, and is hosted on the Brandonsoft forums. Have Fun!", , "Trinity Webbrowser")
    End Sub
End Class