Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/examples/lessons/6_English_yesNoQuestions/js/lesson.js
blob: a842b3d0c57bdf3c6081d0a43a175c11262ade51 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
$(document).ready(function(){
	var k = Karma({
		audio: [{'name':'correct','file':'correct.ogg'},
		        {'name':'incorrect','file':'incorrect.ogg'}
		]});
		  
	k.ready(function(){
		var i,j,flag;
		var TOTAL_QUES = 8;
		var TOTAL_LEVEL = 3;
		var currentDragObject;
		var randPositions = [];
		var randImages = [];
		var randOptions = [];
		var totalCounter;
		var correctCounter;
		var currentQuestion;
		var currentDragObject;
		var arrangedAns = [];
		var checked;
		var sectionNum;  //store the current tab num
		var flag_checked;
		var currentQues;  //store the current Animal name
		var currentAnimal; //store the current Animal Image name
		var correctQuest;   //store 1 if the question and image is same
		
		var animals = new Array('tiger','elephant','jackel','bear','rhino','monkey','turtle','snake',
								'duck','cock','pig','cow','goat','horse','sheep','rabbit',
								'parrot','mynah','crow','sparrow','egret','pigeon','owl','eagle'
					);
		var answersOpts = new Array('Yes','No','it','is',"isn't");
		var tabs = new Array('wild','domestic','bird');
		var correctYes = new Array('Yes','it','is');
		var correctNo = new Array('No','it',"isn't");
		
		var scoreboard = $('#score_box').scoreboard({'layout':'horizontal', 
		       'winningScore': 8});
		/*scoreboard.bind('winGame',function(){    //needs total score counter to be checked problem
				$('#gameOver').show();
				$('#dot').hide();
				$('#questionSection').hide();
				$('#gameOver').append('Game Over !!!');
				$('#gameOver').append('<div id="gameOverInfo">You got  <span class="specialText">'+correctCounter+
						'</span> correct out of <span class="specialText">'+totalCounter+'</span>   questions .</div>');
				
		}); 
		*/
		
		var genRandOpts=function (){
			randOptions[0] = k.rand(0,4);
			for(i=1; i<5; i++){
					do{
						flag = 0;
						randOptions[i] = k.rand(0,4);
						for(j=0; j<i; j++){
							if(randOptions[i] === randOptions[j]){
								flag++;
							}
						}
					}while(flag != 0 );  //end of do while loop	
				}
			
		};	
		var genRandImages=function (){
			randImages[0] = k.rand(0,TOTAL_QUES-1);
			for(i=1; i<TOTAL_QUES; i++){
					do{
						flag = 0;
						randImages[i] = k.rand(0,TOTAL_QUES-1);
						for(j=0; j<i; j++){
							if(randImages[i] === randImages[j]){
								flag++;
							}
						}
					}while(flag != 0 );  //end of do while loop	
				}
		};	
		var genRandPosition=function (){
		  	randPositions[0] = k.rand(0,TOTAL_QUES-1);
			for(i=1; i<TOTAL_QUES; i++){
					do{
						flag = 0;
						randPositions[i] = k.rand(0,TOTAL_QUES-1);
						for(j=0; j<i; j++){
							if(randPositions[i] === randPositions[j]){
								flag++;
							}
						}
					}while(flag != 0 );  //end of do while loop	
				}
		};	
		var display_game_over = function(){
			$('#dot').hide();
			$('#gameOver').show();			
			$('#questionSection').html('').append('<div id="gameOver"></div>');
			$('#gameOver').append('Game Over !!!');
			$('#gameOver').append('<div id="gameOverInfo">You got  <span class="specialText">'+correctCounter+
					'</span> correct out of <span class="specialText">'+totalCounter+'</span>   questions .</div>');
			
		};
		var display_control = function(){
			if(checked === 1){
				next_questions();		
			}		
		};
		var delay_correctShow = function(){
			document.delayForm.delayval.value = 1;
			$('#checkAnswer').hide();
			display_control();						
		}
		
		var check_answers = function(){
			$('#checkAnswer').show();
			var correct = 0;
			var correctCheck = 0;
			if(correctQuest === 1){
				for(var i = 0 ; i< 3; i++){
					if(arrangedAns[i] === correctYes[i]){
						correct++;
					}
				}
				if(correct === 3){
					correctCheck = 1;
				}
			}
			else{
				for(var i = 0 ; i< 3; i++){
					if(arrangedAns[i] === correctNo[i]){
						correct++;
					}
				}
				if(correct === 3){
					correctCheck = 1;
				}
			}				
			if(correctCheck === 1){
				if(flag_checked === 0){
					scoreboard.scoreboard('inc');
					correctCounter++;
				}
				k.audio.correct.play();
				$('#checkAnswer').html('<img src="assets/image/correct.png" />');
				checked = 1;
				totalCounter++;
				scoreboard.scoreboard('incTotal');			
			}
			else{
				k.audio.incorrect.play();
				$('#checkAnswer').html('<img src="assets/image/incorrect.png" />');
				checked = 0;
				flag_checked = 1;
			}
			t=setTimeout(function(){delay_correctShow();},1000);
		};
		
		var assignTabs = function (tabId){
			$('#tabs').append('<div id="tabs'+tabId+'" class="tabBox"></div>');
			var tabImgCss = {
					'width': '179px',
					'height': '50px',
					'background-image': 'url("assets/image/'+tabs[tabId]+'.png")',
					'background-repeat': 'no-repeat'
			};
			$('#tabs'+tabId).css(tabImgCss);
			$('#tabs'+tabId).click(function(){
				for(j = 0; j<TOTAL_LEVEL; j++){
					if(tabId === j){
						$('#tabs'+j).addClass('tabSelected');
					}
					else{
						$('#tabs'+j).removeClass('tabSelected');
					}
				}
				sectionNum = tabId;
				game();
			});
		};
		
		var assignAns = function (ansId){
			$('#answerSection').append('<div id="drop'+ansId+'" class="dropObjects"></div>');
			var dropObjCss = { 
					'width':'125px','height': '30px','margin':'1.7em 0.5em 0.2em 0.5em' ,
					'border-bottom': '2px solid black','float':'left'
				};
			$('.dropObjects').css(dropObjCss);			
		};
		
		var assignDragAns = function (optId){
			$('#dragAnswers').append('<div id="drag'+optId+'" class="dragObjects">'+answersOpts[optId]+'</div>');
			var dragObjCss = {
					'float': 'left','cursor': 'move',
					'height':'30px','padding': '0px 1em',
					'font':'20px/25px bold Arial,Verdana,Geneva,Helvetica'
				};
			$('.dragObjects').css(dragObjCss);	
		};
		
		var next_questions = function(){
			if(totalCounter === TOTAL_QUES){
				display_game_over();
			}
			else{
				currentQuestion = totalCounter;
				flag_checked = 0;
				genRandOpts();
				for(i=0; i<3; i++){
					arrangedAns[i] = 0;
				}
				var randImage = randImages[currentQuestion]+(sectionNum*TOTAL_QUES);			
				var randNum = randPositions[currentQuestion]+(sectionNum*TOTAL_QUES);
				currentQues = animals[randNum];
				currentAnimal = animals[randImage];
				$('#questionSection').html('');
				$('#imgAnimals').html('<img class="imgAnim" src = "assets/image/'+currentAnimal+'.png" />');
				$('#questionSection').append('<div id="ques'+currentQuestion+'" class="questions"></div>');
				$('#ques'+currentQuestion).append(totalCounter+1+'.').append(' Is this ');			
				 
				if(currentQues[0] === 'a' || currentQues[0] === 'e' ||currentQues[0] === 'i' ||currentQues[0] === 'o' ||currentQues[0] === 'u'){
					$('#ques'+currentQuestion).append('an ');
				}
				else{
					$('#ques'+currentQuestion).append('a ');
				}
				$('#ques'+currentQuestion).append(currentQues+' ?');	
				if(currentAnimal === currentQues){
					correctQuest = 1;
				}
				else{
					correctQuest = 0;
				}
				$('#questionSection').append('<div id="answerSection"></div>');
				for(i = 0; i<3 ;i++){
					assignAns(i);
				}	
				$('#answerSection').append('<div id="comma"></div>');	
				$('#section').append('<div id="dot"></div>');	
				$('#questionSection').append('<div id="dragAnswers"></div>');
				var randOption;
				for(i = 0; i<5 ;i++){
					randOption = randOptions[i]; 
					assignDragAns(randOption);
				}
				drag_drop();
			}
									
		};
		
		function game(){	
			scoreboard.scoreboard('reset');
			$('#linkNext').hide();
			$('#gameOver').hide();
			correctCounter = 0;
			totalCounter = 0;	
			genRandPosition();
			genRandImages();
			$('#section').html('');
			$('#tabs1').removeClass('tabSelected');
			$('#tabs2').removeClass('tabSelected');
			$('#tabs'+sectionNum).addClass('tabSelected');
			$('#section').append('<div id="imgAnimals"></div>');
			$('#section').append('<div id="questionSection"></div>');
			next_questions();
		}	
		for(i = 0; i< TOTAL_LEVEL; i++){
			assignTabs(i);
		}
		sectionNum = 0;
		
		$('#linkCheck').click(function(){
			check_answers();
		});
		$('#linkStart').click(function(){
			
			sectionNum = 0;
			game();
		});
	
		$('#linkPlayAgain').click(function(){
			game();		
		});
	
		function drag_drop(){
			$('.dragObjects').draggable({ containment: '#content'});	
			$('.dragObjects').bind('dragstart', function(event, ui) {
				currentDragObject = event.target.id;	
			});
			
			$(".dropObjects").droppable({ tolerence: 'intersect' ,hoverClass: 'drophover' });
			$('.dropObjects').bind('drop', function(event, ui) {
				var currentDropObject = event.target.id;
				var droppedWord = parseInt(currentDropObject.substring(4));
				arrangedAns[droppedWord] = $('#'+currentDragObject).text();
			});
		}
		
		
	}); //end of k.ready
});	//end of document.ready