/* -----------------------------------------------------------------------------

WritingSlideController

----------------------------------------------------------------------------- */
WritingSlideController = Class.extend(
  SlideController,
  {

    //プロパティ----------------------------------------------------------------
    context     : {},           // initialize時必須
    type        : "",           // initialize時必須
    slideid     : "",           // initialize時必須
    nextflash   : "",           // SWF・MP3が終了したときの変数
    data        : {},           // ※タイプがquestion以外のSlideでは無効なプロパティ
    userAnswer  : [],           // 解答セット
    numOfWords  : [],           // 解答語数セット
    elapsed_time: [],           // 解答時間セット
    firstCount  : "true",
    q_firstCount: "true",

    //メソッド------------------------------------------------------------------
    initialize: function(slideId,param) {
      this.parent( slideId );
      this.slideContent = $("div", $("#" + slideId));
    },

    //全スライド共通初期化処理
    commonInitialize: function(slideId,param) {
      this.parent( slideId );
      this.data = param.data;

    },

    setContext: function( context ){
      //Slideを統べる親クラス（Context）をセット
      //  ※SlideからContextにはcontext.notifyメソッド経由で通知すること

      this.context = context;

    },

    play: function(){
      //このSlideを表示するときにContextから呼ばれるメソッドです

      alert("この機能は未実装です：play");

    },

    stop: function(){
      //このSlideを抜けるときにContextから呼ばれるメソッドです

      alert("この機能は未実装です：stop");

    },

    dispQuestion: function(){

      alert("この機能は未実装です：dispQuestion");

    },

    //進捗・成績の操作---------------------------------------------------------
    setFinish: function(){

      r.done();

    },

    setResult: function(){
      //同期通信でカレントスライドの成績・進捗を更新する。PAUSE、FINISHなど確実にResultをpushしたいときはこっち
      if( this.userAnswer[this.partQuestionCount] != "" && this.userAnswer[this.partQuestionCount] != undefined ){
        var temp          = String( this.userAnswer[this.partQuestionCount] );
        var tempCount     = String( this.numOfWords[this.partQuestionCount] );
        var tempTimeCount = String( this.elapsed_time[this.partQuestionCount] );
        r.user_answer( temp );
        r.num_of_words( tempCount );
        r.elapsed_time( tempTimeCount );
      }
      return r.push();
    },

    //Flashの操作-----------------------------------------------------------
    playFlashMovie: function(){
      //各typeのクラスメソッドを参照してください

      alert("この機能は未実装です：playFlashMovie");

    },

    stopFlashMovie: function(){
      //各typeのクラスメソッドを参照してください

      alert("この機能は未実装です：playFlashMovie");

    },

    playSound: function(){
      //各typeのクラスメソッドを参照してください

      alert("この機能は未実装です：playSound");

    },

    stopQuestionSound: function(){

      $( "#MP3PlayerArea" ).html( "" );

    },

    stopFlashMovie: function(){

      var player_area_id = this.slideId + "_FlvPlayer";
      $( "#" + player_area_id ).html( "" );

    },

    //FlashProxyから再生終了通知を受け取るメソッド-----------------------------
    //※現在再生中のSWF・MP3が終了したときに呼ばれるメソッドです
    flashComplete: function(){

      switch( this.nextflash ){
        //音声終了後問題を表示 ※questionのみ
        case "question":
          this.stopQuestionSound();
          this.dispQuestion();
          this.context.notify( 'writing' );
          break;

        //mp3終了後自動で次のスライドへ
        case "nextslide":
          this.stopQuestionSound();
          this.context.notify( 'next' );
          break;

        //swf終了後自動で次のスライドへ
        case "swf-nextslide":
          this.stopFlashMovie();
          this.context.notify( 'next' );
          break;

        //mp3終了後swfの再生へ
        case "next-swf":
          this.stopQuestionSound();
          this.playFlashMovie();
          break;

        //なにもしない
        default:
          break;

      }

    }

  }
);

/*
/* -----------------------------------------------------------------------------

WritingSlideController_Intro

----------------------------------------------------------------------------- */
WritingSlideController_Intro = Class.extend(
  WritingSlideController,
  {
    //プロパティ----------------------------------------------------------------
    //メソッド------------------------------------------------------------------
    initialize: function(slideId,param) {

      //全スライド共通初期化処理
      this.commonInitialize(slideId,param);

    },

    play: function(){

    },

    stop: function(){

    }

  }
);


