﻿/// <reference path="../Default_html.js" />
/// <reference path="Page_xaml.js" />
/// <reference path="Scrollbar.js" />

function Timeline(page, rootElement)
{
    this.Scale = 50;
    this.Interval= 5;
    this.IntervalsAdded = false;
    this.Scrolling = false;
    this.Page = page;
    this.Timeline = rootElement.findName("Timeline");
    this.Shuttle = rootElement.findName("Shuttle");
    this.Media = rootElement.findName("Media");
    this.DownloadProgress = rootElement.findName("DownloadProgress");
    this.ScrollContainer = rootElement.findName("ScrollContainer");
    
    this.ShuttleLine = rootElement.findName("ShuttleLine");
    this.ShuttleLineShadow = rootElement.findName("ShuttleLineShadow");
    this.TimelineBars = rootElement.findName("TimelineBars");
    this.Markers = rootElement.findName("Markers");
    this.Intervals = rootElement.findName("Intervals");
    //this.VerticalScrollBarShadow = rootElement.findName("TimelineVerticalScrollBarShadow");
    this.MovingShuttle = false;
    this.WasPlaying = false;
    //this.VerticalScroll.Visibility = "Collapsed";
    //this.TimelineVerticalScrollButtonScale = rootElement.findName("TimelineVerticalScrollButtonScale");
	this.Shuttle.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.ShuttleDown));
	this.Shuttle.addEventListener("MouseMove", Silverlight.createDelegate(this, this.ShuttleMove));
	this.Shuttle.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.ShuttleUp));
	this.ScrollContainerClip = rootElement.findName("ScrollContainerClip");
	
	this.Media.addEventListener("MediaOpened", Silverlight.createDelegate(this, this.MediaOpened));
}


Timeline.prototype.Initialize = function()
{
    if (this.Media.NaturalDuration.seconds > 0)
    {
        this.Scale = this.Timeline.Width / this.Media.NaturalDuration.seconds;
    }
    
	var xaml = this.Page.XamlFiles["xaml/ScrollBar.xaml"];

    this.HorizontalScrollBar = new ScrollBar(xaml, this.Timeline, this, true);
    this.VerticalScrollBar = new ScrollBar(xaml, this.Timeline, this, false);
	
	this.AddIntervals();
}

Timeline.prototype.Add = function(timeBar)
{
    timeBar.Timeline = this;
    
    timeBar.Span["Canvas.Left"] = this.Media.Position.seconds * this.Scale;
    
    var top = this.TimelineBars.children.count * timeBar.Shape.Height;
    
    timeBar.Shape.SetValue("Canvas.Top", top);
    
    this.TimelineBars.children.Add(timeBar.Shape);
    this.TimelineBars.Height = top + timeBar.Shape.Height;
    
    this.UpdateScrollBars();
}

Timeline.prototype.MediaOpened = function(sender, eventArgs)
{
    var duration = sender.findName("Duration");
    
    if (this.Media.NaturalDuration.seconds > 0)
    {
        duration.Text = formatTime(this.Media.NaturalDuration.seconds);
        this.Scale = this.Timeline.Width / this.Media.NaturalDuration.seconds;
        this.DownloadProgress.Width = this.Media.NaturalDuration.seconds * this.Scale;
        this.AddIntervals();
        this.SetShuttlePosition(0);
    }
    else
    {
        duration.Text = "Streaming...";
    }
}

Timeline.prototype.AddIntervals = function()
{
    var xaml = this.Page.XamlFiles["xaml/TimeInterval.xaml"];
    
    if (xaml == null)
    {
        return;
    }
    
    var totalLength = this.Media.NaturalDuration.seconds;
    
    this.Intervals.children.Clear();
    
    for (var i = 0; i < totalLength; i+= this.Interval)
    {
        var interval = this.Page.content.createFromXaml(xaml, true);
        
        var width = interval.Width;
        
        this.Interval = Math.ceil((this.GetWidth() / (this.GetWidth() / width)) / this.Scale);
        
        var time = interval.findName("Time");
        
        time.Text = formatTime(i);
        
        interval["Canvas.Left"] = i * this.Scale;
        
        this.Intervals.children.Add(interval);
    }
}

Timeline.prototype.Remove = function(bar)
{
    this.TimelineBars.children.Remove(bar.Shape);
}

Timeline.prototype.UpdateScrollBars = function()
{
    if (!this.VerticalScrollBar)
    {
        return;
    }
    
    if (this.TimelineBars.Height < this.Timeline.Height)
    {
        this.VerticalScrollBar.Hide();
        
        this.ScrollContainer["Canvas.Top"] = 0;
        this.VerticalScrollBar.Button["Canvas.Top"] = 0;
    }
    else
    {
        
        this.VerticalScrollBar.Show();
        
        if (this.TimelineBars.Height > 0)
        {
            this.VerticalScrollBar.Button.Height = Math.max(this.VerticalScrollBar.MinButtonSize, this.Timeline.Height * this.Timeline.Height / this.TimelineBars.Height);
        }
    }
    
    if (this.GetWidth() >= this.Timeline.Width)
    {
        this.HorizontalScrollBar.Show();
    }
    else
    {
        this.HorizontalScrollBar.Hide();
        
        this.ScrollContainer["Canvas.Left"] = 0;
        this.HorizontalScrollBar.Button["Canvas.Left"] = 0;
    }
}

