﻿function videoPlayer(Control, Parent)
{
    this.control = Control;
    this._parent = Parent;
    this.handleGrab = false;
    
    //x:Names
    this.videoHolder = this.control.content.findName("videoHolder");
    this.video = this.control.content.findName("video");
    this.trackBack = this.control.content.findName("trackBack");
    this.bar = this.control.content.findName("bar");
    this.handle = this.control.content.findName("handle");
    this.playPauseBTN = this.control.content.findName("playPauseBTN");
    this.pauseIcon = this.control.content.findName("pauseIcon");
    this.playIcon = this.control.content.findName("playIcon");
    this.muteBTN = this.control.content.findName("muteBTN");
    this.soundIcon = this.control.content.findName("soundIcon");
    this.muteIcon = this.control.content.findName("muteIcon");
    this.fullScreenBTN = this.control.content.findName("fullScreenBTN");
    this.fullScreenIcon = this.control.content.findName("fullScreenIcon");
    this.normalScreenIcon = this.control.content.findName("normalScreenIcon");
    this.preloader = this.control.content.findName("preloader");
    
    
    this.play_Highlight = this.control.content.findName("play_Highlight");
    this.mute_Highlight = this.control.content.findName("mute_Highlight");
    this.fullScreen_Highlight = this.control.content.findName("fullScreen_Highlight");

    //Cursor
    this.playPauseBTN.cursor = "Hand";
    this.muteBTN.cursor = "Hand";
    this.fullScreenBTN.cursor = "Hand";
    this.handle.cursor = "Hand";
    
    //Functions
    
    this.playPush = function(sender, args)
    {
        if(this._parent.displayWindow.VideoLoaded)
        {
            if(this.pauseIcon.Visibility == "Visible")
            {
                this.pauseIcon.Visibility = "Collapsed";
                this.playIcon.Visibility = "Visible";
                this.video.Pause();
            }
            else
            {
                this.pauseIcon.Visibility = "Visible";
                this.playIcon.Visibility = "Collapsed";
                this.video.Play();
            }
        }
    }
    this.playOver = function(sender, args)
    {
        this.play_Highlight.Begin();
    }
    this.playOut = function(sender, args)
    {
        this.play_Highlight.Stop();
    }
    
    this.mutePush = function(sender, args)
    {
        if(this.muteIcon.Visibility == "Visible")
        {
            this.muteIcon.Visibility = "Collapsed";
            this.soundIcon.Visibility = "Visible";
            this.video.Volume = 0;
        }
        else
        {
            this.muteIcon.Visibility = "Visible";
            this.soundIcon.Visibility = "Collapsed";
            this.video.Volume = .5;
        }
    }
    this.muteOver = function(sender, args)
    {
        this.mute_Highlight.Begin();
    }
    this.muteOut = function(sender, args)
    {
        this.mute_Highlight.Stop();
    }
    
    this.fullScreenPush = function(sender, args)
    {
        
        if(this.fullScreenIcon.Visibility == "Visible")
        {
            this.fullScreenIcon.Visibility = "Collapsed";
            this.normalScreenIcon.Visibility = "Visible";
            this.control.content.FullScreen = true;
        }
        else
        {
            this.fullScreenIcon.Visibility = "Visible";
            this.normalScreenIcon.Visibility = "Collapsed";
            this.control.content.FullScreen = false;
        }
    }
    this.fullScreenOver = function(sender, args)
    {
        this.fullScreen_Highlight.Begin();
    }
    this.fullScreenOut = function(sender, args)
    {
        this.fullScreen_Highlight.Stop()
    }
    
    this.handlePress = function(sender, args)
    {
        if(this._parent.displayWindow.VideoLoaded)
        {
            this.handleGrab = true;
            sender.CaptureMouse();
            this.setPostion(args);
            clearInterval(this._parent.spinInterval)
        }
    }
    this.handleRelease = function(sender, args)
    {
        this.handleGrab = false;
        sender.ReleaseMouseCapture();
        this._parent.spinInterval = setInterval(preserveScope(this, this.track), 10);
        if(this.pauseIcon.Visibility == "Visible")
        {
            this.video.Play();
        }
    }
    this.handleMove = function(sender, args)
    {
        if(this.handleGrab)
        {
            this.setPostion(args);
        }
    }
    
    this.setPostion = function(eventArgs)
    {
        var myX = eventArgs.getPosition(this.trackBack).x-this.handle.Width/2
        if(myX >=0 && myX <= this.trackBack.Width-this.handle.Width)
        {
            this.handle["Canvas.Left"] = this.trackBack["Canvas.Left"]+myX;
            this.bar.Width = myX;
            var ratio = (this.handle["Canvas.Left"]-this.trackBack["Canvas.Left"])/(this.trackBack.Width-this.handle.Width);
            this.video.position = this.secondsToString(ratio*this.video.NaturalDuration.seconds);
        }
        if(myX <= 0)
        {
            this.video.position = this.secondsToString(0);
        }
        else if(myX >= this.trackBack.Width-this.handle.Width)
        {
            this.video.position = this.secondsToString(this.video.NaturalDuration.seconds);
        }
    }
    
    this.track = function()
    {
       var ratio =  this.video.Position.seconds/this.video.NaturalDuration.seconds;
       this.handle["Canvas.Left"] = this.trackBack["Canvas.Left"] + (this.trackBack.Width-this.handle.Width)*ratio;
       this.bar.Width = this.handle["Canvas.Left"]-this.trackBack["Canvas.Left"]+this.handle.Width/2;
    }
    
    this.secondsToString = function(seconds)
    {
        var intSeconds = Math.floor( seconds );
        var intMinutes = Math.floor( intSeconds / 60 );
        var intHours = Math.floor( intMinutes / 60 );
        var fracSecs = Math.round( (seconds - intSeconds) * 100 );
        intSeconds -= intMinutes * 60;
        intMinutes -= intHours * 60;
	
        return " " + intHours + ":" + intMinutes  + ":" + intSeconds + "." + fracSecs;
    }

    //EventHandlers
    this.playPauseBTN.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.playPush));
    this.playPauseBTN.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.playOver));
    this.playPauseBTN.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.playOut));
    
    this.muteBTN.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.mutePush));
    this.muteBTN.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.muteOver));
    this.muteBTN.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.muteOut));
    
    this.fullScreenBTN.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.fullScreenPush));
    this.fullScreenBTN.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.fullScreenOver));
    this.fullScreenBTN.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.fullScreenOut));
    
    this.handle.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handlePress));
    this.handle.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleRelease));
    this.handle.addEventListener("MouseMove", Silverlight.createDelegate(this, this.handleMove));

    
        

}

