jQuery(function ($) {

// スライド速度、間隔の設定
var o = {
  speed   : 1000,
  interval: 6000
};

// オブジェクト設定 画像の個数により変更すること
var $slider     = $('#slider'),
    $container  = $slider.find('div.slider-container'),
    $contents   = $container.children(),
    $firstChild = $contents.filter(':first-child'),
    $lastChild  = $contents.filter(':last-child'),
    $secondChild = $contents.filter(':eq(1)'),
    $lastBeforeChild = $contents.filter(':eq(2)'),
    $progressbar = $('#progressbar');

var size = {
  width : $container.width(),
  height: $container.height(),
  progressWidth: $progressbar.width()
};

/* size.width 変更 */
size.width = $firstChild.outerWidth(true);

var count = {
  min    : 0,
  max    : $contents.length,
  current: 0
};

// fix $contaienr width /* contents.length + 2 から 4 に変更 */
$container.css({
  width      : size.width * ($contents.length + 4),
  marginLeft : 0,
  paddingLeft: 0
//  marginLeft : -2 * size.width,
//  paddingLeft: 2 * size.width
});

// autoslide
var play, start;

play = function () {
         if(! start){
           // ↓自動スライドをコメントアウト begin ↓
           //start = setInterval(function () {
           //            slide.next();
           //        }, o.interval);
           // ↑自動スライドをコメントアウト begin ↑
         };
       };

// hover event (stop)
$contents.hover(
  function () {
    clearInterval(start);start = null;
    $progressbar.stop(true, false);
    $progressbar.css('width', 0);
  },
  function () {
    $progressbar.css('width', 0);
    fnc.progress(o.interval);
    play();
  }
);

// pager event
$('#slide-prev').click(function (e) {
  $progressbar.stop(true, false);
  $progressbar.css('width', 0);
  fnc.pager('negative', e);
});

$('#slide-next').click(function (e) {
  $progressbar.stop(true, false);
  $progressbar.css('width', 0);
  fnc.pager('positive', e);
});

$('#over-left').click(function (e) {
  $progressbar.stop(true, false);
  $progressbar.css('width', 0);
  fnc.pager('negative', e);
});

$('#over-right').click(function (e) {
  $progressbar.stop(true, false);
  $progressbar.css('width', 0);
  fnc.pager('positive', e);
});

// create pagination
// pagination event
var $pagination = $('<div/>', {'class': 'slider-pagination'});
// pagination を一旦隠す
$pagination.hide();

// slider
var distance; // 移動距離
var slide = {

  next: function (index) {
          $progressbar.stop(true, false);
          $progressbar.css('width', 0);
          fnc.range(index, 'positive');
          if(count.current < count.max - 1) {
            fnc.scroll(distance);
          } else {
            //$firstChild.css('left', size.width * $contents.length); 始めに追加しているのでいらない。
            distance = distance + (2 * size.width);
            fnc.objHide();
            $container.stop(true, false)
                      .animate({left: -distance}, o.speed,
                        function () {
                          $firstChild.css('left', 597); // divのleftと同じ数値を入れる
                          //$firstChild.css('left', 0);
                          $container.css('left', -2 * size.width);
                          //$container.css('left', 0);
                          fnc.objShow();
                          fnc.progress(o.interval - o.speed);
                        }
                      );
            count.current = -1;
          }
          fnc.counter(index, 'increment');
          //fnc.pageNav(count.current);
        },

  prev: function (index) {
          $progressbar.stop(true, false);
          $progressbar.css('width', 0);
          fnc.range(index, 'negative');
          if(count.current > count.min) {
            fnc.scroll(distance);
          } else {
            //$lastChild.css('left', -(size.width * $contents.length)); 始めに追加しているのでいらない。
            distance = distance + (2 * size.width);
            fnc.objHide();
            $container.stop(true, false)
                      .animate({left: -distance}, o.speed,
                        function () {
                          $lastChild.css('left', '');
                          //$container.css('left', -(size.width * ($contents.length - 1)));
                          $container.css('left', -1 * (1 + $contents.length) * size.width);
                          fnc.objShow();
                          fnc.progress(o.interval - o.speed);
                        }
                      );
            count.current = count.max;
          }
          fnc.counter(index, 'decrement');
          //fnc.pageNav(count.current);
        }
};

var fnc = {

  range  : function (n, d) {
             if(n >= 0) {
               distance = size.width * n;
             } else {
               var addNum;
               if(d === 'negative') addNum = -1;
               if(d === 'positive') addNum = +1;
               distance = size.width * (count.current + addNum);
             }
           },

  scroll : function (d) {
             d = d + (2 * size.width);
             fnc.objHide();
             $container.stop(true, false).animate({left: -d}, o.speed, function(){fnc.objShow();fnc.progress(o.interval - o.speed);});
           },

  counter: function (n, c) {
             if(n >= 0) {
               count.current = n;
             } else {
               if(c === 'increment') count.current++;
               if(c === 'decrement') count.current--;
             }
           },

  pageNav: function (n) {
             $pagination.children('a').removeClass('current');
             $pagination.children('a:eq(' + n + ')').addClass('current');
           },

  objHide: function(){
             $("#over-left").hide();
             $("#over-right").hide();
             $("#slide-prev").hide();
             $("#slide-next").hide();
           },

  objShow: function(){
             $("#over-left").show();
             $("#over-right").show();
             $("#slide-prev").show();
             $("#slide-next").show();
           },

  pager  : function (d, e) {
               if(!$container.is(':animated')) {
                 clearInterval(start);start = null;
                 if(d === 'positive') slide.next();
                 if(d === 'negative') slide.prev();
                 play();
               }
               e.preventDefault();
           },

  progress: function(time) {
          $progressbar.animate({width: size.progressWidth}, time);
  }
};

// auto start
var $clone = $lastChild.clone(true);
$container.prepend($clone);
var $clone = $lastBeforeChild.clone(true);
$container.prepend($clone);
$container.css('left', -2 * size.width);

$clone = $firstChild.clone(true);
$container.append($clone);
$clone = $secondChild.clone(true);
$container.append($clone);

// 実行
$progressbar.css('width', 0);
fnc.progress(o.interval);
play();
});

