define(['jquery', 'gameStates/BaseState', 'modules/CountScoreKeeper','modules/SettingsModule','components/scoreControl',
  'text!templates/game.visibleHand.html', 'text!templates/game.visibleDeck.html', 'text!templates/game.scoreControl.html'],
  function($, BaseState, ScoreKeeper, Settings, scoreControlComp, visibleHandHtml, visibleDeckHtml, scoreControlHtml){
  'use strict';
  function CountState(game){
    BaseState.call(this, game, 'Count');
    /*
     Step - Description
     0  - Pone counts hand
     1  - Dealer counts hand
     2  - Dealer counts crib
     */
    game.countStateStep = game.countStateStep || 0;
    this.scoreKeeper = new ScoreKeeper();
  }

  CountState.prototype = Object.create(BaseState.prototype);
  CountState.prototype.constructor = CountState;

  CountState.prototype.templates = function(){
    var templates = BaseState.prototype.templates();
    templates.deck = visibleDeckHtml;
    templates.p2Hand = visibleHandHtml;
    if(this.isScorePoints)
      templates.scoreControl = scoreControlHtml;
    return templates;
  };


  CountState.prototype.init = function(){
    this.p1.maxPoints = 29;
    this.p1.availablePoints = setAvailablePoints(this.p1.maxPoints);
    this.isScorePoints = false;
    this.p1.restoreHand();
    this.p2.restoreHand();
    if(this.game.countStateStep === 0)
      processCountStep.call(this);

    else {
      this.render();
    }
  };

  CountState.prototype.action = function(){
    if(this.isScorePoints){
      this.game.countStateStep--;//hack to fix auto incriment
    }

    processCountStep.call(this);
  };

  CountState.prototype.bindEvents = function(){
    //bind defaults
    BaseState.prototype.bindEvents.call(this);

    scoreControlComp.init(this.p1);
  };

  function setAvailablePoints(size){
    var i = 0;
    var arrayPoints = [];
    while(i < size){
      i++;
      arrayPoints.push(i);
    }
    return arrayPoints;
  }

  function processCountStep(){
    var points = 0;
    if(this.game.countStateStep === 0)
      processFirstStep.call(this, points);
    else if(this.game.countStateStep === 1)
      processSecondStep.call(this, points);
    else if(this.game.countStateStep === 2)
      processThirdStep.call(this);
    else
      processEndOfCount.call(this);
  }

  function processFirstStep(points){
    if(isPlayerOneCribOwner.call(this)){
      evaluatePlayerTwoScore.call(this, points);
    } else {
      evaluatePlayerOneScore.call(this, points);
    }
    this.game.countStateStep += 1;
    this.render();
  }

  function processSecondStep(points){
    if(isPlayerOneCribOwner.call(this)){
      evaluatePlayerOneScore.call(this, points);
      this.p2.hand = [];
    } else {
      evaluatePlayerTwoScore.call(this, points);
      this.p1.hand = [];
    }
    this.game.countStateStep += 1;
    this.render();
  }

  function processThirdStep(){
    this.game.$cribOwner.hand = (this.game.$cribOwner.crib.length === 4) ? this.game.$cribOwner.crib : this.game.$cribOwner.hand;
    this.game.$cribOwner.crib = [];
    if(isPlayerOneCribOwner.call(this)){
      evaluatePlayerOneScore.call(this);
    } else {
      evaluatePlayerTwoScore.call(this);
    }
    this.game.countStateStep += 1;
    this.render();
  }

  function processEndOfCount(){
    if(isPlayerOneCribOwner.call(this)){
      this.game.$cribOwner = this.p2;
    } else {
      this.game.$cribOwner = this.p1;
    }
    showPlayerOneHand.call(this);
    this.game.$showTopCard = false;
    this.game.countStateStep = 0;
    this.render();
    if(this.p1.isWinner() || this.p2.isWinner())
      this.mediator.publish('transition', 'Summary', true);
    else {
      this.mediator.publish('transition', 'Deal');
    }
  }


  function evaluatePlayerOneScore(points){
    showPlayerOneHand.call(this);
    if(!this.isScorePoints){
      this.scoreKeeper.evaluateHand(this.p1, this.game.topCard);
      if(this.p1.isWinner())
        this.mediator.publish('transition', 'Summary', true);
    }
    return points;
  }

  function evaluatePlayerTwoScore(points){
    showPlayerTwoHand.call(this);
    this.scoreKeeper.evaluateHand(this.p2, this.game.topCard);
    if(this.p2.isWinner())
      this.mediator.publish('transition', 'Summary', true);
    return points;
  }

  function isPlayerOneCribOwner(){
    return this.game.$cribOwner === this.p1;
  }

  function showPlayerOneHand(){
    this.game.$player2HandVisible = false;
    this.game.$player1HandVisible = true;
    if(Settings.get('manual-count')){
      this.isScorePoints = this.isScorePoints ? false : true;
      if(this.isScorePoints)
        this.p1.selectedScore = 0;
    }
  }

  function showPlayerTwoHand(){
    this.game.$player2HandVisible = true;
    this.game.$player1HandVisible = false;
  }

  return CountState;
});

@@ -959,32 +959,37 @@
 ml;%0A    if(this.
+game.
 isScorePoints)%0A 
@@ -983,24 +983,24 @@
 corePoints)%0A
-
       templa
@@ -1204,29 +1204,82 @@
 oints);%0A    
+if(
 this.
+game.isScorePoints === undefined)%0A      this.game.
 isScorePoint
@@ -1502,24 +1502,24 @@
 function()%7B%0A
-
     if(this.
@@ -1510,32 +1510,37 @@
 ()%7B%0A    if(this.
+game.
 isScorePoints)%7B%0A
@@ -3844,24 +3844,29 @@
    if(!this.
+game.
 isScorePoint
@@ -4553,24 +4553,29 @@
 %0A      this.
+game.
 isScorePoint
@@ -4579,24 +4579,29 @@
 ints = this.
+game.
 isScorePoint
@@ -4632,16 +4632,21 @@
 if(this.
+game.
 isScoreP

fix CountState scoreControl display