

function Media(){}

Media.Names={};
Media.Names.Main="Media";
Media.Names.MainClip="Media_Clip";
Media.Names.Background="Media_Background";
Media.Names.VideoWindow="VideoWindow";
Media.Names.VideoElement="VideoElement";
Media.Names.SlideWindow="SlideWindow";
Media.Names.SlideWindowClip="SlideWindow_Clip";
Media.Names.SlideImage="SlideImage";

//////////////////////////////////////////////////////////////

function SlideZoom()
{
    this.SlideImage=mss.FindName(Media.Names.SlideImage);
    this.Parent=mss.FindName(Media.Names.Main);
    
    this.SlideOriginX=0;
    this.SlideOriginY=0;
    this.Enable();
}

SlideZoom.prototype=
{
   Enable:function()
    {
        if (this.SlideImage!=null)
        {
            if (this.downToken==null)
            {
	            this.downToken=this.SlideImage.addEventListener("MouseLeftButtonDown", Event.createDelegate(this, this.handleMouseLeftButtonDown));
	        }
        }
    },

    Disable:function()
    {
        if (this.downToken!=null)
        {
            this.SlideImage.removeEventListener("MouseLeftButtonDown", this.downToken);
            this.downToken=null;
        }
    },

    handleMouseLeftButtonDown:function(sender,mouseEventArgs)
    {
        this.Dragging=true;
        this.DragStartX = mouseEventArgs.getPosition(this.Parent).x;
        this.DragStartY = mouseEventArgs.getPosition(this.Parent).y;
	    sender.captureMouse();
    	
	    if (this.moveToken==null)
	    {
	        this.moveToken=this.SlideImage.addEventListener("MouseMove", Event.createDelegate(this, this.handleMouseMove));
	        this.upToken=this.SlideImage.addEventListener("MouseLeftButtonUp", Event.createDelegate(this, this.handleMouseLeftButtonUp));
	    }
    	
    },
    	
    handleMouseLeftButtonUp:function(sender,mouseEventArgs)
    {
        this.Dragging=false;
        this.SlideImage.removeEventListener("MouseMove", this.moveToken);
        this.SlideImage.removeEventListener("MouseLeftButtonUp",this.upToken);
        sender.releaseMouseCapture();
        this.moveToken=null;
        this.upToken=null;
        
        this.SlideOriginX = this.SlideImage.GetValue("Canvas.Left");
        this.SlideOriginY = this.SlideImage.GetValue("Canvas.Top");
    },
    	
    handleMouseMove:function(sender,mouseEventArgs)
    {
        var newX = mouseEventArgs.getPosition(this.Parent).x;
        var newY = mouseEventArgs.getPosition(this.Parent).y;

        var diffX = newX - this.DragStartX;
        var diffY = newY - this.DragStartY;
        
        var tmpOriginX = this.SlideOriginX+diffX;
        var tmpOriginY = this.SlideOriginY+diffY;
        
        if (tmpOriginX>0)
        {
            tmpOriginX=0;
        }
        
        if (tmpOriginY>0)
        {
            tmpOriginY=0;
        }
        var width = this.SlideImage.GetValue("Width");
        var height = this.SlideImage.GetValue("Height");
        
        var parentWidth =this.Parent.GetValue("Width");
        var parentHeight =this.Parent.GetValue("Height");
        
        var leftBound = width-parentWidth;
        var topBound = height-parentHeight;
        
        if (-tmpOriginX>leftBound)
        {
            tmpOriginX=-leftBound;
        }
        
        if (-tmpOriginY>topBound)
        {
            tmpOriginY=-topBound;
        }

        this.SlideImage.SetValue("Canvas.Left",tmpOriginX);
        this.SlideImage.SetValue("Canvas.Top",tmpOriginY);
    }
}




