'use strict';

/**
 * @ngdoc function
 * @name chessmateApp.controller:MainCtrl
 * @description
 * # MainCtrl
 * Controller of the chessmateApp
 */
angular.module('chessmateApp')
  .controller('MainCtrl', function ($scope) {

    $scope.$on('game-updated', function (event, game) {
      console.log(game);
      $scope.currentBoard = game.boards[0];
    });

    $scope.move = function(source, destination) {
      var piece = $("#" + source).children();
      var desinationPosition = $("#" + destination).position();
      var sourcePosition = $("#" + source).position();
      var destinationClass = buildCss(desinationPosition.left - sourcePosition.left, desinationPosition.top - sourcePosition.top);
      piece.css(destinationClass);
    }

    function buildCss(positionX, positionY) {
      var transform = "translate(" + positionX + "px, " + positionY + "px)";
      var cssStyle = { "-webkit-transform": transform,
        "-moz-transform": transform,
        "-o-transform": transform,
        "-ms-transform": transform,
        "transform": transform};
      return cssStyle;
    }
  });

@@ -219,16 +219,37 @@
 ope) %7B%0A%0A
+    var game = null;%0A
     $sco
@@ -292,24 +292,24 @@
 nt, game) %7B%0A
-
       consol
@@ -321,16 +321,40 @@
 (game);%0A
+      this.game = game;%0A
       $s
@@ -398,16 +398,171 @@
    %7D);%0A%0A
+    $scope.$on('next', function (event) %7B%0A      var nextBoard = $scope.currentBoard.turn + 1;%0A      $scope.currentBoard = game.boards%5BnextBoard%5D;%0A    %7D);%0A%0A
     $sco
@@ -933,22 +933,118 @@
 nClass);
+%0A%0A      // TODO fire this event when move complete%0A      //$rootScope.$broadcast('next', null);
 %0A    %7D
+;
 %0A%0A    fu

add next event