Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/examples/6_English_matchingProductsAndWords/js/lesson.js
blob: a0f62af6dedc40ef8b26dd83f9b4c7978404601a (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
$(document).ready(function() {

	var i,j,flag;
	var s=0;	var m=0;	var h=0;   //varoiables for timer
	var clickedObjects = [];   //array storing the clicks of the two succesive clicks
	var clickedObject = 0;    //store the clicked image id
	var matchedObjects = [];//store the matched images
	var objrand = [];
	var actualObjects = [];
	var numClicked = 0;       // If click on image it is incremnted by 1
	var numMatched = 0;      //how many matched objects
	var currentObj; //store the current object clicked
	var play =0;    //not played yet pause
	var restart = 0;   //not restarted
	var clickCounter = 0;
	var NUM_OBJECTS = 24;  //total number of objects in the game
	var products_words = new Array('Cow','Hen','Wood','Bee','Tap','House','Sheep','Cloth','Lamp','Leather','Wheat','Sugar','Milk','Egg','Chair','Honey','Water','Brick','Wool','Shirt','Light','Shoes','Flour','Sweet');
	//var section = $('#section');	
	var $content = $('#content');
		      
	Karma.scaleWindow();
	$('#kHeader').kHeader({title:"English: Matching Words with Products"});
	var $kFooter = $('#kFooter').kFooter({scoreboard: false, startButton: true,
		pauseButton: true, restartButton: true, timer: true});

	$kFooter.bind('kFooterStart', game);  		      
	$kFooter.bind('kFooterRestart', game);
		      
	    


	var checkTime = function(timePara){
	    if (timePara<10 )
	    {
		timePara="0" + timePara;
	    }
	    return timePara;
	};
	
	var generate_random_objects_no = function(){
	    
	    for(i=1; i<NUM_OBJECTS; i++){
		objrand[i] = i;
	    }

	    objrand = Karma.shuffle(objrand);

	    
		
	};
	

	var check_game_over = function(){
		if(numMatched === NUM_OBJECTS){   //show all
			play = 0;
			$('#content').html('');
			$('#content').append('<div id="gameOver">GAME OVER<br/>Congratulations!!!</div>');
			$('#content').append('<div id="gameOverInfo">You have completed the game in <span class="specialText">'+clickCounter+
					'</span> clicks within  <span class="specialText">'+h+'</span> hour  <span class="specialText">'+m+
					'</span> minutes and  <span class="specialText">'+s+'</span> seconds .</div>');
		}
	};
	var store_clicked_object = function(objectClicked){
		
		if(play === 1){			
		    clickedObject = objectClicked;
		    var randClick = objrand[clickedObject];
		    clickedObjects[numClicked] = randClick;
		    actualObjects[numClicked]=clickedObject;
		    numClicked++;
		    clickCounter++;
		    clickCounter = checkTime(clickCounter);  //for showing format as 1 for 01
		    $("#clickBox").html(clickCounter);

		    show_processed_image();
		    
		    return true;
		}
		else{
		    return false;
			    
		}
			
	};

	var process_object = function(){
		var matchedCondition = 0;  //not matched
		/*
		 alert('Clicked Objects:'+clickedObjects[0]+' & '+clickedObjects[1]+'\n'+
			 'Actual Positions:'+actualObjects[0]+' & '+actualObjects[1]+'\n'+
			 'Rand Objects: '+objrand);
		 */
		
		if(Math.abs(clickedObjects[0]-clickedObjects[1]) === 12 && (clickedObjects[0] != clickedObjects[1])){
			matchedCondition = 1;
		}		
		if(matchedCondition === 1){   //matches		
			//check for the * sign and if it was any one having that then show off all for 2 secs
			// future
			
			document.getElementById('object'+actualObjects[0]).innerHTML='';
			document.getElementById('object'+actualObjects[1]).innerHTML='';
			$("#object"+actualObjects[0]).addClass('matched');
			$("#object"+actualObjects[1]).addClass('matched');
			matchedObjects[numMatched] = actualObjects[0];
			numMatched++;
			matchedObjects[numMatched] = actualObjects[1];
			numMatched++;
			check_game_over();	
			numClicked = 0;			
		}
		else{
			$('#object'+actualObjects[0]).html('');
			$('#object'+actualObjects[1]).html('');
			$("#object"+actualObjects[0]).addClass('default');
			$("#object"+actualObjects[1]).addClass('default');
			numClicked = 0;			
		}
		
		
		};
	
	var delay = function(){	
		document.delayForm.delayval.value = 1;
		process_object();
		
	};
	
	var show_processed_image = function(){    //Show the click Image
	    var t;
	    currentObj = objrand[clickedObject];
	    if (numMatched !=0){   //some pairs has matched so be sure not to show them again
			var flag = 0;  //if matched already it is set to 1
			for(i = 0; i<numMatched; i++){
			    if(clickedObject === matchedObjects[i] ){
			    	flag = 1;
			    }
			}
		
			if(flag === 0){    //no matches found
				$('#object'+clickedObject).html('');
				$("#object"+clickedObject).addClass('default');
				$('#object'+clickedObject).html(products_words[currentObj]);
			    if(numClicked === 2){
			    	t=setTimeout(function(){delay();},1000);			
			    }
			}
			else{          // If click on image it is incremnted by 1
			      //how many matched      //matched already so don't show
				$('#object'+clickedObject).html('object'+actualObjects[0]);
			    numClicked = 0;
			}
	    }
		
	    else if(numClicked == 2){    //process the image after 2 successive clicks
	
	    	  $('#object'+clickedObject).html('');
		       $('#object'+clickedObject).html(products_words[currentObj]);
	    	t=setTimeout(function(){delay();},1000);
		
	    }
	    else{	
	       $('#object'+clickedObject).html('');
	       $('#object'+clickedObject).html(products_words[currentObj]);
	    }
	};

	var assignSquares = function (square){	    
	    $content.append('<a href="#"></a>');
	    $('#content a:last-of-type').append('<div class="default" id="object'+square+'"></div>'); 	
	    $('#content a:last-of-type').click(function(){		    
		    store_clicked_object(square);
		});
	};
		
	
	function game(){
		numClicked = 0;
		numMatched = 0;
		clickCounter = 0;
		matchedObjects = [];
		s=0;	m=0;	h=0;   
		$('#content').html('');
		//$('#timerBox1').html('');$('#timerBox2').html('');$('#timerBox3').html('');
		$('#clickBox').html('00');
		generate_random_objects_no();
		//startTimer();
		var square;
		for(i=0; i<NUM_OBJECTS; i++){
		    assignSquares(i);
		}
		play = 1;
		//increaseTime();		
	}
	
	$('#content').html('');
	$('#clickBox').html('00');
	//game();

});//end of DOM