/* -----------------------------------------------------------------------------

speakingContext.js uses:
  context.js
  toeflContext.js
  
------------------------------------------------------------------------------- */

var SpeakingContext = Class.extend( ToeflContext, {

  initialize: function ( part, isLastPart, nextUrl, pauseTestUrl ) {

    this.isExperienceMode = false;

    this.parent( part, isLastPart, nextUrl, pauseTestUrl );

/* memo
     
     event keys are:
     ---------------
      (from  slide or component)
      click { type: componentKey }
      next
      openWindow {type: windowId, modal:boolean }
      closeWindow
      recordAgain
      playback
      startTimer

      (from state)
      sectionDirectionEnd
      replayTalk

     component keys are: 
     -------------------
      continue
      pauseTest
      doPauseTest
      volume
      next
      replayTalk
      confirmResponse
      recordAgain
      playbackResponse
      timer
      questionCounter
      volumeController

     slide keys are:
     -------------------
      examIntro
      generalInfo
      headset
      recordingSetting
      rehearsal
      rehearsalEnd
      sectionDirection
      questionDirection
      questionTq
      questionEndTq
      listening
      preparation
      questionSq
      questionEndSq
      passage
      questionTsq
      questionEndTsq
      examOutro

*/


// create state

    var examIntro            = new ExamIntro(this);
    var partStart            = new PartStart(this);
    var rehearsal            = new Rehearsal(this);
    var rehearsalAgain       = new RehearsalAgain(this);
    var rehearsalEnd         = new RehearsalEnd(this);
    var playbackRehearsal    = new PlaybackRehearsal(this);
    var sectionDirection     = new SectionDirection(this);
    var sectionDirectionEnd  = new SectionDirectionEnd(this);
    var questionDirection    = new QuestionDirection(this);
    var simpleRecording      = new SimpleRecording(this);
    var simpleRecordingAgain = new SimpleRecordingAgain(this);
    var simpleRecordingEnd   = new SimpleRecordingEnd(this);
    var simplePlayback       = new SimplePlayback(this);
    var passage              = new Passage(this);
    var presentation         = new Presentation(this);
    var rePresentation       = new RePresentation(this);
    var preparation          = new Preparation(this);
    var recording            = new Recording(this);
    var recordingAgain       = new RecordingAgain(this);
    var recordingEnd         = new RecordingEnd(this);
    var playback             = new Playback(this);
    var examOutro            = new ExamOutro(this);

// set transit (slide driven)

    examIntro.transition( "headset", partStart );
    partStart.transition( "rehearsal", rehearsal );
    partStart.transition( "direction", questionDirection );
    rehearsal.transition( "rehearsalEnd", rehearsalEnd );
    rehearsal.transition( "sectionDirection", sectionDirection );
    rehearsalAgain.transition( "rehearsalEnd", rehearsalEnd );
    playbackRehearsal.transition( "rehearsalEnd", rehearsalEnd );
    rehearsalEnd.transition( "sectionDirection", sectionDirection );
    sectionDirection.transition( "direction", questionDirection );
    sectionDirectionEnd.transition( "direction", questionDirection );
    questionDirection.transition( "questionTq", simpleRecording );
    questionDirection.transition( "passage", passage );
    questionDirection.transition( "listening", presentation );
    simpleRecording.transition( "questionEndTq", simpleRecordingEnd );
    simpleRecordingAgain.transition( "questionEndTq", simpleRecordingEnd );
    simplePlayback.transition( "questionEndTq", simpleRecordingEnd );
    simpleRecordingEnd.transition( "direction", questionDirection );
    passage.transition( "listening", presentation );
    presentation.transition( "preparation", preparation );
    preparation.transition( "questionSq", recording );
    preparation.transition( "questionTsq", recording );
    recording.transition( "questionEndSq", recordingEnd  );
    recording.transition( "questionEndTsq", recordingEnd  );
    recordingAgain.transition( "questionEndSq", recordingEnd  );
    recordingAgain.transition( "questionEndTsq", recordingEnd  );
    recordingEnd.transition( "direction", questionDirection );
    playback.transition( "questionEndSq", recordingEnd  );
    playback.transition( "questionEndTsq", recordingEnd  );
    simpleRecordingEnd.transition( "examOutro", examOutro );
    recordingEnd.transition( "examOutro", examOutro );

// set transit (event driven)

    sectionDirection.transition( "sectionDirectionEnd", sectionDirectionEnd );
    rehearsalEnd.transition( "recordAgain", rehearsalAgain );
    rehearsalEnd.transition( "playback", playbackRehearsal );
    simpleRecordingEnd.transition( "recordAgain", simpleRecordingAgain );
    simpleRecordingEnd.transition( "playback", simplePlayback );
    recordingEnd.transition( "recordAgain", recordingAgain );
    recordingEnd.transition( "playback", playback );
    recordingEnd.transition( "replayTalk", rePresentation );
    rePresentation.transition( "replayTalk", recordingEnd );

// set default state

    this.defaultState = examIntro;

// set question direction state

    this.questionDirectionState = questionDirection;

// questin direction id prefix

    this.questionDirectionIdPrefix = "direction";

  },

  questionCue: function (num) {

    var id = this.questionDirectionIdPrefix + num;
    
    var slideNum = this.projector.getSlideNumById( id );
    var slideKey = this.projector.slideKeys[slideNum]
    
    this.setPresentationSlideNum(id); // super class method
    this.projector.goto(id);
    this.change(this.questionDirectionState);

  },

  experienceMode: function () {

    this.isExperienceMode = true;

  }

});

