﻿/// <reference path="TimeBar.js" />
/// <reference path="Timeline.js" />

function Overlay(name, shape, timeLine)
{
    /// <summary>Create an overlay </summary>
    /// <param name="name" type="String">the name of the overlay</param>
    /// <param name="shape" type="object">the shape</param>
    /// <param name="timeLine" type="Timeline">a timeline</param>
    /// <return type="Overlay">a new Overlay object</return>
    this.FadeIn = false;
    this.FadeOut = false;
    this.TimeBar = null;
    this.KeyFrames = new Array();
    this.Name = name;
    this.Shape = shape;
    this.Timeline = timeLine;
    var hyperlink = GetHyperlink();
    
    this.Hyperlink = hyperlink.value;
    
    if (!this.Shape.ToolName)
    {
        throw new Error(this.Shape.toString() + " must have a ToolName property.");
    }
}

Overlay.prototype.Delete = function()
{
    ///<summary>Delete the overlay and deactivate the tool</summary>
    var shape = Page.content.findName(this.Name);
    
    shape.getParent().children.Remove(shape);
    
    this.Timeline.Remove(this.TimeBar);
    
    if (Page.Tool)
    {
        Page.Tool.Deactivate();
        
        Page.Tool = null;
    }
}

Overlay.prototype.AddKeyFrame = function(page)
{
    var keyFrame = this.Shape.CreateKeyFrame(page);
    
    if (this.TimeBar == null)
    {
        this.TimeBar = new TimeBar(page.control, this, page.XamlFiles["Marker.xaml"], page.XamlFiles["TimeBar.xaml"]);
        
        this.Timeline.Add(this.TimeBar);
    }
    //page.SelectedOverlay.Add(keyFrame);
    
    if (keyFrame)
    {
        this.KeyFrames.push(keyFrame);
        
        this.TimeBar.Add(keyFrame);
    }
}

Overlay.prototype.Update = function()
{
    ///<summary>Update the tool to reflect the overlay shape
    
    var shape = Page.content.findName(this.Name);
    
    //var tool = 

}

Overlay.prototype.GetXaml = function()
{
    return this.Shape.GetShapeXaml(this.Name, this.KeyFrames, this.Hyperlink);
}

Overlay.prototype.GetAnimationXaml = function()
{
    return this.Shape.GetAnimationXaml(this);
}

Overlay.prototype.Select = function(control)
{
    var media = control.content.findName("Media");
    
    var seconds = media.Position.Seconds;
    
    var nearestKeyFrame = null;
    var nearestDistance = Number.MAX_VALUE;
    
    for (var i = 0; i < this.KeyFrames.length; i++)
    {
        var distance = Math.abs(seconds - this.KeyFrames[i].TimeCode);
        
        if (distance < nearestDistance)
        {
            nearestKeyFrame = this.KeyFrames[i];
            nearestDistance = distance;    
        }
    }
    
    if (nearestKeyFrame)
    {
        media.Position = formatTime(nearestKeyFrame.TimeCode);
        Page.UpdateTime();
    }    
    var button = control.content.findName(this.Shape.ToolName);
    
    Page.OnSelectTool(button);
    
    Page.SelectedOverlay = this;
    
    var hyperlink = GetHyperlink();
    
    hyperlink.value = this.Hyperlink;
    
    try
    {
        Page.Tool.Select(nearestKeyFrame);
    }
    catch (error)
    {
        if (error.name == "TypeError" && error.number == -2146827850)
        {
            alert(Page.Tool.toString() + ".prototype.Select=function(keyFrame){} must be implemented.");
        }
        else
        {
            throw error;
        }
    }
}

Overlay.prototype.Deselect = function()
{
    var hyperlink = GetHyperlink();
    this.Hyperlink = hyperlink.value;
    hyperlink.value = "";
}