var g_count=4; // дефолтное кол-во колонок, генерируемое на сервере

function init(){
	g_result=document.getElementById("shop-tbl");
	if(g_result){
		g_tds=g_result.getElementsByTagName("td");
		window.onresize=resize;
		resize();
	}
}
// Считаем кол-во колонок, которое помещается в экран
function CountColumns(){
	var c=Math.floor(document.documentElement.offsetWidth/(300));
	return c>0?c:1;
}
function resize(){
	var cols=CountColumns();// кол-во столбцов в таблице
	if(g_count==cols){return;}//ничего не делать, если кол-во колонок то же, что и ранее
	g_count=cols; 
	var table=document.createElement("table");
	var tbody=document.createElement("tbody");
	var rows=g_tds.length/cols; // считаем кол-во строк в таблице
	var width=100/cols; // ширина каждой ячейки в процентах
	for(var j=0;j<rows;j++){
		var row1=document.createElement("tr");
		for(var i=0;i<cols;i++){
			if(!g_tds[0]){break;}
			g_tds[0].style.width=width+"%";
			row1.appendChild(g_tds[0]);
		}
		tbody.appendChild(row1);
	}
	table.appendChild(tbody);
	table=g_result.appendChild(table);// добавляем новую таблицу
	var prev=table.previousSibling;
	if(prev!=null)prev.parentNode.removeChild(prev);// удаляем старую таблицу
	
}


$(document).ready(function(){
	init();
	jQuery(window).resize(function(){
		init();
	});
});
