'use strict';

/* Controllers */
function StartGameCtrl( $scope, $http ){
	$scope.userScore = 0;
	$scope.bestScore = localStorage.getItem("bestScore") || 0;
	$scope.gameEnd = false;

	$http.get('cars/cars.json').success( function( data ) {
		//all cars from json
		$scope.carsData = data;
		updateGame( $scope );
	});

	$scope.checkChoice = function( car ) {
		if ( car.name == $scope.randomCarName ) {
			alert('Rigth!');
			updateGame( $scope, 50 );

		} else {
			alert('wrong!');
			updateGame( $scope );
		}
	}
}

function updateGame ( $scope, /* optional */ score, numberOfCars ) {
	var tempArray = [],
		score = score || 0,
		numberOfCars = numberOfCars || 4,
		randomNumber = Math.floor( Math.random() * numberOfCars );

	$scope.userScore += score;
	$scope.bestScore = ( $scope.userScore > $scope.bestScore ) ? $scope.userScore : $scope.bestScore;

	if ( $scope.gameEnd ) {
		endGame( $scope );
		return;
	}

	//playing until we have a cars
	if ( $scope.carsData.length <= numberOfCars ) {
		randomNumber = Math.floor( Math.random() * $scope.carsData.length );
		$scope.gameEnd = true;
	}

	for ( var i = 0; i < numberOfCars; i++ ) {
		tempArray.push( $scope.carsData.pop() );
	}

	$scope.randomCarName = tempArray[ randomNumber ].name;
	$scope.cars = tempArray;
}

function endGame( $scope ){
	localStorage.setItem( 'bestScore', $scope.bestScore );
	$scope.cars = [];
}

function AboutCtrl( $scope ) {

}

function RecordsCtrl( ) {

}

function MainCtrl( ) {

}

myApp.controller('StartGameCtrl', [ '$scope', '$http', StartGameCtrl])
	.controller('RecordsCtrl', [ RecordsCtrl])
	.controller('AboutCtrl', [ AboutCtrl])
	.controller('MainCtrl', [ MainCtrl]);
@@ -284,16 +284,23 @@
 data;%0A%09%09
+$scope.
 updateGa
@@ -298,32 +298,24 @@
 .updateGame(
- $scope 
 );%0A%09%7D);%0A%0A%09$s
@@ -410,32 +410,39 @@
 t('Rigth!');%0A%09%09%09
+$scope.
 updateGame( $sco
@@ -436,24 +436,16 @@
 ateGame(
- $scope,
  50 );%0A%0A
@@ -478,16 +478,23 @@
 !');%0A%09%09%09
+$scope.
 updateGa
@@ -492,32 +492,24 @@
 .updateGame(
- $scope 
 );%0A%09%09%7D%0A%09%7D%0A%7D%0A
@@ -510,40 +510,40 @@
 %0A%09%7D%0A
-%7D%0A%0Afunction updateGame ( $scope,
+%09%0A%09$scope.updateGame = function(
  /* 
@@ -560,16 +560,31 @@
 / score,
+ /* optional */
  numberO
@@ -593,16 +593,17 @@
 ars ) %7B%0A
+%09
 %09var tem
@@ -615,16 +615,17 @@
 y = %5B%5D,%0A
+%09
 %09%09score 
@@ -640,16 +640,17 @@
 %7C%7C 0,%0A%09%09
+%09
 numberOf
@@ -675,16 +675,17 @@
 s %7C%7C 4,%0A
+%09
 %09%09random
@@ -737,17 +737,19 @@
 Cars );%0A
-%0A
+%09%0A%09
 %09$scope.
@@ -768,16 +768,17 @@
  score;%0A
+%09
 %09$scope.
@@ -868,17 +868,19 @@
 tScore;%0A
+%09
 %0A
+%09
 %09if ( $s
@@ -898,24 +898,32 @@
 d ) %7B%0A%09%09
+%09$scope.
 endGame(
  $scope 
@@ -918,29 +918,22 @@
 ame(
- $scope 
 );%0A%09%09
+%09
 return;%0A
 %09%7D%0A%0A
@@ -928,20 +928,23 @@
 return;%0A
+%09
 %09%7D%0A
-%0A
+%09%0A%09
 %09//playi
@@ -967,16 +967,17 @@
  a cars%0A
+%09
 %09if ( $s
@@ -1019,16 +1019,17 @@
 s ) %7B%0A%09%09
+%09
 randomNu
@@ -1087,24 +1087,25 @@
 length );%0A%09%09
+%09
 $scope.gameE
@@ -1115,20 +1115,23 @@
 = true;%0A
+%09
 %09%7D%0A
+%09
 %0A
+%09
 %09for ( v
@@ -1166,16 +1166,17 @@
 i++ ) %7B%0A
+%09
 %09%09tempAr
@@ -1214,12 +1214,15 @@
  );%0A
+%09
 %09%7D%0A
+%09
 %0A
+%09
 %09$sc
@@ -1273,16 +1273,17 @@
 %5D.name;%0A
+%09
 %09$scope.
@@ -1304,39 +1304,43 @@
 ay;%0A
+%09
 %7D%0A%0A
-function endGame( $scope 
+%09$scope.endGame = function(
 )%7B%0A
+%09
 %09loc
@@ -1391,16 +1391,17 @@
 core );%0A
+%09
 %09$scope.
@@ -1411,16 +1411,20 @@
 s = %5B%5D;%0A
+%09%7D%0A%0A
 %7D%0A%0Afunct

put functions of game in scope of controller