  $(function() {
    var left_div_list = [];
    var right_div_list = [];
    var left_count = 0;
    var right_count = 0;
    var moves_left = 0;
    var is_scroller_animated = false;

    /*********************************************************************************************/
    moveLeft = function() {
      if (is_scroller_animated) {
        return false;
      }
      left_div_list = [];
      right_div_list = [];
      left_count = 0;
      right_count = 0;
      $.each($('#slide_container div'), function(id, div) {
        var position = $(div).position();
        var left = position.left;
        if (left <= 93) {
          left_div_list[left_count] = div;
          left_count++;
        } else {
          right_div_list[right_count] = div;
          right_count++;
        }
      });
      if (right_div_list.length == 0) {
        return;
      }
      is_scroller_animated = true;
      moveLeft_LeftArray();
      setTimeout('moveLeft_RightArray()', 100);
      setTimeout('makeRecountLeft()', 210);
      if (moves_left) {
        setTimeout('moveLeft()', 250);
        moves_left--;
      }
      setTimeout('showSpecialInfo()', 205);
    }
    moveLeft_LeftArray = function() {
      $.each(left_div_list, function(i, div) {
        var position = $(div).position();
        var left = position.left;
        $(div).animate({
          'left': left - 80 + 'px'
        }, {
          'duration': 100,
          'queue': false,
          'complete': function() {
            $(this).css('z-index', parseInt($(this).css('z-index')) - 1);
            $(this).attr('rel', $(this).css('z-index'));
          }
        });
      });

    }
    moveLeft_RightArray = function() {
      $.each(right_div_list, function(i, div) {
        $(div).css('z-index', parseInt($(div).css('z-index')) + 1);
        $(div).attr('rel', $(div).css('z-index'));
        var position = $(div).position();
        var left = position.left;
        $(div).animate({
          'left': left - 80 + 'px'
        }, {
          'duration': 100,
          'queue': false
        });
      });
    }
    /*********************************************************************************************/
    moveRight = function() {
      if (is_scroller_animated) {
        return false;
      }
      left_div_list = [];
      right_div_list = [];
      left_count = 0;
      right_count = 0;
      $.each($('#slide_container div'), function(id, div) {
        var position = $(div).position();
        var left = position.left;
        if (left >= 93) {
          right_div_list[right_count] = div;
          right_count++;
        } else {
          left_div_list[left_count] = div;
          left_count++;
        }
      });
      if (left_div_list.length == 0) {
        return;
      }
      is_scroller_animated = true;
      moveRight_RightArray();
      setTimeout('moveRight_LeftArray()', 100);
      setTimeout('makeRecountRight()', 210);
      if (moves_left) {
        setTimeout('moveRight()', 250);
        moves_left--;
      }
      setTimeout('showSpecialInfo()', 205);
    }
    moveRight_RightArray = function() {
      $.each(right_div_list, function(i, div) {
        var position = $(div).position();
        var left = position.left;
        $(div).animate({
          'left': left + 80 + 'px'
        }, {
          'duration': 100,
          'queue': false,
          'complete': function() {
            $(this).css('z-index', parseInt($(this).css('z-index')) - 1);
            $(this).attr('rel', $(this).css('z-index'));
          }
        });
      });


    }
    moveRight_LeftArray = function() {
      $.each(left_div_list, function(i, div) {
        $(div).css('z-index', parseInt($(div).css('z-index')) + 1);
        $(div).attr('rel', $(div).css('z-index'));
        var position = $(div).position();
        var left = position.left;
        $(div).animate({
          'left': left + 80 + 'px'
        }, {
          'duration': 100,
          'queue': false
        });
      });
    }
    /*********************************************************************************************/
    makeRecountLeft = function() {
      left_div_list = [];
      right_div_list = [];
      left_count = 0;
      right_count = 0;
      $.each($('#slide_container div'), function(id, div) {
        var position = $(div).position();
        var left = position.left;
        if (left <= 93) {
          left_div_list[left_count] = div;
          left_count++;
        } else {
          right_div_list[right_count] = div;
          right_count++;
        }
      });
      if (right_div_list.length == 0) {
        $('.left_arr').find('img').addClass('inact');
        $('.right_arr').find('img').removeClass('inact');
      } else {
        $('.left_arr').find('img').removeClass('inact');
        $('.right_arr').find('img').removeClass('inact');
      }
    }
    makeRecountRight = function() {
      left_div_list = [];
      right_div_list = [];
      left_count = 0;
      right_count = 0;
      $.each($('#slide_container div'), function(id, div) {
        var position = $(div).position();
        var left = position.left;
        if (left >= 93) {
          right_div_list[right_count] = div;
          right_count++;
        } else {
          left_div_list[left_count] = div;
          left_count++;
        }
      });
      if (left_div_list.length == 0) {
        $('.left_arr').find('img').removeClass('inact');
        $('.right_arr').find('img').addClass('inact');
        return;
      } else {
        $('.left_arr').find('img').removeClass('inact');
        $('.right_arr').find('img').removeClass('inact');
      }
    }
    /*********************************************************************************************/
    showSpecialInfo = function() {
      self.imageShowHide();
      var center_div = $('#slide_container div[rel=30]');
      var center_image = $('#slide_container div[rel=30] img');
      var n_id = $(center_div).attr('n_id');
      $('#slide_info').html('<a href=\"'+$(center_image).attr('url')+'\">'+$(center_image).attr('title')+'</a><span>Цена: ' + $(center_image).attr('price') + '</span>');
      $('.slide_container_move_to_init').parent('li').removeClass('act');
      $('.slide_container_move_to_init[n_id='+n_id+']').parent('li').addClass('act');
      $('.slide_container_move_to_init').siblings('.act-span').hide();
      $('.slide_container_move_to_init').show();
      $('.slide_container_move_to_init[n_id='+n_id+']').hide();
	  $('.slide_container_move_to_init[n_id='+n_id+']').siblings('.act-span').show();
      is_scroller_animated = false;
    }
    imageShowHide = function() {
      $('#slide_container div img').hide();
      $('#slide_container div[rel=30] img').show();
      $('#slide_container div[rel=29] img').show();
    }
    moveToNId = function(n_id) {
      if (is_scroller_animated) {
        return false;
      }
      if ($('a[n_id='+n_id+']').parent('li.act').length) {
        return false;
      }
      var div = $('#slide_container div[n_id='+n_id+']');
      var position = $(div).position();
      var left = position.left;
      var diff = left - 80;
      if (diff == 0) {
        return;
      }
      moves_left = Math.round(Math.abs(diff / 80)) - 1;
      if (diff <= 0) {
        moveRight();
      } else {
        moveLeft();
      }
    }
    showSpecialInfo();
  });
