﻿/* -----------------------------------------------------------------------------

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

var WritingContext = Class.extend( ToeflContext, {

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

    this.PASSAGE_TIMELIMIT = "180000";
    this.RI_TIMELIMIT      = "1200000";
    this.KE_TIMELIMIT      = "1800000";

    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
      timeout

      (from state)
      replayTalk
      skipTalk
      writing
      returnQuestion

      (by self)
      isInTime
      isOutTime

     component keys are: 
     -------------------
      continue
      pauseTest
      doPauseTest
      volume
      help
      next
      return
      replayTalk
      timer
      questionCounter
      volumeController

     slide keys are:
     -------------------
      examIntro
      generalInfo
      sectionDirection
      directionRl
      directionKe
      passage
      listening
      preparation
      questionRl
      questionKe
      checkTime
      examOutro

*/


// create state

    var examIntro        = new ExamIntro(this);
    var partStart        = new PartStart(this);
    var questionStartRl  = new QuestionStartRl(this);
    var questionStartKe  = new QuestionStartKe(this);
    var passage          = new Passage(this);
    var presentation     = new Presentation(this);
    var preparation      = new Preparation(this);
    var questionRl       = new QuestionRl(this);
    var writingRl        = new WritingRl(this);
    var writingKe        = new WritingKe(this);
    var rePresentation   = new RePresentation(this);
    var checkTime        = new CheckTime(this);
    var inTime           = new InTime(this);
    var outTime          = new OutTime(this);
    var examOutro        = new ExamOutro(this);

// set transit (slide driven)

    examIntro.transition( "sectionDirection", partStart );
    partStart.transition( "directionRl", questionStartRl );
    partStart.transition( "directionKe", questionStartKe );

    questionStartRl.transition( "passage", passage );
    passage.transition( "listening", presentation );
    presentation.transition( "preparation", preparation );
    preparation.transition( "questionRl", questionRl );
    writingRl.transition( "checkTime", checkTime );
    rePresentation.transition( "questionRl", questionRl );
    
    questionStartKe.transition( "questionKe", writingKe );
    writingKe.transition( "checkTime", checkTime );

    checkTime.transition( "isInTime", inTime );
    checkTime.transition( "isOutTime", outTime );

    inTime.transition( "questionRl", questionRl );
    inTime.transition( "questionKe", writingKe );

    inTime.transition( "directionKe", questionStartKe );
    outTime.transition( "directionKe", questionStartKe );

    inTime.transition( "examOutro", examOutro );
    outTime.transition( "examOutro", examOutro );
    

// set transit (event driven)

    questionRl.transition( "writing", writingRl );
    writingRl.transition( "replayTalk", rePresentation );

    checkTime.transition( "inTime", inTime );
    checkTime.transition( "outTime", outTime );

// set default state

    this.defaultState = examIntro;

// set Reading&Listening question state

    this.questionStateRl = questionRl;

// set Knowledge&Experience question state

    this.questionStateKe = writingKe;

// set Presentation Id Prefix

    this.presentationIdPrefix = "listening";

  },

  questionCue: function ( num, time ) {

    var id = this.questionIdPrefix + num;
    
    var slideNum = this.projector.getSlideNumById( id );
    var slideKey = this.projector.slideKeys[slideNum]
    
    if ( slideKey == "questionRl" ) {

      questionState = this.questionStateRl;
      if ( time ) {
        this.RI_TIMELIMIT = this.RI_TIMELIMIT - time;
      }
      
    }
    else {

      questionState = this.questionStateKe;
      if ( time ) {
        this.KE_TIMELIMIT = this.KE_TIMELIMIT - time;
      }

    }
    
    this.setPresentationSlideNum(id); // super class method
    this.projector.goto(id);
    this.change(questionState);

  },

  isInTime: function () {

    return this.components["timer"].isInTime();

  },

  experienceMode: function () {

    this.isExperienceMode = true;
  }

});

var CommonStateMethod = {

  questionCue: function ( arg ) {

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

  },

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

  },

  timeOverIsOk: function () {

    this.context.projector.closeWindow();
    this.context.components["timer"].start();
  }

};

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["skip"].show();

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

  },
 
  click: function ( arg ) {

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

      this.next();

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

      this.pauseTestConfirm();

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

      this.volumeControl();

    }

  }
 
});