var CommonStateMethod = {

  questionCue: function ( arg ) {

    this.context.questionCue( arg["num"] );

  },

  goto: function ( arg ) {

    this.context.projector.goto( arg["num"] );

  },

  next: function () {

    this.context.projector.next();

  },

  prev: function () {

    this.context.projector.prev();

  },

  openWindow: function ( arg ) {

    this.context.projector.openWindow( arg.type, arg.mordal );

  },

  closeWindow: function () {

    this.context.projector.closeWindow();

  },

  allComponentsHide: function () {

    jQuery.each( this.context.components, function ( key, component ) {

        if ( key != "obunsha" ) { // "obunsha" is debug button component
          component.hide();
        }

      }
    );

  },

  pauseTestConfirm: function () {

    this.context.projector.openWindow( "pauseTestConfirm", true );

  },

  volumeControl: function () {

    this.context.components["volumeController"].toggle();

  },

  changeQuestionCounter: function () {

    var slide = this.context.projector.getSlideByNum( this.context.projector.currentSlideNum );

    var slideId = slide.attr("id");

    var questionNum;

    if ( slideId.match(/direction/) ) {
      questionNum = parseInt( slideId.replace("direction","") );
      this.context.components["questionCounter"].change( questionNum );
    }

  },

  isRecording: function () {

    return false;

  },

  isRecordAgain: function () {

    return false;

  },

  goNextUrl: function () {

    this.context.afterSendingResultMethod = this.context.goNextUrl;
    this.context.setResult();

  },

  pauseTest: function () {

    this.context.afterSendingResultMethod = this.context.goPauseTestUrl;
    this.context.setResult();

  },

  retrySendingResult: function () {
    this.context.setResult();
  }
  
};

var ExamIntro = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["continue"].show();

  },
 
  click: function ( arg ) {

    if ( arg.type == "continue" || arg.type == "obunsha" ) {

      this.next();

    }

  }
 
});

var PartStart = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();

    this.context.components["continue"].show();

  },
 
  click: function ( arg ) {

    if ( arg.type == "continue" || arg.type == "obunsha" ) {

      this.next();

    }
    else if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }

  }
 
});

var Rehearsal = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();
    this.context.components["skip"].show();

    this.context.components["recordAgain"].inactive();
    this.context.components["playbackResponse"].inactive();
    this.context.components["volume"].show();
    this.context.components["continue"].inactive();

    if ( this.context.isExperienceMode ) {
     this.next();
    }

  },
 
  click: function ( arg ) {

    if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }
    else if ( arg.type == "obunsha" ) {

      this.next();

    }
    else if ( arg.type == "skip" ) {

      var p = this.context.projector;
      p.fromto( p.currentSlideNum, 2 );

    }

  },

  isRecording: function () {

    return true;

  }

});

var RehearsalAgain = Class.extend( Rehearsal, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  isRecordAgain: function () {

    return true;

  }

});

