//=============================================================================
// RecollectionMode.js
// Copyright (c) 2015 rinne_grid
// This plugin is released under the MIT license.
// http://opensource.org/licenses/mit-license.php
//
// Version
// 1.0.0 2015/12/26 公開
// 1.1.0 2016/04/19 回想一覧にサムネイルを指定できるように対応
// 1.1.1 2016/05/03 セーブデータ20番目のスイッチが反映されない不具合を修正
//                  セーブデータ間のスイッチ共有オプション
//                  (share_recollection_switches)を追加
// 1.1.2 2016/05/09 回想用のCGリストのキーを数字から文字列に変更
//=============================================================================

/*:ja
 * @plugindesc 回想モード機能を追加します。
 * @author rinne_grid
 *
 *
 * @help このプラグインには、プラグインコマンドはありません。
 *
 */

//-----------------------------------------------------------------------------
// ◆ プラグイン設定
//-----------------------------------------------------------------------------
    var rngd_recollection_mode_settings = {
        //---------------------------------------------------------------------
        // ★ 回想モードで再生するBGMの設定をします
        //---------------------------------------------------------------------
        "rec_mode_bgm": {
            "bgm": {
                "name"  : "Dungeon1",
                "pan"   : 0,
                "pitch" : 100,
                "volume": 0
            }
        },
        //---------------------------------------------------------------------
        // ★ 回想CG選択ウィンドウの設定を指定します
        //---------------------------------------------------------------------
        "rec_mode_window" : {
            "x": 260,
            "y": 180,
            "recollection_title": "Recollection Mode",
            "str_select_recollection": "Watch",
            "str_select_cg": "See CG",
            "str_select_back_title": "Back to title"
        },
        //---------------------------------------------------------------------
        // ★ 回想リストウィンドウの設定を指定します
        //---------------------------------------------------------------------
        "rec_list_window": {
            // 1画面に表示する縦の数
            "item_height": 5,
            // 1画面に表示する横の数
            "item_width" : 5,
            // 1枚のCGに説明テキストを表示するかどうか
            "show_title_text": true,
            // タイトルテキストの表示位置(left:左寄せ、center:中央、right:右寄せ）
            "title_text_align": "center",
            // 閲覧したことのないCGの場合に表示するピクチャファイル名
            "never_watch_picture_name": "never_watch_picture",
            // 閲覧したことのないCGのタイトルテキスト
            "never_watch_title_text": "？？？"
        },
        //---------------------------------------------------------------------
        // ★ 回想用のCGを指定します
        //---------------------------------------------------------------------
  "rec_cg_set": {
    "1": {
      "title": "CG01",
      "common_event_id": 201,
      "switch_id": 102,
      "thumbnail": "sCG01_01",
      "pictures": [
        "CG01_01",
        "CG01_02",
        "CG01_03",
        "CG01_04",
        "CG01_05",
        "CG01_06",
        "CG01_07",
        "CG01_08",
        "CG01_09",
        "CG01_10",
        "CG01_10",
        "CG01_11",
        "CG01_12",
        "CG01_13"
      ]
    },
    "2": {
      "title": "CG02",
      "common_event_id": 202,
      "switch_id": 102,
      "thumbnail": "sCG02_01",
      "pictures": [
        "CG02_01",
        "CG02_02",
        "CG02_03",
        "CG02_04",
        "CG02_05",
        "CG02_06",
        "CG02_07",
        "CG02_08",
        "CG02_09",
        "CG02_10"
      ]
    },
    "3": {
      "title": "CG03",
      "common_event_id": 203,
      "switch_id": 103,
      "thumbnail": "sCG03_01",
      "pictures": [
        "CG03_01",
        "CG03_02",
        "CG03_03",
        "CG03_04",
        "CG03_05",
        "CG03_06",
        "CG03_07"
      ]
    },
    "4": {
      "title": "CG04",
      "common_event_id": 204,
      "switch_id": 104,
      "thumbnail": "sCG04_01",
      "pictures": [
        "CG04_01",
        "CG04_02",
        "CG04_03",
        "CG04_04",
        "CG04_05",
        "CG04_06",
        "CG04_07"
      ]
    },
    "5": {
      "title": "CG05",
      "common_event_id": 205,
      "switch_id": 105,
      "thumbnail": "sCG05_01",
      "pictures": [
        "CG05_01",
        "CG05_02",
        "CG05_03",
        "CG05_04",
        "CG05_05",
        "CG05_06",
        "CG05_07"
      ]
    },
    "6": {
      "title": "CG06",
      "common_event_id": 206,
      "switch_id": 106,
      "thumbnail": "sCG06_01",
      "pictures": [
        "CG06_01"
      ]
    },
    "7": {
      "title": "CG07",
      "common_event_id": 207,
      "switch_id": 107,
      "thumbnail": "sCG07_01",
      "pictures": [
        "CG07_01",
        "CG07_02",
        "CG07_03",
        "CG07_04",
        "CG07_05",
        "CG07_06",
        "CG07_07",
        "CG07_08",
        "CG07_09",
        "CG07_10",
        "CG07_11",
        "CG07_12",
        "CG07_13",
        "CG07_14",
        "CG07_15",
        "CG07_16",
        "CG07_17"
      ]
    },
    "8": {
      "title": "CG08",
      "common_event_id": 208,
      "switch_id": 108,
      "thumbnail": "sCG08_01",
      "pictures": [
        "CG08_01",
        "CG08_02",
        "CG08_03",
        "CG08_04",
        "CG08_05",
        "CG08_06",
        "CG08_07",
        "CG08_08",
        "CG08_09",
        "CG08_10",
        "CG08_11",
        "CG08_12",
        "CG08_13",
        "CG08_14",
        "CG08_15",
        "CG08_16"
      ]
    },
    "9": {
      "title": "CG09",
      "common_event_id": 209,
      "switch_id": 109,
      "thumbnail": "sCG09_01",
      "pictures": [
        "CG09_01",
        "CG09_02",
        "CG09_03",
        "CG09_04",
        "CG09_05",
        "CG09_06",
        "CG09_07",
        "CG09_08",
        "CG09_09",
        "CG09_10",
        "CG09_11",
        "CG09_12",
        "CG09_13",
        "CG09_14",
        "CG09_15",
        "CG09_16",
        "CG09_17",
        "CG09_18",
        "CG09_19",
        "CG09_20",
        "CG09_21"
      ]
    },
    "10": {
      "title": "CG10",
      "common_event_id": 210,
      "switch_id": 110,
      "thumbnail": "sCG10_01",
      "pictures": [
        "CG10_01",
        "CG10_02",
        "CG10_03",
        "CG10_04",
        "CG10_05",
        "CG10_06",
        "CG10_07",
        "CG10_08",
        "CG10_09",
        "CG10_10"
      ]
    },
    "11": {
      "title": "CG11",
      "common_event_id": 211,
      "switch_id": 111,
      "thumbnail": "sCG11_01",
      "pictures": [
        "CG11_01",
        "CG11_02",
        "CG11_03",
        "CG11_04",
        "CG11_05",
        "CG11_06",
        "CG11_07",
        "CG11_08",
        "CG11_09",
        "CG11_10",
        "CG11_11",
        "CG11_12",
        "CG11_13"
      ]
    },
    "12": {
      "title": "CG12",
      "common_event_id": 212,
      "switch_id": 112,
      "thumbnail": "sCG12_01",
      "pictures": [
        "CG12_01",
        "CG12_02",
        "CG12_03",
        "CG12_04",
        "CG12_05",
        "CG12_06"
      ]
    },
    "13": {
      "title": "CG13",
      "common_event_id": 213,
      "switch_id": 113,
      "thumbnail": "sCG13_01",
      "pictures": [
        "CG13_01",
        "CG13_02",
        "CG13_03",
        "CG13_04",
        "CG13_05",
        "CG13_06",
        "CG13_07"
      ]
    },
    "14": {
      "title": "CG14",
      "common_event_id": 214,
      "switch_id": 114,
      "thumbnail": "sCG14_01",
      "pictures": [
        "CG14_01"
      ]
    },
    "15": {
      "title": "CG15",
      "common_event_id": 215,
      "switch_id": 115,
      "thumbnail": "sCG15_01",
      "pictures": [
        "CG15_01",
        "CG15_02",
        "CG15_03",
        "CG15_04",
        "CG15_05",
        "CG15_06",
        "CG15_07",
        "CG15_08"
      ]
    },
    "16": {
      "title": "CG16",
      "common_event_id": 216,
      "switch_id": 116,
      "thumbnail": "sCG16_01",
      "pictures": [
        "CG16_01",
        "CG16_02",
        "CG16_03",
        "CG16_04",
        "CG16_05",
        "CG16_06"
      ]
    },
    "17": {
      "title": "CG17",
      "common_event_id": 217,
      "switch_id": 117,
      "thumbnail": "sCG17_01",
      "pictures": [
        "CG17_01",
        "CG17_02",
        "CG17_03",
        "CG17_04",
        "CG17_05",
        "CG17_06",
        "CG17_07",
        "CG17_08",
        "CG17_09",
        "CG17_10",
        "CG17_11",
        "CG17_12",
        "CG17_13",
        "CG17_14"
      ]
    },
    "18": {
      "title": "CG18",
      "common_event_id": 218,
      "switch_id": 118,
      "thumbnail": "sCG18_01",
      "pictures": [
        "CG18_01",
        "CG18_02",
        "CG18_03",
        "CG18_04",
        "CG18_05",
        "CG18_06",
        "CG18_07"
      ]
    },
    "19": {
      "title": "CG19",
      "common_event_id": 219,
      "switch_id": 119,
      "thumbnail": "sCG19_01",
      "pictures": [
        "CG19_01",
        "CG19_02",
        "CG19_03",
        "CG19_04",
        "CG19_05",
        "CG19_06",
        "CG19_07",
        "CG19_08"
      ]
    },
    "20": {
      "title": "CG20",
      "common_event_id": 220,
      "switch_id": 120,
      "thumbnail": "sCG20_01",
      "pictures": [
        "CG20_01"
      ]
    },
    "21": {
      "title": "CG21",
      "common_event_id": 221,
      "switch_id": 121,
      "thumbnail": "sCG21_01",
      "pictures": [
        "CG21_01",
        "CG21_02",
        "CG21_03",
        "CG21_04",
        "CG21_05",
        "CG21_06",
        "CG21_07",
        "CG21_08",
        "CG21_09",
        "CG21_10"
      ]
    },
    "22": {
      "title": "CG22",
      "common_event_id": 222,
      "switch_id": 122,
      "thumbnail": "sCG22_01",
      "pictures": [
        "CG22_01",
        "CG22_02",
        "CG22_03",
        "CG22_04",
        "CG22_05",
        "CG22_06",
        "CG22_07",
        "CG22_08",
        "CG22_09",
        "CG22_10",
        "CG22_11",
        "CG22_12"
      ]
    },
    "23": {
      "title": "CG23",
      "common_event_id": 223,
      "switch_id": 123,
      "thumbnail": "sCG23_01",
      "pictures": [
        "CG23_01",
        "CG23_02",
        "CG23_03",
        "CG23_04",
        "CG23_05",
        "CG23_06",
        "CG23_07",
        "CG23_08",
        "CG23_09",
        "CG23_10"
      ]
    },
    "24": {
      "title": "CG24",
      "common_event_id": 224,
      "switch_id": 124,
      "thumbnail": "sCG24_01",
      "pictures": [
        "CG24_01",
        "CG24_02",
        "CG24_03",
        "CG24_04",
        "CG24_05",
        "CG24_06",
        "CG24_07",
        "CG24_08",
        "CG24_09",
        "CG24_10"
      ]
    },
    "25": {
      "title": "CG25",
      "common_event_id": 225,
      "switch_id": 125,
      "thumbnail": "sCG25_01",
      "pictures": [
        "CG25_01",
        "CG25_02",
        "CG25_03",
        "CG25_04",
        "CG25_05",
        "CG25_06",
        "CG25_07",
        "CG25_08",
        "CG25_09"
      ]
    },
    "26": {
      "title": "CG26",
      "common_event_id": 226,
      "switch_id": 126,
      "thumbnail": "sCG26_01",
      "pictures": [
        "CG26_01",
        "CG26_02",
        "CG26_03",
        "CG26_04"
      ]
    },
    "27": {
      "title": "CG27",
      "common_event_id": 227,
      "switch_id": 127,
      "thumbnail": "sCG27_01",
      "pictures": [
        "CG27_01",
        "CG27_02",
        "CG27_03",
        "CG27_04",
        "CG27_05",
        "CG27_06",
        "CG27_07"
      ]
    },
    "28": {
      "title": "CG28",
      "common_event_id": 228,
      "switch_id": 128,
      "thumbnail": "sCG28_01",
      "pictures": [
        "CG28_01",
        "CG28_02"
      ]
    },
    "29": {
      "title": "CG29",
      "common_event_id": 229,
      "switch_id": 129,
      "thumbnail": "sCG29_01",
      "pictures": [
        "CG29_01",
        "CG29_02",
        "CG29_03",
        "CG29_04",
        "CG29_05",
        "CG29_06",
        "CG29_07",
        "CG29_08",
        "CG29_09",
        "CG29_10",
        "CG29_11",
        "CG29_12",
        "CG29_13",
        "CG29_14"
      ]
    },
    "30": {
      "title": "CG30",
      "common_event_id": 230,
      "switch_id": 130,
      "thumbnail": "sCG30_01",
      "pictures": [
        "CG30_01",
        "CG30_02",
        "CG30_03",
        "CG30_04",
        "CG30_05",
        "CG30_06",
        "CG30_07",
        "CG30_08",
        "CG30_09",
        "CG30_10",
        "CG30_11",
        "CG30_12",
        "CG30_13"
      ]
    },
    "31": {
      "title": "CG31",
      "common_event_id": 231,
      "switch_id": 130,
      "thumbnail": "sCG31_01",
      "pictures": [
        "CG31_01",
        "CG31_02",
        "CG31_03",
        "CG31_04",
        "CG31_05",
        "CG31_06",
        "CG31_07",
        "CG31_08",
        "CG31_09",
        "CG31_10",
        "CG31_11",
        "CG31_12",
        "CG31_13",
        "CG31_14",
        "CG31_15",
        "CG31_16",
        "CG31_17",
        "CG31_18",
        "CG31_19",
        "CG31_20",
        "CG31_21",
        "CG31_22",
        "CG31_23",
        "CG31_24"
      ]
    },
    "32": {
      "title": "CG32",
      "common_event_id": 232,
      "switch_id": 132,
      "thumbnail": "sCG32_01",
      "pictures": [
        "CG32_01",
        "CG32_02",
        "CG32_03",
        "CG32_04",
        "CG32_05",
        "CG32_06",
        "CG32_07",
        "CG32_08",
        "CG32_09",
        "CG32_10",
        "CG32_11",
        "CG32_12",
        "CG32_13",
        "CG32_14",
        "CG32_15",
        "CG32_16",
        "CG32_17",
        "CG32_18",
        "CG32_19",
        "CG32_20",
        "CG32_21",
        "CG32_22"
      ]
    },
    "33": {
      "title": "CG33",
      "common_event_id": 233,
      "switch_id": 133,
      "thumbnail": "sCG33_01",
      "pictures": [
        "CG33_01",
        "CG33_02",
        "CG33_03",
        "CG33_04",
        "CG33_05",
        "CG33_06",
        "CG33_07",
        "CG33_08",
        "CG33_09",
        "CG33_10",
        "CG33_11"
      ]
    },
    "34": {
      "title": "CG34",
      "common_event_id": 234,
      "switch_id": 134,
      "thumbnail": "sCG34_01",
      "pictures": [
        "CG34_01",
        "CG34_02",
        "CG34_03",
        "CG34_04"
      ]
    },
    "35": {
      "title": "CG35",
      "common_event_id": 235,
      "switch_id": 135,
      "thumbnail": "sCG35_01",
      "pictures": [
        "CG35_01",
        "CG35_02"
      ]
    },
    "36": {
      "title": "CG36",
      "common_event_id": 236,
      "switch_id": 136,
      "thumbnail": "sCG36_01",
      "pictures": [
        "CG36_01"
      ]
    },
    "37": {
      "title": "CG37",
      "common_event_id": 237,
      "switch_id": 150,
      "thumbnail": "sCG37_01",
      "pictures": [
        "TVバイプ01",
        "TVバイプ02",
        "TVブレイザー出演01",
        "TVブレイザー出演02",
        "TVブレイザー出演03",
        "TVブレイザー出演04",
        "TVブレイザー出演05",
        "TVブレイザー出演06",
        "TVブレイザー出演07",
        "TV犬ライブ"
      ]
    },
    "38": {
      "title": "CG38",
      "common_event_id": 238,
      "switch_id": 150,
      "thumbnail": "sCG38_01",
      "pictures": [
        "パペットピンチ",
        "パペット死",
        "プランターピンチ",
        "プランター死",
        "強制失禁",
        "強制脱糞",
        "人間死",
        "脱衣ピンチ",
        "脱衣全裸",
        "脱衣全裸2",
        "脱衣半裸",
        "奴隷放尿"
      ]
    },
    "39": {
      "title": "CG39",
      "common_event_id": 239,
      "switch_id": 150,
      "thumbnail": "sCG39_01",
      "pictures": [
        "パンツカット",
        "スカート盗撮"
      ]
    },
    "40": {
      "title": "CG40",
      "common_event_id": 240,
      "switch_id": 150,
      "thumbnail": "sCG40_01",
      "pictures": [
        "爆発",
        "必殺カット"
      ]
    }
  },


        //---------------------------------------------------------------------
        // ★ 回想時に一時的に利用するマップIDを指定します
        //---------------------------------------------------------------------
        // 通常は何もないマップを指定します
        //---------------------------------------------------------------------
        "sandbox_map_id": 076,
        //---------------------------------------------------------------------
        // ★ 回想用スイッチをセーブデータ間で共有するかどうかを指定します
        //---------------------------------------------------------------------
        // パラメータの説明
        // true:
        //      回想用スイッチを共有します。
        //
        //      例1：セーブ1で回想スイッチ1, 2, 3がONとする
        //          ニューゲームで開始し、セーブ1を上書きする
        //          →セーブ1の回想スイッチ1, 2, 3はONのままとなる。
        //
        //      例2: セーブ1で回想スイッチ1, 2, 3がONとする
        //          セーブ1をロードし、セーブ2を保存する
        //          セーブ2で回想スイッチ1, 2, 3, 7がONとする
        //          セーブ1, セーブ2それぞれで、回想スイッチ1, 2, 3, 7がONとなる
        //
        // false:
        //      回想用スイッチを共有しません
        //
        // すべてのセーブデータを削除した場合にのみ、スイッチがリセットされます
        //---------------------------------------------------------------------
        "share_recollection_switches": false
    };

    function rngd_hash_size(obj) {
        var cnt = 0;
        for(var o in obj) {
            cnt++;
        }
        return cnt;
    }

