﻿/// <reference path="../Default_html.js" />
/// <reference path="scripts/Page_xaml.js" />

function Splitter(page, rootElement)
{
    /// <summary>Create a Splitter</summary>
    /// <param name="page" type="MediaOverlayDesigner.Page">the page that contains the splitter </param>
    /// <param name="rootElement" type="Canvas">the root element of the scene</param>
    /// <return type="Splitter">a new Splitter object</return>

    this.Page = page;
    this.Line = rootElement.findName("Splitter");
    this.Top = rootElement.findName("MediaFrame");
    this.Bottom = rootElement.findName("Bottom");
    this.Dragging = false;
    this.Line.AddEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this._StartDrag));
    this.Line.AddEventListener("MouseMove", Silverlight.createDelegate(this, this._Drag));
    this.Line.AddEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this._EndDrag));
}

Splitter.prototype._StartDrag = function(sender, eventArgs)
{
    if (sender.captureMouse())
    {
        this.Offset = eventArgs.getPosition(sender).Y;
        this.Dragging = true;
    }
}

Splitter.prototype._Drag = function(sender, eventArgs)
{
    if (this.Dragging)
    {
        var position = eventArgs.getPosition(this.Bottom.GetParent()).Y - this.Offset;
        
        var host = sender.getHost();
        
        position = Math.max(0, position);
        
        position = Math.min(host.content.ActualHeight, position);
        
        this.Bottom["Canvas.Top"] = position;
        
        this.Resize();
    }
}

Splitter.prototype._EndDrag = function(sender, eventArgs)
{
    if (this.Dragging)
    {
        this.Dragging = false;
    }
}

Splitter.prototype.Resize = function()
{
    var host = this.Line.getHost();

    this.Bottom.Height = host.content.ActualHeight - this.Bottom["Canvas.Top"];
    
    this.Page.ResizePage(host.content.ActualWidth, host.content.ActualHeight);
    
    this.ResizeGraphic();
}

Splitter.prototype.ResizeGraphic = function()
{
    //this.Line.X2 = this.Line.getParent().Width;
}