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

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

var ReadingContext = 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
      back
      openWindow {type: windowId, modal:boolean }
      closeWindow
      readed
      goQuestion { num: questionNum }
      isGuess

      (from state)
      viewText
      viewQuestion
      review
      returnQuestion

     component keys are: 
     -------------------
      continue
      pauseTest
      doPauseTest
      sectionExit
      doSectionExit
      help
      next
      back
      review
      return
      viewText
      guess
      showSolution
      continueQuestion
      timer
      questionCounter

     slide keys are:
     -------------------
      examIntro
      generalInfo
      direction
      passage
      questionDms
      questionDo
      questionD
      questionDs
      questionSs
      questionI
      questionP
      questionM
      questionS
      reviewIndex
      partEnd
      examOutro

*/

// create state

    var examIntro            = new ExamIntro(this);
    var partStart            = new PartStart(this);
    var presentation         = new Presentation(this);
    var presentationReaded   = new PresentationReaded(this);
    var question             = new Question(this);
    var questionDraggable    = new QuestionDraggable(this);
    var reviewIndex          = new ReviewIndex(this);
    var rePresentation       = new RePresentation(this);
    var partEnd              = new PartEnd(this);
    var examOutro            = new ExamOutro(this);


// set transit (slide driven)

    examIntro.transition( "direction", partStart );
    partStart.transition( "passage", presentation );

    jQuery.each ([ presentationReaded, question, questionDraggable, rePresentation, reviewIndex, partEnd ], function () {

      this.transition( "questionSs", question );
      this.transition( "questionI", question );
      this.transition( "questionP", question );
      this.transition( "questionM", question );
      this.transition( "questionS", question );
      this.transition( "questionDms", questionDraggable );
      this.transition( "questionDo", questionDraggable );
      this.transition( "questionD", questionDraggable );
      this.transition( "questionDs", questionDraggable );

    });

    question.transition( "passage", presentation );
    questionDraggable.transition( "passage", presentation );

    question.transition( "partEnd", partEnd );
    questionDraggable.transition( "partEnd", partEnd );
    rePresentation.transition( "partEnd", partEnd );
    reviewIndex.transition( "partEnd", partEnd );

    partEnd.transition( "examOutro", examOutro );

    question.transition( "reviewIndex", reviewIndex );
    questionDraggable.transition( "reviewIndex", reviewIndex );
    rePresentation.transition( "reviewIndex", reviewIndex );
    partEnd.transition( "reviewIndex", reviewIndex );


// set transit (event driven)

    presentation.transition( "readed", presentationReaded );
    questionDraggable.transition( "viewText", rePresentation );
    

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

// set questionDraggable state

    this.questionDraggableState = questionDraggable;

// set reviewIndex slide id
    
    this.reviewIndexSlideId = "reviewIndex";

// beforeReviewIndexSlideNum

    this.beforeReviewIndexSlideNum;

// draggable question slide keys

    this.draggableQuestionSlideKeys = [ "questionDms","questionDo","questionD","questionDs" ];

  },

  questionCue: function (num) {

    var id = this.questionIdPrefix + num;
    
    var slideNum = this.projector.getSlideNumById( id );
    var slideKey = this.projector.slideKeys[slideNum]
    var questionState;
    
    if ( jQuery.inArray( slideKey, this.draggableQuestionSlideKeys ) > -1 ) {

      questionState = this.questionDraggableState;
      
    }
    else {

      questionState = this.questionState;

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

  }

});

var CommonStateMethod = {

  questionCue: function ( arg ) {

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

  },

  goto: function ( arg ) {

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

  },

  next: function () {

    this.context.projector.next();
    this.changeQuestionCounter();

  },

  nextfrom: function ( from ) {

    this.context.projector.fromto( from, 1 );
    this.changeQuestionCounter();

  },

  prev: function () {

    this.context.projector.prev();
    this.changeQuestionCounter();

  },

  prevfrom: function ( from ) {

    this.context.projector.fromto( from, -1 );
    this.changeQuestionCounter();

  },

  openWindow: function ( arg ) {

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

  },

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

  },

  moreText: function () {

    this.context.projector.openWindow( "moreText", 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();

  },
  
  sawSolution: function () {

    this.context.components["showSolution"].turnShow();

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

  },

  setBeforeReviewIndexSlideNum: function () {

    this.context.beforeReviewIndexSlideNum = this.context.projector.currentSlideNum;

  },

  review: function () {

    this.context.projector.goto( this.context.reviewIndexSlideId );

  },

  returnSlide: function () {

    this.context.projector.goto( this.context.beforeReviewIndexSlideNum );

  },

  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 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["review"].inactive();
    this.context.components["help"].show();
    this.context.components["back"].inactive();
    this.context.components["next"].inactive();
    this.context.components["continue"].show();

  },
 
  click: function ( arg ) {

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

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

      this.next();

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

      this.context.notify("viewHelp");

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

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

  },
 
  click: function ( arg ) {

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

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

      this.moreText();

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

      this.context.notify('readed');

    }

  }
 
});

var PresentationReaded = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  click: function ( arg ) {

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

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

      this.next();

    }
    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"].show();
//    this.context.components["guess"].uncheck();
    this.context.components["showSolution"].show();

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

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

  },
 
  click: function ( arg ) {

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

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

      this.toggleGuess();

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

      this.toggleSolution();

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

      this.initSolution();
      this.prev();

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

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

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

      this.setBeforeReviewIndexSlideNum();
      this.review();

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

      this.context.notify("viewHelp");

    }

  },

  isGuess: function () {

    this.checkGuess();

  }
 
});

var QuestionDraggable = 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"].show();
//    this.context.components["guess"].uncheck();
    this.context.components["showSolution"].show();

    this.context.components["viewText"].active();
    this.context.components["review"].active();
    this.context.components["help"].active();
    this.context.components["back"].active();
    this.context.components["next"].active();

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

  },

  click: function ( arg ) {

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

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

      this.toggleGuess();

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

      this.toggleSolution();

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

      this.initSolution();
      this.prev();

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

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

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

      this.setBeforeReviewIndexSlideNum();
      this.review();

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

      this.setBeforeReviewIndexSlideNum();
      this.context.notify("viewText");

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

      this.context.notify("viewHelp");

    }

  },

  isGuess: function () {

    this.checkGuess();

  }
  
});

var ReviewIndex = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent(context);

  },

  startup: function () {

    this.allComponentsHide ();

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

  },

  click: function ( arg ) {

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

      this.returnSlide();

    }

  },

  goQuestion: function ( arg ) {

    this.context.cueQuestion( arg.num );

  }

});


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["viewText"].turnQuestion();

  },


  teardown: function () {

    this.context.components["viewText"].turnText();

  },

  click: function ( arg ) {

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

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

      this.toggleGuess();

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

      this.toggleSolution();

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

      this.context.projector.empty();
      this.context.projector.push();
      this.initSolution();
      this.prevfrom( this.context.beforeReviewIndexSlideNum );

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

      this.context.projector.empty();
      this.context.projector.push();
      this.initSolution();
      this.nextfrom( this.context.beforeReviewIndexSlideNum );

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

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

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

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

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

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

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

  },

  click: function ( arg ) {

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

      this.pauseTestConfirm();

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

      this.sectionExitConfirm();

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

      if ( this.context.isLastPart ) {

        this.next();

      }
      else {

        this.goNextUrl();

      }

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

      this.prev();

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

      this.setBeforeReviewIndexSlideNum();
      this.review();

    }

  }
 
});

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

    }

  }
 
});


