// 今日のワンシーン表示コントロール

// 今日のワンシーンモジュールの名前空間
var OneSceneControl = {};

// 病気体験記表示エリアの HTML テンプレート
OneSceneControl.BookTemplate = 
  "<div id=\"book-img\"><a href=\"#{article_url}\">" +
  "<img src=\"#{cover_image_url}\" alt=\"#{book_title}\" width=\"90\" height=\"113\" />" +
  "</a></div><div id=\"one-scene-text\">" +
  "<div id=\"one-scene-message\"><a href=\"#{article_url}\">#{body}</a></div>" +
  "<div id=\"one-scene-author\">" +
  "著者：#{author}さん<br />疾病名：#{disease}" +
  "</div></div>";

// エラーメッセージ表示時の HTML テンプレート
OneSceneControl.ErrorTemplate = "<div id=\"one-scene-error\">#{message}</div>";

// 今日のワンシーン管理クラス
OneSceneControl.Rotator = Class.create({
  
  // コンストラクタ
  initialize: function(data_set, options) {
    this.currentIndex = 0;
    this.hasErrors = false;
    this.seanes = $A(data_set);
    this._preloadImages(this.seanes);
    this.maxIndex = this.seanes.length - 1;
    this.interval = options.interval;
    this.container = $(options.container);
    this.bookTmpl = new Template(OneSceneControl.BookTemplate);
    this.errorTmpl = new Template(OneSceneControl.ErrorTemplate);
    this.autoExecuter = null;
    this.prevButton = $(options.prevButton);
    this.nextButton = $(options.nextButton);
    if (!this.seanes || this.seanes.length <= 0) {
      var msg = "データの取得に失敗しました。<br />" +
        "ご迷惑をおかけして申し訳ありません。";
      this.container.update(this.errorTmpl.evaluate({message: msg}));
      this.hasErrors = true;
      return;
    }
    
    var cursorStyle = {cursor: "pointer"};
    this.prevButton.setStyle(cursorStyle);
    this.nextButton.setStyle(cursorStyle);
    this.prevButton.observe("click", this.previousScene.bind(this));
    this.nextButton.observe("click", this.nextScene.bind(this));
    this.container.observe("mouseover", this.stopAutoFlip.bind(this));
    this.container.observe("mouseout", this.startAutoFlip.bind(this));
  },
  
  // 表示の開始
  run: function() {
    if (this.hasErrors) { return; }
    this.update();
    this.startAutoFlip();
  },
  
  // 表示の更新
  update: function() {
    this.container.update(this.bookTmpl.evaluate(this.seanes[this.currentIndex]));
  },
  
  // 前のワンシーン表示
  previousScene: function() {
    this.currentIndex += 1;
    if (this.currentIndex > this.maxIndex) { this.currentIndex = 0; }
    this.update();
  },
  
  // 次のワンシーンを表示
  nextScene: function() {
    this.currentIndex -= 1;
    if (this.currentIndex < 0) { this.currentIndex = this.maxIndex; }
    this.update();
  },
  
  // 自動送りを開始する
  startAutoFlip: function() {
    if (this.autoExecuter) { return; }
    this.autoExecuter = new PeriodicalExecuter(
      this.nextScene.bind(this), this.interval);
  },
  
  // 自動送りを停止する
  stopAutoFlip: function() {
    if (!this.autoExecuter) { return; }
    this.autoExecuter.stop();
    this.autoExecuter = null;
  },
  
  // 画像をプリロードする
  _preloadImages: function(seanes) {
    seanes.each(function(seane) {
      if (seane.cover_image_url) {
        var img = new Image();
        img.src = seane.cover_image_url;
      }
    }, this);
  }
});

