function goodsAdded(data)
{
	document.location.href = '/office/cart/';
}
$(document).ready(function() {
    mainTovarBlockResize();
    $(window).resize(function() {
        mainTovarBlockResize();
    });
});
canSetTimeoutsTovars = {};
function mainTovarBlockResize() {
    var tovar = $(".tovar:eq(0)");
    var tovarWidth = parseInt(tovar.outerWidth(true));
    var tovarHeight = parseInt(tovar.outerHeight(true));
    
    $(".goods-block[type]").each(function(i,elem) {
        var elem = $(elem);
        
        var type = elem.attr("type");
        
        var minRows = elem.attr("minRows");
        var minTovar = elem.attr("minTovar");

        var tovarCount = elem.find(".tovar").length;
        var blockWidth = elem.width();
        var blockHeight = elem.height();
        var tovarsInRowCount = (blockWidth-blockWidth%tovarWidth)/tovarWidth;
        var fullRowsCount = (minTovar-minTovar%tovarsInRowCount)/tovarsInRowCount;
        if(minTovar%tovarsInRowCount>0)
            ++fullRowsCount;
        //console.log("fullRowsCount "+fullRowsCount);
        //Проверка на количество строк(если надо меням)
        if(fullRowsCount>=minRows) {
            elem.height(fullRowsCount*tovarHeight);
            nowRows=fullRowsCount;
        }else{
            nowRows = minRows;
            elem.height(nowRows*tovarHeight);
        }
        //Проверка на количество товаров(если надо добавляем)
        if(tovarCount<minRows*tovarsInRowCount) {
            var needDownload = nowRows*tovarsInRowCount-tovarCount;
            if(needDownload+tovarCount<minTovar)
                needDownload = minTovar-tovarCount;
            if(type in canSetTimeoutsTovars)
                clearTimeout(canSetTimeoutsTovars[type]);
            canSetTimeoutsTovars[type] = setTimeout(function(){
                $.ajax({
                    url: "/ajax/goods/goods/",
                    data : {
                        "type" : type,
                        from : tovarCount,
                        kol : needDownload
                    },
                    success: function(html) {
                        elem.append(html);
                    },
                    complete : function() {
                    }
                });
            }, 200);
        }
    });
    
}
