// jQuery as.pageViewer.infoContainer

(function () {

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

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

  $.as.pageViewer.infoContainer = function () {

    var $infoContainer = $("<div>", {
      id: "infoContainer"
    });
    
    var $label = $("<div>", {
      "class": "label"
    });
    
    var $slider = $("<div>", {
      "class": "slider"
    });
    
    var $pages;
    
    var pageWidthList = [];
    
    var duration = 1200;
    var interval = 8000;
    
    var currentPage = -1;
    
    
    // !初期化
    
    $infoContainer.append($label, $slider);
    $infoContainer.css({
      opacity: 0
    });
    
    $label.text("Information:");
    
    
    // !パブリックメソッド
    
    $infoContainer.init = function ($infoContents) {
      
      $infoContents.each(function (i) {

        var $content = $(this);
        
        var $page = $("<div>", {
          "class": "page"
        });
        
        $page.html($content);
        
        $slider.append($page);
        
        pageWidthList[i] = $page.width() + 10;
      
        $page.hide();

      });
      
      $slider.css({
        width: 0,
        height: $label.height()
      });
      
      $pages = $slider.children("div");
      
      $pages.css({
        position: "absolute",
        top: 0,
        right: 0
      });

      $infoContainer.trigger("complete");
      
    };
    
    $infoContainer.showContainer = function () {

      $infoContainer.animate({
        opacity: 1
      }, {
        duration: duration,
        complete: function () {
          if (!$.support.opacity) {
            this.style.removeAttribute('filter');
          }
        }
      });

      (function loop() {
        
        ++currentPage;
        
        if (currentPage >= $pages.length) {
          currentPage = 0;
        }
        
        $pages.not(":eq(" + currentPage + ")").fadeOut(duration);
        
        setTimeout(function () {

          $slider.css({
            width: pageWidthList[currentPage]
          });

          $pages.eq(currentPage).fadeIn(duration);
          
          setTimeout(loop, interval);

        }, duration);

      })();
      
    };
    
    return $infoContainer;
  };

})(jQuery);