//////////////////////////////////////////////////////////////////////
function InfoCard(mplayer)
{
    this.Expanded=false;
    target = mss.FindName("InfoCard");

    if (null!=target)
    {
        mplayer.eventList.addListener(MediasitePlayer.Events.FullScreen,Event.createDelegate(this,this.handleFullScreenChange));
        mplayer.eventList.addListener(MediasitePlayer.Events.PanelSwitch,Event.createDelegate(this,this.handlePanelSwitch));

        var commandBar = mss.FindName("InfoCard_CommandBar");

        if (commandBar != null)
        {
            commandBar.addEventListener("MouseLeftButtonDown", Event.createDelegate(this, this.handleCommandBarClicked));
        }
        
	    var textBox = mss.FindName("Info_TitleText");
        mss.SetText(textBox,Manifest.Title);
        textBox = mss.FindName("Info_SpeakerText");
        mss.SetText(textBox, Manifest.Presenters[0].Name);
        textBox = mss.FindName("Info_AirDateText");
        textBox.Text= Manifest.AirDate;
        textBox = mss.FindName("Info_AirTimeText");
        textBox.Text=Manifest.AirTime;
        textBox = mss.FindName("Info_DurationText");
        textBox.Text=mss.FormatTimeMMSS(Manifest.Duration);
        textBox = mss.FindName("Info_DescriptionText");
        mss.SetText(textBox, Manifest.Description);
        
        var imageBox = mss.FindName("Info_PresentationThumbImage");
        
        var imageUrl=Manifest.PresentationThumbImage;
        
        if (imageUrl.length<1)
        {
            imageUrl=Manifest.Presenters[0].ImageUrl;
        }

        if (imageUrl)
        {
            if (imageUrl.length > 0)
            {
                imageBox.Source = imageUrl;
            }
        }
        
	}
}


InfoCard.Names={};
InfoCard.Names.Main="InfoCard";
InfoCard.Names.Show="InfoCard_Show";
InfoCard.Names.Hide="InfoCard_Hide";


InfoCard.prototype=
{
    handleCommandBarClicked:function(sender,eventArgs) 
    {
        this.Toggle();
    },
    Toggle:function()
    {
        
        if (!this.Expanded)
        {
            window.mplayer.RequestPanelSwitch(InfoCard.Names.Main);
        }
        
        this.Show(!this.Expanded);

    },
    
    handlePanelSwitch:function(panelName) 
    {
       if (panelName!=InfoCard.Names.Main)
       {
            if (this.Expanded)
            {
                this.Show(false);
            }
            
       }
    },
    
    Show:function(show) 
    {
        if (show)
        {
           mss.Animate(InfoCard.Names.Show);
        }
        else
        {
           mss.Animate(InfoCard.Names.Hide);
        }
        this.Expanded=show;
    },
    
    handleFullScreenChange:function(fullScreen) 
    {
       this.SetVisible(!fullScreen);
    },

    SetVisible:function(show) 
    {
        mss.SetVisible(InfoCard.Names.Main,show);
    }

}




//////////////////////////////////////////////////////////////////////


function LinksPanel(mplayer)
{
    this.Expanded=false;
    this.Enabled=false;
    
    target = mss.FindName(LinksPanel.Names.Title);
    
    if (null!=target)
    {
    	this.ListPanel = new ListPanel(LinksPanel.Names.Main);
    
        mplayer.eventList.addListener(MediasitePlayer.Events.FullScreen,Event.createDelegate(this,this.handleFullScreenChange));
        mplayer.eventList.addListener(MediasitePlayer.Events.PanelSwitch,Event.createDelegate(this,this.handlePanelSwitch));

        mss.AddAnimationMouseEventListeners("LinksPanel.GoToStart",Event.createDelegate(this, this.handleGoToStartMouseUp));
        mss.AddAnimationMouseEventListeners("LinksPanel.GoToEnd",Event.createDelegate(this, this.handleGoToEndMouseUp));
        mss.AddAnimationMouseEventListeners("LinksPanel.PageUp",Event.createDelegate(this, this.handlePageUpMouseUp));
        mss.AddAnimationMouseEventListeners("LinksPanel.PageDown",Event.createDelegate(this, this.handlePageDownMouseUp));
        
        this.ListPanel.eventList.addListener("DrawItem",Event.createDelegate(this,this.handleDrawItem));
        this.ListPanel.eventList.addListener("Clicked",Event.createDelegate(this,this.handleClicked));
        this.ListPanel.ObjectList = Manifest.SupportingLinks;
        this.ListPanel.SetTopIndex(0);
        
	    if (Manifest.SupportingLinks.length>0)
        {
            this.Enabled = true;
        }
        else 
        {
            mss.Animate(LinksPanel.Names.ButtonDisable);
        }

        var commandBar = mss.FindName("LinksPanel_CommandBar");

        if (commandBar != null)
        {
            commandBar.addEventListener("MouseLeftButtonDown", Event.createDelegate(this, this.handleCommandBarClicked));
        }
	}
}

