var col = function(minWidth) {
	var that = this;
	that.spacing = 40;
	that.container = $("#container");
	
	that.calculateSize = function() {
	    that.count = Math.floor(that.container.width()/(minWidth+that.spacing));
	    that.colWidth = Math.floor((that.container.width()-((that.count-1)*that.spacing))/that.count);
	    that.colTopLeft = $(that.container).offset();
	    that.colHeight = [];
	    that.isFloat = [];
	    that.lastP = null;
	    that.lastClass = null;
	    for(var i=0;i<that.count;i++) {
	        that.colHeight[i] = 0;
	        that.isFloat[i] = false;
	    }
	}
	
	that.getMinCol = function() {
	    var minIndex = 0;
	    var min = 99999;
	    for(var i=0;i<that.count;i++)
	        if(that.colHeight[i] < min) {
	            min = that.colHeight[i];
	            minIndex = i;
	        }
	    return minIndex;
	}
	
	that.getBottom = function() {
	    var bottom = 0;
	    for(var i=0;i<that.count-1;i++)
	        if(that.colHeight[i] >bottom) {
	            bottom = that.colHeight[i];
	        }
	    return bottom;
	    
	}
	
	that.setBlock = function(el,p) {
	
	    // p.from
	    // p.cols
		// p.top

		el.width(that.colWidth*(p.cols) + that.spacing*(p.cols-1));
		
		var height = el.height();

		el.css({
		//	position:"absolute",
			left:that.colTopLeft.left + (that.colWidth+that.spacing)*p.from,
			top:that.colTopLeft.top + p.top
		});
		
		
		
		for(var i=p.from;i<p.from+p.cols;i++) {
		    that.colHeight[i] = p.top + height+50;
		    that.isFloat[i] = p.isFloat;
		}
		    
		that.lastClass=el.attr("className");
		that.lastP = p;
	}
	
	that.doLayout = function(container) {
	    if(!container) {
	    	that.calculateSize();
			var el = that.container.children("div");
		} else {
		    var el = container.children("div");
		}
		
	    el.each(function() {

			var e = $(this);
			switch(e.attr("className")) {
			
				case "layout-right":
					var col = that.count-1;
					that.setBlock(e,{from:col,cols:1,top:that.colHeight[col] })
				    break;
				    
				default:
				    var bottom = that.getBottom();
				    var b2 = that.colHeight[that.count-1];
				    var cols = that.count - (b2>bottom ? 1 : 0)
				    that.setBlock(e,{from:0,cols:cols,top:bottom })
				    break;
				    
				case "layout-string":
                    var p = that.lastP;
                    if(!p || that.lastClass!="layout-string"){
                        p = {from:0,top:that.getBottom(),cols:1}
                    } else {
                        p.from++;
	                    if(p.from>=that.count || p.top<that.colHeight[p.from]) {
	                        p.from=0;
							p.top = that.getBottom();
	                    }
                    }
                    

                    that.setBlock(e,p);
				    break;

				case "layout-tetris":
                    var col = that.getMinCol();
                    that.setBlock(e,{from:col,cols:1,top:that.colHeight[col] })
					break;
					
				case "layout-passby":
                    that.doLayout(e);
					break;
					
				case "layout-hide":
				    //e.css("display","none");
					break;
			}
		});
		
		if(!container) {
			h = 0;
			for(var i=0;i<that.count;i++)
			    if(that.colHeight[i] > h)
	                h = that.colHeight[i];
	        that.container.height(h);
        }
	}
	
	that.doLayout();
	$(window).resize(function(){that.doLayout();});
}