//-----------------------------------------------------------------------------
// ◆ Scene関数
//-----------------------------------------------------------------------------

    //=========================================================================
    // ■ Scene_Recollection
    //=========================================================================
    // 回想用のシーン関数です
    //=========================================================================
    function Scene_Recollection() {
        this.initialize.apply(this, arguments);
    }

    Scene_Recollection.prototype = Object.create(Scene_Base.prototype);
    Scene_Recollection.prototype.constructor = Scene_Recollection;

    Scene_Recollection.prototype.initialize = function() {
        Scene_Base.prototype.initialize.call(this);
    };

    Scene_Recollection.prototype.create = function() {
        Scene_Base.prototype.create.call(this);
        this.createWindowLayer();
        this.createCommandWindow();
    };

    // 回想モードのカーソル
    Scene_Recollection.rec_list_index = 0;

    // 回想モードの再読み込み判定用 true: コマンドウィンドウを表示せず回想リストを表示 false:コマンドウィンドウを表示
    Scene_Recollection.reload_rec_list = false;

    Scene_Recollection.prototype.createCommandWindow = function() {

        if(Scene_Recollection.reload_rec_list) {
            // 回想モード選択ウィンドウ
            this._rec_window = new Window_RecollectionCommand();
            this._rec_window.setHandler('select_recollection', this.commandShowRecollection.bind(this));
            this._rec_window.setHandler('select_cg', this.commandShowCg.bind(this));
            this._rec_window.setHandler('select_back_title', this.commandBackTitle.bind(this));

            // リロードの場合：選択ウィンドウを非表示にする。通常はここがtrue
            this._rec_window.visible = false;
            this._rec_window.deactivate();
            this.addWindow(this._rec_window);

            // 回想リスト
            this._rec_list = new Window_RecList(0, 0, Graphics.width, Graphics.height);

            // リロードの場合：回想リストを表示にする。通常はここがfalse
            this._rec_list.visible = true;
            this._rec_list.setHandler('ok', this.commandDoRecMode.bind(this));
            this._rec_list.setHandler('cancel', this.commandBackSelectMode.bind(this));
            this._mode = "recollection";
            this._rec_list.activate();
            this._rec_list.select(Scene_Recollection.rec_list_index);

            this.addWindow(this._rec_list);

            // CG参照用ダミーコマンド
            this._dummy_window = new Window_Command(0, 0);
            this._dummy_window.deactivate();
            this._dummy_window.visible = false;
            this._dummy_window.setHandler('ok', this.commandDummyOk.bind(this));
            this._dummy_window.setHandler('cancel', this.commandDummyCancel.bind(this));
            this._dummy_window.addCommand('next', 'ok');
            this.addWindow(this._dummy_window);

            Scene_Recollection.reload_rec_list = false;

        } else {
            // 回想モード選択ウィンドウ
            this._rec_window = new Window_RecollectionCommand();
            this._rec_window.setHandler('select_recollection', this.commandShowRecollection.bind(this));
            this._rec_window.setHandler('select_cg', this.commandShowCg.bind(this));
            this._rec_window.setHandler('select_back_title', this.commandBackTitle.bind(this));
            this.addWindow(this._rec_window);

            // 回想リスト
            this._rec_list = new Window_RecList(0, 0, Graphics.width, Graphics.height);
            this._rec_list.visible = false;
            this._rec_list.setHandler('ok', this.commandDoRecMode.bind(this));
            this._rec_list.setHandler('cancel', this.commandBackSelectMode.bind(this));
            this._rec_list.select(Scene_Recollection.rec_list_index);
            this.addWindow(this._rec_list);

            // CG参照用ダミーコマンド
            this._dummy_window = new Window_Command(0, 0);
            this._dummy_window.deactivate();
            this._dummy_window.visible = false;
            this._dummy_window.setHandler('ok', this.commandDummyOk.bind(this));
            this._dummy_window.setHandler('cancel', this.commandDummyCancel.bind(this));
            this._dummy_window.addCommand('next', 'ok');
            this.addWindow(this._dummy_window);
        }

    };

    //-------------------------------------------------------------------------
    // ● 開始処理
    //-------------------------------------------------------------------------
    Scene_Recollection.prototype.start = function() {
        Scene_Base.prototype.start.call(this);
        this._rec_window.refresh();
        this._rec_list.refresh();
        AudioManager.playBgm(rngd_recollection_mode_settings.rec_mode_bgm.bgm);
        Scene_Recollection._rngd_recollection_doing = false;
    };

    //-------------------------------------------------------------------------
    // ● 更新処理
    //-------------------------------------------------------------------------
    Scene_Recollection.prototype.update = function() {
        Scene_Base.prototype.update.call(this);

    };

    //-------------------------------------------------------------------------
    // ● 「回想を見る」を選択した際のコマンド
    //-------------------------------------------------------------------------
    Scene_Recollection.prototype.commandShowRecollection = function() {
        // モードウィンドウの無効化とリストウィンドウの有効化
        this.do_exchange_status_window(this._rec_window, this._rec_list);
        this._mode = "recollection";
    };

    //-------------------------------------------------------------------------
    // ● 「CGを見る」を選択した際のコマンド
    //-------------------------------------------------------------------------
    Scene_Recollection.prototype.commandShowCg = function() {
        this.do_exchange_status_window(this._rec_window, this._rec_list);
        this._mode = "cg";
    };

    //-------------------------------------------------------------------------
    // ● 「タイトルに戻る」を選択した際のコマンド
    //-------------------------------------------------------------------------
    Scene_Recollection.prototype.commandBackTitle = function() {
        Scene_Recollection.rec_list_index = 0;
        SceneManager.goto(Scene_Title);
    };

    //-------------------------------------------------------------------------
    // ● 回想orCGモードから「キャンセル」して前の画面に戻った場合のコマンド
    //-------------------------------------------------------------------------
    Scene_Recollection.prototype.commandBackSelectMode = function() {
        this.do_exchange_status_window(this._rec_list, this._rec_window);
    };

    //-------------------------------------------------------------------------
    // ● 回想orCGモードにおいて、実際の回想orCGを選択した場合のコマンド
    //-------------------------------------------------------------------------
    Scene_Recollection.prototype.commandDoRecMode = function() {
        var target_index = this._rec_list.index() + 1;
        Scene_Recollection.rec_list_index = target_index - 1;

        if (this._rec_list.is_valid_picture(this._rec_list.index() + 1)) {
            // 回想モードの場合
            if (this._mode == "recollection") {
                Scene_Recollection._rngd_recollection_doing = true;

                DataManager.setupNewGame();
                $gamePlayer.setTransparent(255);
                this.fadeOutAll();
                // TODO: パーティを透明状態にする

                //$dataSystem.optTransparent = false;
                $gameTemp.reserveCommonEvent(rngd_recollection_mode_settings.rec_cg_set[target_index]["common_event_id"]);
                $gamePlayer.reserveTransfer(rngd_recollection_mode_settings.sandbox_map_id, 0, 0, 0);
                SceneManager.push(Scene_Map);

                // CGモードの場合
            } else if (this._mode == "cg") {
                this._cg_sprites = [];
                this._cg_sprites_index = 0;

                // シーン画像をロードする
                rngd_recollection_mode_settings.rec_cg_set[target_index].pictures.forEach(function (name) {
                    var sp = new Sprite();
                    sp.bitmap = ImageManager.loadPicture(name);
                    // 最初のSprite以外は見えないようにする
                    if (this._cg_sprites.length > 0) {
                        sp.visible = false;
                    }

                    // TODO: 画面サイズにあわせて、拡大・縮小すべき
                    this._cg_sprites.push(sp);
                    this.addChild(sp);

                }, this);

                this.do_exchange_status_window(this._rec_list, this._dummy_window);
                this._dummy_window.visible = false;
            }
        } else {
            this._rec_list.activate();
        }
    };

    Scene_Recollection.prototype.commandDummyOk = function() {

        if(this._cg_sprites_index < this._cg_sprites.length - 1) {
            this._cg_sprites[this._cg_sprites_index].visible = false;
            this._cg_sprites_index++;
            this._cg_sprites[this._cg_sprites_index].visible = true;

            this._dummy_window.activate();
        } else {
            this.commandDummyCancel();
        }
    };

    Scene_Recollection.prototype.commandDummyCancel = function() {
        this._cg_sprites.forEach(function(obj) {
            obj.visible = false;
            obj = null;
        });
        this.do_exchange_status_window(this._dummy_window, this._rec_list);
    };

    // コモンイベントから呼び出す関数
    Scene_Recollection.prototype.rngd_exit_scene = function() {
        if(Scene_Recollection._rngd_recollection_doing) {
            // Window_RecListを表示する
            Scene_Recollection.reload_rec_list = true;
            SceneManager.push(Scene_Recollection);
        }
    };

    //-------------------------------------------------------------------------
    // ● ウィンドウの無効化と有効化
    //-------------------------------------------------------------------------
    // win1: 無効化するウィンドウ
    // win2: 有効化するウィンドウ
    //-------------------------------------------------------------------------
    Scene_Recollection.prototype.do_exchange_status_window = function(win1, win2) {
        win1.deactivate();
        win1.visible = false;
        win2.activate();
        win2.visible = true;
    };
    //-------------------------------------------------------------------------
    // ● セーブ・ロード・ニューゲーム時に必要なスイッチをONにする
    //-------------------------------------------------------------------------
    Scene_Recollection.setRecollectionSwitches = function() {
        // 各セーブデータを参照し、RecollectionMode用のスイッチを検索する
        // スイッチが一つでもONになっている場合は回想をONにする
        for(var i = 1; i <= DataManager.maxSavefiles(); i++) {
            var data = null;
            try {
                data = StorageManager.loadFromLocalFile(i);
            } catch(e) {
                data = StorageManager.loadFromWebStorage(i);
            }
            if(data) {
                var save_data_obj = JsonEx.parse(data);
                var rec_cg_max = rngd_hash_size(rngd_recollection_mode_settings.rec_cg_set);

                for(var j = 0; j < rec_cg_max; j++) {
                    var cg = rngd_recollection_mode_settings.rec_cg_set[j+1];
                    if(save_data_obj["switches"]._data[cg.switch_id] &&
                        save_data_obj["switches"]._data[cg.switch_id] == true) {
                        $gameSwitches.setValue(cg.switch_id, true);
                    }
                }
            }
        }
    };

