window.onload=function() {
  if ($('.foto_galery').length) {    
      var heights = new Array();
      var count=0
      $('.foto_galery').each( function () { // цикл по div с class=foto_galery
        //heights.push($(this).outerHeight())
        //if (count%2==0) {alert(count)}
        if (typeof pred != 'undefined') {
            if (count%2!=0) {
                if ($(pred).height() > $(this).height()) { // если предыдущий div с картинкой больше текущего в одной строке
                    $(this).css("height",$(pred).height());
                    $(pred).css("height",$(pred).height());
                }
                else {
                    $(this).css("height",$(this).height());
                    $(pred).css("height",$(this).height()); 
                }   
                //alert($(pred).height())
                //alert($(this).height())
            }
            
        }
        pred=this
        count=count+1
      });
          //for (i = 0; i < heights.length; i++) 
            //alert(heights[i]);
      h=0
      count=0
      var margin_top = $(pred).css('margin-top');
      var margin_bottom = $(pred).css('margin-bottom');
      var margin_top = parseInt(margin_top.substr(0, margin_top.length - 2)); // убираем px
      var margin_bottom = parseInt(margin_bottom.substr(0, margin_bottom.length - 2)); // убираем px

      $('.foto_galery').each( function () { // сумма высот div для вычисления общей #main_text
        h=h+$(this).outerHeight() + margin_top + margin_bottom // значение высоты без margin
        //alert(h)
        count=count+1
        current=this
        //alert(h)
      });
  

      if (count!=0) {
          if (count%2!=0){ h=h+$(current).outerHeight()+margin_bottom+margin_top;  }
          hh=h/2
          $('#foto').css("height",hh+100)
          
          $('#foto').css("height",hh)
      }
  }    
};

