﻿var SilverlightUI = {};

SilverlightUI.Anchor = function() {};

SilverlightUI.Anchor.prototype.AnchoredControls = new Array();
SilverlightUI.Anchor.prototype.AnchorLeftAry = new Object();
SilverlightUI.Anchor.prototype.AnchorRightAry = new Object();
SilverlightUI.Anchor.prototype.AnchorTopAry = new Object();
SilverlightUI.Anchor.prototype.AnchorBottomAry = new Object();
SilverlightUI.Anchor.prototype.Width = new Object();
SilverlightUI.Anchor.prototype.Height = new Object();
SilverlightUI.Anchor.prototype.AnchorLeftOffsetAry = new Object();
SilverlightUI.Anchor.prototype.AnchorRightOffsetAry = new Object();
SilverlightUI.Anchor.prototype.AnchorTopOffsetAry = new Object();
SilverlightUI.Anchor.prototype.AnchorBottomOffsetAry = new Object();

SilverlightUI.Anchor.prototype.GetParentWidth = function(ctl)
{
    if (ctl.toString() == "Canvas" && ctl.GetParent() == null)
    {
        return ctl.GetHost().content.actualWidth;
    }
    else
    {
        return ctl.GetParent().Width;
    }
}

SilverlightUI.Anchor.prototype.GetParentHeight = function(ctl)
{
    if (ctl.toString() == "Canvas" && ctl.GetParent() == null)
    {
        return ctl.GetHost().content.actualHeight;
        //return ctl.Height;
    }
    else
    {
        return ctl.GetParent().Height;
    }
}

SilverlightUI.Anchor.prototype.AddAnchor = function(ctl, AnchorLeft, AnchorRight, AnchorTop, AnchorBottom)
{
    var ParentWidth, ParentHeight;
    ParentWidth = this.GetParentWidth(ctl);
    ParentHeight = this.GetParentHeight(ctl);
    this.AnchoredControls.push(ctl.Name);
    this.AnchorLeftAry[ctl.Name] = AnchorLeft;
    this.AnchorRightAry[ctl.Name] = AnchorRight;
    this.AnchorTopAry[ctl.Name] = AnchorTop;
    this.AnchorBottomAry[ctl.Name] = AnchorBottom;
    this.Width[ctl.Name] = ctl.Width;
    this.Height[ctl.Name] = ctl.Height;
    this.AnchorLeftOffsetAry[ctl.Name] = ctl["Canvas.Left"];
    this.AnchorRightOffsetAry[ctl.Name] = ParentWidth - (ctl["Canvas.Left"] + ctl.Width);
    this.AnchorTopOffsetAry[ctl.Name] = ctl["Canvas.Top"];
    this.AnchorBottomOffsetAry[ctl.Name] = ParentHeight - (ctl["Canvas.Top"] + ctl.Height);
}

SilverlightUI.Anchor.prototype.RemoveAnchor = function(ctl)
{
    if (this.AnchoredControls.indexOf(ctl.Name) > -1)
    {
        this.AnchoredControls.splice(this.AnchoredControls.indexOf(ctl), 1);
    }
}

SilverlightUI.Anchor.prototype.AdjustPosition = function(ctl)
{
    if (this.AnchoredControls.indexOf(ctl.Name) > -1)
    {
    var ParentWidth, ParentHeight;
    ParentWidth = this.GetParentWidth(ctl);
    ParentHeight = this.GetParentHeight(ctl);

        if (this.AnchorLeftAry[ctl.Name] == true)
        {
            //if left anchor is true, anchor it to the left
            ctl["Canvas.Left"] = this.AnchorLeftOffsetAry[ctl.Name];
            //if left and right are both anchored, stretch the control
            if (this.AnchorRightAry[ctl.Name] == true)
            {
                ctl.Width = ParentWidth - ctl["Canvas.Left"] - this.AnchorRightOffsetAry[ctl.Name];
            }
        }
        else if (this.AnchorRightAry[ctl.Name] == true)
        {
            //anchoring to the right, but not to the left
            ctl.Width = this.Width[ctl.Name];
            ctl["Canvas.Left"] = ParentWidth - this.AnchorRightOffsetAry[ctl.Name] - this.Width[ctl.Name];
        }
        else
        {
            //no anchor
            ctl.Width = this.Width[ctl.Name];
        }



        if (this.AnchorTopAry[ctl.Name] == true)
        {
            //if Top anchor is true, anchor it to the Top
            ctl["Canvas.Top"] = this.AnchorTopOffsetAry[ctl.Name];
            //if Top and Bottom are both anchored, stretch the control
            if (this.AnchorBottomAry[ctl.Name] == true)
            {
                ctl.Height = ParentHeight - ctl["Canvas.Top"] - this.AnchorBottomOffsetAry[ctl.Name];
            }
        }
        else if (this.AnchorBottomAry[ctl.Name] == true)
        {
            //anchoring to the Bottom, but not to the Top
            ctl.Height = this.Height[ctl.Name];
            ctl["Canvas.Top"] = ParentHeight - this.AnchorBottomOffsetAry[ctl.Name] - this.Height[ctl.Name];
        }
        else
        {
            //no anchor
            ctl.Height = this.Height[ctl.Name];
        }
    }
}

SilverlightUI.Anchor.prototype.AdjustAllPositions = function(canvas)
{
    if (canvas.GetParent() != null)
    {
        this.AdjustPosition(canvas);
        }
    for (var i = 0; i < canvas.Children.count; i++)
    {
        var child = canvas.Children.GetItem(i);
        if (child.toString() == "Canvas")
            this.AdjustAllPositions(child);
        else
            this.AdjustPosition(child);
    }
}