//-----------------------------------------------------------------------------
// ◆ Window関数
//-----------------------------------------------------------------------------

    //=========================================================================
    // ■ Window_RecollectionCommand
    //=========================================================================
    // 回想モードかCGモードを選択するウィンドウです
    //=========================================================================
    function Window_RecollectionCommand() {
        this.initialize.apply(this, arguments);
    }

    Window_RecollectionCommand.prototype = Object.create(Window_Command.prototype);
    Window_RecollectionCommand.prototype.constructor = Window_RecollectionCommand;

    Window_RecollectionCommand.prototype.initialize = function() {
        Window_Command.prototype.initialize.call(this, 0, 0);
        this.x = rngd_recollection_mode_settings.rec_mode_window.x;
        this.y = rngd_recollection_mode_settings.rec_mode_window.y;

    };

    Window_RecollectionCommand.prototype.makeCommandList = function() {
        Window_Command.prototype.makeCommandList.call(this);
        this.addCommand(rngd_recollection_mode_settings.rec_mode_window.str_select_recollection, "select_recollection");
        this.addCommand(rngd_recollection_mode_settings.rec_mode_window.str_select_cg, "select_cg");
        this.addCommand(rngd_recollection_mode_settings.rec_mode_window.str_select_back_title, "select_back_title");
    };

    //=========================================================================
    // ■ Window_RecollectionList
    //=========================================================================
    // 回想またはCGを選択するウィンドウです
    //=========================================================================
    function Window_RecList() {
        this.initialize.apply(this, arguments);
    }

    Window_RecList.prototype = Object.create(Window_Selectable.prototype);
    Window_RecList.prototype.constructor = Window_RecList;

    //-------------------------------------------------------------------------
    // ● 初期化処理
    //-------------------------------------------------------------------------
    Window_RecList.prototype.initialize = function(x, y, width, height) {
        Window_Selectable.prototype.initialize.call(this, x, y, width, height);
        this.windowWidth = width;
        this.windowHeight = height;
        this.select(0);
        this._formationMode = false;
        this.get_global_variables();
        this.refresh();

    };

    Window_RecList.prototype.maxItems = function() {
        return rngd_hash_size(rngd_recollection_mode_settings.rec_cg_set);
    };

    Window_RecList.prototype.itemHeight = function() {
        return (this.height - this.standardPadding()) / rngd_recollection_mode_settings.rec_list_window.item_height;
    };

    //Window_RecList.prototype.maxRows = function() {
    //    return rngd_recollection_mode_settings.rec_list_window.item_height;
    //};

    Window_RecList.prototype.maxCols = function() {
        return rngd_recollection_mode_settings.rec_list_window.item_width;
    };

    Window_RecList.prototype.maxPageRows = function() {
        var pageHeight = this.height;// - this.padding * 2;
        return Math.floor(pageHeight / this.itemHeight());
    };

    Window_RecList.prototype.drawItem = function(index) {
        // TODO: itemWidthにあわせたサイズに拡大・縮小する
        // 1番目のCGセットを取得
        var rec_cg = rngd_recollection_mode_settings.rec_cg_set[index+1];
        var rect = this.itemRect(index);
        var text_height = 0;
        if(rngd_recollection_mode_settings.rec_list_window.show_title_text) {
            if(this._global_variables["switches"][rec_cg.switch_id]) {
                this.contents.drawText(rec_cg.title, rect.x + 4, rect.y + 4, this.itemWidth(), 32,
                    rngd_recollection_mode_settings.rec_list_window.title_text_align);
            } else {
                this.contents.drawText(rngd_recollection_mode_settings.rec_list_window.never_watch_title_text,
                    rect.x + 4, rect.y + 4, this.itemWidth(), 32,
                    rngd_recollection_mode_settings.rec_list_window.title_text_align);
            }
            text_height = 32;
        }

        // CGセットのスイッチ番号が、全てのセーブデータを走査した後にTrueであればピクチャ表示
        if(this._global_variables["switches"][rec_cg.switch_id]) {

            var thumbnail_file_name = rec_cg.pictures[0];
            if(rec_cg.thumbnail !== undefined && rec_cg.thumbnail !== null) {
                thumbnail_file_name = rec_cg.thumbnail;
            }

            this.drawRecollection(thumbnail_file_name, 0, 0,
                this.itemWidth() - 36, this.itemHeight() - 8 - text_height, rect.x + 16, rect.y + 4 +text_height);


        } else {
            this.drawRecollection(rngd_recollection_mode_settings.rec_list_window.never_watch_picture_name,
                    0, 0 , this.itemWidth() - 36,
                    this.itemHeight() - 8 - text_height, rect.x + 16, rect.y + 4 + text_height);

        }

    };

    //-------------------------------------------------------------------------
    // ● 全てのセーブデータを走査し、対象のシーンスイッチ情報を取得する
    //-------------------------------------------------------------------------
    Window_RecList.prototype.get_global_variables = function() {
        this._global_variables = {
            "switches": {}
        };
        var maxSaveFiles = DataManager.maxSavefiles();
        for(var i = 1; i <= maxSaveFiles; i++) {
            if(DataManager.loadGame(i)) {
                var rec_cg_max = rngd_hash_size(rngd_recollection_mode_settings.rec_cg_set);

                for(var j = 0; j < rec_cg_max; j++) {
                    var cg = rngd_recollection_mode_settings.rec_cg_set[j+1];
                    if($gameSwitches._data[cg.switch_id]) {
                        this._global_variables["switches"][cg.switch_id] = true;
                    }
                }
            }
        }
    };
    //-------------------------------------------------------------------------
    // ● index番目に表示された回想orCGが有効かどうか判断する
    //-------------------------------------------------------------------------
    Window_RecList.prototype.is_valid_picture = function(index) {
        // CG情報の取得と対象スイッチの取得
        var _rec_cg_obj = rngd_recollection_mode_settings.rec_cg_set[index];
        return ( this._global_variables["switches"][_rec_cg_obj.switch_id] == true);

    };


