// jQuery.bbrk.home.slider.js

(function ($) {
  
  if (typeof $.bbrk === "undefined") {
    $.bbrk = {};
  }
  
  if (typeof $.bbrk.home === "undefined") {
    $.bbrk.home = {};
  }
  
  $.bbrk.home.slider = function (data) {
    
    var $slider;
    var $contents;
    
    var events = $.bbrk.home.slider.events;
    
    var nameList = ["basic", "jellybean", "pattern", "flag", "sf1", "sf2", "animal", "cute1", "cute2", "hero", "artist1", "artist2"];
    var pageList = [];
    
    var currentPage = -1;
    var count = 0;
    
    var timer;
    var isPause = false;
    var isPauseAll = false;

    
    $slider = $("<div>", {
      "class": "slider"
    });
    
    $slider.css({
      width: "100%",
      height: "100%"
    });
    
    $contents = $("<div>", {
      "class": "contents"
    });
    
    $contents.css({
      position: "absolute",
      top: "100%",
      left: "0",
      width: "100%",
      height: "100%"
    });
    
    $slider.html($contents);
    
    for (var i = 0; i < nameList.length; ++i) {

      var $page = $.bbrk.home.sliderPage(data[nameList[i]]);

      $page.css({
        position: "absolute",
        top: (100 * i) + "%",
        left: 0
      });
      
      $contents.append($page);

      pageList.push($page);
    }


    // public
    
    $slider.getCurrentPage = function () {
      return currentPage;
    };
    
    $slider.getTotalPages = function () {
      return pageList.length;
    };
    
    $slider.hide = function () {
      $slider.css({
        display: "none"
      });
    };
    
    $slider.show = function (duration, easing) {
      
      $slider.css({
        display: "block"
      });
      
      $slider.slideTo(0, duration, easing);
    };

    $slider.play = function () {
      
      clearTimeout(timer);
      
      if (isPauseAll) {
        pageList[currentPage].play();
        isPauseAll = false;
      }
      else if (isPause) {
        
        var current = currentPage;
        
        pageList[current].reset(300, "easeOutExpo");
        
        setTimeout(function () {
          pageList[current].play();
        }, 300);
        
        isPause = false;
      }
      
      (function loop() {
      
        ++count;
        
        if (count > 50) {
          
          var next = currentPage + 1;
          
          if (next >= pageList.length) {
            next = 0;
          }
          
          count = 0;
          
          $slider.slideTo(next, 300, "easeOutExpo");
        }
        
        if (isPause || isPauseAll) {
          return;
        }
        
        timer = setTimeout(loop, 60);

      })();
    };

    $slider.pause = function () {
      clearTimeout(timer);
      isPause = true;
      count = 0;
    };

    $slider.pauseAll = function () {
      clearTimeout(timer);
      isPauseAll = true;
      pageList[currentPage].pause();
    };
    
    $slider.slideTo = function (i, duration, easing) {
      
      $contents.stop();
      
      if (currentPage === i) {
        return;
      }
      
      if (currentPage >= 0) {
        pageList[currentPage].reset();
      }

      pageList[i].play();

      currentPage = i;
      count = 0;
          
      $slider.trigger(events.SLIDE_START);
      
      $contents.animate({
        top: - (i * 100) + "%"
      },{
        duration: duration,
        easing: easing,
        complete: function () {
          $slider.trigger(events.SLIDE_END);
        }
      });
    };
    
    $slider.resizeCurrent = function (width, height) {
      if (typeof pageList[currentPage] === "undefined") {
        return;
      }
      pageList[currentPage].resize(width, height);
    };
    
    $slider.resizeAll = function (width, height) {
      for (var i = 0; i < pageList.length; ++i) {
        pageList[i].resize(width, height);
      }
    };
    
    return $slider;
  };
  
  $.bbrk.home.slider.events = {
    SLIDE_START: "slideStart",
    SLIDE_END: "slideEnd"
  };
  
})(jQuery);

