﻿function ScrollBar(xaml, parent, container, horizontal)
{
    this.Parent = parent;
    this.Container =container;
    this.Horizontal =horizontal;
    this.MinButtonSize = 15;
    this.Shown = true;
    
    var content = parent.getHost().content;
    
    content.Root.addEventListener("LostFocus", Silverlight.createDelegate(this, this.LostFocus));   
    content.Root.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.LostFocus));
    
	this.Control = content.createFromXaml(xaml, true);
	
	this.Button = this.Control.findName("Button");
	
	this.Scrolling = false;
	
	this.Parent.children.Add(this.Control);
	
	this.Button.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.ScrollButtonDown));
	this.Button.addEventListener("MouseMove", Silverlight.createDelegate(this, this.ScrollButtonMove));
	this.Button.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.ScrollButtonUp));
	this.Width = this.Control.Width;
	this.Height = this.Control.Height;
}

ScrollBar.prototype.Show = function()
{
    if (this.Shown)
    {
        return;
    }
    
    this.Control.findName("Show").Begin();
    
    this.Shown = true;
}

ScrollBar.prototype.Hide = function()
{
    if (!this.Shown)
    {
        return;
    }
    
    this.Control.findName("Hide").Begin();
    
    this.Shown = false;
}


ScrollBar.prototype.Resize = function()
{
	if (this.Horizontal)
	{
	    this.Control["Canvas.Top"] = this.Parent.Height - this.Control.Height;
    	this.Control.Width = this.Parent.Width - this.Control.Height;
	}
	else
	{
	    this.Control["Canvas.Left"] = this.Parent.Width - this.Control.Width;
	    this.Control.Height = this.Parent.Height - this.Control.Width;
	}
}

ScrollBar.prototype.LostFocus = function(sender, eventArgs)
{
    if (this.Scrolling)
    {
        this.Button.ReleaseMouseCapture();
        
        this.Scrolling = false;
    }
}

ScrollBar.prototype.ScrollButtonDown = function(sender, eventArgs)
{
    if (sender.CaptureMouse())
    {
        this.Scrolling = true;
        
        this.MousePosition = eventArgs.getPosition(sender);
    }
}

ScrollBar.prototype.ScrollButtonMove = function(sender, eventArgs)
{
    if (this.Scrolling)
    {
        var position = 0;
        
        if (this.Horizontal)
        {
            position = eventArgs.getPosition(sender.getParent()).X - this.MousePosition.X;
        }
        else
        {
            position = eventArgs.getPosition(sender.getParent()).Y - this.MousePosition.Y;
        }
        
        position = Math.max(0, position);
        
        if (this.Horizontal)
        {
            position = Math.min(position, sender.getParent().Width - sender.Width);
            
            sender["Canvas.Left"] = position;
            
            var percentage = -position / (sender.getParent().Width);
           
            this.Container.ScrollToX(percentage);
        }
        else
        {
            position = Math.min(position, sender.getParent().Height - sender.Height);
            
            sender["Canvas.Top"] = position;
            
            var percentage = -position / (sender.getParent().Height);
           
            this.Container.ScrollToY(percentage);
        }
    }
}

ScrollBar.prototype.ScrollButtonUp = function(sender, eventArgs)
{
    if (this.Scrolling)
    {
        sender.ReleaseMouseCapture();
        
        this.Scrolling = false;
    }
}