LinksPanel.Names={};
LinksPanel.Names.Main="LinksPanel";
LinksPanel.Names.Title="LinksPanel_Title";
LinksPanel.Names.Hide="LinksPanel_Hide";
LinksPanel.Names.Show="LinksPanel_Show";
LinksPanel.Names.ButtonDisable="CommandButton_Links_Disable";


LinksPanel.prototype=
{
    handleCommandBarClicked: function(sender, eventArgs)
    {
        this.Toggle();
    },
    handleDrawItem:function(drawItemArgs)
    {
            
        var itemText="";
          
        if (drawItemArgs.Object!=null)
        {
            var linkDescription=drawItemArgs.Object.Description;
            itemText= linkDescription;
        }
        
        var textCanvas = mss.FindName(drawItemArgs.ItemPanel.Name+"_Text");
        mss.SetText(textCanvas,itemText);
        //    mss.AlignText(textCanvas,TextAlign.CenterVertical);
    },
    
    handleClicked:function(clickedArgs)
    {
        window.open(clickedArgs.Object.Url,'newWindow'); 
        this.Toggle();
    },
    
    handleGoToStartMouseUp:function(sender,eventArgs) 
    {
        this.ListPanel.GoToStart();
    },
    handleGoToEndMouseUp:function(sender,eventArgs) 
    {
        this.ListPanel.GoToEnd();
    },
    handlePageUpMouseUp:function(sender,eventArgs) 
    {
        this.ListPanel.PageUp();
    },
    handlePageDownMouseUp:function(sender,eventArgs) 
    {
       this.ListPanel.PageDown();
    },
    
    Toggle:function() 
    {
        if (!this.Expanded)
        {
            window.mplayer.RequestPanelSwitch(LinksPanel.Names.Main);
        }
        this.Show(!this.Expanded);
    },
    
    handlePanelSwitch:function(panelName) 
    {
       if (panelName!=LinksPanel.Names.Main)
       {
            if (this.Expanded)
            {
                this.Show(false);
            }
            
       }
    },
    
    Show:function(show) 
    {
        if (this.Enabled)
        {
            if (show)
            {
               mss.Animate(LinksPanel.Names.Show);
            }
            else
            {
               mss.Animate(LinksPanel.Names.Hide);
            }
            this.Expanded=show;
        }

    },
    
    handleFullScreenChange:function(fullScreen) 
    {
       this.SetVisible(!fullScreen);
    },
    
    SetVisible:function(show) 
    {
        mss.SetVisible(LinksPanel.Names.Main,show);
    }
}


//////////////////////////////////////////////////////////////////////


function ChaptersPanel(mplayer)
{
    this.Expanded=false;
    this.Enabled=false;
    
	this.ListPanel = new ListPanel(ChaptersPanel.Names.Main);
    
    mplayer.eventList.addListener(MediasitePlayer.Events.FullScreen,Event.createDelegate(this,this.handleFullScreenChange));
    mplayer.eventList.addListener(MediasitePlayer.Events.PanelSwitch,Event.createDelegate(this,this.handlePanelSwitch));

    mss.AddAnimationMouseEventListeners("ChaptersPanel.GoToStart",Event.createDelegate(this, this.handleGoToStartMouseUp));
    mss.AddAnimationMouseEventListeners("ChaptersPanel.GoToEnd",Event.createDelegate(this, this.handleGoToEndMouseUp));
    mss.AddAnimationMouseEventListeners("ChaptersPanel.PageUp",Event.createDelegate(this, this.handlePageUpMouseUp));
    mss.AddAnimationMouseEventListeners("ChaptersPanel.PageDown",Event.createDelegate(this, this.handlePageDownMouseUp));
    
    this.ListPanel.eventList.addListener("DrawItem",Event.createDelegate(this,this.handleDrawItem));
    this.ListPanel.eventList.addListener("Clicked",Event.createDelegate(this,this.handleClicked));
    this.ListPanel.ObjectList = Manifest.Chapters;
    this.ListPanel.SetTopIndex(0);
    
	if (Manifest.Chapters.length>0)
    {
        this.Enabled = true;
    }
    else 
    {
        mss.Animate(ChaptersPanel.Names.ButtonDisable);
    }
    var commandBar = mss.FindName("ChaptersPanel_CommandBar");

    if (commandBar != null)
    {
        commandBar.addEventListener("MouseLeftButtonDown", Event.createDelegate(this, this.handleCommandBarClicked));
    }
}

