
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Diagnostics;

using AHD.Forms;
using AHD.SM.ASMP;
using AHD.SM.Forms;
using AHD.SM.Formats;

namespace AHD.SM
{
    public partial class Frm_Main : Form
    {
        public Frm_Main(string[] Args)
        {
            InitializeComponent();
            LoadSettings();
            //create new default project any way
            Program.ProjectManager.Project = new Project("Untitled");
            Program.ProjectManager.FilePath = "";
            Program.ProjectManager.ProjectSaved += new EventHandler(ProjectManager_ProjectSaved);
            Program.ProjectManager.ProjectDidntSave += new EventHandler<ErrorEventArgs>(ProjectManager_ProjectDidntSave);
            Program.ProjectManager.ProjectLoaded += new EventHandler(ProjectManager_ProjectLoaded);
            Program.ProjectManager.ProjectDidntLoad += new EventHandler<ErrorEventArgs>(ProjectManager_ProjectDidntLoad);
            mediaPlayer1.ChangeMediaRequest += new EventHandler(mediaPlayer1_ChangeMediaRequest);
            ApplyProject();
            Save = false;
            LaunchCommandLines(Args);
        }
        SubtitlesTrack selectedTrack = null;
        Subtitle viewedSubtitle = null;
        List<Subtitle> copiedSubtitles = new List<Subtitle>();
        int sIndex = -1;//current viewed subtitle index
        int pIndex = -1;//set to -1 to refresh view
        bool IsAddingSubtitle = false;//set when the user hold the add button
        Subtitle addSubtitle = null;//The subtitle to add, created when user hold quick add button, 
        //destroyed when user release that button
        int SearchIndex = 0;
        Point downPoint;
        bool canDragDrop = false;
        bool needToSaveSettings = true;

        void LaunchCommandLines(string[] Args)
        {
            if (Args != null)
            {
                if (Args.Length > 0)
                {
                    for (int i = 0; i < Args.Length; i++)
                    {
                        switch (Args[i])
                        {
                            case "/imp"://import subtitles format
                                i++;
                                ImportFormat(Args[i]);
                                break;
                            case "/ly"://load layout
                                i++;
                                LoadLayout(Args[i]);
                                break;
                            case "/med"://change media
                                i++;
                                ChangeMedia(Args[i]);
                                break;
                            default:
                                if (File.Exists(Args[i]))
                                {
                                    LoadProject(Args[i]);
                                }
                                break;
                        }
                    }
                }
            }
        }
        public void ShowQuickStart()
        {
            Frm_QuickStart qq = new Frm_QuickStart(Program.Settings.RecentProject, Program.Settings.ShowQuickStart);
            qq.ShowDialog(this);
            Program.Settings.ShowQuickStart = qq.ShowAtStartUp;
            switch (qq.QuickStartResult)
            {
                case QuickStartResult.NewProject: NewProject(); break;
                case QuickStartResult.OpenProject: openToolStripMenuItem_Click(this, null); break;
                case QuickStartResult.OpenRecentProject: LoadProject(qq.OpenRecentPath); break;
                case QuickStartResult.GettingStarted: Help.ShowHelp(this, Program.StartUpPath + "\\Help.chm",
                    HelpNavigator.KeywordIndex, "Workflow"); break;
                case QuickStartResult.Import://import subtitles format file
                    OpenFileDialog op = new OpenFileDialog();
                    op.Title = Program.ResourceManager.GetString("Title_ImportSubtitlesFormat");
                    op.Filter = SubtitleFormats.GetFilter();
                    if (op.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                    {
                        ImportFormat(op.FileName);
                    }
                    break;
            }
            Program.Settings.Save();
        }
        public void ShowTipOfTheDay()
        {
            Frm_TipOfTheDay tii = new Frm_TipOfTheDay(Program.Settings.ShowTipOfDay,
                Program.Settings.TipOfDayIndex, Program.StartUpPath + "\\" + Program.CultureInfo.Name + "\\Help.chm");
            tii.ShowDialog(this);
            Program.Settings.ShowTipOfDay = tii.DisplayAtStartUp;
            Program.Settings.TipOfDayIndex = tii.TipIndex;
            Program.Settings.Save();
        }
        void ImportFormat(string filePath)
        {
            Frm_Import import = new Frm_Import(filePath, Program.Settings.ImportDeepSearch);
            if (import.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                //prepare format
                SubtitlesFormat format = import.SubtitlesFormat;
                Program.Settings.FavoriteFormat = format.Name;
                format.LoadStarted += new EventHandler(format_LoadStarted);
                format.LoadFinished += new EventHandler(format_LoadFinished);
                format.Progress += new EventHandler<ProgressArgs>(format_Progress);
                //load
                format.Load(filePath, import.SelectedEncoding);
                //get track(s)
                SubtitlesTrack[] tracks = format.SubtitleTracks.ToArray();
                if (!format.IsMultiTrack)
                    tracks = new SubtitlesTrack[1] { format.SubtitleTrack };
                //add the track(s) into the project
                for (int i = 0; i < tracks.Length; i++)
                {
                    int j = 0;
                    string name = "ImportedTrack" + (i + 1).ToString() + "_" + j;
                    while (Program.ProjectManager.Project.IsSubtitlesTrackExist(name))
                    {
                        j++;
                        name = "ImportedTrack" + (i + 1).ToString() + "_" + j;
                    }
                    tracks[i].Name = name;
                    if (!format.IsMultiTrack)
                    {
                        tracks[i].RightToLeft = import.RightToLeftSubtitlesFormat;
                        foreach (Subtitle sub in tracks[i].Subtitles)
                            sub.Text.RighttoLeft = import.RightToLeftSubtitlesFormat;
                    }
                    Program.ProjectManager.Project.SubtitleTracks.Add(tracks[i]);
                }
                format.Dispose();
                //refresh
                RefreshSubtitleTracks();
                //select
                foreach (TreeNode_SubtitlesTrack node in treeView_subtitleTracks.Nodes)
                {
                    if (node.SubtitlesTrack.Name == tracks[0].Name)
                    {
                        treeView_subtitleTracks.SelectedNode = node;
                        break;
                    }
                }
                if (Program.Settings.AutoCheckForErrors)
                {
                    if (!Program.Settings.CurrentLayout.Tab_Errors.Visible)
                    {
                        Program.Settings.CurrentLayout.Tab_Errors.Visible = errorsToolStripMenuItem1.Checked = true;
                        SetTabParent(Program.Settings.CurrentLayout.Tab_Errors, tabPage_errors);
                        CheckForCollapse();
                    }
                    ((TabControl)tabPage_errors.Parent).SelectedTab = tabPage_errors;
                    errorsChecker1.CheckForErrors();
                }
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_ImportSubtitlesFormat"));
            }
        }
        void ApplyTrackToControls()
        {
            errorsChecker1.SubtitlesTrack = subtitleEditor1.SubtitlesTrack = timeLine1.SubtitlesTrack =
              subtitlesDataEditor1.SubtitlesTrack = selectedTrack;
        }

        #region Project New/Save/Open
        void NewProject()
        {
            if (!AskToSave())
                return;
            if (!Program.Settings.ShowNewProjectWizard)
            {
                Program.ProjectManager.Project = new Project(Program.ResourceManager.GetString("Untitled"));
                Program.ProjectManager.FilePath = "";
                Program.ProjectManager.ProjectSaved += new EventHandler(ProjectManager_ProjectSaved);
                Program.ProjectManager.ProjectDidntSave += new EventHandler<ErrorEventArgs>(ProjectManager_ProjectDidntSave);
                Program.ProjectManager.ProjectLoaded += new EventHandler(ProjectManager_ProjectLoaded);
                Program.ProjectManager.ProjectDidntLoad += new EventHandler<ErrorEventArgs>(ProjectManager_ProjectDidntLoad);
                ApplyProject();
                Save = false;
            }
            else
            {
                Frm_NewProject newDialog = new Frm_NewProject();
                if (newDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    Program.ProjectManager.Project = new Project(newDialog.ProjectName);
                    //media
                    if (File.Exists(newDialog.ProjectMediaFile))
                        Program.ProjectManager.Project.MediaPath = newDialog.ProjectMediaFile;
                    //Import
                    if (File.Exists(newDialog.ProjectImport))
                    {
                        ImportFormat(newDialog.ProjectImport);
                    }
                    Program.ProjectManager.FilePath = "";
                    Program.ProjectManager.ProjectSaved += new EventHandler(ProjectManager_ProjectSaved);
                    Program.ProjectManager.ProjectDidntSave += new EventHandler<ErrorEventArgs>(ProjectManager_ProjectDidntSave);
                    Program.ProjectManager.ProjectLoaded += new EventHandler(ProjectManager_ProjectLoaded);
                    Program.ProjectManager.ProjectDidntLoad += new EventHandler<ErrorEventArgs>(ProjectManager_ProjectDidntLoad);
                    ApplyProject();
                    Save = false;
                }
            }
        }

        void ProjectManager_ProjectDidntLoad(object sender, ErrorEventArgs e)
        {
            SetStatus(Program.ResourceManager.GetString("Status_ProjectDidntLoad"), StatusType.Error);
            MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_ErrorWhenLoadingProject") +
                ":\n" + e.GetException().Message, Program.ResourceManager.GetString("MessageCaption_LoadError"),
                MessageDialogButtons.Ok, MessageDialogIcon.Error);
        }
        void ProjectManager_ProjectLoaded(object sender, EventArgs e)
        {
            AddRecent(Program.ProjectManager.FilePath);
            RefreshRecents();
            SetStatus(Program.ResourceManager.GetString("Status_ProjectLoadedSuccessfuly"), StatusType.Good);
        }
        void ProjectManager_ProjectDidntSave(object sender, ErrorEventArgs e)
        {
            SetStatus(Program.ResourceManager.GetString("Status_ProjectDidntSave"), StatusType.Error);
            MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_ErrorWhenSavingProject") + ":\n" + e.GetException().Message,
                Program.ResourceManager.GetString("MessageCaption_SaveError"),
                MessageDialogButtons.Ok, MessageDialogIcon.Error);
        }
        void ProjectManager_ProjectSaved(object sender, EventArgs e)
        {
            AddRecent(Program.ProjectManager.FilePath);
            RefreshRecents();
            SetStatus(Program.ResourceManager.GetString("Status_ProjectSavedSuccessfuly"), StatusType.Good);
        }

