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

// 今日のワンシーンモジュールの名前空間
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: "\u5148\u751f\u306f\u3061\u3087\u3063\u3068\u79c1\u304b\u3089\u76ee\u3092\u80cc\u3051\u3001\u3061\u3087\u3063\u3068\u8a00\u3044\u306b\u304f\u305d\u3046\u306b\u8a00\u3044\u307e\u3057\u305f\u3002\n\u300c\u30fb\u30fb\u30fb\u30fb\u3093?\u30fb\u30fb\u30fb\u60aa\u6027\u3060\u3063\u305f\u3089\u30fb\u30fb\u30fb\u30ac\u30f3\u30fb\u30fb\u30fb\u304b\u306a\u3041\u30fb\u30fb\u30fb\u300d", title: "\u25c6\u3000\u5927\u5b66\u75c5\u9662\u3067\u306e\u5b50\u5bae\u93e1\u691c\u67fb\u3000\u25c6", cover_image_url: "/uimages/conts001/197/book_image/picture/656/thumb/_htC_gr.jpg", author: "\u3061\u3043", id: 360, book_title: "cheeez\uff01\u3061\u3043\u306e\u5b50\u5bae\u304c\u3093\u4f53\u9a13\u8a18", mypalette_url: "http://lifepalette.jp/mypalette/197", article_url: "http://lifepalette.jp/palette_book/show/197/71/2190", disease: "\u5b50\u5bae\u4f53\u764c"},
{body: "\u5148\u751f\u306e\u4eca\u307e\u3067\u306e\u8a3a\u5bdf\u306f\u306a\u3093\u3060\u3063\u305f\u3093\u3067\u3059\u304b\uff1f\uff01", title: "\uff12\uff0e\u78ba\u5b9a\u8a3a\u65ad\u307e\u3067\u306e\u30b4\u30bf\u30b4\u30bf", cover_image_url: "/uimages/conts001/34/book_image/picture/846/thumb/_ayG_pk.jpg", author: "himiko", id: 359, book_title: "\u79c1\u306e\u4e73\u304c\u3093\u95d8\u75c5\u8a18", mypalette_url: "http://lifepalette.jp/mypalette/34", article_url: "http://lifepalette.jp/palette_book/show/34/73/2582", disease: "\u8853\u5f8c\u4e73\u764c"},
{body: "\u5148\u751f\u306b\u805e\u304d\u305f\u3044\u4e8b\u306f\u3001\u524d\u3082\u3063\u3066\u3001\u982d\u306e\u4e2d\u3067\u307e\u3068\u3081\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002", title: "\u5916\u6765", cover_image_url: "/uimages/conts001/123/book_image/picture/594/thumb/_htC_gr.jpg", author: "\u30a8\u30e0", id: 358, 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/1159", disease: "\u5b50\u5bae\u764c"},
{body: "\u5148\u751f\u3001\u4eba\u306e\u9854\u3092\u898b\u308b\u306a\u308a \u3000\u300c\u60aa\u3044\u3001\u3068\u3063\u3066\u3082\u60aa\u3044\u3088\u300d\n\u305d\u308a\u3083\u3041\u9854\u3082\u60aa\u3051\u308a\u3083\u30b9\u30bf\u30a4\u30eb\u3082\u60aa\u3044\u3002 ", 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: 357, 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: "\u5148\u751f\u3000\u201c\u6f0f\u3089\u3057\u3061\u3083\u3063\u305f\u3002\u3059\u307f\u307e\u305b\u3093\u3002\u201d\u3000\u3063\u3066\u4f55\uff1f\uff1f ", 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: 356, 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: "\u820c\u304c\u304a\u304b\u3057\u3044\u3002", title: "\u3010 \uff13\u30af\u30fc\u30eb\u76ee \u3011\uff12\uff10\uff10\uff14\uff0e\uff19\uff0e\uff12\uff10\uff08\u6708\uff09\u3000\u3000\u3000\u3000\u3000\u5473\u899a\u7570\u5e38", cover_image_url: "/uimages/conts001/118/book_image/picture/542/thumb/_PB040047.jpg", author: "JOY", id: 355, 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/792", disease: "\u60aa\u6027\u30ea\u30f3\u30d1\u816b"},
{body: "\u751f\u6daf\u306e\u53cb\u30fb\u30fb\u30fb", 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: 354, 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"}], {
    interval: 5,
    container: "one-scene-content",
    prevButton: "one-scene-prev-button",
    nextButton: "one-scene-next-button"
  });
  control.run();
}
document.observe("dom:loaded", OneSceneControl.start);