function fullScreenChange(sender, eventArgs)
{
    var host = sender.getHost();
    var myVideo = host.content.findName("video");
    var myBackDrop = host.content.findName("backDrop");
    var myControls = host.content.findName("controls");
    var myControlBackground = host.content.findName("controlBackground");
    var myFullScreenIcon = host.content.findName("fullScreenIcon");
    var myNormalScreenIcon = host.content.findName("normalScreenIcon");
    
    if(host.content.FullScreen)
    {   
        myVideo.Height = host.content.actualHeight;
        var myWidth = host.content.actualHeight*1.3333333;
        myVideo.Width = myWidth;
        if(host.content.actualWidth > myWidth)
        {
            myVideo["Canvas.Left"] = -190 +(host.content.actualWidth - myWidth)/2;
            myVideo["Canvas.Top"] = -42;
        }
        else
        {
            myVideo["Canvas.Left"] = -190 +(host.content.actualWidth - myWidth)/2;
            myVideo["Canvas.Top"] = -42;
        }
        myBackDrop.Width = host.content.actualWidth;
        myBackDrop.Height = host.content.actualHeight;
        myBackDrop.Visibility = "Visible";
        
        myControls["Canvas.Top"] = -42;
        myControls["Canvas.Left"] = -360+host.content.actualWidth/2;
        
        myControlBackground["Canvas.Top"] = -230;
        myControlBackground["Canvas.Left"] = -360+host.content.actualWidth/2;
        myControlBackground.Visibility = "Visible";
        
        
    }
    else
    {
        myBackDrop.Visibility = "Collapsed";
        myControlBackground.Visibility = "Collapsed";
        myVideo.Height = 192;
        myVideo.Width = 340;
        myVideo["Canvas.Left"] = 0;
        myVideo["Canvas.Top"] = 0;
        myControls["Canvas.Top"] = 192;
        myControls["Canvas.Left"] = 0;
        myFullScreenIcon.Visibility = "Visible";
        myNormalScreenIcon.Visibility = "Collapsed";
        
    }
}