(function($){ 

	$.fn.autoflow = function() {

		var RIGHTPAD = 16;
		var TOPPAD = 290;
		var LEFTPAD = 16;
		var BOTPAD = 16;
		var BOXW = 312;
		var ENDPAD = 30;
		var MINCOLS = 1;
		
		var cols = 0;
		
		this.col_ar = [];
		
		this.init = function() {	
					
			var items = $("#content > *");
			this.pushed_ar = [];
			var found = false;
			cols = Math.floor(($(window).width() + 20 - LEFTPAD*2) / (BOXW+RIGHTPAD));
			for(i=cols;i>0;i--) {					
				if(i <= (cols-3)) {
					targetSelCol = i;
					break;
				}
			}
			
			/* LOOP THROUGH THE LIST. FIND THE NEW SPOT TO OPEN IN */
			for(i=0;i<items.length;i++) {
				var thisItem = items[i];
				var bottom = thisItem.offsetTop + thisItem.offsetHeight;
				var thisCol = ((thisItem.offsetLeft-30)/(BOXW+BOTPAD))+1;
				this.pushed_ar.push(thisItem.id);

			}
					
			this.col_ar = [];
			if(!cols || cols<MINCOLS) cols = MINCOLS;
			this.col_ar.push({x:0,y:0}); //the imprint
			for(var i=1; i<cols; i++) this.col_ar.push({x:i,y:0});
				
			this.draw();
		}
		
		this.draw = function()
		{
			var ar = this.pushed_ar;
			for(var o in ar)
			{			
				o = ar[o];
				this.col_ar.sort(this.ySort);
				var W = Math.round($("#"+o).outerWidth() / BOXW);		
					
				if(W>1) {	
					this.drawWide(o);
				} else {
					var c = this.col_ar[0];				
					this.drawItem(o,c.x,c.y);
				} 
			}		
		}
		
		this.drawWide = function(o)
		{
			var pc_ar = [];
			this.col_ar.sort(this.xSort);
			var W = Math.round($("#"+o).outerWidth() / BOXW);
			var uc = cols-W;
			/* HIDE STUFF THAT IS TOO BIG FOR THE SCREEN */
			if(uc<0){
				$("#"+o).css("display","none");
				return;
			}
			
			/* RESIZE THE CONTENT TO A SMALLER SIZE */
			if(uc<0) { uc=1; W = 2; }
			
			for(var i=0; i<=uc; i++){
				pc_ar.push(this.col_ar[i]);
				var my=0;
				var mi=null;
				for(var j=0; j<W; j++){
					var ty = this.col_ar[i+j].y;
					if(ty>=my){
						my=ty;
						mi=i+j;
					}
				}
				var td = my;
				for(var j=0; j<W; j++) td+=Math.abs(this.col_ar[mi].y - this.col_ar[i+j].y);
				pc_ar[i].d = td;
				pc_ar[i].od = pc_ar[i].y - my;
			}
			pc_ar.sort(this.dSort);
			var x = pc_ar[0].x;
			var y = pc_ar[0].y
			if(pc_ar[0].od<0) y-= pc_ar[0].od;
			this.col_ar.sort(this.ySort);
			this.drawItem(o,x,y);	
		}
				
		
		this.xSort = function(a,b){
			return(a.x-b.x);
		}
		
		this.ySort = function (a,b){
			if(a.y==b.y) return(a.x-b.x);
			else return(a.y-b.y);
		}
		
		this.dSort = function(a,b){
			if(a.d==b.d) return(a.x-b.x);
			else return(a.d-b.d);
		}
		
		this.drawItem = function(o,x,y)
		{
			var W = Math.round($("#"+o).outerWidth() / BOXW);
			var offsetX = ($(window).width() - (cols * (BOXW + RIGHTPAD) + LEFTPAD)) / 2 + 1;
			
			$("#"+o).css("left",x * (BOXW+RIGHTPAD) + LEFTPAD + offsetX + "px");
			$("#"+o).css("top",y + TOPPAD - this.getVerticalOffset() + "px");
			$("#"+o).css("position","absolute");
			$("#"+o).css("visibility","visible");
			$("#"+o).css("display","block");
			
			if(W>1){
				this.col_ar.sort(this.xSort);
				for(var i=0; i<W; i++) this.col_ar[x+i].y = y + $("#"+o).outerHeight() + BOTPAD;
			} else this.col_ar[0].y = y + $("#"+o).outerHeight() + BOTPAD;
		}
		
		this.findSameTop = function(myt,t) {
			if(myt == t) return true;
			else return false;
		}
		this.findProximity = function(myt,myb,b) {
			if(b >= myt && b <= myb) return true;
			else return false;
		}
		this.findClosest = function(p1,p2) {
			if(p1>=p2) return p1;
			else return p2;
		}
		this.findSameCol = function(t1,t2,c1,c2) {
			if(c1==c2) {
				if(t1==t2) return true;
				else return false;
			} else return false
		}
		this.getVerticalOffset = function() {
			if(cols==1) return 178;
			else if(cols==2) return 134;
			else if(cols==3) return 90;
			else if(cols==4) return 46;
			else return 0;
		}
	
		this.init();
	
	};
		
})(jQuery);