var RehearsalEnd = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();

    this.context.components["recordAgain"].active();
    this.context.components["playbackResponse"].active();
    this.context.components["volume"].show();
    this.context.components["continue"].active();

    if ( this.context.isExperienceMode ) {
     this.next();
    }

  },
 
  click: function ( arg ) {

    if ( arg.type == "continue" || arg.type == "obunsha" ) {

      this.next();

    }
    else if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "recordAgain" ) {

      this.context.notify("recordAgain");
      this.prev();


    }
    else if ( arg.type == "playbackResponse" ) {

      this.prev();
      this.context.notify("playback");

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }
    else if ( arg.type == "obunsha" ) {

      this.next();

    }

  }
 
});

var PlaybackRehearsal = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();

    this.context.components["recordAgain"].inactive();
    this.context.components["playbackResponse"].inactive();
    this.context.components["volume"].show();
    this.context.components["continue"].inactive();

  },
 
  click: function ( arg ) {

    if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }
    else if ( arg.type == "obunsha" ) {

      this.next();

    }

  }

});

var SectionDirection = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();
    this.context.components["skip"].show();

    this.context.components["volume"].show();
    this.context.components["continue"].inactive();

    if ( this.context.isExperienceMode ) {
      this.context.notify("sectionDirectionEnd");
    }

  },
 
  click: function ( arg ) {

    if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }
    else if ( arg.type == "obunsha" ) {

      this.context.notify("sectionDirectionEnd");

    }
    else if ( arg.type == "skip" ) {

      this.next();

    }

  }
 
});

var SectionDirectionEnd = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();

    this.context.components["volume"].show();
    this.context.components["continue"].active();

    if ( this.context.isExperienceMode ) {
     this.next();
    }

  },
 
  click: function ( arg ) {

    if ( arg.type == "continue" || arg.type == "obunsha" ) {

      this.next();

    }
    else if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }

  }
 
});

var QuestionDirection = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();
    this.context.components["skip"].show();

    this.context.components["volume"].show();
    this.context.components["continue"].inactive();

    this.changeQuestionCounter();

    if ( this.context.isExperienceMode ) {
      this.next();
    }

  },
 
  click: function ( arg ) {

    if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }
    else if ( arg.type == "obunsha" || arg.type == "skip" ) {

      this.next();

    }

  }
 
});

var SimpleRecording = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();

    this.context.components["confirmResponse"].inactive();
    this.context.components["recordAgain"].inactive();
    this.context.components["playbackResponse"].inactive();
    this.context.components["volume"].show();

    this.context.components["questionCounter"].show();

  },
 
  click: function ( arg ) {

    if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }
    else if ( arg.type == "obunsha" ) {

      this.next();

    }

  },

  isRecording: function () {

    return true;

  }
 
});

var SimpleRecordingAgain = Class.extend( SimpleRecording, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  click: function ( arg ) {

    if ( arg.type == "obunsha" ) {

      this.next();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }

  },

  isRecordAgain: function () {

    return true;

  }

});

var SimpleRecordingEnd = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();

    this.context.components["confirmResponse"].active();
    this.context.components["recordAgain"].active();
    this.context.components["playbackResponse"].active();
    this.context.components["volume"].show();

    this.context.components["questionCounter"].show();

  },
 
  click: function ( arg ) {

    if ( arg.type == "confirmResponse" || arg.type == "obunsha" ) {

      this.next();

    }
    else if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }
    else if ( arg.type == "recordAgain" ) {

      this.context.notify("recordAgain");
      this.prev();

    }
    else if ( arg.type == "playbackResponse" ) {

      this.prev();
      this.context.notify("playback");

    }
    else if ( arg.type == "obunsha" ) {

      this.next();

    }

  }
 
});

var SimplePlayback = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();

    this.context.components["confirmResponse"].inactive();
    this.context.components["recordAgain"].inactive();
    this.context.components["playbackResponse"].inactive();
    this.context.components["volume"].show();

    this.context.components["questionCounter"].show();

  },
 
  click: function ( arg ) {

    if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }
    else if ( arg.type == "obunsha" ) {

      this.next();

    }

  }

});

var Passage = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();
    this.context.components["skip"].show();

    this.context.components["confirmResponse"].inactive();
    this.context.components["recordAgain"].inactive();
    this.context.components["playbackResponse"].inactive();
    this.context.components["replayTalk"].inactive();
    this.context.components["volume"].show();

    this.context.components["questionCounter"].show();

  },
 
  click: function ( arg ) {

    if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "obunsha" || arg.type == "skip" ) {

      this.next();

    }

  },

  startTimer: function () {

    this.context.components["timer"].show();
    this.context.components["timer"].reset( 10000 );
    this.context.components["timer"].start( this.context.currentSlide );

  }

});


