$(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 "180px" }
}

function Columns(){
    var widthcontentcenter = $("div#ContentCenter").width();
    
	// Показываем RTS
    columnnum = Math.floor((widthcontentcenter + 10) / 190);
    // Показываем "скоро в продаже"
    var HeightRow = 100;
    $("div.RTSRow").each(function(){
      var news = $(this).children("div.ItemBoxStretchRTS");
      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{
          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 + 25);
      }
    });
    
    // Показываем "Акции"
    columnnum = Math.floor((widthcontentcenter + 10) / 225);
    
    $("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{
          item.css("padding", "0 10px 0 0");
        }
        if(count > columnnum - 1) {
          item.css("display", "none");
        }else{
          item.css("display", "block");
        }
        count++;
        if(count == 20) {
          count = 0;
        }
      }
    });


    columnnum = Math.floor((widthcontentcenter + 10) / 190);

    // Показываем "аукционы"
    var HeightRow = 100;
    $("div.AuctionRow").each(function(){
      var news = $(this).children("div.ItemBoxStretchAuction");
      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{
          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);
      }
    });

    // Показываем "видеоролики"
    var HeightRow = 100;
    $("div.VideoRow").each(function(){
      var news = $(this).children("div.ItemBoxStretchVideo");
      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{
          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) / 225);
    // Показываем "скоро в продаже"
    var HeightRow = 100;
    $("div.IncomingRow").each(function(){
      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{
          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);
    // Показываем новинки
    var HeightRow = 100;
    $("div.NewsRow").each(function(){
      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{
          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 best = $(this).children("div.ItemBoxStretchBest");
      count = 0;
      for(i = 0; i < best.length; i++) {
        var item = best.eq(i);
        item.css("width", ColumnWidth(columnnum));
        if (count == columnnum - 1) {
          item.css("padding", "0 0 0 0");
        }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 < best.length; i++) {
        	var record = $(best.eq(i)).children("div.Record:first");
        	record.css("height", HeightRow);
      }
    });

}

