﻿if(!Sollers) var Sollers = {};
Sollers.Favorits = Class.create();

var FavoritsInstances = new Array();

if (window.addEventListener)
{
    window.addEventListener('resize', Favorits_WindowSizeChanged, false);
}
else
{
    window.attachEvent('onresize', Favorits_WindowSizeChanged);
}

function Favorits_WindowSizeChanged()
{
    var i;
    for(i=0;i<FavoritsInstances.length;i++) FavoritsInstances[i].WindowSizeChanged();
}

Sollers.Favorits.prototype =
{
    initialize : function(instanceName, containerId, minAlbumsPerRow, minAlbumsPerColumn, albumWidth, albumHeight, minHSpace, maxHSpace, minVSpace, maxVSpace, albumPanels)
    {
        var favorits = this;
        this.instanceName = instanceName;
        this.container = $(containerId);
        this.refContainer = this.container.parentNode.parentNode;
        this.containerFromRefLeft = this.container.offsetLeft;
        this.containerFromRefTop = this.container.offsetTop;
        var obj = this.container;
        while(obj.offsetParent != this.refContainer)
        {
            obj = obj.offsetParent;
            this.containerFromRefLeft += obj.offsetLeft;
            this.containerFromRefTop += obj.offsetTop;
        }
        this.refContainerLeft = this.refContainer.offsetLeft;
        this.refContainerTop = this.refContainer.offsetTop;
        obj = this.refContainer;
        while(obj.offsetParent)
        {
            obj = obj.offsetParent;
            this.refContainerLeft += obj.offsetLeft;
            this.refContainerTop += obj.offsetTop;
        }
        var firstUnderChild = null;
        var lastUnderChild = null;
        var i;
        var containerReached = false;
        for(i = 0; i < this.refContainer.childNodes.length; i++)
        {
            var child = this.refContainer.childNodes.item(i);
            if (child.tagName)
            {
                if (containerReached)
                {
                    if (!firstUnderChild) firstUnderChild = child;
                    lastUnderChild = child;
                }
                else
                {
                    if (child == this.container.parentNode)
                    {
                        containerReached = true;
                    }
                }
            }
        }
        var underContainerHeight = 0;
        if (firstUnderChild)
        {
            underContainerHeight = lastUnderChild.offsetTop + lastUnderChild.offsetHeight - firstUnderChild.offsetTop;
        }
        this.AvailHeight = this.refContainer.offsetHeight - (this.containerFromRefTop + underContainerHeight);
        this.minAlbumsPerRow = minAlbumsPerRow;
        this.minAlbumsPerColumn = minAlbumsPerColumn;
        this.minHSpace = minHSpace;
        this.maxHSpace = maxHSpace;
        this.minVSpace = minVSpace;
        this.maxVSpace = maxVSpace;
        this.albumPanels = new Array();
        for(i = 0; i < albumPanels.length; i++) this.albumPanels[i] = $(albumPanels[i]);
        this.albumWidth = albumWidth;
        this.albumHeight = albumHeight;
        this.GetWindowSize();
        this.containerWidthOffset = this.WndWidth - this.container.offsetWidth;
        this.PerformLayout();
        FavoritsInstances[FavoritsInstances.length] = favorits;
    },
    dispose : function()
    {
    },
    WindowSizeChanged : function()
    {
        this.GetWindowSize();
        this.PerformLayout();
    },
    GetWindowSize : function()
    {
        if (typeof(window.innerWidth) == 'number')
        {
            //Non-IE
            this.WndWidth = window.innerWidth;
            this.WndHeight = window.innerHeight;
        }
        else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
        {
            //IE 6+ in 'standards compliant mode'
            this.WndWidth = document.documentElement.clientWidth;
            this.WndHeight = document.documentElement.clientHeight;
        }
        else if (document.body && (document.body.clientWidth || document.body.clientHeight))
        {
            //IE 4 compatible
            this.WndWidth = document.body.clientWidth;
            this.WndHeight = document.body.clientHeight;
        }
        //alert('Width = ' + WndWidth + ' Height = ' + WndHeight);
    },
    PerformLayout : function()
    {
        //this.container.style.width = '';
        //var containerWidth = this.container.offsetWidth;
        //this.container.style.width = '300px';
        var initialWidth = this.WndWidth - this.containerWidthOffset;
        var initialHeight = this.AvailHeight;
        var nColumns = Math.floor((initialWidth + this.minHSpace)/(this.albumWidth + this.minHSpace));
        if (nColumns < this.minAlbumsPerRow) nColumns = this.minAlbumsPerRow;
        var hSpace = Math.floor((initialWidth - this.albumWidth * nColumns)/(nColumns - 1));
        if (hSpace < this.minHSpace) hSpace = this.minHSpace;
        if (hSpace > this.maxHSpace) hSpace = this.maxHSpace;
        var nRows = Math.floor((initialHeight + this.minVSpace)/(this.albumHeight + this.minVSpace));
        if (nRows < this.minAlbumsPerColumn) nRows = this.minAlbumsPerColumn;
        var vSpace = Math.floor((initialHeight - this.albumHeight * nRows)/(nRows - 1));
        if (vSpace < this.minVSpace) vSpace = this.minVSpace;
        if (vSpace > this.maxVSpace) vSpace = this.maxVSpace;
        var left = 0;
        var top = 0;
        var col = 0;
        var row = 0;
        for(i = 0; i < this.albumPanels.length; i++)
        {
            if (row >= nRows)
            {
                this.albumPanels[i].style.display = 'none';
            }
            else
            {
                this.albumPanels[i].style.left = this.refContainerLeft + this.containerFromRefLeft + left + 'px';
                this.albumPanels[i].style.top = this.refContainerTop + this.containerFromRefTop + top + 'px';
                this.albumPanels[i].style.display = '';
                col++;
                left += this.albumWidth + hSpace;
                if (col >= nColumns)
                {
                    col = 0;
                    left = 0;
                    row++;
                    top += this.albumHeight + vSpace;
                }
            }
        }
        this.container.style.width = (this.albumWidth * nColumns + hSpace * (nColumns - 1)) + 'px';
        this.container.style.height = (this.albumHeight * nRows + vSpace * (nRows - 1)) + 'px';
        //this.container.style.height = this.AvailHeight + 'px';
    }
}