(function(){

//-----------------------------------------------------------------------------
// ◆ 組み込み関数Fix
//-----------------------------------------------------------------------------

    Window_Base.prototype.drawRecollection = function(bmp_name, x, y, width, height, dx, dy) {
        var bmp = ImageManager.loadPicture(bmp_name);

        var _width = width;
        var _height = height;
        if(_width > bmp.width) {
            _width = bmp.width - 1;
        }

        if(_height > bmp.height) {
            _height = bmp.height - 1;
        }
        this.contents.blt(bmp, x, y, _width, _height, dx, dy);
    };

    var Window_TitleCommand_makeCommandList =
        Window_TitleCommand.prototype.makeCommandList;

    Window_TitleCommand.prototype.makeCommandList = function() {
        Window_TitleCommand_makeCommandList.call(this);
        this.clearCommandList();
        this.addCommand(TextManager.newGame,   'newGame');
        this.addCommand(TextManager.continue_, 'continue', this.isContinueEnabled());
        this.addCommand(rngd_recollection_mode_settings.rec_mode_window.recollection_title, 'recollection');
        this.addCommand(TextManager.options,   'options');
    };

    Scene_Title.prototype.commandRecollection = function() {
        SceneManager.push(Scene_Recollection);
    };

    var Scene_Title_createCommandWindow = Scene_Title.prototype.createCommandWindow;
    Scene_Title.prototype.createCommandWindow = function() {
        Scene_Title_createCommandWindow.call(this);
        this._commandWindow.setHandler('recollection', this.commandRecollection.bind(this));
    };

    // セーブデータ共有オプションが指定されている場合のみ、カスタマイズ
    if(rngd_recollection_mode_settings["share_recollection_switches"]) {
        DataManager.makeSaveContents = function() {
            // A save data does not contain $gameTemp, $gameMessage, and $gameTroop.

            Scene_Recollection.setRecollectionSwitches();

            var contents = {};
            contents.system       = $gameSystem;
            contents.screen       = $gameScreen;
            contents.timer        = $gameTimer;
            contents.switches     = $gameSwitches;
            contents.variables    = $gameVariables;
            contents.selfSwitches = $gameSelfSwitches;
            contents.actors       = $gameActors;
            contents.party        = $gameParty;
            contents.map          = $gameMap;
            contents.player       = $gamePlayer;

            return contents;
        };

        DataManager.extractSaveContents = function(contents) {
            $gameSystem        = contents.system;
            $gameScreen        = contents.screen;
            $gameTimer         = contents.timer;
            $gameSwitches      = contents.switches;
            $gameVariables     = contents.variables;
            $gameSelfSwitches  = contents.selfSwitches;
            $gameActors        = contents.actors;
            $gameParty         = contents.party;
            $gameMap           = contents.map;
            $gamePlayer        = contents.player;

            Scene_Recollection.setRecollectionSwitches();
        };

        DataManager.setupNewGame = function() {
            this.createGameObjects();
            Scene_Recollection.setRecollectionSwitches();
            this.selectSavefileForNewGame();
            $gameParty.setupStartingMembers();
            $gamePlayer.reserveTransfer($dataSystem.startMapId,
                $dataSystem.startX, $dataSystem.startY);
            Graphics.frameCount = 0;
        };
    }

})();