var QuestionStartRl = 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["help"].inactive();
    this.context.components["next"].inactive();
    this.context.components["timer"].stop();

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

  },
 
  click: function ( arg ) {

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

      this.volumeControl();

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

      this.pauseTestConfirm();

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

      this.next();

    }

  }
 
});

var QuestionStartKe = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

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

    this.context.components["help"].inactive();
    this.context.components["next"].inactive();
    this.context.components["continue"].show();
    this.context.components["timer"].stop();
    this.context.components["timer"].reset( this.context.KE_TIMELIMIT );

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

    }


  }
 
});

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["replayTalk"].inactive();
    this.context.components["volume"].inactive();
    this.context.components["help"].inactive();
    this.context.components["next"].inactive();
    this.context.components["timer"].show();
    this.context.components["timer"].reset( this.context.PASSAGE_TIMELIMIT );
    this.context.components["timer"].start();

  },
 
  teardown: function () {

    this.context.components["timer"].reset( this.context.RI_TIMELIMIT );
  
  },
 
  click: function ( arg ) {

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

      this.pauseTestConfirm();

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

      this.next();

    }

  },

  timeout: function () {

     this.next();

  }
 
});


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


    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["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 == "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["replayTalk"].turnSkip();

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

    this.context.components["timer"].pause();
    this.context.components["timer"].hide();

  },


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

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

      this.next();

    }

  },

  next: function () {

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

  },
 
  isReplay: function () {

    return true;

  }
 
});


var QuestionRl = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

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

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

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

  },

  click: function ( arg ) {

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

      this.pauseTestConfirm();

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

      this.volumeControl();

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

      this.context.notify('writing');

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

      this.next();

    }

  }
 
});

var WritingRl = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);
    this.loopCounter = 0;

  },

  startup: function () {

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

    //１問目から開始した場合
    if( minorNo == "1" && this.loopCounter == 0 ) {
      this.context.components["timer"].reset( this.context.RI_TIMELIMIT );
      if ( this.context.RI_TIMELIMIT < 0 ) {

         this.context.components["timer"].isRemaining = false;
      }
      this.loopCounter++;
    }

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

  },
 
  click: function ( arg ) {

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

      this.pauseTestConfirm();

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

      this.volumeControl();

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

      this.viewHelp();

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

      this.next();

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

      this.context.notify("replayTalk");

    }

  },

  timeout: function () {

    this.context.components["timer"]._display();
    this.context.components["timer"].pause();
    this.context.projector.openWindow( "timeOver", true );

  }

});


var CheckTime  = 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();

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

    this.context.components["timer"].pause();

    //if ( this.context.components["timer"].isInTime() ) {
      this.context.notify("inTime");
    //}
    //else {
    //  this.context.notify("outTime");
    //}

  }

});

var InTime  = Class.extend( State, {

  include: CommonStateMethod,

  startup: function () {

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

  },

  click: function ( arg ) {

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

      this.pauseTestConfirm();

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

      this.prev();

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

      this.next();

    }

  }

});

var OutTime  = Class.extend( State, {

  include: CommonStateMethod,

  startup: function () {

  },

  click: function ( arg ) {

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

      this.pauseTestConfirm();

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

      this.next();

    }

  }

});

var WritingKe = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);
    this.loopCounter = 0;

  },

  startup: function () {

    this.allComponentsHide ();

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

    this.context.components["help"].show();
    this.context.components["next"].active();

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

    //２問目から開始した場合
    if( minorNo == "2" && this.loopCounter == 0 ) {
      this.context.components["timer"].reset( this.context.KE_TIMELIMIT );
      if ( this.context.KE_TIMELIMIT < 0 ) {
         this.context.components["timer"].isRemaining = false;
      }
      this.loopCounter++;
    }

    this.context.components["timer"].show();
    //if( isExperienceMode ) {
    //  this.context.components["timer"].reset("1800000");
    //}
    //else {
      this.context.components["timer"].start();
    //}

  },
 
  click: function ( arg ) {

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

      this.pauseTestConfirm();

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

      this.viewHelp();

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

      this.next();

    }

  },
 
  timeout: function () {

    this.context.components["timer"]._display();
    this.context.components["timer"].pause();
    this.context.projector.openWindow( "timeOver", true );

  }

});


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

    }

  }
 
});