var Presentation = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();

    this.context.components["confirmResponse"].inactive();
    this.context.components["recordAgain"].inactive();
    this.context.components["playbackResponse"].inactive();
    this.context.components["replayTalk"].inactive();
    this.context.components["volume"].show();

    this.context.components["questionCounter"].show();

    this.context.projector.empty();
    this.context.projector.push();

  },
 
  click: function ( arg ) {

    if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }
    else if ( arg.type == "obunsha" ) {

      this.next();

    }

  }
 
});

var Preparation = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();

    this.context.components["confirmResponse"].inactive();
    this.context.components["recordAgain"].inactive();
    this.context.components["playbackResponse"].inactive();
    this.context.components["replayTalk"].inactive();
    this.context.components["volume"].show();

    this.context.components["questionCounter"].show();


  },
 
  click: function ( arg ) {

    if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }
    else if ( arg.type == "obunsha" ) {

      this.next();

    }

  }
 
});

var RePresentation = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.context.projector.unshift();
    this.context.projector.pop();

    this.context.components["confirmResponse"].inactive();
    this.context.components["recordAgain"].inactive();
    this.context.components["playbackResponse"].inactive();
    this.context.components["replayTalk"].turnSkip();

  },

  teardown: function () {

    this.context.components["replayTalk"].turnReplay();

  },

  click: function ( arg ) {

    if ( arg.type == "replayTalk" ) {

      this.context.projector.unshift();
      this.context.projector.pop();
      this.context.notify("replayTalk")

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }
    else if ( arg.type == "obunsha" ) {

      this.next();

    }

  },

  next: function () {

      this.context.projector.unshift();
      this.context.projector.pop();
      this.context.notify("replayTalk")

  },
 
  isReplay: function () {

    return true;

  }
 
});

var Recording = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();

    this.context.components["confirmResponse"].inactive();
    this.context.components["recordAgain"].inactive();
    this.context.components["playbackResponse"].inactive();
    this.context.components["replayTalk"].inactive();
    this.context.components["volume"].show();

    this.context.components["questionCounter"].show();

  },
 
  click: function ( arg ) {

    if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }
    else if ( arg.type == "obunsha" ) {

      this.next();

    }

  },

  isRecording: function () {

    return true;

  }
 
});

var RecordingAgain = Class.extend( Recording, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  isRecordAgain: function () {

    return true;

  },

  click: function ( arg ) {
  
    if ( arg.type == "obunsha" ) {

      this.next();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }

  }


});

var RecordingEnd = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();

    this.context.components["confirmResponse"].active();
    this.context.components["recordAgain"].active();
    this.context.components["playbackResponse"].active();
    this.context.components["replayTalk"].active();
    this.context.components["volume"].show();

    this.context.components["questionCounter"].show();

  },
 
  click: function ( arg ) {

    if ( arg.type == "confirmResponse" || arg.type == "obunsha" ) {

      this.next();

    }
    else if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }
    else if ( arg.type == "recordAgain" ) {

      this.context.notify("recordAgain");
      this.prev();

    }
    else if ( arg.type == "playbackResponse" ) {

      this.prev();
      this.context.notify("playback");

    }
    else if ( arg.type == "replayTalk" ) {

      this.context.notify("replayTalk");

    }

  }
 
});

var Playback = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["pauseTest"].show();

    this.context.components["confirmResponse"].inactive();
    this.context.components["recordAgain"].inactive();
    this.context.components["playbackResponse"].inactive();
    this.context.components["replayTalk"].inactive();
    this.context.components["volume"].show();

    this.context.components["questionCounter"].show();

  },
 
  click: function ( arg ) {

    if ( arg.type == "pauseTest" ) {

      this.pauseTestConfirm();

    }
    else if ( arg.type == "volume" ) {

      this.volumeControl();

    }
    else if ( arg.type == "next" ) {

      this.next();

    }

  }

});

var ExamOutro = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

    this.context.components["continue"].active();


  },

  click: function ( arg ) {

    if ( arg.type == "continue" || arg.type == "obunsha" ) {

      this.goNextUrl();

    }

  }
 
});


