////////////////////////////////////////////////////////////
function PipMode(){}

PipMode.VideoInSlide=0;
PipMode.SlideFullScreen=1;
PipMode.SlideInVideo=2;
PipMode.VideoFullScreen=3;
PipMode.SideBySide=4;

function PrimaryDisplay() { }
PrimaryDisplay.None = 0;
PrimaryDisplay.Video = 1;
PrimaryDisplay.Slide = 2;

function DisplayType() { }
DisplayType.Solo = 0;
DisplayType.SideBySide = 1;
DisplayType.Pip = 1;

function PipManager(mplayer)
{
    this.CurrentPrimaryDisplay = PrimaryDisplay.Video;
    this.CurrentDisplayType = DisplayType.Solo;
    
    this.CurrentModeIndex=0;
    
    this.FullScreenModes = [PipMode.VideoInSlide,PipMode.SlideFullScreen,PipMode.SlideInVideo,PipMode.VideoFullScreen];
    
    if (PlayerOptions.PipModes)
    {
        this.WindowedModes=PlayerOptions.PipModes;
    }
    else
    {
        this.WindowedModes = [PipMode.VideoInSlide,PipMode.SlideFullScreen,PipMode.SlideInVideo,PipMode.VideoFullScreen];
    }
    
    if (this.IsVideoOnlyPlayer())
    {
       this.FullScreenModes = [PipMode.VideoFullScreen];
    }
    
    this.CurrentModes=this.WindowedModes;

    this.eventList = new EventList(PipManager.Events.Changed,PipManager.Events.Update);
    
    mplayer.eventList.addListener(MediasitePlayer.Events.FullScreen,Event.createDelegate(this,this.handleFullScreenChange));
    this.SetWindowed();
}

PipManager.Events = {};
PipManager.Events.Changed = "PipModeChanged";
PipManager.Events.Update = "PipModeUpdate";


