/// <reference path="Support.js" />

///-codeFile-//////////////////////////////////////////////////////////////////
// FruityDock.js
// Written by Doug Greenall (dg at outerin dot com)
///////////////////////////////////////////////////////////////////////////////


///-class-/////////////////////////////////////////////////////////////////////
// FruityDockItem
///////////////////////////////////////////////////////////////////////////////
function FruityDockItem(Parent, Title, HREF, ImageSRC, Size)
{
    ///-data-//////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////

    this._parent = Parent;
    this._ID = this._parent._ID + 'Item' + this._parent._items.length;
    this._index = this._parent._items.length;

    this._title = Title;
    this._href = HREF;
    this._element = null;

    this._size = Size;
    this._targetSize = this._size.Clone();
    this._sizeIncrease = new Vector2D(0, 0);

    this._image = new Image();
    this._image.src = ImageSRC;


    ///-methods-///////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////

    this.GetElement = function()
    {
        if (!this._element)
            this._element = wGet(this._ID);

        return this._element;
    };

    this.GetMarkup = function()
    {
        return '<a href="' + this._href + '" onmousemove="javascript:' + this._parent._ID + '.MouseMove(event, ' + this._index + ');">\n' +
               '    <img id="' + this._ID + '" style="position: relative; left: 0px; bottom: 0px; border: none;" src="' + this._image.src + '" width="' + this._size.GetXInt() + '" height="' + this._size.GetYInt() + '" alt="" title="" /></a>\n';
    };

    this.SetTargetSize = function(Vector)
    {
        this._targetSize = Vector;
        this._sizeIncrease = this._targetSize.Clone().SubVec(this._size).DivVal(5);
    };

    this.Resize = function()
    {
        this._size.AddVec(this._sizeIncrease);

        if (this._sizeIncrease._x >= 0 && this._size._x >= this._targetSize._x ||
            this._sizeIncrease._x < 0 && this._size._x <= this._targetSize._x)
            this._size._x = this._targetSize._x;

        if (this._sizeIncrease._y >= 0 && this._size._y >= this._targetSize._y ||
            this._sizeIncrease._y < 0 && this._size._y <= this._targetSize._y)
            this._size._y = this._targetSize._y;

        if (this.GetElement())
        {
            this._element.width = this._size.GetXInt();
            this._element.height = this._size.GetYInt();
        }
    };

    this.TargetReached = function()
    {
        return this._size.Equals(this._targetSize);
    };
}


///-class-/////////////////////////////////////////////////////////////////////
// FruityDock
///////////////////////////////////////////////////////////////////////////////
function FruityDock(ID, LayerID, BaseWidth, BaseHeight, TargetWidth, TargetHeight)
{
    ///-data-//////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////

    this._ID = ID;
    this._layerID = LayerID;
    this._interval = '';
    this._items = new Array();
    this._baseSize = new Vector2D(BaseWidth, BaseHeight);
    this._increaseSize = new Vector2D(TargetWidth - BaseWidth, TargetHeight - BaseHeight);
    this._mouseOut = false;
    this._titleLayer = null;


    ///-methods-///////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////

    this.GetTitleLayer = function()
    {
        if (!this._titleLayer)
            this._titleLayer = wGet(this._layerID + 'Title');

        return this._titleLayer;
    };

    this.AddItem = function(Title, HREF, ImageSRC)
    {
        this._items[this._items.length] = new FruityDockItem(this, Title, HREF, ImageSRC, this._baseSize.Clone());
    };

    this.DrawItems = function()
    {
        var layer = wGet(this._layerID);
        if (layer)
        {
            var height = this._baseSize._y + this._increaseSize._y + 10;
            var markup = '<img style="position: relative; left: 0px; bottom: 0px; border: none;" src="images/trans.gif" width="1" height="' + height + 'px" alt="" title="" />\n';

            for (var count = 0; count < this._items.length; ++count)
                markup += this._items[count].GetMarkup();

            markup += '<img style="position: relative; left: 0px; bottom: 0px; border: none;" src="images/trans.gif" width="1" height="' + height + 'px" alt="" title="" />\n';

            layer.innerHTML = markup;
        }
    };

    this.Animate = function()
    {
        var stop = true;

        for (var count = 0; count < this._items.length; ++count)
        {
            this._items[count].Resize();
            stop = stop && this._items[count].TargetReached();
        }

        if (stop)
            this.Stop();
    };

    this.Start = function()
    {
        if (this._interval == '')
            this._interval = setInterval(this._ID + '.Animate()', 10);
    };

    this.Stop = function()
    {
        if (this._interval != '')
            clearInterval(this._interval);

        this._interval = '';
    };

    this.MouseMove = function(Event, Index)
    {
        this._mouseOut = false;

        if (!Event)
            Event = window.event;

        var itemX = 0;
        if (Event.offsetX)
            itemX = Event.offsetX;
        else
            itemX = Event.layerX;

        if (!isNaN(itemX) && itemX <= this._baseSize._x + this._increaseSize._x)
        {
            var itemWidth = (this._baseSize._x + this._increaseSize._x) / 2;
            itemX -= itemWidth;
            itemX /= itemWidth;
            var vecMod = this._increaseSize.Clone().MulVal(0.2 * itemX);

            for (var count = 0; count < this._items.length; ++count)
                if (count == Index)
                {
                    this._items[count].SetTargetSize(this._baseSize.Clone().AddVec(this._increaseSize));

                    if (this.GetTitleLayer())
                        this._titleLayer.innerHTML = this._items[count]._title;
                }
                else if (count == Index - 1)
                    this._items[count].SetTargetSize(this._baseSize.Clone().AddVec(this._increaseSize.Clone().MulVal(0.7).SubVec(vecMod)));
                else if (count == Index + 1)
                    this._items[count].SetTargetSize(this._baseSize.Clone().AddVec(this._increaseSize.Clone().MulVal(0.7).AddVec(vecMod)));
                else if (count == Index - 2)
                    this._items[count].SetTargetSize(this._baseSize.Clone().AddVec(this._increaseSize.Clone().MulVal(0.3).SubVec(vecMod)));
                else if (count == Index + 2)
                    this._items[count].SetTargetSize(this._baseSize.Clone().AddVec(this._increaseSize.Clone().MulVal(0.3).AddVec(vecMod)));
                else
                    this._items[count].SetTargetSize(this._baseSize.Clone());

            this.Start();
        }
    };

    this.MouseOut = function(Run)
    {
        if (!Run)
        {
            this._mouseOut = true;
            setTimeout(this._ID + '.MouseOut(true)', 1000);
            return;
        }

        if (this._mouseOut)
        {
            if (this.GetTitleLayer())
                this._titleLayer.innerHTML = '- select a team member -';

            for (var count = 0; count < this._items.length; ++count)
                this._items[count].SetTargetSize(this._baseSize.Clone());

            this.Start();
        }
    };
}
