// JavaScript Document

var target  ;
var MARGIN_LEFT =   1;
var margin_top  =   0;
var item_width  ;
var win_width   =   window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
var targetX;
var margin_bottom;
var margin_top=0;

function init(_target, _item_width, _targetX, _margin_top, _margin_bottom)
{
    this.target     = _target;
    this.item_width = _item_width;
    this.targetX = _targetX;
	
	if(_margin_bottom!=undefined) 
	{
		margin_bottom=_margin_bottom;
	}else{
		margin_bottom=0;
	}	
	if(_margin_top!=undefined) 
	{
		margin_top=_margin_top;
	}	
    window.onresize = projectLayout;   
    projectLayout();
}
function projectLayout()
{   
   
	win_width       = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
	win_width       = Math.max(win_width,800);
	var monMain     = document.getElementById(target);
	
	if(monMain)
	{
	    var target_width= win_width - targetX;
	    var children    = monMain.childNodes;
	    var posX        = MARGIN_LEFT;
	    var colNum = Math.floor((target_width - MARGIN_LEFT) / item_width);	   
	    var items = new Array();
		for (var j=0; j < children.length ; j++)
	    {
			if(children[j].nodeType==1)
		    {
	       		 items.push (children[j]);
				 children[j].style.display= "none";
			}
	    }
		var itemPerCol = Math.ceil(items.length/colNum);
		var maxHeight=0;
		
		var count=0;
			for(var colCount=0;colCount<colNum;colCount++)
			{
				for(var l=0;l<itemPerCol;l++)
				{
					if(items[count])
					{
						items[count].style.position = "absolute";
						items[count].style.left = colCount*item_width+"px";
						items[count].style.top = margin_top+"px";
						if(l==0)
						{
							
							items[count].style.top = margin_top+"px";
						}else{
							var posY=Number(items[count-1].style.top.substr(0,items[count-1].style.top.length-2))+items[count-1].clientHeight+margin_bottom;
							maxHeight=Math.max(maxHeight,posY+items[count-1].clientHeight+margin_bottom);
							posY=posY+"px";
							
							//alert(posY);
							items[count].style.top = posY;
							//alert(items[count-1].style.top.substr(0,items[count-1].style.top.length-2)+"      "+items[count-1].style.top.length-2);
							
						}
						items[count].style.display = "block";
						count++;
					}
				}
			}
			monMain.style.height=(maxHeight+30)+"px";
			var myHeader=document.getElementById("headerTagRequest");
			if(myHeader)
			myHeader.style.width=win_width-targetX-20+"px";
		}
}