var SlideController = Class.extend( Slide, {

  initialize: function ( slideId, examId, part, mode ) {

     this.slideId = slideId;
     this.parent( this.slideId );
     this.examId = examId;
     this.mode   = mode;
     this.part = part;
     this.slideContent = $( "#" + this.slideId );
     this.minor   = this.slideContent.find(".passage").attr("class").split(" ")[1].replace("minor","");
     this.major   = this.slideContent.find(".passage").attr("class").split(" ")[2].replace("major","");
     this.qNo    = this.slideId.replace("question","");
     this.changed_spans = [];

  },

  play: function () {

    var target_spans = this.slideContent.find(".passage").find("span");

    var controller = this;

    jQuery.each( target_spans, function () {

      var hcn = "highlight_" + controller.minor;
      var acn = "arrow_" + controller.minor;
      var icn = "insert_" + controller.minor; // is prefix. ex) insert_1_1

      if( $(this).attr("class") == hcn  ){

        $(this).css( "background-color", "#ccc" );
        controller.changed_spans.push( $(this) );

      }
      else if ( $(this).attr("class") == acn  ) {

        $(this).html("<span class='arrow'><span class='alt'>→</span>&nbsp;</span>");
        controller.changed_spans.push( $(this) );

      }
      // type i
      else if ( $(this).attr("class").match( icn ) ) {

        var slide = controller.slideContent;

        var correct      = slide.find(".alt .correct").text();
        var user_answer  = slide.find(".alt .user_answer").text();
        var sentence     = slide.find(".sentence.en").text();
        var option_index = $(this).attr("class").replace( icn + "_", "" );

        if ( option_index == correct ) {
          $(this).text( sentence );
          $(this).css("background-color","#ff0");
        }
        else if ( option_index == user_answer ) {
          $(this).text( sentence );
          $(this).css("font-weight","bold");
        }
        else {
          $(this).text("■");
        }
        controller.changed_spans.push( $(this) );

      }

    });

    if( this.changed_spans.length > 0 && $(this.changed_spans[0]).attr("class") ) {

      var scroll_target = this.slideContent.find(".passage");
      scroll_target.scrollTo( this.changed_spans[0], 0, { offset: -40 } );

    }

    target_spans = this.slideContent.find(".q-and-a").find("span");

    jQuery.each( target_spans, function () {

      var hcn = "highlight_" + controller.minor;

      if( $(this).attr("class") == hcn  ){

        $(this).css( "background-color", "#ccc" );

      }

    });

    if ( this.mode == "listening" || ( this.mode == "writing" && this.slideId == "question1" ) || ( this.mode == "speaking" && Number(this.qNo) >= 3 ) ){

      this.playPassageSound();

      if ( this.slideContent.attr("class").split(" ")[1] == "questionS" ) {

        this.playQuestionSound_s();
      }

    }

    if ( this.mode == "speaking" ) {

      this.play_for_speaking();
      this.modelAnswerSound();

    }

  },

  stop: function () {

    var controller = this;
    jQuery.each( controller.changed_spans, function () {

      $(this).css( "background-color", "transparent" );
      $(this).css( "color", "#333" );
      if (  $(this).attr("class") && $(this).attr("class").match("arrow") ) {
        $(this).text("");
      }

    });

    this.changed_spans = [];

    //現在再生中のMP3を停止する
    this.stopQuestionSound();

    if ( this.mode == "speaking" ) {

      this.stop_for_speaking();

    }

  },

  playFlashMovie: function(){
    var player_area_id = this.slideId+"_FlvPlayer";

    var tempExamId = String("0"+this.examId).slice(-2);
    var tempPart   = String("0"+this.part).slice(-2);
    var tempMajor  = String("0"+this.major).slice(-2);

    var filename = tempExamId + '-L-' + tempPart + '-' + tempMajor + '-000.swf';
    createNonSlideFlvPlayer(this,player_area_id,filename);
  },

  playPassageSound: function(){
    var player_area_id = this.slideId+"_MP3Player";

    var tempExamId = String("0"+this.examId).slice(-2);
    var tempPart   = String("0"+this.part).slice(-2);
    var tempMajor  = String("0"+this.major).slice(-2);
    var tempMinor  = String("00"+this.qNo).slice(-3);

    if( this.mode == "writing" ) {

      var filename = tempExamId + '-W-' + tempPart + '-01-001-issue_l.mp3';
    }
    else if ( this.mode == "speaking" ) {

      var filename = tempExamId + '-S-' + tempPart + '-' + tempMajor + '-' + tempMinor + '-issue_l.mp3';
    }
    else {

      var filename = tempExamId + '-L-' + tempPart + '-' + tempMajor + '-000.mp3';
    }
    
    var playlist = [
      {
        "name" : "list",
        "value": [
          {
             path:root+"static/mp3/exam/"+filename
          }
        ]
      },
      {
          "name" : "autoplay",
          "value": false
      }
    ]
    createMP3PlayerForReview(this,player_area_id,playlist);
  },

  playQuestionSound_s: function(){
    var player_area_id = this.slideId+"_MP3Player_s";

    var tempExamId = String("0"+this.examId).slice(-2);
    var tempPart   = String("0"+this.part).slice(-2);
    var tempMajor  = String("0"+this.major).slice(-2);
    var tempMinor  = String("00"+this.qNo).slice(-3);

    var filename = tempExamId + '-L-' + tempPart + '-' + tempMajor + '-' + tempMinor + '-02.mp3';

    var playlist = [
      {
        "name" : "list",
        "value": [
          {
             path:root+"static/mp3/exam/"+filename
          }
        ]
      },
      {
          "name" : "autoplay",
          "value": false
      }
    ]
    createMP3PlayerForReview2(this,player_area_id,playlist);
  },

  modelAnswerSound: function(){
  
    var player_area_id = this.slideId + "_MP3Player_modelAnswers";

    var tempExamId = String("0"+this.examId).slice(-2);
    var tempPart   = String("0"+this.part).slice(-2);
    var tempMajor  = String("0"+this.major).slice(-2);
    var tempMinor  = String("00"+this.qNo).slice(-3);

    var filename = tempExamId + '-S-' + tempPart + '-' + tempMajor + '-' + tempMinor + '-model_answers.mp3';
    
    var playlist = [
      {
        "name" : "list",
        "value": [
          {
             path:root+"static/mp3/exam/"+filename
          }
        ]
      },
      {
          "name" : "autoplay",
          "value": false
      }
    ]
    createMP3PlayerForReview(this,player_area_id,playlist);
  },

  stopFlashMovie: function(){
    var player_area_id = this.slideId+"_FlvPlayer";
    $("#"+player_area_id).html("");
  },

  stopQuestionSound: function(){

    var player_area_id = this.slideId+"_MP3Player";
    $("#"+player_area_id).html("");

    var player_area_id = this.slideId+"_MP3Player_s";
    $("#"+player_area_id).html("");

    var player_area_id = this.slideId+"_MP3Player_modelAnswers";
    $("#"+player_area_id).html("");

  },

  //FlashProxyから再生終了通知を受け取るメソッド-----------------------------
  //  ※現在再生中のSWF・MP3が終了したときに呼ばれるメソッドです
  flashComplete: function(){
  }
  
});
