


    $(function () {
      $('.conteiner_line div img').hide();//hide all the images on the page
    });
    
    var i = 0;//initialize
    var int=0;//Internet Explorer Fix
    $(window).bind("load", function() {//The load event will only fire if the entire page or document is fully loaded
      var int = setInterval("doThis(i)", 100);//500 is the fade in speed in milliseconds
    });

    function doThis() {
      var imgs = $('.conteiner_line div img').length;//count the number of images on the page
      if (i >= imgs) {// Loop the images
        clearInterval(int);//When it reaches the last image the loop ends
      }
      $('img:hidden').eq(0).fadeIn(500);//fades in the hidden images one by one
      i++;//add 1 to the count
    } 



$(document).ready(function(){
  
  var $foto_click = $('.conteiner_line div a'),
      $fc = $('.conteiner_line div'),
      $fcl = $('.conteiner_line div').length,
      $conteiner_loads = $('.conteiner_loads_append');
      $conteiner_line = $('.conteiner_line'),
      $gallery_small_foto = $('.gallery_small_foto'),
      $foto_small_right = $('.foto_small_right'),
      $foto_small_left = $('.foto_small_left'),
      $foto_big_left = $('.foto_big_left'),
      $foto_big_right = $('.foto_big_right'),
       $fcInWi = $fc.eq(0).innerWidth();
  

  
  $('.gallery_big_foto').hide();
  
  $conteiner_line.width($fcInWi * $fcl);

  foto_smal_buffer = Math.round($gallery_small_foto.innerWidth() / $fcInWi);
  
  foto_smal_buffer_width = foto_smal_buffer * $fcInWi;
  
  $fc.append('<i></i>');
  
  
  // Стрелки большой фотографии ------------------------------------
  foto_big_buffer = $fc.index(this);
 
  $foto_big_left.click(function(){
        $fci = $fci - 1;
        if ($fci == 'undefined' || $fci <= -1) {$fci = $fcl - 1}
        foto_bufer_load = $foto_click.eq($fci).attr('href');
        foto_load_conteiner();
        foto_big_line_animate();
    });
  $foto_big_right.click(function(){
        $fci = $fci + 1;
        if ($fci > $fcl-1) {$fci = 0};
        foto_bufer_load = $foto_click.eq($fci).attr('href');
        foto_load_conteiner();
        foto_big_line_animate();
    });
    
   $('.conteiner_loads .conteiner_loads_append').click(function(){
        $fci = $fci + 1;
        if ($fci > $fcl-1) {$fci = 0};
        foto_bufer_load = $foto_click.eq($fci).attr('href');
        foto_load_conteiner();
        foto_big_line_animate();
    }); 
    
  // Стрелки большой фотографии ------------------------------------

  //последовательное прокручивание стрелками -------------------------------------
  var foto_big_line_animate = (function(){
      //$('.mega').text($fci);
      $('.foto_small_right_active').show();
      $('.foto_small_left_active').show();
      $fc.removeClass('foto_click_active');
      $fc.eq($fci).addClass('foto_click_active');
      if ($fci) {
          foto_small_animate = ($fci-1) * $fcInWi;
          if ($fci > $fcl-foto_smal_buffer) {
            foto_small_animate = ($fcl - foto_smal_buffer) * $fcInWi;
          }
      }
      if ($fci == 0) {
        foto_small_animate = 0;
      } 
      if ($fci == $fcl-1){
        foto_small_animate = ($fcl - foto_smal_buffer) * $fcInWi;
      }
      
      
      if (foto_smal_buffer < $fcl) {
          $conteiner_line.stop().animate({'left': -foto_small_animate +'px'});
        }
      foto_activeit();
    });

  //последовательное прокручивание стрелками -------------------------------------

  
  
  // Листание линейки ------------------------------------------------------------
  crtyop = 0;
  $('.foto_small_right_active').click(function(){
      if (crtyop == 0){ $fci = 0; crtyop = 1; };
      $fci = $fci + 2;
      scroll_move_line();
    });
  $('.foto_small_left_active').click(function(){
      if (crtyop == 0){ $fci = 0; crtyop = 1; }; 
      
      if ($fci > $fcl-foto_smal_buffer) {
        $fci = $fcl-foto_smal_buffer;
      };
      $fci = $fci - 2;
      scroll_move_line();
    });
    
  var scroll_move_line = (function(){
      $('.foto_small_right_active').show();
      $('.foto_small_left_active').show(); 
      foto_line_animate = $fci * $fcInWi;
      
      if ($fci >= $fcl-foto_smal_buffer) {
          $('.foto_small_right_active').hide();
          foto_line_animate = ($fcl - foto_smal_buffer)* $fcInWi;
        };
      if ($fci <= 0) {
          $('.foto_small_left_active').hide();
          foto_line_animate = 0;
          $fci = 0;
        };
      $conteiner_line.stop().animate({'left': -foto_line_animate +'px'});
  });
  // Листание линейки ------------------------------------------------------------ 
 




  // Загрузка большой фотографии --------------------
  var foto_load_conteiner = (function (){
    $conteiner_loads.html('<img src="'+ foto_bufer_load +'" />');
    });
  // Загрузка большой фотографии -------------------- 

  // Определение активной фотографии ----------------
  var innerheight_foto = $('.conteiner_line div i ').eq(0).innerHeight();
  var foto_activeit = (function(){
      $('.conteiner_line div i ').css({'top': innerheight_foto +"px"});
      $('.conteiner_line div i ').eq($fci).css({'top':'0'});
    });
  // Определение активной фотографии ----------------
  

 // Клик а --------------------
  $foto_click.click(function(){
    $fci = $foto_click.index(this);
    $fc.removeClass('foto_click_active');
    $fc.eq($fci).addClass('foto_click_active');
    foto_bufer_load = $foto_click.eq($fci).attr('href');
    foto_load_conteiner();
    $('.gallery_big_foto').show();
    foto_big_line_animate();
    crtyop = 1;
    foto_activeit();
    return false;
    });
 // Клик а --------------------

 // Closer --------------------
  $('.gallery_closer').click(function(){
    $('.gallery_big_foto').hide();
    $('.conteiner_line div i ').css({'top': innerheight_foto +"px"});
    });
 // Closer --------------------
   
   
   
 // --------------
 
 //alert(document.documentElement.clientHeight);
   
   var rize_windot_fotogllery = (function(){
        $('.gallery_big_foto').height(document.documentElement.clientHeight);
    }); rize_windot_fotogllery();
   
    



  $(window).resize(function() {
    rize_windot_fotogllery();
  });



});