PipManager.prototype =
{
    handleFullScreenChange: function(fullScreen)
    {
        if (fullScreen)
        {
            this.SetFullScreen(FullScreenBar.SecondaryRectOffset);
            this.Update();
        }
        else
        {
            this.SetWindowed();
            this.Update();
        }

    },

    SwitchMode: function()
    {
        var newIndex = this.CurrentModeIndex;

        newIndex++;

        if (newIndex >= this.CurrentModes.length)
        {
            newIndex = 0;
        }

        if (newIndex != this.CurrentModeIndex)
        {
            this.CurrentModeIndex = newIndex;
            this.SetCurrentMode(this.CurrentModes[this.CurrentModeIndex]);
        }
    },

    SetCurrentMode: function(mode)
    {
        this.CurrentModeIndex = 0;

        for (var x = 0; x < this.CurrentModes.length; x++)
        {
            if (this.CurrentModes[x] == mode)
            {
                this.CurrentModeIndex = x;
                break;
            }
        }

        /////
        if (mode == PipMode.VideoInSlide)
        {
            this.CurrentPrimaryDisplay = PrimaryDisplay.Slide;
            this.CurrentDisplayType = DisplayType.Pip;
        }
        else if (mode == PipMode.SlideFullScreen)
        {
            this.CurrentPrimaryDisplay = PrimaryDisplay.Slide;
            this.CurrentDisplayType = DisplayType.Solo;
        }
        else if (mode == PipMode.SlideInVideo)
        {
            this.CurrentPrimaryDisplay = PrimaryDisplay.Video;
            this.CurrentDisplayType = DisplayType.Pip;
        }
        else if (mode == PipMode.VideoFullScreen)
        {
            this.CurrentPrimaryDisplay = PrimaryDisplay.Video;
            this.CurrentDisplayType = DisplayType.Solo;
        }
        else if (mode == PipMode.SideBySide)
        {
            this.CurrentPrimaryDisplay = PrimaryDisplay.Video;
            this.CurrentDisplayType = DisplayType.SideBySide;
        }

        /////
        this.eventList.Invoke(PipManager.Events.Changed, mode);
        
        this.Update();
    },

    GetCurrentMode: function()
    {
        return this.CurrentModes[this.CurrentModeIndex];
    },

    IsSideBySidePlayer: function()
    {
        if (this.WindowedModes.length < 2)
        {
            if (this.WindowedModes[0] == PipMode.SideBySide)
            {
                return true;
            }
        }

        return false;

    },

    IsVideoOnlyPlayer: function()
    {
        if (this.WindowedModes.length < 2)
        {
            if (this.WindowedModes[0] == PipMode.VideoFullScreen)
            {
                return true;
            }
        }

        return false;
    },


    SetFullScreen: function(pipRectOffset)
    {
        this.CurrentModes = this.FullScreenModes;
        this.SecondaryRect = new Rectangle(mss.content.actualWidth - pipRectOffset.Left, mss.content.actualHeight - pipRectOffset.Top, pipRectOffset.Width, pipRectOffset.Height);
        this.PrimaryRect = new Rectangle(0, 0, mss.content.actualWidth, mss.content.actualHeight);
    },

    SetWindowed: function()
    {
        this.CurrentModes = this.WindowedModes;

        if (this.CurrentModeIndex >= this.CurrentModes.length)
        {
            this.CurrentModeIndex = 0;
        }

        if (PlayerOptions.SecondaryRect != null)
        {
            this.SecondaryRect = PlayerOptions.SecondaryRect;
        }
        else
        {
            this.SecondaryRect = new Rectangle(330, 250, 80, 60);
        }


        if (PlayerOptions.PrimaryRect != null)
        {
            this.PrimaryRect = PlayerOptions.PrimaryRect;
        }
        else
        {
            var parent = mss.FindName(Media.Names.Main);

            if (parent != null)
            {
                this.PrimaryRect = new Rectangle(0, 0, parent.GetValue("Width"), parent.GetValue("Height"));
            }
        }
    },


    Update: function()
    {

        var slideWindow = mss.FindName(Media.Names.SlideWindow);
        var slideImage = mss.FindName(Media.Names.SlideImage);
        var videoWindow = mss.FindName(Media.Names.VideoWindow);
        var videoElement = mss.FindName(Media.Names.VideoElement);

        var secondaryRect = this.SecondaryRect;
        var primaryRect = this.PrimaryRect;

        if (this.CurrentPrimaryDisplay == PrimaryDisplay.Slide)
        {
            primaryRect.SetCanvasSize(slideImage);

            if (slideWindow)
            {
                primaryRect.SetCanvasRect(slideWindow);
                slideWindow.SetValue("Canvas.ZIndex", 1);
                slideWindow.SetValue("Visibility", "Visible");
            }

            if (this.CurrentDisplayType == DisplayType.Solo)
            {
                if (videoWindow)
                {
                    videoWindow.SetValue("Visibility", "Collapsed");
                }
            }
            else
            {
                secondaryRect.SetCanvasSize(videoElement);

                if (videoWindow)
                {
                    secondaryRect.SetCanvasRect(videoWindow);
                    videoWindow.SetValue("Canvas.ZIndex", 2);
                    videoWindow.SetValue("Visibility", "Visible");
                }
            }
        }
        else if (this.CurrentPrimaryDisplay == PrimaryDisplay.Video)
        {
            primaryRect.SetCanvasSize(videoElement);

            if (videoWindow)
            {
                primaryRect.SetCanvasRect(videoWindow);
                videoWindow.SetValue("Canvas.ZIndex", 1);
                videoWindow.SetValue("Visibility", "Visible");
            }
            if (this.CurrentDisplayType == DisplayType.Solo)
            {
                if (slideWindow)
                {
                    slideWindow.SetValue("Visibility", "Collapsed");
                }
            }
            else
            {
                secondaryRect.SetCanvasSize(slideImage);

                if (slideWindow)
                {
                    secondaryRect.SetCanvasRect(slideWindow);
                    slideWindow.SetValue("Canvas.ZIndex", 2);
                    slideWindow.SetValue("Visibility", "Visible");
                }
            }
        }

        this.eventList.Invoke(PipManager.Events.Update, this.GetCurrentMode());
    }
}

