/*
Copyright (c) 2009, straightline All rights reserved.
Code licensed under the GPL License
http://www.opensource.org/licenses/gpl-license.php
*/
var GridLayout = new Class({
    w: null,
    h: null,
    wrapperWidth: null,
    gridWidth: null,
    initialize: function(options) {
        var gridItem = $$('.side-item,.grid-item')[0];
        this.gridWidth = gridItem.getSize().x;
    
        this.resize();
        
        window.addEvent('resize', this.resize.bind(this));
    },
        
    resize: function() {
        this.w = window.getWidth();
        var colCount = Math.floor(this.w / this.gridWidth);
        if (colCount == 0) {
            colCount = 1;
        }
        var gridCols = $$('.grid-col');
        var gridItems = $$('.side-item,.grid-item');
        var rowCount = Math.ceil(gridItems.length / colCount);
        
        gridCols.each(function(element) {
            element.dispose();
        });
        
        for (var i = 0; i < colCount; i++) {
            var gridCol = new Element('div', {
                'id': 'grid-col-' + i,
                'class': 'grid-col'
            });
            $('grid').adopt(gridCol);
        }
        
        var gridColIndex = 0;
        gridItems.each(function(element, index) {
            gridColIndex = Math.floor(index / rowCount);
            $('grid-col-' + gridColIndex).adopt(element);
        });
        
        $('wrapper').set('styles', {
            width: this.gridWidth * (gridColIndex + 1) + 'px'
        });
        
    }
    
});
