$(document).ready(function(){
 
  var $div_foto = $('.conteiner_foto div'),
      $Fconteiner = $('.conteiner_foto'),
      $gallery_line = $('.gallery_line');

   $('.big_foto').append('<div class="bg_closer_click"></div><div class="conteiner_foto_interface"><div class="gallery_closer"></div><div class="arrow_left_big"></div><div class="arrow_right_big"></div><div class="conteiner_loads_append"></div></div>');
   $('.gallery_line').append('<div class="arrow_left"><i></i></div><div class="arrow_right"><i></i></div>');

  // ---- Определение количества линейных галерей на странице
  var quantity = $Fconteiner.length;
  for (galLeng=0; galLeng<quantity; galLeng++){
      $conteinerFotoEqL = $('.conteiner_foto:eq('+galLeng+') div').length;
      $conteinerFotoEqI = $('.conteiner_foto:eq('+galLeng+') div').eq(0).innerWidth();
      $('.gallery_line:eq('+galLeng+') .conteiner_line .conteiner_foto').width($conteinerFotoEqL * $conteinerFotoEqI);
      
      LinkSlider = $Fconteiner.eq(galLeng).find('span a').attr('href');
      $('.conteiner_loads_append').eq(galLeng).html('<img src="'+LinkSlider+'" />');
      $('.conteiner_foto:eq('+galLeng+') div').eq(0).addClass('active_foto');

      $('.conteiner_foto:eq('+galLeng+') div p').eq(0).clone().appendTo($('.conteiner_foto_interface').eq(galLeng));
      
      if ($('.conteiner_foto').eq(galLeng).innerWidth() < $('.conteiner_line').eq(galLeng).innerWidth()) {
            $('.arrow_right').eq(galLeng).hide();
            $('.arrow_left').eq(galLeng).hide();
        };
  };
  
  // ---- Прокрутка линейки
  $('.arrow_right', this).click(function(){
        arwR = $('.arrow_right').index(this);
        pl = 'right';
        line_animate();
    });
  $('.arrow_left', this).click(function(){
        arwR = $('.arrow_left').index(this);
        pl = 'left';
        line_animate();
    });

  var line_animate = (function(){

      $divFotoWidth = $('.conteiner_foto:eq('+arwR+') div').innerWidth();
      $conteinerLineWidth = $('.conteiner_line').eq(arwR).innerWidth();
      $conteinerFotoWidth = $Fconteiner.eq(arwR).innerWidth()
      $conteinerFotoLenght = $('.conteiner_foto:eq('+arwR+') div').length;
      $NumberFotoMin = Math.ceil($conteinerLineWidth / $divFotoWidth);
      Fposition = $Fconteiner.eq(arwR).position().left;

      if (pl == 'left') {
            MoveAnimateLine = Fposition + $divFotoWidth;
            if ($Fconteiner.eq(arwR).position().left >= 0) { MoveAnimateLine = 0; };
        } else if (pl == 'right') {
              MoveAnimateLine = Fposition - $divFotoWidth;
              if ($Fconteiner.eq(arwR).position().left <= -($conteinerFotoLenght - $NumberFotoMin) * $divFotoWidth){ return false; };
          };
      $Fconteiner.eq(arwR).stop().animate({'left': MoveAnimateLine +'px'}, 'fast', function(){ MoveAlign(); });
  });

  var MoveAlign = (function(){
      if (pl == 'right') { FinishFotoLine = (Math.floor(Fposition / $divFotoWidth) - 1) * $divFotoWidth; } 
      if (pl == 'left') { FinishFotoLine = (Math.round(Fposition / $divFotoWidth) + 1) * $divFotoWidth; }
      if ($Fconteiner.eq(arwR).position().left >= 0) { FinishFotoLine = 0; };
      if ($Fconteiner.eq(arwR).position().left <= -($conteinerFotoLenght - $NumberFotoMin) * $divFotoWidth){
          FinishFotoLine = -($conteinerFotoLenght - $NumberFotoMin) * $divFotoWidth;
        };
      $Fconteiner.eq(arwR).animate({'left': FinishFotoLine +'px'});
    });

  // ---- Клосер
    $('.gallery_closer').click(function(){ $(this).closest('.big_foto').hide(); });
    $('.bg_closer_click').click(function(){ $(this).closest('.big_foto').hide(); });

  // ---- Клик фото в линейке
  $('.conteiner_foto div span a').click(function(){
       $(this).closest('.gallery_line').find('.big_foto').show();
       $(this).closest('.gallery_line').find('.conteiner_loads_append').html('<img src="'+$(this).attr('href')+'" />');
       $(this).closest('.conteiner_foto').find('div').removeClass('active_foto');
       $(this).closest('div').addClass('active_foto');
       thisNumberFotoLine = Math.round($(this).closest('div').position().left / $(this).closest('div').innerWidth());
       thisHtmlappend = $(this).closest('.conteiner_foto').find('div p').eq(thisNumberFotoLine).html();
       $(this).closest('.gallery_line').find('.conteiner_foto_interface p').html(thisHtmlappend);
       return false;
    });

  // ---- Пролистывание фотографий
  $('.arrow_left_big').click(function(){
        arwR = $('.arrow_left_big').index(this);
        pl = 'left';
        NextBackSlider();
    });
  $('.arrow_right_big').click(function(){
        arwR = $('.arrow_right_big').index(this);
        pl = 'right';
        NextBackSlider();
    });
  $('.conteiner_loads_append').click(function(){
        arwR = $('.conteiner_loads_append').index(this);
        pl = 'right';
        NextBackSlider();
    });
 
 
 
  var NextBackSlider = (function(){

      $ConteinerFotoArDiv = $('.conteiner_foto:eq('+arwR+') div');
      $divFotoWidth = $('.conteiner_foto:eq('+arwR+') div').innerWidth();
      $conteinerLineWidth = $('.conteiner_line').eq(arwR).innerWidth();
      $conteinerFotoWidth = $Fconteiner.eq(arwR).innerWidth()
      $conteinerFotoLenght = $('.conteiner_foto:eq('+arwR+') div').length;
      $NumberFotoMin = Math.ceil($conteinerLineWidth / $divFotoWidth);
      Fposition = $Fconteiner.eq(arwR).position().left;

       asd = Math.round($('.conteiner_foto').eq(arwR).find('.active_foto').position().left / $divFotoWidth);

       $('.conteiner_foto').eq(arwR).find('div').removeClass('active_foto');

       if (pl == 'right') { asd = asd + 1; }
       if (pl == 'left') { asd = asd - 1; }
       if (asd <= -1) {
            asd = $ConteinerFotoArDiv.length-1;
            rightMotionFinish = -(asd - $NumberFotoMin + 1) * $divFotoWidth;
            $('.conteiner_foto').eq(arwR).animate({'left': rightMotionFinish +'px'}, 300);
          };
       if (asd >= $ConteinerFotoArDiv.length) {
            asd = 0;
            $('.conteiner_foto').eq(arwR).animate({'left': 0}, 300);
          };

       $ConteinerFotoArDiv.eq(asd).addClass('active_foto');
       LinkSlider = $ConteinerFotoArDiv.eq(asd).find('span a').attr('href');
       $('.gallery_line').eq(arwR).find('.conteiner_loads_append').html('<img src="'+LinkSlider+'" />');

       thisHtmlappend = $('.conteiner_foto:eq('+arwR+') div p').eq(asd).html();

       $('.conteiner_foto_interface:eq('+arwR+') p').html(thisHtmlappend);

       if ($('.conteiner_foto').eq(arwR).innerWidth() > $('.conteiner_line').eq(arwR).innerWidth()) {
            if (asd == $ConteinerFotoArDiv.length-1) {
            } else if (asd == 0) {
            } else {
              line_animate();
            }
        };
    });


});








