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

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

var ListeningContext = Class.extend( ToeflContext, {

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

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

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

      (from state)
      replayTalk
      skipTalk
      decision

     component keys are: 
     -------------------
      continue
      pauseTest
      doPauseTest
      sectionExit
      doSectionExit
      volume
      help
      ok
      next
      replayTalk
      guess
      showSolution
      timer
      questionCounter
      volumeController

     slide keys are:
     -------------------
      examIntro
      generalInfo
      headset
      volumeCheck
      direction
      passage
      instruction
      questionC
      questionDo
      questionDs
      questionS
      questionT
      questionM
      partEnd
      examOutro

*/


// create state

    var examIntro      = new ExamIntro(this);
    var examStart      = new ExamStart(this);
    var sectionStart   = new SectionStart(this);
    var partStart      = new PartStart(this);
    var presentation   = new Presentation(this);
    var instruction    = new Instruction(this);
    var question       = new Question(this);
    var thinking       = new Thinking(this);
    var answer         = new Answer(this);
    var rePresentation = new RePresentation(this);
    var decision       = new Decision(this);
    var partEnd        = new PartEnd(this);
    var examOutro      = new ExamOutro(this);

// set transit (slide driven)

    examIntro.transition( "headset", examStart );
    examStart.transition( "volumeCheck", sectionStart );
    sectionStart.transition( "direction", partStart );
    partStart.transition( "passage", presentation );
    presentation.transition( "instruction", instruction );

    jQuery.each( [ instruction, decision, rePresentation  ], function () {

      this.transition( "questionC", question );
      this.transition( "questionDo", question );
      this.transition( "questionDs", question );
      this.transition( "questionS", question );
      this.transition( "questionT", question );
      this.transition( "questionM", question );

    });
    
    decision.transition( "passage", presentation );
    decision.transition( "partEnd", partEnd );
    partEnd.transition( "examOutro", examOutro );

// set transit (event driven)

    question.transition( "thinking", thinking );
    thinking.transition( "answerd", answer );
    thinking.transition( "replayTalk", rePresentation );
    answer.transition( "thinking", thinking );
    answer.transition( "replayTalk", rePresentation );
    decision.transition( "thinking", thinking );
    decision.transition( "replayTalk", rePresentation );
    rePresentation.transition( "skipTalk", question );
    answer.transition( "decision", decision );

    // set default state
    if ( this.part > 1 ) {
      this.defaultState = presentation;
    }
    else {
      this.defaultState = examIntro;
    }

    // set question state
    this.questionState = question;

  }

});

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 );

  },

  sectionExitConfirm: function () {

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

  },

  volumeControl: function () {

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

  },

  requiredAnswer: function () {

    this.context.projector.openWindow( "requiredAnswer", true, 400, 250 );

  },

  checkGuess: function () {

    this.context.components["guess"].check();

  },

  toggleGuess: function () {

    this.context.components["guess"].toggle();
    this.context.currentSlide.toggleGuess();

  },

  toggleSolution: function () {

    this.context.components["showSolution"].toggle();
    this.context.currentSlide.toggleSolution();

  },

  initSolution: function () {
    
    this.context.components["showSolution"].turnHide();

  },

  changeQuestionCounter: function () {

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

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

    var questionNum;

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

  },

  isReplay: 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();
  },

  viewHelp: function () {

    this.context.projector.openWindow( "helpWindow", true, 650, 580 );

  }

};

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 ExamStart = 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 SectionStart = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

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

    if ( this.context.components["sectionExit"] ) {
      this.context.components["sectionExit"].show();
    }

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

  },
 
  click: function ( arg ) {

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

      this.next();

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

      this.volumeControl();

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

    }

  }
 
});

var PartStart = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

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

    if ( this.context.components["sectionExit"] ) {
      this.context.components["sectionExit"].show();
    }

    this.context.components["volume"].show();
    this.context.components["help"].inactive();
    this.context.components["ok"].inactive();
    this.context.components["next"].inactive();

  },
 
  click: function ( arg ) {

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

      this.volumeControl();

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

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

      this.next();

    }

  }

});

var Presentation = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

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

    if ( this.context.components["sectionExit"] ) {
      this.context.components["sectionExit"].show();
    }

    this.context.components["replayTalk"].inactive();
    this.context.components["volume"].show();
    this.context.components["help"].inactive();
    this.context.components["ok"].inactive();
    this.context.components["next"].inactive();

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

  },
 
  click: function ( arg ) {

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

      this.volumeControl();

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

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

      this.next();

    }

  }
 
});

var Instruction = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

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

    if ( this.context.components["sectionExit"] ) {
      this.context.components["sectionExit"].show();
    }

    this.context.components["replayTalk"].inactive();
    this.context.components["volume"].inactive();
    this.context.components["help"].inactive();
    this.context.components["next"].inactive();

  },
 
  click: function ( arg ) {

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

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

      this.next();

    }

  }
 
});

var Question = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

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

    if ( this.context.components["sectionExit"] ) {
      this.context.components["sectionExit"].show();
    }

//    this.context.components["guess"].inactive();
//    this.context.components["guess"].uncheck();
    this.context.components["showSolution"].inactive();

    this.context.components["replayTalk"].inactive();
    this.context.components["volume"].active();
    this.context.components["help"].inactive();
    this.context.components["ok"].inactive();
    this.context.components["next"].inactive();

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

  },
 
  click: function ( arg ) {

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

      this.volumeControl();

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

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

      this.context.notify("thinking");

    }

  },

  isGuess: function () {

    this.checkGuess();

  }
 
});

var Thinking = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

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

    if ( this.context.components["sectionExit"] ) {
      this.context.components["sectionExit"].show();
    }

//    this.context.components["guess"].active();
    this.context.components["showSolution"].active();

    this.context.components["replayTalk"].active();
    this.context.components["volume"].active();
    this.context.components["help"].active();
    this.context.components["ok"].inactive();
    this.context.components["next"].active();

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

  },
 
  click: function ( arg ) {

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

      this.volumeControl();

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

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

      this.requiredAnswer();

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

      this.context.notify("replayTalk");

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

      this.toggleGuess();

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

      this.toggleSolution();

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

      this.context.notify("viewHelp");

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

      this.context.notify("answerd");

    }

  }
 
});

var Answer = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  click: function ( arg ) {

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

      this.volumeControl();

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

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

      this.context.notify("decision");

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

      this.context.notify("replayTalk");

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

      this.toggleGuess();

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

      this.toggleSolution();

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

      this.context.notify("viewHelp");

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

      this.context.notify("decision");

    }

  }

});

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["replayTalk"].turnSkip();
    this.context.components["next"].inactive();

  },


  teardown: function () {

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

  },

  click: function ( arg ) {

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

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

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

      this.volumeControl();

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

      this.context.notify("viewHelp");

    }

  },

  next: function () {

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

  },
 
  isReplay: function () {

    return true;

  }
 
});


var Decision = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

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

  },

  click: function ( arg ) {

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

      this.volumeControl();

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

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

      this.context.notify("replayTalk");

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

      this.initSolution();
      this.next();

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

      this.toggleGuess();

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

      this.toggleSolution();

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

      this.context.notify("viewHelp");

    }

  }

});

var PartEnd = 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 == "pauseTest" ) {

      this.pauseTestConfirm();

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

      if ( this.context.isLastPart ) {

        this.next();

      }
      else {

        this.goNextUrl();

      }

    }

  }
 
});

var ExamOutro = 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.goNextUrl();

    }

  }
 
});


