// jQuery delta.slider v1.0

(function ($) {

  if (typeof $.delta == "undefined") {
    $.delta = {};
  }

  $.delta.slider = function (width, height, options) {
    
    var $slider = $("<div>");
  
    var settings = $.extend({
      interval: 2000,
      duration: 400,
      easing: "swing",
      clipped: true
    }, options);
    
    var overflow = settings.clipped ? "hidden": "visible";
    
    var currentItem = 0;
    var totalItems = 0;
    
    var playTimer;
    var isAutoPlay = false;


    // !初期化
    $slider.css({
      position: "relative",
      width: width,
      height: height,
      overflow: overflow
    });

    
    // !プライベート関数
    function autoPlay() {
      $slider.slideNext();
      playTimer = setTimeout(autoPlay, settings.interval);
    }
    
    
    // !パブリックメソッド
    
    $slider.getCurrentItem = function () {
      return currentItem;
    };

    $slider.getTotalItems = function () {
      return totalItems;
    };
    
    $slider.addItem = function (contents) {
    
      var $item = $("<div>", {
        css: {
          position: "absolute",
          top: "0%",
          left: "0%",
          width: "100%",
          height: "100%",
          display: "none",
          opacity: 0
        }
      });
      
      if (totalItems < 1) {
        $item.css({
          display: "block",
          opacity: 1
        });
      }
      
      ++totalItems;
      
      $item.append(contents);
      
      $slider.append($item);
    };
    
    $slider.slideTo = function (index) {
      
      if (totalItems === 1) {
        return;
      }
      
      var $items = $slider.children("div");
      
      currentItem = index;
      
      $items.each(function (i) {
        
        var $this = $(this);
        
        if ($this.index() === currentItem) {
          $this.stop().css({
            display: "block",
            zIndex: 2
          }).animate({
            opacity: 1
          },{
            duration: settings.duration,
            easing: settings.easing,
            complete: function () {
              if (!$.support.opacity) {
                this.style.removeAttribute('filter');
              }
            }
          });
        }
        else {
          $this.stop().css({
            zIndex: 1
          }).animate({
            opacity: 0
          },{
            duration: settings.duration,
            easing: settings.easing,
            complete: function () {
              $(this).css({
                display: "none"
              });
            }
          });
        }
      });
      
    };
    
    $slider.slidePrev = function () {

      var nextItem = currentItem - 1;
      
      if (nextItem < 0) {
        nextItem = totalItems - 1;
      }

      $slider.slideTo(nextItem);
    };
    
    $slider.slideNext = function () {

      var nextItem = currentItem + 1;
      
      if (nextItem >= totalItems) {
        nextItem = 0;
      }

      $slider.slideTo(nextItem);
    };
    
    $slider.slidePlay = function () {
      clearTimeout(playTimer);
      isAutoPlay = true;
      playTimer = setTimeout(autoPlay, settings.interval);
    };
    
    $slider.slideStop = function () {
      clearTimeout(playTimer);
      isAutoPlay = false;
    };
    
    return $slider;
  };

})(jQuery);