// 今日のワンシーン実行の実行を開始する
OneSceneControl.start = function() {
  var control = new OneSceneControl.Rotator([{body: "\u9023\u308c\u3066\u5e30\u308a\u305f\u3044\u30fb\u30fb\u30fb(T.T)", title: "2004\u5e74\u590f \u30b7\u30e3\u30f3\u30c8\u8853(1)", cover_image_url: "/uimages/conts001/153/book_image/picture/553/thumb/_ayB_bl.jpg", author: "\u30b8\u30e5\u30f3\u30de\u30de", id: 395, book_title: "\u3058\u3085\u3093\u30b8\u30e5\u30f3JUN\u3061\u3083\u3093\u306e\u95d8\u75c5\u8a18", mypalette_url: "http://lifepalette.jp/mypalette/153", article_url: "http://lifepalette.jp/palette_book/show/153/58/1644", disease: "\u8133\u816b\u760d"},
{body: "\u51b7\u9759\u306b\u8a71\u3092\u805e\u3044\u3066\u3044\u308b\u3064\u3082\u308a\u3067\u3082\u3001\u7a81\u7136\u964d\u308a\u304b\u304b\u3063\u305f\n\u60aa\u5922\u3092\u5b8c\u5168\u306b\u53d7\u3051\u6b62\u3081\u308b\u4e8b\u306f\u4e0d\u53ef\u80fd\u3060\u3063\u305f\u306e\u3067\u3059\u3002", title: "\u301c\u305d\u306e\uff12\u3000\u544a\u77e5\u301c", cover_image_url: "/uimages/conts001/122/book_image/picture/2999/thumb/_clematis8.jpg", author: "\u96eb", id: 394, book_title: "Smile And Tears", mypalette_url: "http://lifepalette.jp/mypalette/122", article_url: "http://lifepalette.jp/palette_book/show/122/41/1121", disease: "\u4e73\u764c"},
{body: "\u6d99\u304c\u3001\u6d99\u304c\u3001\u3042\u3075\u308c\u51fa\u3066\u6b62\u307e\u3089\u306a\u304b\u3063\u305f\u3002", title: "\u3010 \uff12\u30af\u30fc\u30eb\u76ee \u3011\uff12\uff10\uff10\uff14\uff0e\uff19\uff0e\uff11\uff08\u6c34\uff09\u3044\u3064\u304b\u266a", cover_image_url: "/uimages/conts001/118/book_image/picture/542/thumb/_PB040047.jpg", author: "JOY", id: 393, book_title: "\uff2a\uff2f\uff39\u3061\u3083\u3093\u306e\u60aa\u6027\u30ea\u30f3\u30d1\u816b", mypalette_url: "http://lifepalette.jp/mypalette/118", article_url: "http://lifepalette.jp/palette_book/show/118/37/773", disease: "\u60aa\u6027\u30ea\u30f3\u30d1\u816b"},
{body: "\u826f\u3044\u5b50\u306f\u307e\u306d\u3092\u3057\u306a\u3044\u69d8\u306b\uff01", title: "\u5165\u9662\uff11\uff08\u624b\u8853\uff1a\u5e83\u6c4e\u5b50\u5bae\u5168\u5ae1\u8853\uff0b\u9aa8\u76e4\u30ea\u30f3\u30d1\u7bc0\u5ed3\u6e05\u8853\uff09", cover_image_url: "/uimages/conts001/123/book_image/picture/594/thumb/_htC_gr.jpg", author: "\u30a8\u30e0", id: 392, book_title: "\u764c\u3092\u30ac\u30c4\u30f3\u3063\u3068\uff01", mypalette_url: "http://lifepalette.jp/mypalette/123", article_url: "http://lifepalette.jp/palette_book/show/123/42/1150", disease: "\u5b50\u5bae\u764c"},
{body: "\u672c\u5f53\u306b\u3042\u306e\u6642\u3001\u5c0f\u66fd\u6839\u771f\u3055\u3093\u306e\u30b3\u30f3\u30b5\u30fc\u30c8\u304c\u3042\u3063\u305f\u4e8b\u306b\u611f\u8b1d\u3057\u3066\u3044\u307e\u3059\u3002", title: "\u5c0f\u66fd\u6839\u771f\u3055\u3093\u306e\u30d4\u30a2\u30ce\u306e\u97f3\u30fb\u30fb\u30fb", cover_image_url: "/uimages/conts001/44/book_image/picture/857/thumb/_naB_pk.jpg", author: "\u305f\u3063\u3061\u3083\u3093\u306e\u59bb", id: 391, book_title: "\u305f\u3063\u3061\u3083\u3093\u306e\u80ba\u304c\u3093\u95d8\u75c5\u8a18", mypalette_url: "http://lifepalette.jp/mypalette/44", article_url: "http://lifepalette.jp/palette_book/show/44/75/2712", disease: "\u539f\u767a\u6027\u80ba\u764c"},
{body: "\u672c\u4eba\u3001\u3042\u307e\u308a\u306e\u30b7\u30e7\u30c3\u30af\u306b\u753b\u50cf\u304b\u3089\u76ee\u3068\u795e\u7d4c\u304c\u96e2\u308c\u306a\u3044\u3002 \u60c5\u3051\u306a\u3084\u301c\u30fb\u30fb\u30fb ", title: "\u62e1\u5f35\u578b\u5fc3\u7b4b\u75c7\u7591\u3044\u304b\u3089\u78ba\u5b9a\u307e\u3067", cover_image_url: "/uimages/conts001/215/book_image/picture/936/thumb/_naA_gr.jpg", author: "p-con", id: 390, book_title: "\u6211\u304c\u5c45\u5019", mypalette_url: "http://lifepalette.jp/mypalette/215", article_url: "http://lifepalette.jp/palette_book/show/215/80/2755", disease: "\u62e1\u5f35\u578b\u5fc3\u7b4b\u75c7"},
{body: "\u5fd9\u3057\u3059\u304e\u305f\u3002\n", title: "\u5165\u9662\u6c7a\u5b9a", cover_image_url: "/uimages/conts001/118/book_image/picture/542/thumb/_PB040047.jpg", author: "JOY", id: 389, book_title: "\uff2a\uff2f\uff39\u3061\u3083\u3093\u306e\u60aa\u6027\u30ea\u30f3\u30d1\u816b", mypalette_url: "http://lifepalette.jp/mypalette/118", article_url: "http://lifepalette.jp/palette_book/show/118/37/746", disease: "\u60aa\u6027\u30ea\u30f3\u30d1\u816b"}], {
    interval: 5,
    container: "one-scene-content",
    prevButton: "one-scene-prev-button",
    nextButton: "one-scene-next-button"
  });
  control.run();
}
document.observe("dom:loaded", OneSceneControl.start);