ChaptersPanel.Names={};
ChaptersPanel.Names.Main="ChaptersPanel";
ChaptersPanel.Names.Title="ChaptersPanel_Title";
ChaptersPanel.Names.Hide="ChaptersPanel_Hide";
ChaptersPanel.Names.Show="ChaptersPanel_Show";
ChaptersPanel.Names.ButtonDisable="CommandButton_Chapters_Disable";


ChaptersPanel.prototype = 
{
    handleCommandBarClicked: function(sender, eventArgs)
    {
        this.Toggle();
    },
    handleDrawItem:function(drawItemArgs)
    {
            
        var itemText="";
          
        if (drawItemArgs.Object!=null)
        {
            var chapterDescription=drawItemArgs.Object.Text;
            var chapterNumber=drawItemArgs.ObjectIndex+1;
            var chapterTime=mss.FormatTimeHHMMSS(drawItemArgs.Object.Time);
            itemText= chapterNumber+")  "+chapterTime+"\r\n"+chapterDescription;
        }
        
        var textCanvas = mss.FindName(drawItemArgs.ItemPanel.Name+"_Text");
        mss.SetText(textCanvas,itemText);
        //    mss.AlignText(textCanvas,TextAlign.CenterVertical);
    },

    handleClicked:function(clickedArgs)
    {
        window.mplayer.SeekTime(clickedArgs.Object.Time);
        this.Toggle();
    },
    
    handleGoToStartMouseUp:function(sender,eventArgs) 
    {
        this.ListPanel.GoToStart();
    },
    handleGoToEndMouseUp:function(sender,eventArgs) 
    {
        this.ListPanel.GoToEnd();
    },
    handlePageUpMouseUp:function(sender,eventArgs) 
    {
        this.ListPanel.PageUp();
    },
    handlePageDownMouseUp:function(sender,eventArgs) 
    {
       this.ListPanel.PageDown();
    },

    Toggle:function() 
    {
        if (!this.Expanded)
        {
            window.mplayer.RequestPanelSwitch(ChaptersPanel.Names.Main);
        }
        this.Show(!this.Expanded);
    },
    
    
    handlePanelSwitch:function(panelName) 
    {
       if (panelName!=ChaptersPanel.Names.Main)
       {
            if (this.Expanded)
            {
                this.Show(false);
            }
       }
    },
    
    handleFullScreenChange:function(fullScreen) 
    {
       this.SetVisible(!fullScreen);
    },
    
    Show:function(show) 
    {
        if (this.Enabled)
        {
            if (show)
            {
               mss.Animate(ChaptersPanel.Names.Show);
            }
            else
            {
               mss.Animate(ChaptersPanel.Names.Hide);
            }
            this.Expanded=show;
        }

    },
    
    SetVisible:function(show) 
    {
        mss.SetVisible(ChaptersPanel.Names.Main,show);
    }
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////
function ClosedCaptionPanel(mplayer)
{
    this.CurrentIndex=-1;
    this.IsShowing=false;
    
    var visible=false;
    
    if (Transcript.EventList.length>0)
    {
        visible=true;
    }
    
    if (visible)
    {
        mplayer.eventList.addListener(MediasitePlayer.Events.FullScreen,Event.createDelegate(this,this.handleFullScreenChange));
        mplayer.eventList.addListener(MediasitePlayer.Events.StateChanged,Event.createDelegate(this,this.handleStateChange));
        mplayer.eventList.addListener(MediasitePlayer.Events.Seek,Event.createDelegate(this,this.handleSeek));
        mplayer.eventList.addListener(MediasitePlayer.Events.Timer,Event.createDelegate(this,this.handleTimer));
    }
    else
    {
        mss.Animate(ClosedCaptionPanel.Names.ButtonDisable);
    }

//    mss.SetVisible(ClosedCaptionPanel.Names.CaptionButton,visible);
//    mss.SetVisible(ClosedCaptionPanel.Names.FullScreenCaptionButton,visible);
}

ClosedCaptionPanel.Names={};
ClosedCaptionPanel.Names.Main="ClosedCaptioning";
ClosedCaptionPanel.Names.CaptionButton="CaptionButton";
ClosedCaptionPanel.Names.FullScreenCaptionButton="FullScreen_CaptionButton";
ClosedCaptionPanel.Names.Background="ClosedCaptioning_Background";
ClosedCaptionPanel.Names.Text="ClosedCaptioning_Text";
ClosedCaptionPanel.Names.ButtonDisable = "ClosedCaptioning_Disable";

ClosedCaptionPanel.prototype =
{

    handleStateChange: function(state)
    {
        if (state == MediaPlayer.States.Stopped)
        {
            this.Reset();
        }
    },

    handleSeek: function(currentPosition)
    {
        if (!Transcript)
        {
            return;
        }

        var currentTranscriptIndex = -1;

        while ((currentTranscriptIndex + 1) < Transcript.EventList.length)
        {
            var time = Transcript.EventList[currentTranscriptIndex + 1].Time;

            if (currentPosition >= time)
            {
                currentTranscriptIndex++;
            }
            else
            {
                break;
            }
        }

        this.SetTextIndex(currentTranscriptIndex);
    },

    Reset: function()
    {
        if (!Transcript)
        {
            return;
        }
        this.SetTextIndex(-1);
    },


    handleTimer: function(currentPosition)
    {
        if (!Transcript)
        {
            return;
        }


        var currentTranscriptIndex = this.CurrentIndex;

        if (currentTranscriptIndex < 0)
        {
            currentTranscriptIndex = -1;
        }

        while ((currentTranscriptIndex + 1) < Transcript.EventList.length)
        {
            var time = Transcript.EventList[currentTranscriptIndex + 1].Time;

            if (currentPosition >= time)
            {
                currentTranscriptIndex++;
            }
            else
            {
                break;
            }
        }

        if (currentTranscriptIndex > this.CurrentIndex)
        {
            this.SetTextIndex(currentTranscriptIndex);
        }
    },

    handleFullScreenChange: function(fullScreen)
    {
        var content = mss.content;
        var panel = mss.FindName(ClosedCaptionPanel.Names.Main);

        if (panel == null)
        {
            return;
        }

        if (fullScreen)
        {
            this.restoreCaptionRect = new Rectangle();
            this.restoreCaptionRect.GetCanvasRect(panel);

            var rc = new Rectangle(0, content.actualHeight - 100, content.actualWidth, 100);

            rc.SetCanvasRect(panel);
            panel = content.findName(ClosedCaptionPanel.Names.Background);
            rc.SetCanvasSize(panel);

            panel = content.findName(ClosedCaptionPanel.Names.Text);
            rc.SetCanvasSize(panel);
            this.restoreFontSize = panel.FontSize;
            panel.FontSize = "34";
        }
        else
        {
            this.restoreCaptionRect.SetCanvasRect(panel);
            panel = content.findName(ClosedCaptionPanel.Names.Background);
            this.restoreCaptionRect.SetCanvasSize(panel);
            panel = content.findName(ClosedCaptionPanel.Names.Text);
            this.restoreCaptionRect.SetCanvasSize(panel);
            panel.FontSize = this.restoreFontSize;
        }
    },

    SetTextIndex: function(index)
    {
        this.CurrentIndex = index;

        if (index < 0)
        {
            this.SetText("");
        }
        else if (index < Transcript.EventList.length)
        {
            this.SetText(Transcript.EventList[index].Text);
        }

    },

    SetText: function(text)
    {
        var ccPanel = mss.FindName(ClosedCaptionPanel.Names.Text);

        if (ccPanel != null)
        {
            text = text.replace(/<br>/g, "\r\n");
            ccPanel.Text = text;
        }
    }
}