Timeline.prototype.Resize = function()
{
    this.Timeline.Width = this.Timeline.getParent().Width - Margin * 2;
    
    this.Timeline.Height = this.Timeline.getParent().Height - this.Timeline["Canvas.Top"] - Margin;
    this.Markers.Width = this.Timeline.Width;
    this.TimelineBars.Width = this.Timeline.Width;

    //this.TimelineBars.Height = this.Timeline.Height;
    if (this.Media.NaturalDuration.seconds > 0)
    {
        this.DownloadProgress.Width = this.Media.NaturalDuration.seconds * this.Scale;
    }
    this.DownloadProgress.Height = this.Timeline.Height;
    this.ShuttleLine.Height = this.Timeline.Height;
    this.ShuttleLineShadow.Height = this.ShuttleLine.Height;
    var intervalsClip = this.Page.content.findName("IntervalsClip");
    intervalsClip.Rect = "0,0," + this.Timeline.Width + "," + this.Timeline.Height;
    this.ScrollContainerClip.Rect = "0,0," + this.Timeline.Width + "," + this.Timeline.Height;

    if (this.VerticalScrollBar && this.HorizontalScrollBar)
    {
        this.VerticalScrollBar.Resize();
        this.HorizontalScrollBar.Resize();
    }    

    if (this.HorizontalScrollBar)
    {
        this.Intervals.Height = this.Timeline.Height;
        for (var i = 0; i < this.Intervals.children.Count; i++)
        {
            var interval = this.Intervals.children.getItem(i);
            
            interval.findName("Line").Y2 = this.Intervals.Height;
        }
    }
    
    this.UpdateScrollBars();
}

Timeline.prototype.SetShuttlePosition = function(percentage)
{
    this.Shuttle["Canvas.Left"] = percentage * this.Scale * this.Media.NaturalDuration.Seconds;

}

/*

Timeline.prototype.ScrollButtonDown = function(sender, eventArgs)
{
    if (sender.CaptureMouse())
    {
        this.Scrolling = true;
        
        this.MousePosition = eventArgs.getPosition(sender).Y;
    }
}

Timeline.prototype.ScrollButtonMove = function(sender, eventArgs)
{
    if (this.Scrolling)
    {
        var position = eventArgs.getPosition(sender.getParent()).Y - this.MousePosition;
        
        position = Math.max(0, position);
        position = Math.min(position, sender.getParent().Height - sender.Height);
        
        sender["Canvas.Top"] = position;
        
        var percentage = -position / (sender.getParent().Height);
       
        this.TimelineBars["Canvas.Top"] = percentage * this.TimelineBars.Height;
    }
}
Timeline.prototype.ScrollButtonUp = function(sender, eventArgs)
{
    if (this.Scrolling)
    {
        sender.ReleaseMouseCapture();
        
        this.Scrolling = false;
    }
}
*/

Timeline.prototype.ShuttleDown = function(sender, eventArgs)
{
    if (sender.CaptureMouse())
    {
        this.MovingShuttle = true;
        
        if (this.Media.CurrentState == "Playing")
        {
            this.WasPlaying = true;
            
            this.Page.OnPause(sender);
        }
        else
        {
            var animation = sender.findName("HideOverlay");
            
            animation.Begin();
            
            this.WasPlaying = false;
        }
        
        this.Relative = eventArgs.getPosition(sender).X;
    }
}

Timeline.prototype.GetWidth = function()
{
    return this.Media.NaturalDuration.seconds * this.Scale;
}

Timeline.prototype.ShuttleMove = function(sender, eventArgs)
{
    if (this.MovingShuttle)
    {
        var position = eventArgs.getPosition(this.Timeline).X - this.Relative;
        
        position = Math.max(0, position);
        position = Math.min(this.GetWidth(), position);
        
        sender["Canvas.Left"] = position;
        
        if (this.Media.CanSeek)
        {
            var percentage = position / (this.Media.NaturalDuration.seconds * this.Scale);
            
            var timePosition = this.Media.NaturalDuration.seconds * percentage;
            
            var timePosString = formatTime(timePosition);
            this.Media.Position = timePosString;
            
            this.Page.CurrentTime.Text = formatTime(this.Media.Position.seconds);
            
            var animation = sender.findName("MediaAnimation");
            
            if (animation)
            {
                animation.Seek(timePosString);
            }            
        }
    }
}


Timeline.prototype.ShuttleUp = function(sender, eventArgs)
{
    if (this.MovingShuttle)
    {
        sender.ReleaseMouseCapture();
        
        this.MovingShuttle = false;
        
        if (this.WasPlaying)
        {
            this.Page.OnPlay(sender);
        }
        else
        {
            var animation = sender.findName("ShowOverlay");
            
            animation.Begin();
        }
        
        this.Page.UpdateTool();
    }
}

Timeline.prototype.ScrollToX = function(x)
{
    this.ScrollContainer["Canvas.Left"] = x * this.Timeline.Width;
    this.Intervals["Canvas.Left"] = x * this.Timeline.Width;
}

Timeline.prototype.ScrollToY = function(y)
{
    this.ScrollContainer["Canvas.Top"] = y * this.Timeline.Height;
}