/* -----------------------------------------------------------------------------

WritingSlideController_VolumeCheck

----------------------------------------------------------------------------- */
WritingSlideController_VolumeCheck = Class.extend(
  WritingSlideController,
  {
    //プロパティ----------------------------------------------------------------
    //メソッド------------------------------------------------------------------
    initialize: function(slideId,param) {

      //全スライド共通初期化処理
      this.commonInitialize(slideId,param);

    },

    play: function(){

    },

    stop: function(){

    },

    playSound: function(){

    },

    playLoopSound: function(){

    }
    
  }
);


/* -----------------------------------------------------------------------------

WritingSlideController_sectionDirection

----------------------------------------------------------------------------- */
WritingSlideController_sectionDirection = Class.extend(
  WritingSlideController,
  {
    //プロパティ----------------------------------------------------------------
    //メソッド------------------------------------------------------------------
    initialize: function(slideId,param) {

      //全スライド共通初期化処理
      this.commonInitialize(slideId,param);
    },

    play: function(){

      this.playSound();

    },

    stop: function(){

      this.stopQuestionSound();

    },

    playSound: function(){

      //ListeningSlideSoundController.jsのfinishメソッド
      this.nextflash = "nextslide";

      var playlist = [
        {
          "name" : "list",
          "value": [
            {
               path : static_path + "mp3/exam/Dir4-1.mp3"
            }
          ]
        },
        {
            "name" : "autoplay",
            "value": true
        }
      ]

      //ListeningSlideSoundController.jsのcreateMP3Playerメソッド
      createMP3Player( this, playlist );

    },

    skipIntro: function(){
      c.notify( 'next' );

    }

  }

);


/* -----------------------------------------------------------------------------

WritingSlideController_Passage

----------------------------------------------------------------------------- */
WritingSlideController_Passage = Class.extend(
  WritingSlideController,
  {
    //プロパティ----------------------------------------------------------------
    //メソッド------------------------------------------------------------------
    initialize: function(slideId,param) {
      //全スライド共通初期化処理

      this.commonInitialize(slideId,param);
    },

    play: function(){

    },

    stop: function(){

    },

    skipIntro: function(){

      c.notify( 'next' );

    }

  }
);


/* -----------------------------------------------------------------------------

WritingSlideController_Instruction

----------------------------------------------------------------------------- */
WritingSlideController_Instruction = Class.extend(
  WritingSlideController,
  {
    //プロパティ----------------------------------------------------------------
    //メソッド------------------------------------------------------------------
    initialize: function(slideId,param) {

      //全スライド共通初期化処理
      this.commonInitialize(slideId,param);

    },

    slideTimeout: function(){

      this.context.notify( 'next' );

    },

    play: function(){

    },

    stop: function(){

    }
    
  }
);


/* -----------------------------------------------------------------------------

WritingSlideController_checkTime

----------------------------------------------------------------------------- */
WritingSlideController_checkTime = Class.extend(
  WritingSlideController,
  {
    //プロパティ----------------------------------------------------------------
    //メソッド------------------------------------------------------------------
    initialize: function(slideId,param) {
      //全スライド共通初期化処理

      this.commonInitialize(slideId,param);

    },

    play: function(){

      if( c.isInTime() == false ) {
        //制限時間残りなし
        $( ".checkTime-on" ).css( "display", "none" );
        $( ".checkTime-off" ).css( "display", "block" );
      }
      else {
        //制限時間残りあり
        $( ".checkTime-on" ).css( "display", "block" );
        $( ".checkTime-off" ).css( "display", "none" );
      }

    },

    stop: function(){

    }

  }
);


/* -----------------------------------------------------------------------------

WritingSlideController_preparation

----------------------------------------------------------------------------- */
WritingSlideController_preparation = Class.extend(
  WritingSlideController,
  {
    //プロパティ----------------------------------------------------------------
    //メソッド------------------------------------------------------------------
    initialize: function(slideId,param) {

      //全スライド共通初期化処理
      this.commonInitialize(slideId,param);
    },

    slideTimeout: function(){
      this.context.notify( 'next' );
    },

    play: function(){

      //外部からSlide(Question)を開始するメソッド
      var tempController = this;
        
      //3秒後にnext
      this.timerId = setTimeout(
        function(){ tempController.slideTimeout( tempController ) },
        3000
      );

    },

    stop: function(){

      //外部からSlide(Question)を終了するメソッドです
      clearTimeout( this.timerId );

    },

    playFlashMovie: function(){

    }

  }
);


