$(window).load(function(){ Columns(); });
$(window).resize( function(){ moveColumns(); });

function ColumnWidth(num) {
 if (num == 2) { return "49%" } 
 else if (num == 3) { return "32.14%" }
 else if (num == 4) { return "24%" } 
 else if (num == 5) { return "19.15%" } 
 else if (num == 6) { return "15.93%" } 
 else if (num == 7) { return "13.64%" } 
 else if (num == 8) { return "11.92%" } 
 else { return "190px" }
}

function Columns(){
    var HeightRow = 100;
    var widthcontentcenter = $("div#ContentCenter").width();

    columnnum = Math.floor((widthcontentcenter + 10) / 190);
    // Показываем "скоро в продаже"
    $("div.IncomingRow").each(function(){
      var HeightRow = 100;
      var news = $(this).children("div.ItemBoxStretchIncoming");
      count = 0;
      for(i = 0; i < news.length; i++) {
        var item = news.eq(i);
        item.css("width", ColumnWidth(columnnum));
        if (count == columnnum - 1) {
          item.css("padding", "0 0 0 0");
        }else{
          if($("div#ContentCenter").css("direction")=="rtl") {
            item.css("padding", "0 0 0 10px");
          }else{
            item.css("padding", "0 10px 0 0");
          }
        }
        if(count > columnnum - 1) {
          item.css("display", "none");
        }else{
          item.css("display", "block");
          var heightRecord = $(item).children("div.Record:first").children("div.HeightRecord:first").height();
          if (heightRecord > HeightRow) HeightRow = heightRecord;
        }
        count++;
        if(count == 20) {
          count = 0;
        }
      }
      for(i = 0; i < news.length; i++) {
      	var record = $(news.eq(i)).children("div.Record:first");
      	record.css("height", HeightRow);
      }
    });

    columnnum = Math.floor((widthcontentcenter + 10) / 190);

    // Показываем новинки
    $("div.NewsRow").each(function(){
      var HeightRow = 100;
      var news = $(this).children("div.ItemBoxStretchNews");
    count = 0;
    for (i = 0; i < news.length; i++) {
      var item = news.eq(i); 
      item.css("width", ColumnWidth(columnnum));
      if (count == columnnum - 1) {
        item.css("padding", "0 0 0 0");
      }else{
        if($("div#ContentCenter").css("direction")=="rtl") {
          item.css("padding", "0 0 0 10px");
        }else{
          item.css("padding", "0 10px 0 0");
        }
      }
      if (count > columnnum - 1) {
        item.css("display", "none");
      }else{
        item.css("display", "block");
          var heightRecord = $(item).children("div.Record:first").children("div.HeightRecord:first").height();
          if (heightRecord > HeightRow) HeightRow = heightRecord;
      }
      count++;
      if (count == 20) {
        count = 0;
      }
    }
      for(i = 0; i < news.length; i++) {
        var record = $(news.eq(i)).children("div.Record:first");
        record.css("height", HeightRow);
      }
    });

    // Показываем бестселлеры
    $("div.BestRow").each(function(){
    var HeightRow = 100;
    var news = $(this).children("div.ItemBoxStretchBest");
    count = 0;
    for (i = 0; i < news.length; i++) {
      var item = news.eq(i); 
      item.css("width", ColumnWidth(columnnum));
      if (count == columnnum - 1) {
        item.css("padding", "0 0 0 0");
      }else{
        if($("div#ContentCenter").css("direction")=="rtl") {
          item.css("padding", "0 0 0 10px");
        }else{
          item.css("padding", "0 10px 0 0");
        }
      }
      
      if (count > columnnum - 1) {
        item.css("display", "none");
      }else{
        item.css("display", "block");
          var heightRecord = $(item).children("div.Record:first").children("div.HeightRecord:first").height();
          if (heightRecord > HeightRow) HeightRow = heightRecord;
      }
      count++;
      if (count == 20) {
        count = 0;
      }
    }
      for(i = 0; i < news.length; i++) {
      	var record = $(news.eq(i)).children("div.Record:first");
      	record.css("height", HeightRow);
      }
    });


    // Показываем Акции
    $("div.ActionRow").each(function(){
      var news = $(this).children("div.ItemBoxStretchAction");
    count = 0;
      for (i = 0; i < news.length; i++) {
        var item = news.eq(i); 
      item.css("width", ColumnWidth(columnnum));
      if (count == columnnum - 1) {
        item.css("padding", "0 0 0 0");
      }else{
        if($("div#ContentCenter").css("direction")=="rtl") {
          item.css("padding", "0 0 0 10px");
        }else{
          item.css("padding", "0 10px 0 0");
        }
      }
      if (count > columnnum - 1) {
        item.css("display", "none");
      }else{
        item.css("display", "block");
          var heightRecord = $(item).children("div.Record:first").children("div.HeightRecord:first").height();
          if (heightRecord > HeightRow) HeightRow = heightRecord;
      }
      count++;
      if (count == 20) {
        count = 0;
      }
    }
    for(i = 0; i < news.length; i++) {
    	var record = $(news.eq(i)).children("div.Record:first");
    	record.css("height", HeightRow);
    }
    });

}
