/**
 * Grid-A-Licious(tm)
 * Copyright (c) 2008 Suprb - info(at)suprb(dot)com
 *
 * License Agreement: By downloading Grid-A-Licious(tm),
 * you agree to the following: The copyright information 
 * must remain intact in the product.
 *
 * The product may be used for personal use only, no 
 * commercial projects. You are not free to remove the 
 * copyright information (anywhere).
 * 
 * You are not free to use or copy any of the 
 * "grid-a-licious.js" (this file) code on your own products
 * without asking for permission.
 * 
 * Thanks for understanding.
 */

    var MIN_COLS = 2;
    var COL_WIDTH = 270;
    var columns = 2;
    var maxHeight = 0;
    
    var offx, offy = 0;
    maxy = new Array();
    
    /*on site load (DOM READY)
    $(function() { 
        offy = 0;
        offx = 0;
        arrange(); 
    });
    */
    
    // on site load (WHOLE PAGE READY)
    $(window).load(function() { 
        offy = 0;
        offx = 0;

        arrange(); 
    });
    
        // -150 for left margin of posts div
     $(window).resize( function() { 
             if(columns != Math.max(MIN_COLS, parseInt(($(window).width()-150) / (COL_WIDTH))))
                 arrange();
         } );
    
    
    function arrange() {
        // how many columns fits here?
        // columns = Math.max(MIN_COLS, parseInt(($(window).width()-150) / (COL_WIDTH)));
        
        //we set max 3 columns
        if(parseInt(($(window).width()-150) / (COL_WIDTH)) >= 3){
            columns = 3;
        }
        else{
            columns = MIN_COLS;
        }
        
        var count = 0;

        $('.post').css('width',COL_WIDTH  + 'px');

        for (x=0; x < columns; x++) {
            maxy[x] = 0;
        }

        //$('#contact').css('margin-left', (columns-2)*300);

        $('#menu').css('width', (columns-1)*COL_WIDTH);
        
        // lets iterate over all posts
        $('.post').each(function(i) {
            count ++;

            var pos, cursor;
    
            cursor = 0;


                for (x=0; x < columns; x++) {
                    cursor = maxy[x] < maxy[cursor] ? x : cursor;
                }

                //var individualTextWidth = randomBetween(150,280);
                var individualPostMargin = 60;
                //var individualTextMargin = 10;
                //var individualImageMargin = random(20) - 10;
                //console.log(individualImageMargin);

                //console.log(individualTextWidth + '   ' + individualTextMargin + '   ' + individualPostMargin);

                $(this).css({
                    'position':        'absolute',
                    'margin-left':    cursor*(COL_WIDTH) + offx,
                    'margin-top':    maxy[cursor] + offy + individualPostMargin
                });
                //$(this).children('.post-intro').css({
                    //'width' : individualTextWidth,
                //    'margin-bottom' : individualTextMargin
                //});
                /*
                $(this).children('img').css({
                    'margin-left' : individualImageMargin
                });
                */

                /*if(count <= columns){
                    $(this).css('margin-top',    maxy[cursor] + offy + $('#menu').outerHeight()+ 15);
                    maxy[cursor] += $(this).outerHeight() + $('#menu').outerHeight()+ 15;
                }
                */
                /*
                else if (count == columns){
                    $(this).css('margin-top',    maxy[cursor] + offy + $('#contact').outerHeight()+ individualPostMargin);
                    maxy[cursor] += $(this).outerHeight() + $('#menu').outerHeight()+ individualPostMargin;
                }
                */
                //else
                    maxy[cursor] += $(this).outerHeight() + individualPostMargin;
                    if(maxy[cursor] > maxHeight){
                        maxHeight = maxy[cursor];
                    }
            
        });
        $('#posts').css({'height': maxHeight});
        $('#posts').width(columns*COL_WIDTH);
		$('#curtain').height($(document).height());
      	$('#curtain').fadeOut('slow');
        
    }

    function random(X) {
        return Math.floor(X * (Math.random() % 1));
    }


    function randomBetween(MinV, MaxV) {
      return MinV + random(MaxV - MinV + 1);
    }

