Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/js/lesson.js
blob: 9585211664266ade87fe7c5ba3c7c50aad7624c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
$(document).ready(
    function(){

	
    //preloads assets into karma 'collections'
    var k = Karma({
		audio: [{'name':'correct','file':'correct.ogg'},
			{'name':'incorrect','file':'incorrect.ogg'}
			],
		image: [{name: 'bear', file: 'bear.png'},
			{name : 'goat', file: 'goat.png'},
			{name: 'tiger', file: 'tiger.png'},
			{name: 'elephant', file: 'elephant.png'},
			{name: 'horse', file: 'horse.png'},
			{name: 'cow', file: 'cow.png'}			       
			]
		  });

    //this command will scale down the lesson if the user's browser window
    //is smaller than 950px X 600px
    k.scaleWindow();

    //sets locale, otherwise defaults to English
    //$.i18n.setLocale('en');

    //put your main lesson code here
    k.ready(
	function(){ 	      

	    
	    $('#kHeader').kHeader({'title': 'English Animal Identification',
				   lessonPlan: true, teachersNote: true});

	    //Set up feedback widget, this shows the user a correct or incorrect
	    //icon and sound when triggered programmatically
	    var $feedback = $('#feedback').feedback();
	    
	    var kFooter = $('#kFooter').kFooter({'winningScore': 6});

	    var score = 0;
	    var names = [];
	    var namesUsed = [];
	    var correctIndex = 0;
	    var $img = $('#imgObject');
	    var $options = $('.option');
	   
	    $.i18n.gettext("bear"); 
	    $.i18n.gettext("goat");
	    $.i18n.gettext("tiger");
	    $.i18n.gettext("elephant");
	    $.i18n.gettext("horse");
	    $.i18n.gettext("cow");
	    
	    var populateListNames = function() {
		var i = 0;
		$.each(k.image, function (img){
			   names[i] = img;
			   i++;
		       });
	    };	 

		
	    var checkSelection = function(selectedOption){
		if(selectedOption === correctIndex){

		    score++;
		    kFooter.kFooter('inc');  
		    kFooter.kFooter('incTotal');

		    if (score === 6){
			$feedback.feedback('win');
		    } else{
			$feedback.feedback('correct');
			game();
		    }
		}
		else {
		    $feedback.feedback('incorrect');
		    kFooter.kFooter('incTotal');
		}
	    };    

	    var shuffleGlobal = function (list) {
		var i = 0, j = 0, t = 0;
		for (i = list.length - 1; i > 0; i -= 1) {
		    j = Karma.rand(0, i);
		    t = list[i];
		    list[i] = list[j];
		    list[j] = t;
		}
	    };

	    var game = function(){
		correctIndex = 0;
		
		var pickCorrect = function(){
		    var correct = 0;

		    var used = function(index){
			var name = names[index];
			for (var i = 0; i < namesUsed.length; i++){
			    if (namesUsed[i] === name){
				return true;
			    }
			}
			return false;
		    };

		    var getUnusedName = function(){
			correct = k.rand(0,3);
			while(used(correct)){
			    shuffleGlobal(names);
			    correct = k.rand(0,3);
			}
			return correct;
		    };

		    shuffleGlobal(names);
		    correct = getUnusedName();
		    namesUsed.push(names[correct]);
		    
		    return correct;
		};
		
		correctIndex = pickCorrect();

		for (var i = 0; i < 4; i++){
		    $($options[i]).text($.i18n.gettext(k.image[names[i]].name));
		}
		
		$img.attr('src', k.image[names[correctIndex]].src)
		    .css('visibility', 'visible');
		
	    };


	    kFooter.bind('kFooterWinGame', 
			 function(){
			     $('.optImg').hide();
			     $('.imageBox').hide();
			     $('#gameOver').show();
			 });  
	    kFooter.bind('kFooterRestart',
			 function() {
			     namesUsed = [];
			     correctIndex = 0;
			     score = 0;
			     game();     
			 }
			);

	    $options.click(
		function(e){
		    checkSelection(parseInt(e.target.id.slice(-1)));
		}
	    );

	    
	    populateListNames();
	    game();     //let the game begin
	    

	}); //end of games	

});