if (!window.FixedSizeMediaPlayer)
	window.FixedSizeMediaPlayer = {};

FixedSizeMediaPlayer.Scene = function() 
{
}

FixedSizeMediaPlayer.Scene.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
	},
	
	// Sample event handler
	handleMouseDown: function(sender, eventArgs) 
	{
	}
}

function MediaEnded(sender, eventArgs)
{
    if (sender.name == "mediaElement")
    {
        sender.findName("mediaElementStoryboard").begin();
        //Reset media to begining
        sender.Position = "00:00:00"
        
        // Swap Pause and Play graphics since media has ended
        var playTriangle = sender.findName("playTriangle");
        var leftPauseLine = sender.findName("leftPauseLine");
        var rightPauseLine = sender.findName("rightPauseLine");
        
        playTriangle.opacity = 1;
        leftPauseLine.opacity = 0;
        rightPauseLine.opacity = 0;        
    }
}

function ButtonMouseEnter(sender, eventArgs)
{    
    if(sender.name == "playButton")
    {
      // Get a reference to the glass rectangle.
      var playBtnGlassRectangle = sender.findName("playBtnGlassRectangle");
      playBtnGlassRectangle.opacity = 1;
    }
    else if(sender.name == "stopButton")
    {
      // Get a reference to the glass rectangle.
      var stopBtnGlassRectangle = sender.findName("stopBtnGlassRectangle");
      stopBtnGlassRectangle.opacity = 1;
    }
    else if(sender.name == "fullScreenButton")
    {
      var fullScreenBtnGlassRectangle= sender.findName("fullScreenBtnGlassRectangle");
      fullScreenBtnGlassRectangle.opacity = 1;
    }
    
}

function ButtonMouseLeave(sender, eventArgs)
{
    
    if(sender.name == "playButton")
    {
      // Get a reference to the glass rectangle.
      var playBtnGlassRectangle = sender.findName("playBtnGlassRectangle");
      playBtnGlassRectangle.opacity = 0;
    }
    else if(sender.name == "stopButton")
    {
      // Get a reference to the glass rectangle.
      var stopBtnGlassRectangle = sender.findName("stopBtnGlassRectangle");
      stopBtnGlassRectangle.opacity = 0;
    }
    else if(sender.name == "fullScreenButton")
    {
      // Get a reference to the glass rectangle.
      var fullScreenBtnGlassRectangle = sender.findName("fullScreenBtnGlassRectangle");
      fullScreenBtnGlassRectangle.opacity = 0;
    }
}


function ButtonMouseClick(sender, eventArgs)
{
    
    if(sender.name == "playButton")
    {

      // Run the animation for when the play button is clicked.
      sender.findName("playButtonStoryboard").begin();

      // Swap Pause and Play graphics.
      var playTriangle = sender.findName("playTriangle");
      var leftPauseLine = sender.findName("leftPauseLine");
      var rightPauseLine = sender.findName("rightPauseLine");

      // If the playTriangle is visible then swap the play triangle for
      // the pause lines and start the video.
      if(playTriangle.opacity == 1)
      {
        playTriangle.opacity = 0;
        leftPauseLine.opacity = 1;
        rightPauseLine.opacity = 1;

        // Play the video
        
        sender.findName("mediaElement").play();
      }
      else
      {
        playTriangle.opacity = 1;
        leftPauseLine.opacity = 0;
        rightPauseLine.opacity = 0;

        // Pause the video
        sender.findName("mediaElement").pause();
      }

    }
    else if(sender.name == "stopButton")
    {
      // Run the animation for when the stop button is clicked.
      sender.findName("stopButtonStoryboard").begin();
      sender.findName("mediaElementStoryboard").begin();
      // Stop the video
      sender.findName("mediaElement").stop();

      // Because the user has stopped the video, you need to
      // set the play/pause button back to the "play" state.
      var playTriangle = sender.findName("playTriangle");
      playTriangle.opacity = 1;
      var leftPauseLine = sender.findName("leftPauseLine");
      var rightPauseLine = sender.findName("rightPauseLine");
      leftPauseLine.opacity = 0;
      rightPauseLine.opacity = 0;
 
    }
    else if(sender.name == "fullScreenButton")
    {
      // Run the animation for when the full screen button is clicked.
      sender.findName("fullScreenButtonStoryboard").begin();

      // Get a reference to the Silverlight control that is hosted in the HTML page. 
      var silverlightControl = sender.getHost();

      // Toggle the fullscreen property value back and forth between true and false.
      // If it is false, set it to true, if it is true, set it to false.
      silverlightControl.content.fullScreen = !silverlightControl.content.fullScreen;
    }
}


// The purpose of this function is to dynamically attach an event to
// the Silverlight control hosted in the HTML. This has to be done dynamically
// because this control is not accessible through XAML.
function canvas_loaded(sender, args)
{
  
    // Get a reference to the Silverlight control that is hosted in the HTML page. 
    var control = sender.getHost();

    // Attach the event "onFullScreenChange" to the control and when the event fires
    // execute the function "onFullScreenChanged"
    control.content.onfullScreenChange = onFullScreenChanged;
    
}

// This function executes whenever the fullscreen property changes.
// The function adjusts the size of the video screen to take up
// the entire screen when in fullscreen mode and take up it's original
// size when it leaves fullscreen mode. In addition, the buttons are
// hidden in fullscreen mode and visible out of fullscreen mode.
function onFullScreenChanged(sender, args)
{

    // Get a reference to the Silverlight control that is hosted in the HTML page.
    var silverlightControl = sender.getHost();

    // Get a reference to the Canvas that contains all of the buttons.
    var buttonPanel = sender.findName("buttonContainer");
        
    // Get a reference to the media player.
    var mediaElement = sender.findName("mediaElement");
    var canvasMain = sender.findName("canvasMain");

    if (silverlightControl.content.fullScreen == true)
    {
        buttonPanel.opacity = .4;
        buttonPanel.setValue("Canvas.Left",(silverlightControl.content.actualWidth / 2) - 260);
        buttonPanel.setValue("Canvas.Top",silverlightControl.content.actualHeight - 100);
        // Set the media player to go full screen with border
        mediaElement.width = silverlightControl.content.actualWidth - 16;
        mediaElement.height = silverlightControl.content.actualHeight - 16;
        canvasMain.width = silverlightControl.content.actualWidth ;
        canvasMain.height = silverlightControl.content.actualHeight;
        canvasMain.background = "White"
     }
     else
     {
        // Since the Silverlight control is back to its original mode, make buttons visible.
        buttonPanel.opacity = 1;
        buttonPanel.setValue("Canvas.Left",46.0);
        buttonPanel.setValue("Canvas.Top",411.5);
        //Hard-code back to original design-time values to keep it simple for fix size video player
        mediaElement.width = 561;
        mediaElement.height = 396.5;
        canvasMain.background="#FF000000"
     }
}

