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

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

var ReviewContext = Class.extend( ToeflContext, {

  initialize: function ( part, isLastPart, nextUrl, prevUrl, returnUrl, exitUrl, isPromotion ) {

    this.parent( part, isLastPart, nextUrl );

    this.prevUrl   = prevUrl;
    this.returnUrl = returnUrl;
    this.exitUrl   = exitUrl;
    this.isPromotion = isPromotion;

/* memo
     
     event keys are:
     ---------------
      (from  slide or component)
      click { type: componentKey }
      is_first
      is_middle
      is_last

     component keys are: 
     -------------------
      next
      back
      reviewReturn
      exit
      questionCounter

     slide keys are:
     -------------------
      question

*/

    // create state

    var question        = new Question(this);
    var first_question  = new FirstQuestion(this);
    var last_question   = new LastQuestion(this);
    var review_outro    = new ReviewOutro(this);

    // set transit (slide driven)

    question.transition( "reviewOutro", review_outro );
    first_question.transition( "reviewOutro", review_outro );

    review_outro.transition( "question", question );
    review_outro.transition( "questionC", question );
    review_outro.transition( "questionD", question );
    review_outro.transition( "questionDo", question );
    review_outro.transition( "questionDs", question );
    review_outro.transition( "questionDms", question );
    review_outro.transition( "questionDs", question );
    review_outro.transition( "questionI", question );
    review_outro.transition( "questionM", question );
    review_outro.transition( "questionP", question );
    review_outro.transition( "questionS", question );
    review_outro.transition( "questionSs", question );
    review_outro.transition( "questionT", question );

    // set transit (event driven)

    question.transition( "is_first", first_question );
    last_question.transition( "is_first", first_question );

    if ( !this.isPromotion ) {
      question.transition( "is_last", last_question );
      first_question.transition( "is_last", last_question );
    }
    else {
      first_question.transition( "is_last", question );
    }

    first_question.transition( "is_middle", question );
    last_question.transition( "is_middle", question );

    // set default state

    this.defaultState = question;

    // set question state

    this.questionState = question;

  },

  goPrevUrl: function () {

    window.location.href = this.prevUrl;

  },

  goReturnUrl: function () {

    if ( this.isPromotion ) {

      this.state.alertForPromotion();

    }
    else {

      window.location.href = this.returnUrl;
    }

  },

  goExitUrl: function () {

    window.location.href = this.exitUrl;

  }

});

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

  },

  prev: function () {

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

  },

  openWindow: function ( arg ) {

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

  },

  closeWindow: function () {

    this.context.projector.closeWindow();

  },

  volumeControl: function () {

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

  },

  allComponentsHide: function () {

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

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

      }
    );

  },

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

  },

  goNextUrl: function () {

    this.context.goNextUrl();

  },

  goPrevUrl: function () {

    this.context.goPrevUrl();

  },

  goReturnUrl: function () {

    this.context.goReturnUrl();

  },

  goExitUrl: function () {

    this.context.goExitUrl();

  },

  alertForPromotion: function () {

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

  }

};

var Question = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent( context );

  },

  startup: function () {

    this.allComponentsHide ();

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

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

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

  },

  click: function ( arg ) {


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

      this.prev();

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

      this.next();

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

      this.volumeControl();

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

      this.goReturnUrl();

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

      this.goExitUrl();

    }

  }

});

var FirstQuestion = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent( context );

  },

  startup: function () {

    this.allComponentsHide ();

    if ( this.context.part > 1 ) {
      this.context.components["back"].show();
    }

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

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

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

  },

  click: function ( arg ) {

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

      this.goPrevUrl();

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

      this.next();

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

      this.volumeControl();

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

      this.goReturnUrl();

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

      this.goExitUrl();

    }

  }

});

var LastQuestion = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent( context );

  },

  startup: function () {

    this.allComponentsHide ();

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

    if ( ! this.context.isLastPart ) {
      this.context.components["next"].show();
    }

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

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

  },

  click: function ( arg ) {

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

      this.prev();

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

      this.goNextUrl();

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

      this.volumeControl();

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

      this.goReturnUrl();

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

      this.goExitUrl();

    }

  }

});

var ReviewOutro = Class.extend( State, {

  include: CommonStateMethod,

  initialize: function ( context ) {

    this.parent( context );

  },

  startup: function () {

    this.allComponentsHide ();

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

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

    this.context.components["reviewReturn"].show();
    this.context.components["exit"].show();

    $("h1").css("display","none");
    $("#top-instruction").css("display","none");

  },

  click: function ( arg ) {

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

      $("h1").css("display","block");
      $("#top-instruction").css("display","block");
      this.prev();

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

      this.volumeControl();

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

      this.goReturnUrl();

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

      this.goExitUrl();

    }

  }

});