/* -----------------------------------------------------------------------------

WritingSlideController_listening

----------------------------------------------------------------------------- */
WritingSlideController_listening = Class.extend(
  WritingSlideController,
  {
    //プロパティ----------------------------------------------------------------
    //メソッド------------------------------------------------------------------
    initialize: function(slideId,param) {
      //全スライド共通初期化処理

      this.commonInitialize(slideId,param);

    },

    play: function(){

      if( this.q_firstCount ) {

        this.q_firstCount = false
        this.playSound();
      }
      else {

        this.playFlashMovie();
      }

    },

    stop: function(){

      this.stopFlashMovie();

    },

    playFlashMovie: function(){

      this.nextflash = "swf-nextslide";

      var player_area_id = this.slideId + "_FlvPlayer";

      var filename = examID + '-W-01-01-001-issue_l.swf';
      
      createFlvPlayer( this,player_area_id, filename );
    },

    playSound: function(){

      //ListeningSlideSoundController.jsのfinishメソッド
      this.nextflash = "next-swf";
      //this.nextflash = "nextslide";

      var playlist = [
        {
          "name" : "list",
          "value": [
            {
               path : static_path + "mp3/exam/before_issue_l.mp3"
            }
          ]
        },
        {
            "name" : "autoplay",
            "value": true
        }
      ]

      //ListeningSlideSoundController.jsのcreateMP3Playerメソッド
      createMP3Player( this, playlist );

    }

  }
);


/* -----------------------------------------------------------------------------

WritingSlideController_examOutro

----------------------------------------------------------------------------- */
WritingSlideController_examOutro = Class.extend(
  WritingSlideController,
  {
    //プロパティ----------------------------------------------------------------
    //メソッド------------------------------------------------------------------
    initialize: function(slideId,param) {

      //全スライド共通初期化処理
      this.commonInitialize(slideId,param);

    },

    play: function(){

      //examOutroの表示時にfinish処理
      this.setFinish();

    },

    stop: function(){

    }

  }
);


/* -----------------------------------------------------------------------------

WritingSlideController_Qtype_rl

----------------------------------------------------------------------------- */
WritingSlideController_Qtype_rl = Class.extend(
  WritingSlideController,
  {
    //プロパティ----------------------------------------------------------------
    //メソッド------------------------------------------------------------------
    initialize: function(slideId,param) {

      //全スライド共通初期化処理
      this.commonInitialize(slideId,param);
      
      this.partQuestionCount = param.partQuestionCount;

    },

    play: function(){

      var temp = this;
      $( "#pauseTest" ).bind(
        "click",
        function(e) {
          temp.pausetest();
        }
      );

      if( this.firstCount ) {

        // 初めて解く場合
        this.firstCount = false ;
        this.playSound();
      }
      else {

        // RETURNして戻った場合
        this.nextflash = "question";
        this.flashComplete();
      }

      buttonCotroll( "copy1", "cut1", "put1", "textedit1", "count1", "350" );

      r.seek( this.partQuestionCount );
      r.seen();

      //解答の書き戻し
      if( $( "#textedit1" ).val() == "" ) {

        $( "#textedit1" ).val( r.user_answer() );
        $( "#count1" ).text( wordCount( $( "#textedit1" ).val() ) );
      }

    },

    stop: function(){

      this.stopQuestionSound();

      //１問目の解答をセット
      this.userAnswer[this.partQuestionCount] = $( "#textedit1" ).val();
      this.numOfWords[this.partQuestionCount] = $( "#count1" ).text();

      if( $( "#hour" ).text().slice( 0,1 ) == "-" ) {

        this.elapsed_time[this.partQuestionCount] = 1200 + Number( $( "#hour" ).text() ) * 3600 + Number( $( "#minute" ).text() ) * 60 + Number( $( "#second" ).text() );
      }
      else {

        this.elapsed_time[this.partQuestionCount] = 1200 - ( Number( $( "#minute" ).text() ) * 60 + Number( $( "#second" ).text() ) );
      }

      this.setResult();

    },

    dispQuestion: function() {

      $( "#question-text-right1" ).show();

    },

    playSound: function(){

      //ListeningSlideSoundController.jsのfinishメソッド
      this.nextflash = "question";

      var playlist = [
        {
          "name" : "list",
          "value": [
            {
               path : static_path + "mp3/exam/" + examID + "-W-01-01-001-question.mp3"
            }
          ]
        },
        {
            "name" : "autoplay",
            "value": true
        }
      ]
      
      //ListeningSlideSoundController.jsのcreateMP3Playerメソッド
      createMP3Player( this, playlist );

    },

    pausetest: function(){

      //「PAUSE TEST」時の１問目の解答をセット
      this.userAnswer[this.partQuestionCount] = $( "#textedit1" ).val();
      this.numOfWords[this.partQuestionCount] = $( "#count1" ).text();

      if( $( "#hour" ).text().slice( 0,1 ) == "-" ) {

        this.elapsed_time[this.partQuestionCount] = 1200 + Number( $( "#hour" ).text() ) * 3600 + Number( $( "#minute" ).text() ) * 60 + Number( $( "#second" ).text() );
      }
      else {

        this.elapsed_time[this.partQuestionCount] = 1200 - ( Number( $( "#minute" ).text() ) * 60 + Number( $( "#second" ).text() ) );
      }

    }

  }
);