        bool SaveProject()
        {
            if (File.Exists(Program.ProjectManager.FilePath))
            {
                if (Program.ProjectManager.Save())
                {
                    Save = false;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                SaveFileDialog sav = new SaveFileDialog();
                sav.Title = Program.ResourceManager.GetString("Title_SaveProject");
                sav.Filter = Filters.ASMP;
                if (sav.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    if (Program.ProjectManager.Save(sav.FileName))
                    {
                        Save = false;
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return false;
        }
        void ApplyProject()
        {
            //Clear all
            ClearAll();
            ClearHistory();
            AddHistory(Program.ResourceManager.GetString("History_ProjectOpened"));
            //subtitle tracks
            RefreshSubtitleTracks();
            //Log
            richTextBox_Log.Text = Program.ProjectManager.Project.Log;
            //Description
            richTextBox_description.Text = Program.ProjectManager.Project.Description;
            //Media
            ChangeMedia(Program.ProjectManager.Project.MediaPath);
            //marks
            timeLine1.TimeMarks = Program.ProjectManager.Project.TimeMarks;
            RefreshMarks();
        }
        void LoadProject(string FilePath)
        {
            if (!AskToSave())
                return;
            if (Program.ProjectManager.Load(FilePath))
            {
                ApplyProject();
                Save = false;
                if (Program.Settings.AutoSelectFirstTrack)
                {
                    if (treeView_subtitleTracks.Nodes.Count > 0)
                        treeView_subtitleTracks.SelectedNode = treeView_subtitleTracks.Nodes[0];
                }
            }
        }
        void ChangeMedia(string FilePath)
        {
            ChangeMedia(FilePath, true);
        }
        void ChangeMedia(string FilePath, bool warn)
        {
            mediaPlayer1.LoadMedia(FilePath);
            Program.ProjectManager.Project.MediaPath = FilePath;
            Timer_Player.Start();
            iD3TagsSynchronizedLyricsToolStripMenuItem.Enabled =
                Path.GetExtension(FilePath).ToLower() == ".mp3";
            matroskaRipperToolStripMenuItem.Enabled =
                 Path.GetExtension(FilePath).ToLower() == ".mkv" |
                 Path.GetExtension(FilePath).ToLower() == ".mks";
            if (warn)
            {
                if (Path.GetExtension(FilePath).ToLower() == ".mp3")
                {
                    if (Program.Settings.WarnMeWhenID3Detected)
                    {
                        MessageDialogResult result = MessageDialog.ShowMessage(this, Program.ResourceManager.GetString("Message_ID3Detected"),
                            Program.ResourceManager.GetString("MessageCaption_ID3Detected"),
                            MessageDialogButtons.OkNo | MessageDialogButtons.Checked,
                            MessageDialogIcon.Question, Program.Settings.WarnMeWhenID3Detected, Program.ResourceManager.GetString("Button_Yes"),
                             Program.ResourceManager.GetString("Button_No"),
                            "", Program.ResourceManager.GetString("MessageCheckBox_AlwaysWarnMeWhenID3Detected"));
                        Program.Settings.WarnMeWhenID3Detected = ((result & MessageDialogResult.Checked) == MessageDialogResult.Checked);
                        if ((result & MessageDialogResult.Ok) == MessageDialogResult.Ok)
                        {
                            iD3TagsSynchronizedLyricsToolStripMenuItem_Click(this, null);
                        }
                    }
                }
                if (Path.GetExtension(FilePath).ToLower() == ".mkv" |
                     Path.GetExtension(FilePath).ToLower() == ".mks")
                {
                    if (Program.Settings.WarnMeWhenMKVDetected)
                    {
                        MessageDialogResult result = MessageDialog.ShowMessage(this, Program.ResourceManager.GetString("Message_MKVDetected"),
                            Program.ResourceManager.GetString("MessageCaption_MKVDetected"), MessageDialogButtons.OkNo | MessageDialogButtons.Checked,
                            MessageDialogIcon.Question, Program.Settings.WarnMeWhenMKVDetected, Program.ResourceManager.GetString("Button_Yes"),
                             Program.ResourceManager.GetString("Button_No"),
                            "", Program.ResourceManager.GetString("MessageCheckBox_AlwaysWarnMeWhenMKVDetected"));
                        Program.Settings.WarnMeWhenMKVDetected = ((result & MessageDialogResult.Checked) == MessageDialogResult.Checked);
                        if ((result & MessageDialogResult.Ok) == MessageDialogResult.Ok)
                        {
                            matroskaRipperToolStripMenuItem_Click(this, null);
                        }
                    }
                }
            }
        }
        void RefreshMarks()
        {
            ComboBox_marks.Items.Clear();
            foreach (TimeMark mark in Program.ProjectManager.Project.TimeMarks)
            {
                ComboBox_marks.Items.Add(mark.Name + " (" + mark.Time.ToString("F3") + ")");
            }
        }
        void SelectMarkAtTime(double time)
        {
            int index = -1;
            foreach (TimeMark mark in Program.ProjectManager.Project.TimeMarks)
            {
                if (time >= mark.Time)
                    index++;
            }
            ComboBox_marks.SelectedIndex = index;
        }
        #endregion
        #region Save/ Load settings
        void LoadSettings()
        {
            //languages
            for (int i = 0; i < Program.SupportedLanguages.Length / 3; i++)
            {
                ToolStripMenuItem item = new ToolStripMenuItem();
                item.Text = Program.SupportedLanguages[i, 2];
                item.Checked = Program.SupportedLanguages[i, 0] == Program.Settings.Language;
                languageToolStripMenuItem.DropDownItems.Add(item);
            }
            try
            {
                this.Size = Program.Settings.Frm_Main_Size;
                this.Location = Program.Settings.Frm_Main_Location;
                if (Program.Settings.CurrentLayout == null)
                    Program.Settings.CurrentLayout = new LayoutPresent();
                ApplyLayout(Program.Settings.CurrentLayout);

                RefreshRecents();
                RefreshFindRecents();
                ApplySettings();
                needToSaveSettings = true;
            }
            catch (Exception ex)
            {
                MessageDialogResult result = MessageDialog.ShowMessage(this, Program.ResourceManager.GetString("Message_SettingsLoadError") + "\n" +
                       Program.ResourceManager.GetString("Title_SystemMessage") + "\n" + ex.Message + "\n" + ex.ToString(),
                       Program.ResourceManager.GetString("MessageCaption_Settings"), MessageDialogButtons.OkNoCancel,
                       MessageDialogIcon.Error, false, Program.ResourceManager.GetString("Button_ResetSettings"),
                       Program.ResourceManager.GetString("Button_RetryLoading"), Program.ResourceManager.GetString("Button_Abort"), "");
                if (result == MessageDialogResult.Ok)
                {
                    Program.Settings.Reset();
                    LoadSettings();
                }
                if (result == MessageDialogResult.No)
                {
                    LoadSettings();
                }
                if (result == MessageDialogResult.Cancel)
                {
                    needToSaveSettings = false;
                    Application.Exit();
                    return;
                }
            }
        }
        void SaveSettings()
        {
            Program.Settings.Frm_Main_Size = this.Size;
            Program.Settings.Frm_Main_Location = this.Location;
            Program.Settings.CurrentLayout.SplitterDistance1 = splitContainer1.SplitterDistance;
            Program.Settings.CurrentLayout.SplitterDistance2 = splitContainer2.SplitterDistance;
            Program.Settings.CurrentLayout.SplitterDistance3 = splitContainer3.SplitterDistance;
            Program.Settings.CurrentLayout.SplitterDistance4 = splitContainer4.SplitterDistance;
            Program.Settings.CurrentLayout.SplitterDistance5 = splitContainer5.SplitterDistance;
            Program.Settings.AskWhenChangingRTL = subtitleEditor1.AskWhenChangingRTLToApplyToAll;
            Program.Settings.Save();
            //controls
            subtitlesDataEditor1.SaveSettings();
        }
        void ApplySettings()
        {
            //player
            mediaPlayer1.AdvanceTime = Program.Settings.AdvanceTime;
            nextFrameToolStripMenuItem.Text = "+ " + Program.Settings.AdvanceTime.ToString("F3") + " sec";
            previousFrameToolStripMenuItem.Text = "- " + Program.Settings.AdvanceTime.ToString("F3") + " sec";
            mediaPlayer1.EditorBackColor = Program.Settings.SubtitleTextEditorBackColor;
            mediaPlayer1.ShowEditorStatusStrip = Program.Settings.SubtitleTextEditorShowStatusStrip;
            Timer_Player.Interval = Program.Settings.PlayerTimer;
            //timeline
            timeLine1.MegnaticAccuracy = Program.Settings.MegnaticAccuracy;
            timeLine1.SmartToolAccuracy = Program.Settings.SmartToolAccuracy;
            //formats
            LoadFormats();
            //others
            subtitleEditor1.AskWhenChangingRTLToApplyToAll = Program.Settings.AskWhenChangingRTL;
            subtitleEditor1.EditorBackColor = Program.Settings.SubtitleTextEditorBackColor;
            subtitleEditor1.ShowStatusStrip = Program.Settings.SubtitleTextEditorShowStatusStrip;
        }
        void AddRecent(string fileName)
        {
            if (Program.Settings.RecentProject == null)
                Program.Settings.RecentProject = new System.Collections.Specialized.StringCollection();
            //remove the same one
            for (int i = 0; i < Program.Settings.RecentProject.Count; i++)
            {
                if (fileName == Program.Settings.RecentProject[i])
                {
                    Program.Settings.RecentProject.RemoveAt(i);
                    i = -1;
                }
            }
            //Insert
            Program.Settings.RecentProject.Insert(0, fileName);
            //Limit to 9
            if (Program.Settings.RecentProject.Count > 9)
            {
                Program.Settings.RecentProject.RemoveAt(6);
            }
        }
        void RefreshRecents()
        {
            if (Program.Settings.RecentProject == null)
                Program.Settings.RecentProject = new System.Collections.Specialized.StringCollection();
            recentToolStripMenuItem.DropDownItems.Clear();
            foreach (string Recc in Program.Settings.RecentProject)
            {
                ToolStripMenuItem IT = new ToolStripMenuItem();
                IT.Text = Path.GetFileNameWithoutExtension(Recc);
                IT.ToolTipText = Recc;
                recentToolStripMenuItem.DropDownItems.Add(IT);
            }
        }
        void AddFindRecent(string searchWord)
        {
            if (searchWord == "")
                return;
            if (!Program.Settings.RecentFindItems.Contains(searchWord))
                Program.Settings.RecentFindItems.Add(searchWord);
            //limit to 20 items
            if (Program.Settings.RecentFindItems.Count > 20)
                Program.Settings.RecentFindItems.RemoveAt(0);
            RefreshFindRecents();
        }
        void RefreshFindRecents()
        {
            ComboBox_search.Items.Clear();
            if (Program.Settings.RecentFindItems == null)
                Program.Settings.RecentFindItems = new System.Collections.Specialized.StringCollection();
            foreach (string item in Program.Settings.RecentFindItems)
                ComboBox_search.Items.Add(item);
            ComboBox_search.SelectedIndex = ComboBox_search.Items.Count - 1;
        }
        void LoadFormats()
        {
            if (Program.Settings.EnabledFormats == null)
                Program.Settings.EnabledFormats = new BooleansCollection();
            if (Program.Settings.EnabledFormats.Count != SubtitleFormats.Formats.Length)
            {
                Program.Settings.EnabledFormats = new BooleansCollection();
                foreach (SubtitlesFormat format in SubtitleFormats.Formats)
                {
                    Program.Settings.EnabledFormats.Add(true);
                }
            }
            for (int i = 0; i < Program.Settings.EnabledFormats.Count; i++)
            {
                SubtitleFormats.Formats[i].Enabled = Program.Settings.EnabledFormats[i];
            }
        }
        #endregion
        #region Layout Present
        void ApplyLayout(LayoutPresent layout)
        {
            if (layout == null)
                return;
            //containers
            splitContainer1.SplitterDistance = layout.SplitterDistance1;
            splitContainer2.SplitterDistance = layout.SplitterDistance2;
            splitContainer3.SplitterDistance = layout.SplitterDistance3;
            splitContainer4.SplitterDistance = layout.SplitterDistance4;
            splitContainer5.SplitterDistance = layout.SplitterDistance5;
            //menu items
            mediaToolStripMenuItem.Checked = layout.Tab_Media.Visible;
            subtitlesDataToolStripMenuItem.Checked = layout.Tab_SubtitlesData.Visible;
            subtitleTracksToolStripMenuItem.Checked = layout.Tab_SubtitleTracks.Visible;
            projectDescriptionToolStripMenuItem.Checked = layout.Tab_ProjectDescription.Visible;
            historyToolStripMenuItem.Checked = layout.Tab_History.Visible;
            logToolStripMenuItem.Checked = layout.Tab_Log.Visible;
            errorsToolStripMenuItem1.Checked = layout.Tab_Errors.Visible;
            timelineToolStripMenuItem.Checked = layout.Tab_TimeLine.Visible;
            propertiesToolStripMenuItem2.Checked = layout.Tab_Properties.Visible;
            //tabs
            SetTabParent(layout.Tab_Errors, tabPage_errors);
            SetTabParent(layout.Tab_History, tabPage_history);
            SetTabParent(layout.Tab_Log, tabPage_log);
            SetTabParent(layout.Tab_Media, tabPage_media);
            SetTabParent(layout.Tab_ProjectDescription, tabPage_ProjectDescription);
            SetTabParent(layout.Tab_Properties, tabPage_Properties);
            SetTabParent(layout.Tab_SubtitlesData, tabPage_subitltesData);
            SetTabParent(layout.Tab_SubtitleTracks, tabPage_subtitleTracks);
            SetTabParent(layout.Tab_TimeLine, tabPage_TimeLine);
            //toolbars
            toolStrip_edit.Visible = editToolStripMenuItem1.Checked = layout.Bar_Edit.Visible;
            SetBarParent(layout.Bar_Edit, toolStrip_edit);
            toolStrip_main.Visible = mainToolStripMenuItem.Checked = layout.Bar_Main.Visible;
            SetBarParent(layout.Bar_Main, toolStrip_main);
            toolStrip_marks.Visible = marksToolStripMenuItem1.Checked = layout.Bar_Marks.Visible;
            SetBarParent(layout.Bar_Marks, toolStrip_marks);
            CheckForCollapse();
        }

        void SaveLayout(string FilePath, LayoutPresent layout)
        {
            try
            {
                Stream str = new FileStream(FilePath, FileMode.Create, FileAccess.Write);
                XmlSerializer ser = new XmlSerializer(typeof(LayoutPresent));
                ser.Serialize(str, layout);
                str.Close();
            }
            catch
            { }
        }
        LayoutPresent LoadLayout(string FilePath)
        {
            try
            {
                LayoutPresent layout;
                Stream str = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
                XmlSerializer ser = new XmlSerializer(typeof(LayoutPresent));
                layout = (LayoutPresent)ser.Deserialize(str);
                str.Close();
                return layout;
            }
            catch
            { }
            return null;
        }
        void SetTabParent(TabProperties properties, TabPage page)
        {
            if (!properties.Visible) { page.Parent = null; }
            else
            {
                switch (properties.Parent)
                {
                    case TabParent.Down: page.Parent = tabControl_down_middle; break;
                    case TabParent.DownLeft: page.Parent = tabControl_down_left; break;
                    case TabParent.DownRight: page.Parent = tabControl_down_Right; break;
                    case TabParent.Top: page.Parent = tabControl_top_middle; break;
                    case TabParent.TopLeft: page.Parent = tabControl_top_left; break;
                    case TabParent.TopRight: page.Parent = tabControl_top_right; break;
                    case TabParent.None:
                    default: page.Parent = null; break;
                }
            }
        }
        void SetBarParent(ToolbarProperties properties, ToolStrip toolstrip)
        {
            switch (properties.Parent)
            {
                case ToolbarParent.Top: toolstrip.Parent = toolStripContainer1.TopToolStripPanel; break;
                case ToolbarParent.Right: toolstrip.Parent = toolStripContainer1.RightToolStripPanel; break;
                case ToolbarParent.Left: toolstrip.Parent = toolStripContainer1.LeftToolStripPanel; break;
                case ToolbarParent.Bottom: toolstrip.Parent = toolStripContainer1.BottomToolStripPanel; break;
            }
            toolstrip.Location = properties.Location;
        }

        void CheckForCollapse()
        {
            //clear view tab
            if (tabControl_down_left.TabPages.Count > 0)
                if (tabControl_down_left.TabPages[0].Text == "")
                    tabControl_down_left.TabPages.RemoveAt(0);
            if (tabControl_down_middle.TabPages.Count > 0)
                if (tabControl_down_middle.TabPages[0].Text == "")
                    tabControl_down_middle.TabPages.RemoveAt(0);
            if (tabControl_down_Right.TabPages.Count > 0)
                if (tabControl_down_Right.TabPages[0].Text == "")
                    tabControl_down_Right.TabPages.RemoveAt(0);
            if (tabControl_top_left.TabPages.Count > 0)
                if (tabControl_top_left.TabPages[0].Text == "")
                    tabControl_top_left.TabPages.RemoveAt(0);
            if (tabControl_top_middle.TabPages.Count > 0)
                if (tabControl_top_middle.TabPages[0].Text == "")
                    tabControl_top_middle.TabPages.RemoveAt(0);
            if (tabControl_top_right.TabPages.Count > 0)
                if (tabControl_top_right.TabPages[0].Text == "")
                    tabControl_top_right.TabPages.RemoveAt(0);

            //Right Area
            if (tabControl_top_right.TabCount == 0)
                splitContainer2.Panel1Collapsed = true;
            if (tabControl_down_Right.TabCount == 0)
                splitContainer2.Panel2Collapsed = true;
            if ((tabControl_top_right.TabCount == 0) & (tabControl_down_Right.TabCount == 0))
                splitContainer1.Panel2Collapsed = true;
            //Left area
            //TOP
            if (tabControl_top_middle.TabCount == 0)
                splitContainer4.Panel2Collapsed = true;
            if (tabControl_top_left.TabCount == 0)
                splitContainer4.Panel1Collapsed = true;
            if ((tabControl_top_middle.TabCount == 0) & (tabControl_top_left.TabCount == 0))
                splitContainer3.Panel1Collapsed = true;
            //DOWN
            if (tabControl_down_middle.TabCount == 0)
                splitContainer5.Panel2Collapsed = true;
            if (tabControl_down_left.TabCount == 0)
                splitContainer5.Panel1Collapsed = true;
            if ((tabControl_down_middle.TabCount == 0) & (tabControl_down_left.TabCount == 0))
                splitContainer3.Panel2Collapsed = true;
            //
            if ((tabControl_top_middle.TabCount == 0) & (tabControl_top_left.TabCount == 0) &
                (tabControl_down_middle.TabCount == 0) & (tabControl_down_left.TabCount == 0))
                splitContainer1.Panel1Collapsed = true;
        }
        void CheckDragAndDrop()
        {
            if (tabControl_down_left.TabCount == 0)
                tabControl_down_left.TabPages.Add("");
            if (tabControl_down_middle.TabCount == 0)
                tabControl_down_middle.TabPages.Add("");
            if (tabControl_down_Right.TabCount == 0)
                tabControl_down_Right.TabPages.Add("");
            if (tabControl_top_left.TabCount == 0)
                tabControl_top_left.TabPages.Add("");
            if (tabControl_top_middle.TabCount == 0)
                tabControl_top_middle.TabPages.Add("");
            if (tabControl_top_right.TabCount == 0)
                tabControl_top_right.TabPages.Add("");
        }
        void ExpandTabControls()
        {
            splitContainer1.Panel1Collapsed = false;
            splitContainer1.Panel2Collapsed = false;
            splitContainer2.Panel1Collapsed = false;
            splitContainer2.Panel2Collapsed = false;
            splitContainer3.Panel1Collapsed = false;
            splitContainer3.Panel2Collapsed = false;
            splitContainer4.Panel1Collapsed = false;
            splitContainer4.Panel2Collapsed = false;
            splitContainer5.Panel1Collapsed = false;
            splitContainer5.Panel2Collapsed = false;
        }
        void SaveTabParent(TabPage page, TabParent newParent)
        {
            if (page == tabPage_errors)
                Program.Settings.CurrentLayout.Tab_Errors.Parent = newParent;
            else if (page == tabPage_history)
                Program.Settings.CurrentLayout.Tab_History.Parent = newParent;
            else if (page == tabPage_log)
                Program.Settings.CurrentLayout.Tab_Log.Parent = newParent;
            else if (page == tabPage_media)
                Program.Settings.CurrentLayout.Tab_Media.Parent = newParent;
            else if (page == tabPage_ProjectDescription)
                Program.Settings.CurrentLayout.Tab_ProjectDescription.Parent = newParent;
            else if (page == tabPage_Properties)
                Program.Settings.CurrentLayout.Tab_Properties.Parent = newParent;
            else if (page == tabPage_subitltesData)
                Program.Settings.CurrentLayout.Tab_SubtitlesData.Parent = newParent;
            else if (page == tabPage_subtitleTracks)
                Program.Settings.CurrentLayout.Tab_SubtitleTracks.Parent = newParent;
            else if (page == tabPage_TimeLine)
                Program.Settings.CurrentLayout.Tab_TimeLine.Parent = newParent;
        }
        ToolbarParent GetBarParent(ToolStrip bar)
        {
            if (bar.Parent == toolStripContainer1.TopToolStripPanel)
                return ToolbarParent.Top;
            if (bar.Parent == toolStripContainer1.BottomToolStripPanel)
                return ToolbarParent.Bottom;
            if (bar.Parent == toolStripContainer1.LeftToolStripPanel)
                return ToolbarParent.Left;
            if (bar.Parent == toolStripContainer1.RightToolStripPanel)
                return ToolbarParent.Right;
            return ToolbarParent.Top;
        }
        #endregion
        #region History and log
        void AddHistory(string title)
        {
            //remove uneeded events
            int oldIndex = listBox_history.SelectedIndex;
            for (int i = oldIndex + 1; i < listBox_history.Items.Count; i++)
            {
                listBox_history.Items.RemoveAt(i);
                i = oldIndex;
            }
            //add event
            listBox_history.Items.Add(title);
            listBox_history.SelectedIndex = listBox_history.Items.Count - 1;
            //Save the event into project file
            ProjectManager historyManager = new ProjectManager();
            historyManager.Project = Program.ProjectManager.Project;
            historyManager.Save(Path.GetTempPath() + "\\ASM\\HIST" + listBox_history.SelectedIndex);
            //log
            WriteLog(DateTime.Now.ToShortDateString() + " " +
                DateTime.Now.ToShortTimeString() + ": " + title);
        }
        void WriteLog(string text)
        {
            richTextBox_Log.SelectionStart = richTextBox_Log.Text.Length;
            richTextBox_Log.SelectedText = text + "\n";
            richTextBox_Log.ScrollToCaret();
        }
        //open history at history listbox index
        void OpenHistory()
        {
            if (listBox_history.SelectedIndex == -1)
                return;
            //load the project as history
            string path = Program.ProjectManager.FilePath;
            Program.ProjectManager.Load(Path.GetTempPath() + "\\ASM\\HIST" + listBox_history.SelectedIndex, false);
            Program.ProjectManager.FilePath = path;
            //apply
            //save selection
            int selectedNodeIndex = 0;
            int selectedSubtitle = -1;
            if (treeView_subtitleTracks.SelectedNode != null)
                selectedNodeIndex = treeView_subtitleTracks.SelectedNode.Index;
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length == 1)
                selectedSubtitle = subtitlesDataEditor1.SelectedSubtitleItems[0].Index;
            //clear
            ClearAll();
            //refresh
            RefreshSubtitleTracks();
            //reselect track
            try { treeView_subtitleTracks.SelectedNode = treeView_subtitleTracks.Nodes[selectedNodeIndex]; }
            catch { }
            //reselect subtitle
            if (selectedSubtitle >= 0 & subtitlesDataEditor1.SubtitlesCount > selectedSubtitle)
                subtitlesDataEditor1.SelectItem(selectedSubtitle);
            //others
            richTextBox_description.Text = Program.ProjectManager.Project.Description;
            ChangeMedia(Program.ProjectManager.Project.MediaPath, false);
            timeLine1.TimeMarks = Program.ProjectManager.Project.TimeMarks;
            RefreshMarks();
        }
        void ClearHistory()
        {
            if (!Directory.Exists(Path.GetTempPath() + "\\ASM\\"))
            { Directory.CreateDirectory(Path.GetTempPath() + "\\ASM\\"); return; }
            for (int i = 0; i < Directory.GetFiles(Path.GetTempPath() + "\\ASM\\").Length; i++)
            {
                File.Delete(Directory.GetFiles(Path.GetTempPath() + "\\ASM\\")[0]);
                i = -1;
            }
            listBox_history.Items.Clear();
        }
        void EnableUndoRedo()
        {
            if (listBox_history.SelectedIndex == -1)
                undoToolStripMenuItem.Enabled = redoToolStripMenuItem.Enabled = false;

            undoToolStripMenuItem.Enabled = listBox_history.SelectedIndex > 0;

            redoToolStripMenuItem.Enabled = listBox_history.SelectedIndex < listBox_history.Items.Count - 1;
        }
        #endregion
        #region SAVE
        bool save;
        bool Save
        {
            get { return save; }
            set
            {
                save = value;
                if (value)
                    this.Text = Program.ProjectManager.Project.Name + "* - BNJ Subtitles Maker Professional";
                else
                    this.Text = Program.ProjectManager.Project.Name + " - BNJ Subtitles Maker Professional";
            }
        }
        bool AskToSave()
        {
            if (save)
            {
                MessageDialogResult result = MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_DoYouWantToSaveProjectFirst")
                    , Program.ResourceManager.GetString("MessageCaption_SaveProject"),
                    MessageDialogButtons.OkNoCancel, MessageDialogIcon.Save, false, Program.ResourceManager.GetString("Button_Save"),
                   Program.ResourceManager.GetString("Button_DontSave"), Program.ResourceManager.GetString("Button_Cancel"));
                switch (result)
                {
                    case MessageDialogResult.Ok:
                        if (SaveProject())
                            return true;
                        return false;
                    case MessageDialogResult.No: return true;
                    case MessageDialogResult.Cancel: return false;
                }
            }
            return true;
        }
        #endregion
        #region Status
        enum StatusType { Error, Good, Warning, None };
        void SetStatus(string status, StatusType statusType)
        {
            StatusLabel.Text = status;
            switch (statusType)
            {
                case StatusType.None: StatusLabel.ForeColor = Color.Black; break;
                case StatusType.Good: StatusLabel.ForeColor = Color.Green; break;
                case StatusType.Error: StatusLabel.ForeColor = Color.Red; break;
                case StatusType.Warning: StatusLabel.ForeColor = Color.Yellow; break;
            }
        }
        #endregion

        #region Refreshs and clears
        /*Clear everything in tabs*/
        void ClearAll()
        {
            treeView_subtitleTracks.Nodes.Clear();
            mediaPlayer1.ClearMedia();
            mediaPlayer1.HideSubtitle();
            subtitlesDataEditor1.Clear();
            subtitleEditor1.Clear();
            ComboBox_marks.Items.Clear();
            if (timeLine1.TimeMarks != null)
                timeLine1.TimeMarks.Clear();
            errorsChecker1.SubtitlesTrack = timeLine1.SubtitlesTrack = subtitleEditor1.SubtitlesTrack =
                subtitlesDataEditor1.SubtitlesTrack = null;
        }
        void ClearEditors()
        {
            subtitlesDataEditor1.Clear();
            subtitleEditor1.Clear();
            timeLine1.SubtitlesTrack = errorsChecker1.SubtitlesTrack =
                subtitleEditor1.SubtitlesTrack = subtitlesDataEditor1.SubtitlesTrack = null;
        }
        void RefreshSubtitleTracks()
        {
            selectedTrack = null;
            treeView_subtitleTracks.Nodes.Clear();
            foreach (SubtitlesTrack track in Program.ProjectManager.Project.SubtitleTracks)
            {
                TreeNode_SubtitlesTrack node = new TreeNode_SubtitlesTrack();
                node.SubtitlesTrack = track;
                treeView_subtitleTracks.Nodes.Add(node);
            }
        }
        #endregion
        #region Add items
        private void AddSubtitlesTracks(object sender, EventArgs e)
        {
            int i = 1;
            string name = Program.ResourceManager.GetString("SubtitlesTrack") + i;
            while (Program.ProjectManager.Project.IsSubtitlesTrackExist(name))
            {
                i++;
                name = Program.ResourceManager.GetString("SubtitlesTrack") + i;
            }
            SubtitlesTrack newTrack = new SubtitlesTrack(name);
            //template
            if (Program.Settings.NewSubtitlesTrackTemplate)
            {
                //add subtitle at begaining
                Subtitle sub = new Subtitle(1, 3, SubtitleText.FromString("Subtitles created by bnj Subtitles Maker Professional\nE-mail: hellas2012@yahoo.com"));
                newTrack.Subtitles.Add(sub);
                //add at the end
                if (mediaPlayer1.Duration > 0)
                {
                    sub = new Subtitle(mediaPlayer1.Duration - 4, mediaPlayer1.Duration - 2,
                        SubtitleText.FromString("Subtitles created by bnj Subtitles Maker Professional\nE-mail: hellas2012@yahoo.com"));
                    newTrack.Subtitles.Add(sub);
                }
                else//no media, add after 10 second from the first one
                {
                    sub = new Subtitle(11, 13,
                       SubtitleText.FromString("Subtitles created by bnj Subtitles Maker Professional\nE-mail: hellas2012@yahoo.com"));
                    newTrack.Subtitles.Add(sub);
                }
            }
            Program.ProjectManager.Project.SubtitleTracks.Add(newTrack);
            RefreshSubtitleTracks();
            treeView_subtitleTracks.Nodes[treeView_subtitleTracks.Nodes.Count - 1].BeginEdit();
            Save = true;
            AddHistory(Program.ResourceManager.GetString("History_SubtitlesTrackAdded") + " '" + name + "'");
        }
        private void AddSubtitle(object sender, EventArgs e)
        {
            if (selectedTrack == null || treeView_subtitleTracks.SelectedNode == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                    Program.ResourceManager.GetString("MessageCaption_AddSubtitle"));
                return;
            }
            Subtitle sub = new Subtitle(mediaPlayer1.CurrentPosition, mediaPlayer1.CurrentPosition +
                Program.Settings.NewSubtitleDuration, new SubtitleText());
            ListViewItem_Subtitle item = new ListViewItem_Subtitle();
            sub.Text.RighttoLeft = selectedTrack.RightToLeft;
            item.Subtitle = sub;
            Frm_SubtitleEdit subtitleEdit = new Frm_SubtitleEdit(item, selectedTrack, Program.Settings.SubtitleTextEditorBackColor);
            if (subtitleEdit.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                selectedTrack.Subtitles.Add(item.Subtitle);
                selectedTrack.Subtitles.Sort(new SubtitleComparer());
                subtitlesDataEditor1.RefreshSubtitles();
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SubtitleAddedAt") + " " +
                    TimeFormatConvertor.To_TimeSpan_Milli(sub.StartTime));
                ApplyTrackToControls();
            }
            pIndex = -1;
        }
        private void InsertSubtitle(object sender, EventArgs e)
        {
            if (selectedTrack == null || treeView_subtitleTracks.SelectedNode == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                   Program.ResourceManager.GetString("MessageCaption_InsertSubtitle"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length != 1)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitleFirst"),
                         Program.ResourceManager.GetString("MessageCaption_InsertSubtitle"));
                return;
            }
            Subtitle sub = new Subtitle(subtitlesDataEditor1.SelectedSubtitleItems[0].Subtitle.EndTime,
                subtitlesDataEditor1.SelectedSubtitleItems[0].Subtitle.EndTime + Program.Settings.NewSubtitleDuration, new SubtitleText());
            ListViewItem_Subtitle item = new ListViewItem_Subtitle();
            sub.Text.RighttoLeft = selectedTrack.RightToLeft;
            item.Subtitle = sub;
            Frm_SubtitleEdit subtitleEdit = new Frm_SubtitleEdit(item, selectedTrack, Program.Settings.SubtitleTextEditorBackColor);
            if (subtitleEdit.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                selectedTrack.Subtitles.Add(item.Subtitle);
                selectedTrack.Subtitles.Sort(new SubtitleComparer());
                subtitlesDataEditor1.RefreshSubtitles();
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SubtitleInsertedAt") +
                    " " + TimeFormatConvertor.To_TimeSpan_Milli(sub.StartTime));
                ApplyTrackToControls();
            }
            pIndex = -1;
        }
        #endregion
        #region Edit and delete
        ActiveMode activeMode = ActiveMode.SubtitlesTrack;
        void Delete()
        {
            switch (activeMode)
            {
                case ActiveMode.SubtitlesTrack:
                    if (treeView_subtitleTracks.SelectedNode == null)
                    {
                        SetStatus(Program.ResourceManager.GetString("Message_CantDeleteNoSubtitlesTrackSelected"), StatusType.Error);
                        return;
                    }

                    if (MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_AreYouSure"),
                       Program.ResourceManager.GetString("MessageCaption_DeleteSelectedSubtitlesTrack"),
                        MessageDialogButtons.OkNo, MessageDialogIcon.Question, false, Program.ResourceManager.GetString("Button_Yes"),
                       Program.ResourceManager.GetString("Button_No")) == MessageDialogResult.Ok)
                    {
                        Program.ProjectManager.Project.SubtitleTracks.Remove(((TreeNode_SubtitlesTrack)treeView_subtitleTracks.SelectedNode).SubtitlesTrack);
                        ClearEditors();
                        RefreshSubtitleTracks();
                        Save = true;
                        AddHistory(Program.ResourceManager.GetString("History_SubtitlesTrackRemove."));
                    }
                    break;
                case ActiveMode.Subtitles:
                    if (subtitlesDataEditor1.SelectedSubtitleItems.Length > 0)
                    {
                        if (MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_AreYouSureYouWantToDeleteSelected") +
                            subtitlesDataEditor1.SelectedSubtitleItems.Length + " " +
                            Program.ResourceManager.GetString("Message_Subtitles") + " ?", "Delete Subtitle(s)",
                           MessageDialogButtons.OkNo, MessageDialogIcon.Question, false, Program.ResourceManager.GetString("Button_Yes"),
                       Program.ResourceManager.GetString("Button_No")) == MessageDialogResult.Ok)
                        {
                            foreach (ListViewItem_Subtitle item in subtitlesDataEditor1.SelectedSubtitleItems)
                            {
                                selectedTrack.Subtitles.Remove(item.Subtitle);
                            }
                            subtitlesDataEditor1.RefreshSubtitles();
                            timeLine1.Invalidate();
                            Save = true;
                            AddHistory(Program.ResourceManager.GetString("History_SubtitlesDelete"));
                        }
                    }
                    else
                        SetStatus(Program.ResourceManager.GetString("Status_CantDeleteNoSubtitleSelected"), StatusType.Error);
                    break;
            }
            ApplyTrackToControls();
            EnableDisableEditButtons();
        }
        private void Cut(object sender, EventArgs e)
        {
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length > 0)
            {
                Copy(sender, e);
                //delete
                foreach (ListViewItem_Subtitle item in subtitlesDataEditor1.SelectedSubtitleItems)
                {
                    selectedTrack.Subtitles.Remove(item.Subtitle);
                }
                subtitlesDataEditor1.RefreshSubtitles();

                Save = true;
                AddHistory(copiedSubtitles.Count + " " + Program.ResourceManager.GetString("History_SubtitlesCut"));
            }
            else
            {
                SetStatus(Program.ResourceManager.GetString("Status_CantCopyOrCutNoSubtitleSelected"), StatusType.Error);
            }
            EnableDisableEditButtons();
            ApplyTrackToControls();
        }
        private void Copy(object sender, EventArgs e)
        {
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length > 0)
            {
                copiedSubtitles = new List<Subtitle>();
                foreach (ListViewItem_Subtitle item in subtitlesDataEditor1.SelectedSubtitleItems)
                {
                    copiedSubtitles.Add(item.Subtitle.Clone());
                }
                SetStatus(copiedSubtitles.Count + " " + Program.ResourceManager.GetString("Status_SubtitlesCopied"), StatusType.None);
            }
            else
            {
                SetStatus(Program.ResourceManager.GetString("Status_CantCopyOrCutNoSubtitleSelected"), StatusType.Error);
            }
            EnableDisableEditButtons();
        }
        private void Paste(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                SetStatus(Program.ResourceManager.GetString("Status_CantPasteNoSubtitesTrackSelected"), StatusType.Error);
                return;
            }
            int SubIndex = -1;
            int Pastedd = 0;
            int Removed = 0;
            double mooo = 0;
            bool ShouldRemove = false;
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length > 0)
            {
                MessageDialogResult result = MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_WhereToPasteSubtitles"),
                        Program.ResourceManager.GetString("MessageCaption_PasteSubtitles"),
                        MessageDialogButtons.OkNoCancel, MessageDialogIcon.Question, false, Program.ResourceManager.GetString("Button_Insert"),
                      Program.ResourceManager.GetString("Button_OnTime"), Program.ResourceManager.GetString("Button_Cancel")
                          );
                if (result == MessageDialogResult.Cancel)
                { return; }
                if (result == MessageDialogResult.Ok)
                {
                    ListViewItem_Subtitle suuu = subtitlesDataEditor1.SelectedSubtitleItems[subtitlesDataEditor1.SelectedSubtitleItems.Length - 1];
                    mooo = suuu.Subtitle.EndTime + 0.001;
                }
                else
                {
                    mooo = mediaPlayer1.CurrentPosition;
                }
                ShouldRemove = true;
            }
            else
            {
                mooo = mediaPlayer1.CurrentPosition;
                ShouldRemove = false;
            }
            double Moved = mooo - copiedSubtitles[0].StartTime;
            foreach (Subtitle sub in copiedSubtitles)
            {
                sub.StartTime += Moved;
                sub.EndTime += Moved;
                #region Remove subtitles in our way
                if (ShouldRemove)
                {
                    for (int i = 0; i < selectedTrack.Subtitles.Count; i++)
                    {
                        //copied subtitle is in another
                        if (sub.StartTime >= selectedTrack.Subtitles[i].StartTime & sub.EndTime <= selectedTrack.Subtitles[i].EndTime)
                        {
                            selectedTrack.Subtitles.RemoveAt(i);
                            i = -1;
                            Removed++;
                        }
                        else
                        {
                            //copied subtitle is over another
                            if (sub.StartTime <= selectedTrack.Subtitles[i].StartTime & sub.EndTime >= selectedTrack.Subtitles[i].EndTime)
                            {
                                selectedTrack.Subtitles.RemoveAt(i);
                                i = -1;
                                Removed++;
                            }
                            else
                            {
                                //copied subtitle start time is in another
                                if (sub.StartTime <= selectedTrack.Subtitles[i].EndTime & sub.StartTime >= selectedTrack.Subtitles[i].StartTime)
                                {
                                    selectedTrack.Subtitles.RemoveAt(i);
                                    i = -1;
                                    Removed++;
                                }
                                else
                                {
                                    //copied subtitle end time is in another
                                    if (sub.EndTime >= selectedTrack.Subtitles[i].StartTime & sub.EndTime <= selectedTrack.Subtitles[i].EndTime)
                                    {
                                        selectedTrack.Subtitles.RemoveAt(i);
                                        i = -1;
                                        Removed++;
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion
                //Set index
                SubIndex = -1;
                for (int i = 0; i < selectedTrack.Subtitles.Count; i++)
                {
                    if (sub.StartTime >= selectedTrack.Subtitles[i].StartTime)
                    { SubIndex = i; }
                    else
                    { break; }
                }
                SubIndex++;
                //Add the subtitle
                Subtitle newSubtitle = sub.Clone();

                selectedTrack.Subtitles.Insert(SubIndex, newSubtitle);
                Pastedd++;
            }
            subtitlesDataEditor1.RefreshSubtitles();
            timeLine1.Invalidate();
            Save = true;
            AddHistory(Pastedd.ToString() + " " + Program.ResourceManager.GetString("History_SubtitlesPasted") + ", " + Removed.ToString()
                + " " + Program.ResourceManager.GetString("History_SubtitlesReplaced"));
            StatusLabel.Text = Pastedd.ToString() + " " + Program.ResourceManager.GetString("History_SubtitlesPasted") +
                ", " + Removed.ToString() + " " + Program.ResourceManager.GetString("History_SubtitlesReplaced");
            EnableDisableEditButtons();
            ApplyTrackToControls();
        }
        private void SubtitleProperties(object sender, EventArgs e)
        {
            //edit ...
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length == 1)
            {
                mediaPlayer1.Stop();
                Frm_SubtitleEdit subtitleEdit = new Frm_SubtitleEdit(subtitlesDataEditor1.SelectedSubtitleItems[0], selectedTrack, Program.Settings.SubtitleTextEditorBackColor);
                if (Program.Settings.EditMoreThanOneSubtitleUsingEditorWindow)
                {
                    subtitleEdit.TopMost = true;
                    subtitleEdit.OkButtonPressed += new EventHandler(subtitleEdit_OkButtonPressed);
                    subtitleEdit.Show();
                }
                else
                {
                    if (subtitleEdit.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                    {
                        Save = true;
                        AddHistory(Program.ResourceManager.GetString("History_SubtitlePropertiesChanged"));
                    }
                }
                pIndex = -1;
            }
            ApplyTrackToControls();
        }

        void subtitleEdit_OkButtonPressed(object sender, EventArgs e)
        {
            Save = true;
            AddHistory(Program.ResourceManager.GetString("History_SubtitlePropertiesChanged"));
        }
        //call this to auto enable disable edit buttons (copy, cut and delete)
        void EnableDisableEditButtons()
        {
            copyToolStripMenuItem.Enabled = cutToolStripMenuItem.Enabled = subtitlesDataEditor1.SelectedSubtitleItems.Length > 0;
            pasteToolStripMenuItem.Enabled = copiedSubtitles.Count > 0;
        }
        #endregion
        #region Timers
        private void Timer_Player_Tick(object sender, EventArgs e)
        {
            //update player
            mediaPlayer1.UpdateTimer();
            if (timeLine1.MediaDuration != mediaPlayer1.Duration)
            {
                timeLine1.MediaDuration = mediaPlayer1.Duration;
            }
            //if (mediaPlayer1.IsPlaying)
            {
                timeLine1.UpdateTime(mediaPlayer1.CurrentPosition);
                timeLine1.IsPlaying = mediaPlayer1.IsPlaying;
            }
            //view subtitle
            ViewSubtitle();
            //Show add subtitle info
            if (IsAddingSubtitle)
            {
                addSubtitle.EndTime = mediaPlayer1.CurrentPosition;
                string info = Program.ResourceManager.GetString("Status_AddingNewSubtitle") + ":\n" +
                Program.ResourceManager.GetString("Status_Start") + ": " + TimeFormatConvertor.To_TimeSpan_Milli(addSubtitle.StartTime) + "\n"
              + Program.ResourceManager.GetString("Status_End") + ": " + TimeFormatConvertor.To_TimeSpan_Milli(addSubtitle.EndTime) + "\n"
              + Program.ResourceManager.GetString("Status_Duration") + ": " + addSubtitle.Duration.ToString("F3") + "\n";
                mediaPlayer1.SetInfoTextOfAddButton(info);
            }
        }
        #endregion
        #region Subtitle View and add enablation
        void ViewSubtitle()
        {
            sIndex = FindSubtitleIndex(mediaPlayer1.CurrentPosition);
            if (sIndex >= 0)
            {
                if (sIndex != pIndex)
                {
                    viewedSubtitle = selectedTrack.Subtitles[sIndex];
                    mediaPlayer1.ShowSubtitle(selectedTrack.Subtitles[sIndex].Text);
                    subtitlesDataEditor1.SetViewedSubtitle(sIndex, true);
                }
                pIndex = sIndex;
                //enable buttons
                if (selectedTrack != null & mediaPlayer1.IsPlaying)
                {
                    mediaPlayer1.AddButtonEnabled = false;
                    mediaPlayer1.EndSetButtonEnabled = true;
                }
                else
                {
                    mediaPlayer1.AddButtonEnabled = false;
                    mediaPlayer1.EndSetButtonEnabled = false;
                }
            }
            else
            {
                viewedSubtitle = null;
                subtitlesDataEditor1.SetViewedSubtitle(pIndex, false);
                mediaPlayer1.HideSubtitle();
                //enable buttons
                if (selectedTrack != null & mediaPlayer1.IsPlaying)
                {
                    mediaPlayer1.AddButtonEnabled = true;
                    mediaPlayer1.EndSetButtonEnabled = false;
                }
                else
                {
                    mediaPlayer1.AddButtonEnabled = false;
                    mediaPlayer1.EndSetButtonEnabled = false;
                }
            }
        }
        int FindSubtitleIndex(double time)
        {
            if (selectedTrack != null)
            {
                int i = 0;
                foreach (Subtitle sub in selectedTrack.Subtitles)
                {
                    if (sub.StartTime <= time & sub.EndTime >= time)
                        return i;

                    i++;
                }
            }
            return -1;
        }
        #endregion

        private void Frm_Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!AskToSave())
            {
                e.Cancel = true;
                return;
            }
            this.WindowState = FormWindowState.Normal;
            ClearHistory();
            if (needToSaveSettings)
                SaveSettings();
            needToSaveSettings = false;
        }
        private void Frm_Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sav = new SaveFileDialog();
            sav.Filter = "Layout (*.ly)|*.ly";
            sav.Title = Program.ResourceManager.GetString("Title_SaveLayout");
            if (sav.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                SaveLayout(sav.FileName, Program.Settings.CurrentLayout);
            }
        }
        private void loadAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "Layout (*.ly)|*.ly";
            op.Title = Program.ResourceManager.GetString("Title_LoadLayout");
            if (op.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                ApplyLayout(LoadLayout(op.FileName));
            }
        }
        private void resetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ApplyLayout(new LayoutPresent());
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewProject();
        }
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveProject();
        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Title = Program.ResourceManager.GetString("Title_OpenProject");
            op.Filter = Filters.ASMP;
            if (op.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                LoadProject(op.FileName);
        }
        private void saveAsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sav = new SaveFileDialog();
            sav.Title = Program.ResourceManager.GetString("Title_SaveProject");
            sav.Filter = Filters.ASMP;
            if (sav.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                if (Program.ProjectManager.Save(sav.FileName))
                    Save = false;
            }
        }
        private void richTextBox_description_TextChanged(object sender, EventArgs e)
        {
            Program.ProjectManager.Project.Description = richTextBox_description.Text;
        }
        private void richTextBox_Log_TextChanged(object sender, EventArgs e)
        {
            Program.ProjectManager.Project.Log = richTextBox_Log.Text;
        }
        private void clearLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_AreYouSure"),
                Program.ResourceManager.GetString("MessageCaption_ClearLog"),
                MessageDialogButtons.OkNo, MessageDialogIcon.Question) == MessageDialogResult.Ok)
            {
                richTextBox_Log.Text = "";
                Save = true;
            }
        }
        private void saveLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sav = new SaveFileDialog();
            sav.Title = Program.ResourceManager.GetString("Title_SaveLog");
            sav.Filter = "Text file (*.txt)|*txt";
            if (sav.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                File.WriteAllLines(Program.ProjectManager.Project.Log, richTextBox_Log.Lines);
            }
        }
        /*Subtitle Tracks drag and drop*/
        private void treeView_subtitleTracks_ItemDrag(object sender, ItemDragEventArgs e)
        {
            DoDragDrop(e.Item, DragDropEffects.Move);
        }
        private void treeView_subtitleTracks_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.AllowedEffect;
        }
        private void treeView_subtitleTracks_DragOver(object sender, DragEventArgs e)
        {
            // Retrieve the client coordinates of the mouse position.

            Point targetPoint = treeView_subtitleTracks.PointToClient(new Point(e.X, e.Y));

            // Select the node at the mouse position.

            treeView_subtitleTracks.SelectedNode = treeView_subtitleTracks.GetNodeAt(targetPoint);
        }
        private void treeView_subtitleTracks_DragDrop(object sender, DragEventArgs e)
        {
            // Retrieve the client coordinates of the drop location.

            Point targetPoint = treeView_subtitleTracks.PointToClient(new Point(e.X, e.Y));

            // Retrieve the node at the drop location.

            TreeNode targetNode = treeView_subtitleTracks.GetNodeAt(targetPoint);

            // Retrieve the node that was dragged.

            TreeNode_SubtitlesTrack draggedNode = (TreeNode_SubtitlesTrack)e.Data.GetData(typeof(TreeNode_SubtitlesTrack));
            if (draggedNode == null)
                return;
            if (targetNode == null)
                return;
            // Make new track node instead of drag the same one
            TreeNode_SubtitlesTrack NewTrackNode = new TreeNode_SubtitlesTrack();
            NewTrackNode.SubtitlesTrack = draggedNode.SubtitlesTrack;

            //delete
            Program.ProjectManager.Project.SubtitleTracks.Remove(draggedNode.SubtitlesTrack);
            //insert
            Program.ProjectManager.Project.SubtitleTracks.Insert(targetNode.Index, NewTrackNode.SubtitlesTrack);
            RefreshSubtitleTracks();

            treeView_subtitleTracks.SelectedNode = treeView_subtitleTracks.GetNodeAt(targetPoint);
            Save = true;
        }
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Delete();
        }
        //after select track
        private void treeView_subtitleTracks_MouseClick(object sender, MouseEventArgs e)
        {
            activeMode = ActiveMode.SubtitlesTrack; pIndex = -1;
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (treeView_subtitleTracks.GetNodeAt(e.Location) != null)
                {
                    treeView_subtitleTracks.SelectedNode = treeView_subtitleTracks.GetNodeAt(e.Location);
                    selectedTrack = errorsChecker1.SubtitlesTrack = subtitleEditor1.SubtitlesTrack = timeLine1.SubtitlesTrack =
                        subtitlesDataEditor1.SubtitlesTrack = ((TreeNode_SubtitlesTrack)treeView_subtitleTracks.SelectedNode).SubtitlesTrack;
                    toolStripButton_selectedTrackRTL.Checked = selectedTrack.RightToLeft;
                    toolStrip_subtitleTracks.Enabled = true;
                    subtitleTracksStatus.Text = selectedTrack.Subtitles.Count + " sub.";
                }
                else
                {
                    treeView_subtitleTracks.SelectedNode = treeView_subtitleTracks.GetNodeAt(e.Location);
                    selectedTrack = errorsChecker1.SubtitlesTrack = subtitleEditor1.SubtitlesTrack = timeLine1.SubtitlesTrack = subtitlesDataEditor1.SubtitlesTrack = null;
                    toolStripButton_selectedTrackRTL.Checked = false;
                    toolStrip_subtitleTracks.Enabled = false;
                    subtitleTracksStatus.Text = Program.ResourceManager.GetString("Status_SelectTrack");
                }
            }
        }
        private void treeView_subtitleTracks_AfterSelect(object sender, TreeViewEventArgs e)
        {
            activeMode = ActiveMode.SubtitlesTrack; pIndex = -1;
            if (treeView_subtitleTracks.SelectedNode != null)
            {
                selectedTrack = errorsChecker1.SubtitlesTrack = subtitleEditor1.SubtitlesTrack = timeLine1.SubtitlesTrack =
                    subtitlesDataEditor1.SubtitlesTrack = ((TreeNode_SubtitlesTrack)treeView_subtitleTracks.SelectedNode).SubtitlesTrack;
                toolStripButton_selectedTrackRTL.Checked = selectedTrack.RightToLeft;
                toolStrip_subtitleTracks.Enabled = true;
                subtitleTracksStatus.Text = selectedTrack.Subtitles.Count + " sub.";
            }
            else
            {
                selectedTrack = errorsChecker1.SubtitlesTrack = subtitleEditor1.SubtitlesTrack = timeLine1.SubtitlesTrack = subtitlesDataEditor1.SubtitlesTrack = null;
                toolStripButton_selectedTrackRTL.Checked = false;
                toolStrip_subtitleTracks.Enabled = false;
                subtitleTracksStatus.Text = Program.ResourceManager.GetString("Status_SelectTrack");
            }
        }
        private void renameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView_subtitleTracks.SelectedNode == null)
                return;
            treeView_subtitleTracks.SelectedNode.BeginEdit();
        }
        private void duplicateToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (treeView_subtitleTracks.SelectedNode == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_SelectSubtitlesTrackFirst"),
                  Program.ResourceManager.GetString("MessageCaption_Error"), MessageDialogButtons.Ok, MessageDialogIcon.Error);
                return;
            }
            SubtitlesTrack desiredSubtitlesTrack = ((TreeNode_SubtitlesTrack)treeView_subtitleTracks.SelectedNode).SubtitlesTrack;
            SubtitlesTrack newTrack = new SubtitlesTrack();
            //Name
            int i = 0;
            while (Program.ProjectManager.Project.IsSubtitlesTrackExist(desiredSubtitlesTrack.Name + "_Duplication" + (i + 1).ToString()))
            {
                i++;
            }
            newTrack.Name = desiredSubtitlesTrack.Name + "_Duplication" + (i + 1).ToString();
            //subtitles
            foreach (Subtitle sub in desiredSubtitlesTrack.Subtitles)
            {
                Subtitle newSubtitle = new Subtitle();
                newSubtitle.EndTime = sub.EndTime;
                newSubtitle.StartTime = sub.StartTime;
                newSubtitle.Text = SubtitleTextWrapper.Clone(sub.Text);
                newTrack.Subtitles.Add(newSubtitle);
            }
            //add track
            Program.ProjectManager.Project.SubtitleTracks.Add(newTrack);
            RefreshSubtitleTracks();
            Save = true;
            AddHistory(Program.ResourceManager.GetString("History_SubtitlesTrackDuplicate") + " '" + desiredSubtitlesTrack.Name + "'");
        }
        private void changeMediaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Title = Program.ResourceManager.GetString("Title_ChangeMedia");
            op.Filter = Filters.Media;
            if (op.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                ChangeMedia(op.FileName);
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_MediaChanged"));
            }
        }
        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            subtitlesDataEditor1.SelectAll();
        }
        private void selectNoneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            subtitlesDataEditor1.SelectNone();
        }
        private void selectSameTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            subtitlesDataEditor1.SelectWithSameText();
        }
        private void subtitlesDataEditor1_SubtitlesSelected(object sender, EventArgs e)
        {
            subtitleEditor1.SelectedItems = subtitlesDataEditor1.SelectedSubtitleItems;

            List<Subtitle> selectedSubtitles = new List<Subtitle>();
            foreach (ListViewItem_Subtitle item in subtitleEditor1.SelectedItems)
                selectedSubtitles.Add(item.Subtitle);
            timeLine1.SelectedSubtitles = selectedSubtitles;

            EnableDisableEditButtons();
        }
        private void subtitleEditor1_SaveRequest(object sender, EventArgs e)
        {
            AddHistory(Program.ResourceManager.GetString("History_SubtitlePropertiesChanged"));
            Save = true;
        }
        private void listBox_history_SelectedIndexChanged(object sender, EventArgs e)
        {
            EnableUndoRedo();
        }
        private void undoToolStripMenuItem_EnabledChanged(object sender, EventArgs e)
        {
            toolStripButton7.Enabled = undoToolStripMenuItem.Enabled;
        }
        private void redoToolStripMenuItem_EnabledChanged(object sender, EventArgs e)
        {
            toolStripButton8.Enabled = redoToolStripMenuItem.Enabled;
        }
        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                listBox_history.SelectedIndex--;
                OpenHistory();
            }
            catch { }
        }
        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                listBox_history.SelectedIndex++;
                OpenHistory();
            }
            catch { }
        }
        private void listBox_history_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                OpenHistory();
            }
        }

        private void mediaPlayer1_SaveRequest(object sender, EventArgs e)
        {
            try
            {
                subtitlesDataEditor1.SubtitleItems[sIndex].RefreshText();
                subtitlesDataEditor1.SubtitleItems[sIndex].ChangeTimingView();
            }
            catch { }
            AddHistory(Program.ResourceManager.GetString("History_SubtitlePropertiesChanged"));
            Save = true;
        }
        private void subtitlesDataEditor1_TimeChangeRequest(object sender, Controls.TimeChangeArgs e)
        {
            if (Program.Settings.MediaSeekOnSelect & !mediaPlayer1.IsPlaying)
                mediaPlayer1.CurrentPosition = e.NewTime;
        }
        private void subtitlesDataEditor1_SubtitleEditRequest(object sender, EventArgs e)
        {
            //this rised only if one subtitle selected via mouse double click
            if (mediaPlayer1.IsPlaying)
            {
                if (subtitlesDataEditor1.SelectedSubtitleItems.Length == 1)
                    mediaPlayer1.CurrentPosition = subtitlesDataEditor1.SelectedSubtitleItems[0].Subtitle.StartTime;
            }
            else
            {
                SubtitleProperties(this, e);
            }
        }
        private void mediaPlayer1_AddButtonHoldPress(object sender, EventArgs e)
        {
            if (mediaPlayer1.IsPlaying)
            {
                addSubtitle = new Subtitle(mediaPlayer1.CurrentPosition, mediaPlayer1.CurrentPosition + 0.01,
                    SubtitleText.FromString(Program.ResourceManager.GetString("Title_NewSubtitle"),
                    Program.Settings.NewSubtitleFont, Program.Settings.NewSubtitleColor));
                IsAddingSubtitle = true;
            }
        }
        private void mediaPlayer1_AddButtonHoldRelease(object sender, EventArgs e)
        {
            if (IsAddingSubtitle)
            {
                IsAddingSubtitle = false;
                if (Program.Settings.PauseAfterAddUsingQuickMode)
                    mediaPlayer1.Pause();
                mediaPlayer1.SetInfoTextOfAddButton("");
                Subtitle sub = new Subtitle(addSubtitle.StartTime, addSubtitle.EndTime,
                    SubtitleText.FromString(Program.ResourceManager.GetString("Title_NewSubtitle"),
                    Program.Settings.NewSubtitleFont, Program.Settings.NewSubtitleColor));
                ListViewItem_Subtitle item = new ListViewItem_Subtitle();
                sub.Text.RighttoLeft = selectedTrack.RightToLeft;
                item.Subtitle = sub;
                if (Program.Settings.EditSubtitleAfterAddUsingQuickMode)
                {
                    Frm_SubtitleEdit subtitleEdit = new Frm_SubtitleEdit(item, selectedTrack, Program.Settings.SubtitleTextEditorBackColor);
                    if (subtitleEdit.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                    {
                        selectedTrack.Subtitles.Add(item.Subtitle);
                        selectedTrack.Subtitles.Sort(new SubtitleComparer());
                        subtitlesDataEditor1.RefreshSubtitles();
                        Save = true;
                        AddHistory(Program.ResourceManager.GetString("History_SubtitleAddedAt") + " " +
                            TimeFormatConvertor.To_TimeSpan_Milli(sub.StartTime));
                        //seek
                        if (Program.Settings.PauseAfterAddUsingQuickMode)
                            mediaPlayer1.CurrentPosition = sub.StartTime;
                    }
                }
                else
                {
                    //add without edit
                    selectedTrack.Subtitles.Add(item.Subtitle);
                    selectedTrack.Subtitles.Sort(new SubtitleComparer());
                    subtitlesDataEditor1.RefreshSubtitles();
                    Save = true;
                    AddHistory(Program.ResourceManager.GetString("History_SubtitleAddedAt") + " " +
                        TimeFormatConvertor.To_TimeSpan_Milli(sub.StartTime));
                    //seek
                    if (Program.Settings.PauseAfterAddUsingQuickMode)
                        mediaPlayer1.CurrentPosition = sub.StartTime;
                }
                addSubtitle = null;
                pIndex = -1;
            }
        }
        private void renameProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EnterNameForm dial = new EnterNameForm(Program.ResourceManager.GetString("Title_EnterProjectName"),
                Program.ProjectManager.Project.Name, true, false);
            if (dial.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                Program.ProjectManager.Project.Name = dial.EnteredName;
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_ProjectNameChange"));
            }
        }
        private void mediaPlayer1_EndSetButtonPressed(object sender, EventArgs e)
        {
            if (viewedSubtitle != null)
            {
                viewedSubtitle.EndTime = mediaPlayer1.CurrentPosition;
                subtitlesDataEditor1.SubtitleItems[sIndex].RefreshText();
                subtitlesDataEditor1.SubtitleItems[sIndex].ChangeTimingView();

                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SetCurrentTimeAsEndTimeForSubtitleAt") + " " +
                    TimeFormatConvertor.To_TimeSpan_Milli(viewedSubtitle.StartTime));
            }
        }
        private void cutToolStripMenuItem_EnabledChanged(object sender, EventArgs e)
        {
            cutToolStripMenuItem1.Enabled = toolStripButton4.Enabled = cutToolStripMenuItem.Enabled;
        }
        private void copyToolStripMenuItem_EnabledChanged(object sender, EventArgs e)
        {
            copyToolStripMenuItem1.Enabled = toolStripButton5.Enabled = copyToolStripMenuItem.Enabled;
        }
        private void pasteToolStripMenuItem_EnabledChanged(object sender, EventArgs e)
        {
            pasteToolStripMenuItem1.Enabled = toolStripButton6.Enabled = pasteToolStripMenuItem.Enabled;
        }
        private void subtitlesDataEditor1_Enter(object sender, EventArgs e)
        {
            activeMode = ActiveMode.Subtitles;
        }
        private void recentToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int index = 0;
            for (int i = 0; i < recentToolStripMenuItem.DropDownItems.Count; i++)
            {
                if (e.ClickedItem == recentToolStripMenuItem.DropDownItems[i])
                { index = i; break; }
            }
            if (File.Exists(Program.Settings.RecentProject[index]))
            {
                LoadProject(Program.Settings.RecentProject[index]);
            }
            else
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_ThisProjectIsMissing"),
                    Program.ResourceManager.GetString("MessageCaption_CantFindProject"));
                RefreshRecents();
            }
        }
        private void timeLine1_TimeChangeRequest(object sender, Controls.TimeChangeArgs e)
        {
            mediaPlayer1.CurrentPosition = e.NewTime;
            //refresh view
            pIndex = -1;
        }
        private void timeLine1_SubtitlesSelected(object sender, EventArgs e)
        {
            activeMode = ActiveMode.Subtitles;
            subtitlesDataEditor1.SelectNone();
            foreach (Subtitle sub in timeLine1.SelectedSubtitles)
                subtitlesDataEditor1.SelectItem(sub);

            subtitleEditor1.SelectedItems = subtitlesDataEditor1.SelectedSubtitleItems;

            EnableDisableEditButtons();
        }
        private void timeLine1_SubtitleDoubleClick(object sender, EventArgs e)
        {
            //this rised only if one subtitle selected via mouse double click
            if (mediaPlayer1.IsPlaying)
            {
                if (timeLine1.SelectedSubtitles.Count == 1)
                    mediaPlayer1.CurrentPosition = timeLine1.SelectedSubtitles[0].StartTime;
            }
            else
            {
                SubtitleProperties(this, e);
            }
        }
        private void timeLine1_SubtitlePropertiesChanged(object sender, EventArgs e)
        {
            //refresh controls
            subtitlesDataEditor1.RefreshSubtitles();
            //set save and history
            AddHistory(Program.ResourceManager.GetString("History_SubtitlePropertiesChanged"));
            Save = true;
        }
        private void playPauseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mediaPlayer1.TogglePlayPause();
        }
        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mediaPlayer1.Stop();
        }
        private void upToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                mediaPlayer1.Volume += 2;
            }
            catch { }
        }
        private void downToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                mediaPlayer1.Volume -= 2;
            }
            catch { }
        }
        private void muteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mediaPlayer1.ToggleMute();
        }
        private void mediaPlayer1_MuteToggle(object sender, EventArgs e)
        {
            muteToolStripMenuItem.Checked = mediaPlayer1.Mute;
        }
        private void jumpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_JumpTime frm = new Frm_JumpTime();
            frm.StartPosition = FormStartPosition.Manual;
            frm.Time = mediaPlayer1.CurrentPosition;
            frm.Location = Cursor.Position;
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                mediaPlayer1.CurrentPosition = frm.Time;
        }
        private void nextFrameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mediaPlayer1.NextFrame();
        }
        private void previousFrameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mediaPlayer1.PreviousFrame();
        }
        private void translateToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                 Program.ResourceManager.GetString("MessageCaption_Translate"));
                return;
            }
            Frm_Translate translate = new Frm_Translate(selectedTrack);
            if (translate.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                if (translate.IsCreateNew)
                {
                    //just add the translated track
                    string newName = selectedTrack.Name + "_Translation";
                    int i = 0;
                    while (Program.ProjectManager.Project.IsSubtitlesTrackExist(newName))
                    {
                        newName = selectedTrack.Name + "_Translation" + i.ToString();
                        i++;
                    }
                    translate.TranslationSubtitlesTrack.Name = newName;
                    Program.ProjectManager.Project.SubtitleTracks.Add(translate.TranslationSubtitlesTrack);
                    RefreshSubtitleTracks();
                }
                else//replace original, add the translated subtitles to the same track
                {
                    selectedTrack.Subtitles.Clear();
                    selectedTrack.Subtitles = translate.TranslationSubtitlesTrack.Subtitles;
                    subtitlesDataEditor1.RefreshSubtitles();
                    timeLine1.Refresh();
                }
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SubtitlesTrackTranslated"));
            }
        }
        private void synchronizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                  Program.ResourceManager.GetString("MessageCaption_Synchronise"));
                return;
            }
            Frm_Synchronize frm = new Frm_Synchronize();
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                if (frm.TargetFramerate == frm.BaseFramerate)
                    return;
                if (frm.TargetFramerate == 0)
                {
                    MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_TheTargetFramerateMustBeNotZero"),
                          Program.ResourceManager.GetString("MessageCaption_Synchronise"));
                    return;
                }
                if (frm.BaseFramerate == 0)
                {
                    MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_TheBaseFramerateMustBeNotZero"),
                        Program.ResourceManager.GetString("MessageCaption_Synchronise"));
                    return;
                }
                //synch
                ProgressBar1.Visible = true;
                ProgressBar1.Maximum = 100;
                SetStatus(Program.ResourceManager.GetString("Status_Synchronising"), StatusType.None);
                int i = 0;
                foreach (Subtitle sub in selectedTrack.Subtitles)
                {
                    sub.StartTime = (sub.StartTime * frm.BaseFramerate) / frm.TargetFramerate;
                    sub.EndTime = (sub.EndTime * frm.BaseFramerate) / frm.TargetFramerate;
                    ProgressBar1.Value = (i * 100) / selectedTrack.Subtitles.Count;
                    i++;
                }
                ProgressBar1.Visible = false;
                SetStatus(Program.ResourceManager.GetString("Status_Done"), StatusType.Good);
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SubtitlesTrackSynchronised"));
                subtitlesDataEditor1.RefreshSubtitles();
            }
        }
        private void exportGTTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                  Program.ResourceManager.GetString("MessageCaption_ExportGT"));
                return;
            }
            SaveFileDialog sav = new SaveFileDialog();
            sav.Filter = "GT-Text (*txt)|*.txt";
            if (sav.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                List<string> LINES = new List<string>();
                foreach (Subtitle sub in selectedTrack.Subtitles)
                {
                    for (int i = 0; i < sub.Text.TextLines.Count; i++)
                        LINES.Add(sub.Text.TextLines[i].ToString());
                    LINES.Add("==========");
                }
                File.WriteAllLines(sav.FileName, LINES.ToArray(), Encoding.Unicode);
            }
        }
        private void importGTTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                  Program.ResourceManager.GetString("MessageCaption_ImportGT"));
                return;
            }
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "GT-Text (*txt)|*.txt";
            if (op.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                string[] LINES = File.ReadAllLines(op.FileName);
                string text = "";
                int index = 0;
                try
                {
                    foreach (string line in LINES)
                    {
                        if (line != "==========")
                            text += line + "\n";
                        else
                        {
                            text = text.Substring(0, text.Length - 1);
                            selectedTrack.Subtitles[index].Text = SubtitleText.FromString(text, Program.Settings.NewSubtitleFont, Program.Settings.NewSubtitleColor);
                            index++;
                            text = "";
                        }
                    }
                }
                catch
                {
                    MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_FaildToImportGTText"),
                   Program.ResourceManager.GetString("MessageCaption_FaildToImportGTText"), MessageDialogButtons.Ok, MessageDialogIcon.Error);
                }
                subtitlesDataEditor1.RefreshSubtitles();
                Save = true;
                AddHistory("GT-Text import.");
            }
        }
        private void shiftTimeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                   Program.ResourceManager.GetString("MessageCaption_ShiftTime"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length <= 0)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_YouMustSelectSubtitleToShift"),
                     Program.ResourceManager.GetString("MessageCaption_ShiftTime"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length != 1 && subtitlesDataEditor1.SelectedSubtitleItems.Length != selectedTrack.Subtitles.Count)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_YouMustSelectSubtitleToShift"),
                     Program.ResourceManager.GetString("MessageCaption_ShiftTime"));
                return;
            }
            double Pre = 0;
            double nxt = 0;
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length == 1)//Shift selected
            {
                if (subtitlesDataEditor1.SelectedSubtitleItems[0].Index == 0)
                {
                    ListViewItem_Subtitle suuu = subtitlesDataEditor1.SelectedSubtitleItems[0];
                    Pre = 0;
                    if (subtitlesDataEditor1.SubtitleItems.Length > 1)
                    {
                        ListViewItem_Subtitle suuu1 = subtitlesDataEditor1.SubtitleItems[suuu.Index + 1];
                        nxt = suuu1.Subtitle.StartTime - 0.001;
                    }
                    else
                    {
                        if (mediaPlayer1.Duration > 0)
                        {
                            nxt = mediaPlayer1.Duration - 0.001;
                        }
                        else
                        {
                            MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_TheMediaDurationMustBeLargerThanZero"),
                            Program.ResourceManager.GetString("MessageCaption_ShiftTime"), MessageDialogButtons.Ok, MessageDialogIcon.Error); return;
                        }
                    }
                    Frm_Shift shh = new Frm_Shift(suuu.Subtitle.StartTime, suuu.Subtitle.EndTime, suuu.Subtitle.Duration, Pre, nxt, Program.Settings.ShiftTime);
                    shh.ShowDialog(this);
                    if (shh.OK)
                    {
                        double moved = shh.Shifted;
                        shh.Close();
                        suuu.Subtitle.StartTime += moved;
                        suuu.Subtitle.EndTime += moved;
                        selectedTrack.Subtitles[suuu.Index] = suuu.Subtitle;
                        Save = true;
                        subtitlesDataEditor1.RefreshSubtitles();
                        AddHistory(Program.ResourceManager.GetString("History_SubtitleShift"));
                    }
                }
                else
                {
                    ListViewItem_Subtitle suuu = subtitlesDataEditor1.SelectedSubtitleItems[0];
                    ListViewItem_Subtitle suuuPrev = subtitlesDataEditor1.SubtitleItems[suuu.Index - 1];
                    Pre = suuuPrev.Subtitle.EndTime + 0.001;
                    if (suuu.Index != subtitlesDataEditor1.SubtitleItems.Length - 1)
                    {
                        ListViewItem_Subtitle suuuNxt = subtitlesDataEditor1.SubtitleItems[suuu.Index + 1];
                        nxt = suuuNxt.Subtitle.StartTime - 0.001;
                    }
                    else
                    {
                        if (mediaPlayer1.Duration > 0)
                        {
                            nxt = mediaPlayer1.Duration - 0.001;
                        }
                        else
                        {
                            MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_TheMediaDurationMustBeLargerThanZero"),
                              Program.ResourceManager.GetString("MessageCaption_ShiftTime"), MessageDialogButtons.Ok, MessageDialogIcon.Error); return;
                        }
                    }
                    Frm_Shift shh = new Frm_Shift(suuu.Subtitle.StartTime, suuu.Subtitle.EndTime, suuu.Subtitle.Duration, Pre, nxt, Program.Settings.ShiftTime);
                    shh.ShowDialog(this);
                    if (shh.OK)
                    {
                        double moved = shh.Shifted;
                        shh.Close();
                        suuu.Subtitle.StartTime += moved;
                        suuu.Subtitle.EndTime += moved;
                        selectedTrack.Subtitles[suuu.Index] = suuu.Subtitle;
                        Save = true;
                        subtitlesDataEditor1.RefreshSubtitles();
                        AddHistory(Program.ResourceManager.GetString("History_SubtitleShift"));
                    }
                }
            }
            else//Shift all
            {
                if (mediaPlayer1.Duration == 0)
                {
                    MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_TheMediaDurationMustBeLargerThanZero"),
                                Program.ResourceManager.GetString("MessageCaption_ShiftTime"), MessageDialogButtons.Ok, MessageDialogIcon.Error); return;
                }

                if (IsAddingSubtitle)
                    return;
                double Pre1 = 0;
                double nxt1 = mediaPlayer1.Duration - 0.001;
                ListViewItem_Subtitle startsub = subtitlesDataEditor1.SubtitleItems[0];
                ListViewItem_Subtitle endsub = subtitlesDataEditor1.SubtitleItems[subtitlesDataEditor1.SubtitleItems.Length - 1];
                Frm_Shift shh = new Frm_Shift(startsub.Subtitle.StartTime,
                    endsub.Subtitle.EndTime, (double)(endsub.Subtitle.EndTime - startsub.Subtitle.StartTime), Pre1, nxt1, Program.Settings.ShiftTime);
                shh.ShowDialog(this);
                if (shh.OK)
                {
                    double moved = shh.Shifted;
                    for (int i = 0; i < subtitlesDataEditor1.SubtitleItems.Length; i++)
                    {
                        ListViewItem_Subtitle sub = subtitlesDataEditor1.SubtitleItems[i];
                        sub.Subtitle.StartTime += moved;
                        sub.Subtitle.EndTime += moved;
                    }
                    Save = true;
                    subtitlesDataEditor1.RefreshSubtitles();
                    AddHistory(Program.ResourceManager.GetString("History_SubtitlesTrackShift"));
                }
            }
        }
        private void fillSpaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                  Program.ResourceManager.GetString("MessageCaption_FillSpace"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length != 1)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_YouMustSelectOneSubtitleToFillSpaceFor"),
                    Program.ResourceManager.GetString("MessageCaption_FillSpace"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems[0].Index == 0)
            {
                ListViewItem_Subtitle suuu = subtitlesDataEditor1.SelectedSubtitleItems[0];
                suuu.Subtitle.StartTime = 0;
                if (subtitlesDataEditor1.SubtitleItems.Length > 1)
                {
                    ListViewItem_Subtitle suuu1 = subtitlesDataEditor1.SubtitleItems[suuu.Index + 1];
                    suuu.Subtitle.EndTime = suuu1.Subtitle.StartTime - 0.001;
                }
                else
                {
                    if (mediaPlayer1.Duration > 0)
                    {
                        suuu.Subtitle.EndTime = mediaPlayer1.Duration - 0.001;
                    }
                    else
                    {
                        MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_TheMediaDurationMustBeLargerThanZero"),
                        Program.ResourceManager.GetString("MessageCaption_FillSpace"), MessageDialogButtons.Ok, MessageDialogIcon.Error); return;
                    }
                }
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SubtitleFillSpace"));
                subtitlesDataEditor1.RefreshSubtitles();
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems[0].Index == subtitlesDataEditor1.SubtitleItems.Length - 1)
            {
                ListViewItem_Subtitle suuu = subtitlesDataEditor1.SelectedSubtitleItems[0];
                if (subtitlesDataEditor1.SubtitleItems.Length > 1)
                {
                    ListViewItem_Subtitle suuuPre = subtitlesDataEditor1.SubtitleItems[suuu.Index - 1];
                    suuu.Subtitle.StartTime = suuuPre.Subtitle.EndTime + 0.001;
                }
                else
                {
                    suuu.Subtitle.StartTime = 0;
                }
                if (mediaPlayer1.Duration > 0)
                {
                    suuu.Subtitle.EndTime = mediaPlayer1.Duration - 0.001;
                }
                else
                {
                    MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_TheMediaDurationMustBeLargerThanZero"),
                    Program.ResourceManager.GetString("MessageCaption_FillSpace"), MessageDialogButtons.Ok, MessageDialogIcon.Error); return;
                }

                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SubtitleFillSpace"));
                subtitlesDataEditor1.RefreshSubtitles();
                return;
            }
            else
            {
                ListViewItem_Subtitle suuu = subtitlesDataEditor1.SelectedSubtitleItems[0];
                ListViewItem_Subtitle suuuPre = subtitlesDataEditor1.SubtitleItems[suuu.Index - 1];
                ListViewItem_Subtitle suuunxt = subtitlesDataEditor1.SubtitleItems[suuu.Index + 1];
                suuu.Subtitle.StartTime = suuuPre.Subtitle.EndTime + 0.001;
                suuu.Subtitle.EndTime = suuunxt.Subtitle.StartTime - 0.001;
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SubtitleFillSpace"));
                subtitlesDataEditor1.RefreshSubtitles();
            }
        }
        private void stretchToNextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                    Program.ResourceManager.GetString("MessageCaption_StretchToNext"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length != 1)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_YouMustSelectOneSubtitleToStretch"),
                    Program.ResourceManager.GetString("MessageCaption_StretchToNext"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems[0].Index != 0 &
       subtitlesDataEditor1.SelectedSubtitleItems[0].Index != subtitlesDataEditor1.SubtitleItems.Length - 1)
            {
                int index = subtitlesDataEditor1.SelectedSubtitleItems[0].Index + 1;
                subtitlesDataEditor1.SelectedSubtitleItems[0].Subtitle.EndTime =
                subtitlesDataEditor1.SubtitleItems[index].Subtitle.StartTime - 0.001;
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SubtitleStretchToNext"));
                subtitlesDataEditor1.RefreshSubtitles();
            }
            else if (subtitlesDataEditor1.SelectedSubtitleItems[0].Index == 0 & subtitlesDataEditor1.SubtitleItems.Length > 1)
            {
                int index = subtitlesDataEditor1.SelectedSubtitleItems[0].Index + 1;
                subtitlesDataEditor1.SelectedSubtitleItems[0].Subtitle.EndTime =
                subtitlesDataEditor1.SubtitleItems[index].Subtitle.StartTime - 0.001;
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SubtitleStretchToNext"));
                subtitlesDataEditor1.RefreshSubtitles();
            }
        }
        private void stretchToPreviousToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                  Program.ResourceManager.GetString("MessageCaption_StretchToPrevious"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length != 1)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_YouMustSelectOneSubtitleToStretch"),
                   Program.ResourceManager.GetString("MessageCaption_StretchToPrevious"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems[0].Index != 0 &
            subtitlesDataEditor1.SubtitleItems.Length > 1)
            {
                int index = subtitlesDataEditor1.SelectedSubtitleItems[0].Index - 1;
                subtitlesDataEditor1.SelectedSubtitleItems[0].Subtitle.StartTime =
                subtitlesDataEditor1.SubtitleItems[index].Subtitle.EndTime + 0.001;
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SubtitleStretchToPrevious"));
                subtitlesDataEditor1.RefreshSubtitles();
            }
        }
        private void splitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                   Program.ResourceManager.GetString("MessageCaption_Split"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length != 1)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_YouMustSelectOneSubtitleToSplit"),
                    Program.ResourceManager.GetString("MessageCaption_Split"));
                return;
            }
            Frm_Split frm = new Frm_Split(subtitlesDataEditor1.SelectedSubtitleItems[0].Subtitle);
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                Subtitle newSubtitle = new Subtitle(frm.SplitTime, subtitlesDataEditor1.SelectedSubtitleItems[0].Subtitle.EndTime,
               SubtitleTextWrapper.Clone(subtitlesDataEditor1.SelectedSubtitleItems[0].Subtitle.Text));
                //update original subtitle
                subtitlesDataEditor1.SelectedSubtitleItems[0].Subtitle.EndTime = frm.SplitTime - 0.001;
                //Insert new one
                int index = subtitlesDataEditor1.SelectedSubtitleItems[0].Index + 1;
                selectedTrack.Subtitles.Add(newSubtitle);
                selectedTrack.Subtitles.Sort(new SubtitleComparer());
                subtitlesDataEditor1.RefreshSubtitles();
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SubtitleSplited"));
            }
        }
        private void timeLine1_JumpIntoTimeRequest(object sender, EventArgs e)
        {
            jumpToolStripMenuItem_Click(sender, e);
        }

        private void addMarkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_AddTimeMark frm = new Frm_AddTimeMark(mediaPlayer1.CurrentPosition);
            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = Cursor.Position;
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                Program.ProjectManager.Project.TimeMarks.Add(new TimeMark(frm.MarkName, frm.MarkTime));
                Program.ProjectManager.Project.TimeMarks.Sort(new TimeMarkComparer());
                RefreshMarks();
                SelectMarkAtTime(mediaPlayer1.CurrentPosition);
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_MarkAddedAt") + " " + frm.MarkTime);
            }
        }
        private void deleteSelectedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ComboBox_marks.SelectedIndex >= 0)
            {
                if (MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_AreYouSure"),
                   Program.ResourceManager.GetString("MessageCaption_RemoveMark"), MessageDialogButtons.OkNo, MessageDialogIcon.Question, false, "&Yes", "&No") == MessageDialogResult.Ok)
                {
                    Program.ProjectManager.Project.TimeMarks.RemoveAt(ComboBox_marks.SelectedIndex);
                    Program.ProjectManager.Project.TimeMarks.Sort(new TimeMarkComparer());
                    RefreshMarks();
                    SelectMarkAtTime(mediaPlayer1.CurrentPosition);
                    Save = true;
                    AddHistory(Program.ResourceManager.GetString("History_MarkRemoved"));
                }
            }
        }
        private void jumpIntoSelectedTimeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ComboBox_marks.SelectedIndex >= 0)
            {
                mediaPlayer1.CurrentPosition = Program.ProjectManager.Project.TimeMarks[ComboBox_marks.SelectedIndex].Time;
            }
        }
        private void ComboBox_marks_DropDownClosed(object sender, EventArgs e)
        {
            jumpIntoSelectedTimeToolStripMenuItem_Click(sender, e);
        }
        private void timeLine1_JumpIntoMarkRequest(object sender, EventArgs e)
        {
            jumpIntoSelectedTimeToolStripMenuItem_Click(sender, e);
        }
        private void timeLine1_AddMarkRequest(object sender, EventArgs e)
        {
            addMarkToolStripMenuItem_Click(sender, e);
        }
        private void timeLine1_RemoveMarkRequest(object sender, EventArgs e)
        {
            deleteSelectedToolStripMenuItem_Click(sender, e);
        }
        private void timeLine1_MarkEdit(object sender, MarkEditArgs e)
        {
            Save = true;
            AddHistory(Program.ResourceManager.GetString("History_MarkEdit"));
            RefreshMarks();
            SelectMarkAtTime(Program.ProjectManager.Project.TimeMarks[e.MarkIndex].Time);
        }
        private void timeLine1_SelectMarkRequest(object sender, MarkEditArgs e)
        {
            SelectMarkAtTime(Program.ProjectManager.Project.TimeMarks[e.MarkIndex].Time);
        }
        /*Find And Replace*/
        private void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddFindRecent(ComboBox_search.Text);
            //if we already have a find window, ignore this call
            foreach (Form frm in Application.OpenForms)
            {
                if (frm.GetType() == typeof(Frm_FindAndReplace))
                {
                    ((Frm_FindAndReplace)frm).FindWord = ComboBox_search.Text;
                    ((Frm_FindAndReplace)frm).IsFindAndReplace = false;
                    frm.Select();
                    return;
                }
            }
            Frm_FindAndReplace findAndReplaceForm = new Frm_FindAndReplace(false, ComboBox_search.Text);
            findAndReplaceForm.FindNextRequest += new EventHandler<FindAndReplaceArgs>(findAndReplaceForm_FindNextRequest);
            findAndReplaceForm.Show();
        }
        private void findAndReplaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddFindRecent(ComboBox_search.Text);
            //if we already have a find window, ignore this call
            foreach (Form frm in Application.OpenForms)
            {
                if (frm.GetType() == typeof(Frm_FindAndReplace))
                {
                    ((Frm_FindAndReplace)frm).FindWord = ComboBox_search.Text;
                    ((Frm_FindAndReplace)frm).IsFindAndReplace = true;
                    frm.Select();
                    return;
                }
            }
            Frm_FindAndReplace findAndReplaceForm = new Frm_FindAndReplace(true, ComboBox_search.Text);
            findAndReplaceForm.FindNextRequest += new EventHandler<FindAndReplaceArgs>(findAndReplaceForm_FindNextRequest);
            findAndReplaceForm.Show();
        }
        void findAndReplaceForm_FindNextRequest(object sender, FindAndReplaceArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                   Program.ResourceManager.GetString("MessageCaption_FindSubtitle"));
                return;
            }
            bool stopSearch = false;
            bool found = false;
            if (e.All)
                subtitlesDataEditor1.SelectNone();
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length > 0)
            {
                SearchIndex = subtitlesDataEditor1.SelectedSubtitleItems[0].Index + 1;
                if (SearchIndex == selectedTrack.Subtitles.Count)
                    SearchIndex = 0;
            }
            for (int i = SearchIndex; i < selectedTrack.Subtitles.Count; i++)
            {
                if (stopSearch)
                    break;
                if (selectedTrack.Subtitles[i].Text.ToString().Length >= e.FindWhat.Length)
                {
                    if (!e.MatchWholeWord)
                    {
                        for (int SearchWordIndex = 0; SearchWordIndex <
                                (selectedTrack.Subtitles[i].Text.ToString().Length - e.FindWhat.Length) + 1; SearchWordIndex++)
                        {
                            string Ser = selectedTrack.Subtitles[i].Text.ToString().Substring(SearchWordIndex, e.FindWhat.Length);
                            if (!e.MatchCase)
                            {
                                if (Ser.ToLower() == e.FindWhat.ToLower())
                                {
                                    //this is it
                                    if (!e.All)
                                        subtitlesDataEditor1.SelectNone();
                                    subtitlesDataEditor1.SelectItem(i);
                                    subtitlesDataEditor1.Select();
                                    found = true;
                                    if (!e.All)
                                        stopSearch = true;
                                    //replace ?
                                    if (e.ReplaceWith != "")
                                    {
                                        //search for the word in the subtile text lines
                                        foreach (SubtitleLine line in subtitlesDataEditor1.SubtitleItems[i].Subtitle.Text.TextLines)
                                        {
                                            if (line.ToString().Length >= e.FindWhat.Length)
                                            {
                                                for (int c = 0; c < line.Chars.Count - e.FindWhat.Length; c++)
                                                {
                                                    Ser = line.ToString().Substring(c, e.FindWhat.Length);
                                                    if (Ser.ToLower() == e.FindWhat.ToLower())
                                                    {
                                                        //we found the index of the first char of the find word
                                                        line.Chars.RemoveRange(c, e.FindWhat.Length);
                                                        //Add the replace word
                                                        for (int o = 0; o < e.ReplaceWith.Length; o++)
                                                        {
                                                            line.Chars.Insert(c, new SubtitleChar(e.ReplaceWith.ToCharArray()[o],
                                                                Program.Settings.NewSubtitleFont, Program.Settings.NewSubtitleColor));
                                                            c++;
                                                        }
                                                        //break !!
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    subtitlesDataEditor1.SubtitleItems[i].RefreshText();
                                    subtitlesDataEditor1.SubtitleItems[i].ChangeTimingView();
                                    break;
                                }
                            }
                            else
                            {
                                if (Ser == e.FindWhat)
                                {
                                    //this is it
                                    if (!e.All)
                                        subtitlesDataEditor1.SelectNone();
                                    subtitlesDataEditor1.SelectItem(i);
                                    subtitlesDataEditor1.Select();
                                    found = true;
                                    if (!e.All)
                                        stopSearch = true;
                                    if (e.ReplaceWith != "")
                                    {
                                        //search for the word in the subtile text lines
                                        foreach (SubtitleLine line in subtitlesDataEditor1.SubtitleItems[i].Subtitle.Text.TextLines)
                                        {
                                            if (line.ToString().Length >= e.FindWhat.Length)
                                            {
                                                for (int c = 0; c < line.Chars.Count - e.FindWhat.Length; c++)
                                                {
                                                    Ser = line.ToString().Substring(c, e.FindWhat.Length);
                                                    if (Ser == e.FindWhat)
                                                    {
                                                        //we found the index of the first char of the find word
                                                        line.Chars.RemoveRange(c, e.FindWhat.Length);
                                                        //Add the replace word
                                                        for (int o = 0; o < e.ReplaceWith.Length; o++)
                                                        {
                                                            line.Chars.Insert(c, new SubtitleChar(e.ReplaceWith.ToCharArray()[o],
                                                                Program.Settings.NewSubtitleFont, Program.Settings.NewSubtitleColor));
                                                            c++;
                                                        }
                                                        //break !!
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    subtitlesDataEditor1.SubtitleItems[i].RefreshText();
                                    subtitlesDataEditor1.SubtitleItems[i].ChangeTimingView();
                                    break;
                                }
                            }
                        }
                    }
                    else//match word search
                    {
                        string[] wordToFind = e.FindWhat.Split(new char[] { ' ' });
                        string[] subtitleTexts = selectedTrack.Subtitles[i].Text.ToString().Split(new char[] { ' ' });
                        int matched = 0;
                        for (int j = 0; j < subtitleTexts.Length; j++)
                        {
                            for (int h = 0; h < wordToFind.Length; h++)
                            {
                                if (!e.MatchCase)
                                {
                                    if (subtitleTexts[j].ToLower() == wordToFind[h].ToLower())
                                    {
                                        matched++;
                                        wordToFind[h] = "";
                                        break;
                                    }
                                }
                                else
                                {
                                    if (subtitleTexts[j] == wordToFind[h])
                                    {
                                        wordToFind[h] = "";
                                        matched++;
                                        break;
                                    }
                                }
                            }
                        }
                        if (matched >= wordToFind.Length)
                        {
                            //this is it
                            if (!e.All)
                                subtitlesDataEditor1.SelectNone();
                            subtitlesDataEditor1.SelectItem(i);
                            subtitlesDataEditor1.Select();
                            found = true;
                            if (!e.All)
                                stopSearch = true;
                            if (e.ReplaceWith != "")
                            {
                                //search for the word in the subtile text lines
                                foreach (SubtitleLine line in subtitlesDataEditor1.SubtitleItems[i].Subtitle.Text.TextLines)
                                {
                                    if (line.ToString().Length >= e.FindWhat.Length)
                                    {
                                        for (int c = 0; c < line.Chars.Count - e.FindWhat.Length; c++)
                                        {
                                            string Ser = line.ToString().Substring(c, e.FindWhat.Length);
                                            if (e.MatchCase)
                                            {
                                                if (Ser == e.FindWhat)
                                                {
                                                    //we found the index of the first char of the find word
                                                    line.Chars.RemoveRange(c, e.FindWhat.Length);
                                                    //Add the replace word
                                                    for (int o = 0; o < e.ReplaceWith.Length; o++)
                                                    {
                                                        line.Chars.Insert(c, new SubtitleChar(e.ReplaceWith.ToCharArray()[o],
                                                            Program.Settings.NewSubtitleFont, Program.Settings.NewSubtitleColor));
                                                        c++;
                                                    }
                                                    //break !!
                                                    break;
                                                }
                                            }
                                            else
                                            {
                                                if (Ser.ToLower() == e.FindWhat.ToLower())
                                                {
                                                    //we found the index of the first char of the find word
                                                    line.Chars.RemoveRange(c, e.FindWhat.Length);
                                                    //Add the replace word
                                                    for (int o = 0; o < e.ReplaceWith.Length; o++)
                                                    {
                                                        line.Chars.Insert(c, new SubtitleChar(e.ReplaceWith.ToCharArray()[o],
                                                            Program.Settings.NewSubtitleFont, Program.Settings.NewSubtitleColor));
                                                        c++;
                                                    }
                                                    //break !!
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            subtitlesDataEditor1.SubtitleItems[i].RefreshText();
                            subtitlesDataEditor1.SubtitleItems[i].ChangeTimingView();
                            break;
                        }
                    }
                }
            }
            //Reset index if nothing found
            if (!found)
            {
                SearchIndex = 0;
                subtitlesDataEditor1.SelectNone();
            }
            //Status
            if (e.All)
            {
                if (e.ReplaceWith != "")
                {
                    SetStatus(Program.ResourceManager.GetString("Status_ReplaceAllDone") + ", " +
                        subtitlesDataEditor1.SelectedSubtitleItems.Length + " " +
                        Program.ResourceManager.GetString("Status_subtitlesFound"), StatusType.None);
                }
                else
                {
                    SetStatus(Program.ResourceManager.GetString("Status_FindAllDone") + ", " + subtitlesDataEditor1.SelectedSubtitleItems.Length + " " +
                        Program.ResourceManager.GetString("Status_subtitlesFound"), StatusType.None);
                }
            }
            else
            {
                if (e.ReplaceWith != "")
                {
                    SetStatus(Program.ResourceManager.GetString("Status_ReplaceDone"), StatusType.None);
                }
                else
                {
                    SetStatus(Program.ResourceManager.GetString("Status_FindDone"), StatusType.None);
                }
            }
        }
        private void findNextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //we should have a find window to request a next find
            foreach (Form frm in Application.OpenForms)
            {
                if (frm.GetType() == typeof(Frm_FindAndReplace))
                {
                    ((Frm_FindAndReplace)frm).FindNext();
                    return;
                }
            }
        }
        private void clonetimingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                   Program.ResourceManager.GetString("MessageCaption_Clone"));
                return;
            }
            if (Program.ProjectManager.Project.SubtitleTracks.Count < 2)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_TheProjectMustHave2TracksOrMore"),
                    Program.ResourceManager.GetString("MessageCaption_Clone"));
                return;
            }
            Frm_ChooseSubtitlesTrack frm = new Frm_ChooseSubtitlesTrack(selectedTrack, Program.ProjectManager.Project);
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                if (frm.SelectedSubtitlesTrack.Subtitles.Count != selectedTrack.Subtitles.Count)
                {
                    MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_SelectedTracksHaveNotTheSameCountOfSubtitles"),
                          Program.ResourceManager.GetString("MessageCaption_Clone"));
                    return;
                }
                //clone
                ProgressBar1.Maximum = 100;
                ProgressBar1.Visible = true;
                SetStatus(Program.ResourceManager.GetString("Status_CloningTimings"), StatusType.None);
                for (int i = 0; i < selectedTrack.Subtitles.Count; i++)
                {
                    frm.SelectedSubtitlesTrack.Subtitles[i].StartTime = selectedTrack.Subtitles[i].StartTime;
                    frm.SelectedSubtitlesTrack.Subtitles[i].EndTime = selectedTrack.Subtitles[i].EndTime;
                    ProgressBar1.Value = (i * 100) / selectedTrack.Subtitles.Count;
                }
                ProgressBar1.Visible = false;
                SetStatus(Program.ResourceManager.GetString("Status_Done"), StatusType.Good);
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SubtitlesTrackTimingsClone"));
            }
        }
        private void cloneTextsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
               Program.ResourceManager.GetString("MessageCaption_Clone"));
                return;
            }
            if (Program.ProjectManager.Project.SubtitleTracks.Count < 2)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_TheProjectMustHave2TracksOrMore"),
                    Program.ResourceManager.GetString("MessageCaption_Clone"));
                return;
            }
            Frm_ChooseSubtitlesTrack frm = new Frm_ChooseSubtitlesTrack(selectedTrack, Program.ProjectManager.Project);
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                if (frm.SelectedSubtitlesTrack.Subtitles.Count != selectedTrack.Subtitles.Count)
                {
                    MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_SelectedTracksHaveNotTheSameCountOfSubtitles"),
                              Program.ResourceManager.GetString("MessageCaption_Clone"));
                    return;
                }
                //clone
                ProgressBar1.Maximum = 100;
                ProgressBar1.Visible = true;
                SetStatus(Program.ResourceManager.GetString("Status_CloningTexts"), StatusType.None);
                for (int i = 0; i < selectedTrack.Subtitles.Count; i++)
                {
                    frm.SelectedSubtitlesTrack.Subtitles[i].Text = SubtitleTextWrapper.Clone(selectedTrack.Subtitles[i].Text);
                    ProgressBar1.Value = (i * 100) / selectedTrack.Subtitles.Count;
                }
                ProgressBar1.Visible = false;
                SetStatus(Program.ResourceManager.GetString("Status_Done"), StatusType.Good);
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SubtitlesTrackTextsClone"));
            }
        }
        private void errorsChecker1_CheckProgress(object sender, ProgressArgs e)
        {
            ProgressBar1.Value = e.PrecentageCompleted;
            SetStatus(e.Status, StatusType.None);
        }
        private void errorsChecker1_CheckFinished(object sender, EventArgs e)
        {
            ProgressBar1.Visible = false;
        }
        private void errorsChecker1_CheckStarted(object sender, EventArgs e)
        {
            ProgressBar1.Visible = true;
        }
        private void errorsChecker1_SelectSubtitlesRequest(object sender, SubtitlesSelectArgs e)
        {
            subtitlesDataEditor1.SelectNone();
            foreach (int index in e.Indices)
                subtitlesDataEditor1.SelectItem(index);
        }
        private void checkForErrorsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!Program.Settings.CurrentLayout.Tab_Errors.Visible)
            {
                Program.Settings.CurrentLayout.Tab_Errors.Visible = errorsToolStripMenuItem1.Checked = true;
                SetTabParent(Program.Settings.CurrentLayout.Tab_Errors, tabPage_errors);
                CheckForCollapse();
            }
            ((TabControl)tabPage_errors.Parent).SelectedTab = tabPage_errors;
            errorsChecker1.CheckForErrors();
        }
        private void selectAllToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            errorsChecker1.SelectAllItems();
        }
        private void jumpIntoSelectedErrorSubtitlesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            errorsChecker1.SelectErrorSubtitles();
        }
        private void iD3TagsSynchronizedLyricsToolStripMenuItem_EnabledChanged(object sender, EventArgs e)
        {
            toolStripButton_id3.Enabled = iD3TagsSynchronizedLyricsToolStripMenuItem.Enabled;
        }
        private void iD3TagsSynchronizedLyricsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mediaPlayer1.ClearMedia();

            Frm_ID3v2 frm = new Frm_ID3v2(Program.ProjectManager.Project, Program.Settings.ID3_DropUncompatibleFrames,
                Program.Settings.ID3_WriteFooter, byte.Parse(Program.Settings.ID3_SaveVersion.Substring(2, 1)));

            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                RefreshSubtitleTracks();
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_ID3Managed"));
            }
            //reload media
            ChangeMedia(Program.ProjectManager.Project.MediaPath, false);
        }

        private void mediaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExpandTabControls();
            Program.Settings.CurrentLayout.Tab_Media.Visible = mediaToolStripMenuItem.Checked;
            SetTabParent(Program.Settings.CurrentLayout.Tab_Media, tabPage_media);
            CheckForCollapse();
        }
        private void subtitlesDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExpandTabControls();
            Program.Settings.CurrentLayout.Tab_SubtitlesData.Visible = subtitlesDataToolStripMenuItem.Checked;
            SetTabParent(Program.Settings.CurrentLayout.Tab_SubtitlesData, tabPage_subitltesData);
            CheckForCollapse();
        }
        private void subtitleTracksToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExpandTabControls();
            Program.Settings.CurrentLayout.Tab_SubtitleTracks.Visible = subtitleTracksToolStripMenuItem.Checked;
            SetTabParent(Program.Settings.CurrentLayout.Tab_SubtitleTracks, tabPage_subtitleTracks);
            CheckForCollapse();
        }
        private void projectDescriptionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExpandTabControls();
            Program.Settings.CurrentLayout.Tab_ProjectDescription.Visible = projectDescriptionToolStripMenuItem.Checked;
            SetTabParent(Program.Settings.CurrentLayout.Tab_ProjectDescription, tabPage_ProjectDescription);
            CheckForCollapse();
        }
        private void historyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExpandTabControls();
            Program.Settings.CurrentLayout.Tab_History.Visible = historyToolStripMenuItem.Checked;
            SetTabParent(Program.Settings.CurrentLayout.Tab_History, tabPage_history);
            CheckForCollapse();
        }
        private void logToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExpandTabControls();
            Program.Settings.CurrentLayout.Tab_Log.Visible = logToolStripMenuItem.Checked;
            SetTabParent(Program.Settings.CurrentLayout.Tab_Log, tabPage_log);
            CheckForCollapse();
        }
        private void errorsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ExpandTabControls();
            Program.Settings.CurrentLayout.Tab_Errors.Visible = errorsToolStripMenuItem1.Checked;
            SetTabParent(Program.Settings.CurrentLayout.Tab_Errors, tabPage_errors);
            CheckForCollapse();
        }
        private void timelineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExpandTabControls();
            Program.Settings.CurrentLayout.Tab_TimeLine.Visible = timelineToolStripMenuItem.Checked;
            SetTabParent(Program.Settings.CurrentLayout.Tab_TimeLine, tabPage_TimeLine);
            CheckForCollapse();
        }
        private void propertiesToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            ExpandTabControls();
            Program.Settings.CurrentLayout.Tab_Properties.Visible = propertiesToolStripMenuItem2.Checked;
            SetTabParent(Program.Settings.CurrentLayout.Tab_Properties, tabPage_Properties);
            CheckForCollapse();
        }
        /*Tabs drag and drop*/
        private void tabControl_top_left_MouseUp(object sender, MouseEventArgs e)
        {
            canDragDrop = false;
        }
        private void tabControl_top_left_MouseDown(object sender, MouseEventArgs e)
        {
            downPoint = e.Location;
            canDragDrop = true;
        }
        private void tabControl_top_left_MouseMove(object sender, MouseEventArgs e)
        {
            if (canDragDrop & ((e.X - downPoint.X) > 10 | (e.Y - downPoint.Y) > 10))
            {
                ExpandTabControls();
                CheckDragAndDrop();
                DoDragDrop(tabControl_top_left.SelectedTab, DragDropEffects.Move);
                CheckForCollapse();
            }
        }
        private void tabControl_top_left_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(TabPage)))
            {
                ((TabPage)e.Data.GetData(typeof(TabPage))).Parent = tabControl_top_left;
                SaveTabParent(((TabPage)e.Data.GetData(typeof(TabPage))), TabParent.TopLeft);
                canDragDrop = false;
            }
        }
        private void tabControl_top_middle_MouseMove(object sender, MouseEventArgs e)
        {
            if (canDragDrop & ((e.X - downPoint.X) > 10 | (e.Y - downPoint.Y) > 10))
            {
                ExpandTabControls();
                CheckDragAndDrop();
                DoDragDrop(tabControl_top_middle.SelectedTab, DragDropEffects.Move);
                CheckForCollapse();
            }
        }
        private void tabControl_top_middle_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(TabPage)))
            {
                ((TabPage)e.Data.GetData(typeof(TabPage))).Parent = tabControl_top_middle;
                SaveTabParent(((TabPage)e.Data.GetData(typeof(TabPage))), TabParent.Top);
                canDragDrop = false;
            }
        }
        private void tabControl_top_right_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(TabPage)))
            {
                ((TabPage)e.Data.GetData(typeof(TabPage))).Parent = tabControl_top_right;
                SaveTabParent(((TabPage)e.Data.GetData(typeof(TabPage))), TabParent.TopRight);
                canDragDrop = false;
            }
        }
        private void tabControl_top_right_MouseMove(object sender, MouseEventArgs e)
        {
            if (canDragDrop & ((e.X - downPoint.X) > 10 | (e.Y - downPoint.Y) > 10))
            {
                ExpandTabControls();
                CheckDragAndDrop();
                DoDragDrop(tabControl_top_right.SelectedTab, DragDropEffects.Move);
                CheckForCollapse();
            }
        }
        private void tabControl_down_left_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(TabPage)))
            {
                ((TabPage)e.Data.GetData(typeof(TabPage))).Parent = tabControl_down_left;
                SaveTabParent(((TabPage)e.Data.GetData(typeof(TabPage))), TabParent.DownLeft);
                canDragDrop = false;
            }
        }
        private void tabControl_down_left_MouseMove(object sender, MouseEventArgs e)
        {
            if (canDragDrop & ((e.X - downPoint.X) > 10 | (e.Y - downPoint.Y) > 10))
            {
                ExpandTabControls();
                CheckDragAndDrop();
                DoDragDrop(tabControl_down_left.SelectedTab, DragDropEffects.Move);
                CheckForCollapse();
            }
        }
        private void tabControl_down_middle_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(TabPage)))
            {
                ((TabPage)e.Data.GetData(typeof(TabPage))).Parent = tabControl_down_middle;
                SaveTabParent(((TabPage)e.Data.GetData(typeof(TabPage))), TabParent.Down);
                canDragDrop = false;
            }
        }
        private void tabControl_down_middle_MouseMove(object sender, MouseEventArgs e)
        {
            if (canDragDrop & ((e.X - downPoint.X) > 10 | (e.Y - downPoint.Y) > 10))
            {
                ExpandTabControls();
                CheckDragAndDrop();
                DoDragDrop(tabControl_down_middle.SelectedTab, DragDropEffects.Move);
                CheckForCollapse();
            }
        }
        private void tabControl_down_Right_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(TabPage)))
            {
                ((TabPage)e.Data.GetData(typeof(TabPage))).Parent = tabControl_down_Right;
                SaveTabParent(((TabPage)e.Data.GetData(typeof(TabPage))), TabParent.DownRight);
                canDragDrop = false;
            }
        }
        private void tabControl_down_Right_MouseMove(object sender, MouseEventArgs e)
        {
            if (canDragDrop & ((e.X - downPoint.X) > 10 | (e.Y - downPoint.Y) > 10))
            {
                ExpandTabControls();
                CheckDragAndDrop();
                DoDragDrop(tabControl_down_Right.SelectedTab, DragDropEffects.Move);
                CheckForCollapse();
            }
        }
        private void tabControl_top_left_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }

        private void mainToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.Settings.CurrentLayout.Bar_Main.Visible = toolStrip_main.Visible = mainToolStripMenuItem.Checked;
        }
        private void editToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Program.Settings.CurrentLayout.Bar_Edit.Visible = toolStrip_edit.Visible = editToolStripMenuItem1.Checked;
        }
        private void marksToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Program.Settings.CurrentLayout.Bar_Marks.Visible = toolStrip_marks.Visible = marksToolStripMenuItem1.Checked;
        }

        private void toolStrip_main_ParentChanged(object sender, EventArgs e)
        {
            Program.Settings.CurrentLayout.Bar_Main.Parent = GetBarParent(toolStrip_main);
        }
        private void toolStrip_marks_ParentChanged(object sender, EventArgs e)
        {
            Program.Settings.CurrentLayout.Bar_Marks.Parent = GetBarParent(toolStrip_marks);
        }
        private void toolStrip_edit_ParentChanged(object sender, EventArgs e)
        {
            Program.Settings.CurrentLayout.Bar_Edit.Parent = GetBarParent(toolStrip_edit);
        }
        private void exportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Program.ProjectManager.Project.SubtitleTracks.Count == 0)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_NoSubtitlesTrackToExport"),
                  Program.ResourceManager.GetString("MessageCaption_CantExport"),
                    MessageDialogButtons.Ok, MessageDialogIcon.Error);
                return;
            }
            mediaPlayer1.Stop();
            Frm_Export export = new Frm_Export(Program.ProjectManager.Project, Program.Settings.FavoriteFormat);
            export.ShowDialog(this);
            Program.Settings.FavoriteFormat = export.FavoriteFormat;
        }
        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Title = Program.ResourceManager.GetString("Title_ImportSubtitlesFormat");
            op.Filter = SubtitleFormats.GetFilter();
            if (op.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                ImportFormat(op.FileName);
            }
        }
        void format_Progress(object sender, ProgressArgs e)
        {
            ProgressBar1.Value = e.PrecentageCompleted;
            SetStatus(e.Status, StatusType.None);
        }
        void format_LoadFinished(object sender, EventArgs e)
        {
            ProgressBar1.Visible = false;
        }
        void format_LoadStarted(object sender, EventArgs e)
        {
            ProgressBar1.Visible = true;
        }

        private void treeView_subtitleTracks_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (e.Label == "" || e.Label == null)
            {
                e.CancelEdit = true;
                return;
            }
            if (Program.ProjectManager.Project.IsSubtitlesTrackExist(e.Label))
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_NameAlreadyTaken"),
                   Program.ResourceManager.GetString("MessageCaption_CantRename"));
                e.CancelEdit = true;
                return;
            }
            ((TreeNode_SubtitlesTrack)treeView_subtitleTracks.SelectedNode).SubtitlesTrack.Name = e.Label;
            Save = true;
            AddHistory(Program.ResourceManager.GetString("History_SubtitlesTrackRenamed"));
        }
        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_Settings frm = new Frm_Settings();
            frm.ShowDialog(this);
            ApplySettings();
        }
        private void aboutAHDSubtitlesMakerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_AboutASM about = new Frm_AboutASM(Program.Version);
            about.ShowDialog(this);
        }
        private void websiteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("http://barnamenevisjavan.com");
        }
        private void checkForupdatesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ProcessStartInfo str = new ProcessStartInfo();
                str.Arguments = "/checknow /lang " + "\"" + Program.Settings.Language + "\"";
                str.FileName = Program.StartUpPath + "\\Updater.exe";
                Process.Start(str);
            }
            catch { }
        }
        private void tipOfTheDayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowTipOfTheDay();
        }
        private void quickStartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowQuickStart();
        }
        private void autoFixErrorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            errorsChecker1.AutoFixErrors();
        }
        private void errorsChecker1_AutoFixFinished(object sender, EventArgs e)
        {
            ProgressBar1.Visible = false;
            Save = true;

            //refresh controls
            subtitlesDataEditor1.RefreshSubtitles();
            timeLine1.Refresh();

            AddHistory(Program.ResourceManager.GetString("History_ErrorsAutoFix"));
        }
        private void errorsChecker1_AutoFixProgress(object sender, ProgressArgs e)
        {
            ProgressBar1.Value = e.PrecentageCompleted;
            SetStatus(e.Status, StatusType.None);
        }
        private void errorsChecker1_AutoFixStarted(object sender, EventArgs e)
        {
            ProgressBar1.Visible = true;
        }
        private void setRightToLeftToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
                return;
            foreach (Subtitle sub in selectedTrack.Subtitles)
            {
                sub.Text.RighttoLeft = true;
            }
            Save = true;
            AddHistory(Program.ResourceManager.GetString("History_RTLValueChanged"));
            SetStatus(Program.ResourceManager.GetString("Status_RightToLeftSet"), StatusType.None);
        }
        private void setLeftToRightToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
                return;
            foreach (Subtitle sub in selectedTrack.Subtitles)
            {
                sub.Text.RighttoLeft = false;
            }
            Save = true;
            AddHistory(Program.ResourceManager.GetString("History_RTLValueChanged"));
            SetStatus(Program.ResourceManager.GetString("Status_LeftToRightSet"), StatusType.None);
        }
        private void toolStripButton_selectedTrackRTL_Click(object sender, EventArgs e)
        {
            if (selectedTrack != null)
            {
                selectedTrack.RightToLeft = !selectedTrack.RightToLeft;
                toolStripButton_selectedTrackRTL.Checked = selectedTrack.RightToLeft;
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_DefaultRTLChanged"));
            }
        }
        private void toolStripButton29_Click(object sender, EventArgs e)
        {
            activeMode = ActiveMode.SubtitlesTrack;
            Delete();
        }
        private void defaultRTLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButton_selectedTrackRTL_Click(sender, e);
        }
        private void toolStripButton_selectedTrackRTL_CheckedChanged(object sender, EventArgs e)
        {
            defaultRTLToolStripMenuItem.Checked = toolStripButton_selectedTrackRTL.Checked;
        }
        private void mediaPlayer1_TimeSlide(object sender, EventArgs e)
        {
            pIndex = -1;
        }
        private void viewHelpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Help.ShowHelp(this, Program.StartUpPath + "\\" + Program.CultureInfo.Name + "\\Help.chm", HelpNavigator.TableOfContents);
        }
        private void languageToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int i = 0;
            int index = 0;
            foreach (ToolStripMenuItem item in languageToolStripMenuItem.DropDownItems)
            {
                if (item.Text == e.ClickedItem.Text)
                {
                    item.Checked = true;
                    index = i;
                }
                else
                    item.Checked = false;
                i++;
            }
            Program.Language = Program.SupportedLanguages[index, 0];
            Program.Settings.Language = Program.SupportedLanguages[index, 0];

            MessageBox.Show(Program.ResourceManager.GetString("Message_YouMustRestartTheProgramToApplyLanguage"),
                Program.ResourceManager.GetString("MessageCaption_ApplyLanguage"));
        }
        private void setFontOfSelectedSubtitlesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                  Program.ResourceManager.GetString("MessageCaption_StretchToPrevious"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length == 0)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_YouMustSelectAtLeastOneSubtitle"),
                   Program.ResourceManager.GetString("MessageCaption_NosubtitleSelected"));
                return;
            }
            FontDialog fontDial = new FontDialog();
            if (fontDial.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                foreach (ListViewItem_Subtitle subItem in subtitlesDataEditor1.SelectedSubtitleItems)
                {
                    subItem.Subtitle.Text.SetFont(fontDial.Font);
                }
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_FontSetForSelected"));
            }
        }
        private void setColorForSelectedSubtitlesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                  Program.ResourceManager.GetString("MessageCaption_StretchToPrevious"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length == 0)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_YouMustSelectAtLeastOneSubtitle"),
                   Program.ResourceManager.GetString("MessageCaption_NosubtitleSelected"));
                return;
            }

            ColorDialog colorDial = new ColorDialog();
            if (colorDial.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                foreach (ListViewItem_Subtitle subItem in subtitlesDataEditor1.SelectedSubtitleItems)
                {
                    subItem.Subtitle.Text.SetColor(colorDial.Color);
                }
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_ColorSetForSelected"));
            }
        }
        private void setrightToLeftForSelectedSubtitleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                  Program.ResourceManager.GetString("MessageCaption_StretchToPrevious"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length == 0)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_YouMustSelectAtLeastOneSubtitle"),
                   Program.ResourceManager.GetString("MessageCaption_NosubtitleSelected"));
                return;
            }
            foreach (ListViewItem_Subtitle subItem in subtitlesDataEditor1.SelectedSubtitleItems)
            {
                subItem.Subtitle.Text.RighttoLeft = true;
            }
            Save = true;
            AddHistory(Program.ResourceManager.GetString("History_SetRightToLeftForSelectedSubtitle"));
        }
        private void setleftToRightForSelectedSubtitleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                  Program.ResourceManager.GetString("MessageCaption_StretchToPrevious"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length == 0)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_YouMustSelectAtLeastOneSubtitle"),
                   Program.ResourceManager.GetString("MessageCaption_NosubtitleSelected"));
                return;
            }
            foreach (ListViewItem_Subtitle subItem in subtitlesDataEditor1.SelectedSubtitleItems)
            {
                subItem.Subtitle.Text.RighttoLeft = false;
            }
            Save = true;
            AddHistory(Program.ResourceManager.GetString("History_SetLeftToRightForSelectedSubtitle"));
        }
        private void setpositionForSelectedSubtitlesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                  Program.ResourceManager.GetString("MessageCaption_StretchToPrevious"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length == 0)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_YouMustSelectAtLeastOneSubtitle"),
                   Program.ResourceManager.GetString("MessageCaption_NosubtitleSelected"));
                return;
            }

            SubtitleText text = new SubtitleText();
            AHD.SM.Controls.Frm_SubtitlePosition frm = new AHD.SM.Controls.Frm_SubtitlePosition(text);
            frm.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                foreach (ListViewItem_Subtitle subItem in subtitlesDataEditor1.SelectedSubtitleItems)
                {
                    subItem.Subtitle.Text.Position = text.Position;
                    subItem.Subtitle.Text.IsCustomPosition = text.IsCustomPosition;
                    subItem.Subtitle.Text.CustomPosition = text.CustomPosition;
                }
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_PostitionSetforSelectedSubtitles"));
            }
        }
        private void setAlignmentForSelectedSubtitleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedTrack == null)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_PleaseSelectSubtitlesTrackFirst"),
                  Program.ResourceManager.GetString("MessageCaption_StretchToPrevious"));
                return;
            }
            if (subtitlesDataEditor1.SelectedSubtitleItems.Length == 0)
            {
                MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_YouMustSelectAtLeastOneSubtitle"),
                   Program.ResourceManager.GetString("MessageCaption_NosubtitleSelected"));
                return;
            }

            Frm_SetAlignment frm = new Frm_SetAlignment();
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                foreach (ListViewItem_Subtitle subItem in subtitlesDataEditor1.SelectedSubtitleItems)
                {
                    foreach (SubtitleLine line in subItem.Subtitle.Text.TextLines)
                        line.Alignement = frm.LineAlignement;
                }
                Save = true;
                AddHistory(Program.ResourceManager.GetString("History_SetAlignmentForSelectedSubtitles"));
            }
        }
        private void mediaPlayer1_ChangeMediaRequest(object sender, EventArgs e)
        {
            MessageDialogResult result = MessageDialog.ShowMessage(Program.ResourceManager.GetString("Message_NoMediaDoYouWantToChangeMedia"),
                Program.ResourceManager.GetString("MessageCaption_NoMedia"),
                MessageDialogButtons.OkNo, MessageDialogIcon.Question, false, Program.ResourceManager.GetString("Button_Yes"),
                Program.ResourceManager.GetString("Button_No"));

            if (result == MessageDialogResult.Ok)
            {
                changeMediaToolStripMenuItem_Click(this, e);
            }
        }
        private void matroskaRipperToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_MatroskaRipper frm = new Frm_MatroskaRipper(Program.ProjectManager.Project);
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                int i = 1;
                string name = "MKV_Rip" + i;
                while (Program.ProjectManager.Project.IsSubtitlesTrackExist(name))
                {
                    i++;
                    name = "MKV_Rip" + i;
                }
                SubtitlesTrack track = frm.SubtitlesTrackGenerated;
                track.Name = name;
                Program.ProjectManager.Project.SubtitleTracks.Add(track);
                RefreshSubtitleTracks();
                treeView_subtitleTracks.Nodes[treeView_subtitleTracks.Nodes.Count - 1].BeginEdit();
                Save = true;
                AddHistory("Matroska subtitles track rip");
            }
        }
        private void matroskaRipperToolStripMenuItem_EnabledChanged(object sender, EventArgs e)
        {
            toolStripButton30.Enabled = matroskaRipperToolStripMenuItem.Enabled;
        }
    }
    public enum ActiveMode
    { SubtitlesTrack, Subtitles }
}