/* -----------------------------------------------------------------------------

WritingSlideController_Qtype_ke

----------------------------------------------------------------------------- */
WritingSlideController_Qtype_ke = Class.extend(
  WritingSlideController,
  {
    //プロパティ----------------------------------------------------------------
    //メソッド------------------------------------------------------------------
    initialize: function(slideId,param) {

      //全スライド共通初期化処理
      this.commonInitialize(slideId,param);

      this.partQuestionCount = param.partQuestionCount;

    },

    play: function(){

      var temp = this;
      $( "#pauseTest" ).bind(
        "click",
        function(e) {
            temp.pausetest();
        }
      );
    
      this.nextflash = "question";
      this.flashComplete();

      buttonCotroll( "copy2", "cut2", "put2", "textedit2", "count2", "420" );

      r.seek( this.partQuestionCount );
      r.seen();

      //解答の書き戻し
      if( $( "#textedit2" ).val() == "" ) {

        $( "#textedit2" ).val( r.user_answer() );
        $( "#count2" ).text( wordCount( $( "#textedit2" ).val() ) );
      }

    },

    stop: function(){

      this.stopQuestionSound();

      if( !isExperienceMode ) {

        //２問目の解答をセット
        this.userAnswer[this.partQuestionCount] = $( "#textedit2" ).val();
        this.numOfWords[this.partQuestionCount] = $( "#count2" ).text();

        if( $( "#hour" ).text().slice( 0,1 ) == "-" ) {

          this.elapsed_time[this.partQuestionCount] = 1800 + Number( $( "#minute" ).text() ) * 60 + Number( $( "#second" ).text() );
        }
        else {

          this.elapsed_time[this.partQuestionCount] = 1800 - ( Number( $( "#minute" ).text() ) * 60 + Number( $( "#second" ).text() ) );
        }

        this.setResult();
      
      }

    },

    dispQuestion: function() {

      $( "#question-text-right2" ).show();

    },

    pausetest: function(){

      //「PAUSE TEST」時の２問目の解答をセット
      this.userAnswer[this.partQuestionCount] = $( "#textedit2" ).val();
      this.numOfWords[this.partQuestionCount] = $( "#count2" ).text();

      if( $( "#hour" ).text().slice( 0,1 ) == "-" ) {

        this.elapsed_time[this.partQuestionCount] = 1800 + Number( $( "#minute" ).text() ) * 60 + Number( $( "#second" ).text() );
      }
      else {

        this.elapsed_time[this.partQuestionCount] = 1800 - ( Number( $( "#minute" ).text() ) * 60 + Number( $( "#second" ).text() ) );
      }

    }

  }
);


/* -----------------------------------------------------------------------------

WritingSlideController_directionRl

----------------------------------------------------------------------------- */
WritingSlideController_directionRl = Class.extend(
  WritingSlideController,
  {
    //プロパティ----------------------------------------------------------------
    //メソッド------------------------------------------------------------------
    initialize: function(slideId,param) {

      //全スライド共通初期化処理
      this.commonInitialize(slideId,param);
    },
    play: function(){

      this.playSound();

    },

    stop: function(){

      this.stopQuestionSound();

    },

    playSound: function(){

      //ListeningSlideSoundController.jsのfinishメソッド
      this.nextflash = "nextslide";

      var playlist = [
        {
          "name" : "list",
          "value": [
            {
               path : static_path + "mp3/exam/Dir4-2.mp3"
            }
          ]
        },
        {
            "name" : "autoplay",
            "value": true
        }
      ]
      
      //ListeningSlideSoundController.jsのcreateMP3Playerメソッド
      createMP3Player( this, playlist );

    },

    skipIntro: function(){

      c.notify( 'next' );

    }

  }
);


/* -----------------------------------------------------------------------------

WritingSlideController_directionKe

----------------------------------------------------------------------------- */
WritingSlideController_directionKe = Class.extend(
  WritingSlideController,
  {
    //プロパティ----------------------------------------------------------------
    //メソッド------------------------------------------------------------------
    initialize: function(slideId,param) {

      //全スライド共通初期化処理
      this.commonInitialize(slideId,param);

    },
    play: function(){

      //２問目の準備
      r.next();

    },

    stop: function(){

    }

  }
);
