/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */

componentMemory = function(name) 
{
	this.componentElement = componentElement;
	this.componentElement(name);

	this.resourcesStatus = "online";
	this.resourcesURL = "";
	this.howManyOpen = 0;
	this.rememberOpen = new Array();
	this.rememberTexts = new Array();
	this.rememnerSequence = new Array();
	this.rememnerPopups = new Array();	
	this.pairsArr = new Array();	
	this.twoDimArr= new Array();		
	this.elementsAllInfo = new Array();
	this.timer = null;
	this.openCards = new Array();
	
	this.interactions = true;
	this.interactionsType = "matching";  
	
};

componentMemory.prototype = new componentElement;
componentMemory.prototype.type = "componentMemory";

componentMemory.prototype.onCreate = function(obj)
{
	this.captureMouseEvents();

	if ( lms && this.disableOnlineOfflineMix != false ) 
	{
		this.resourcesStatus 	= lms.resourcesStatus();
		this.resourcesURL 		= lms.resourcesURL();
	} 
	else this.resourcesURL = resourcesURL;
	this.resourcesURL = unescape(resourcesURL);

	this.correctAnswersList = this.correctAnswer;
	
	for (var i=0; i < this.rowsNumber; i++){
		this.twoDimArr[i] = new Array();
	};
};

componentMemory.prototype.onPaint = function(obj)
{	
	this.openCards = [];
	
	var changed = unescape(this.content);
	var changed = quotedString(changed);
	var splitedContent = changed.split("|!|");
	
	this.pairsArr = this.getPairsIndex(splitedContent); 
	
	var doubleArr = this.makeDoubleArray(splitedContent, this.pairsArr);
	var randomed = this.setRandomArray(doubleArr);
	this.twoDimArr = this.arrIntoTwoDimArr(this.pairsArr);
	var fontSize = this.getFontSize(this.fontSize, 'px');

	var width 	= this.w + this.borderWidth*2;
	var height 	= this.h + this.borderWidth*2;  
	var t = '<table style="width:' + width + 'px; height:' + height + 'px;" id="tab_' + this.id + '" >';
	
	if (PageInterface.GetValue("state") == psDesigning){
		var index = 0;
		for (var i=0; i < this.rowsNumber; i++){
			t += '<tr>';
			for (var j=0; j < this.colsNumber; j++){
				var oneCell = randomed[index].split('#!#');
				t += '<td align="center" valign="middle" width="'+eval(width/this.colsNumber)+'px" height="'+eval(height/this.rowsNumber)+'">';
				if (this.displayImage){
					t += '<img id=image_cell_'+i+';'+j+' src="'+this.resourcesURL + oneCell[1] + '">';
				}else if (this.displayText){
					if (oneCell[3] != 'OnlyImage') {
						t += '<font style="font-size:' + fontSize + '; color:' + this.fontColor + '; font-family:' + this.fontFamily + '">'
						t += oneCell[2];
						t += '</font>';		
					}else{
						t += '<img id=image_cell_'+i+';'+j+' src="'+this.resourcesURL + oneCell[1] + '">';
					};
				}else if (this.displayDiffImage){
					if (oneCell[3] != 'OnlyDiffImage') {
						t += '<img id=image_cell_'+i+';'+j+' src="'+this.resourcesURL + oneCell[5] + '">';	
					}else{
						t += '<img id=image_cell_'+i+';'+j+' src="'+this.resourcesURL + oneCell[1] + '">';
					};
				};
				t += '</td>';
				index++;
			};
			t += '</tr>';
		};
	}else{
		var index = 0;
		for (var i=0; i < this.rowsNumber; i++){
			t += '<tr>';
			for (var j=0; j < this.colsNumber; j++){
				t += '<td align="center" valign="middle" width="'+eval(width/this.colsNumber)+'px" height="'+eval(height/this.rowsNumber)+'">';
				var oneCell = randomed[index].split('#!#');				
				index++;
				if (this.displayImage){
					t += '<img id=image_cell_'+i+';'+j+' src="'+this.resourcesURL + this.imageTop+'" style="cursor: hand;" onClick="pc.'+this.id+'.changeIntoImage(this,\''+ oneCell[1] +'\', \''+ oneCell[4] +'\', true);" >';
					this.elementsAllInfo[i+';'+j] = oneCell.join('#');
				}else if (this.displayText){
					t += '<table cellSpacing="0" cellPadding="0"><tr><td id=replace_cell_'+i+';'+j+' align="center" valign="middle" >';
					if (oneCell[3] != 'OnlyImage') {
						t += '<img id=image_cell_'+i+';'+j+' src="'+this.resourcesURL + this.imageTop+'" style="cursor: hand;" onClick="pc.'+this.id+'.changeIntoText(\''+ oneCell[2] +'\', \''+ oneCell[4] +'\', '+i+','+j+', true );">';
						this.elementsAllInfo[i+';'+j] = oneCell.join('#');
					}else{
						t += '<img id=image_cell_'+i+';'+j+' src="'+this.resourcesURL + this.imageTop+'" style="cursor: hand;" onClick="pc.'+this.id+'.changeIntoImage(this,\''+ oneCell[1] +'\', \''+ oneCell[4] +'\', true );" >';
						this.elementsAllInfo[i+';'+j] = oneCell.join('#');
					};
					t += '</td></tr></table>';
				}else if (this.displayDiffImage){
					if (oneCell[3] != 'OnlyDiffImage') {
						t += '<img id=image_cell_'+i+';'+j+' src="'+this.resourcesURL + this.imageTop+'" style="cursor: hand;" onClick="pc.'+this.id+'.changeIntoImage(this,\''+ oneCell[5] +'\', \''+ oneCell[4] +'\', true);" >';
						this.elementsAllInfo[i+';'+j] = oneCell.join('#');
					}else{
						t += '<img id=image_cell_'+i+';'+j+' src="'+this.resourcesURL + this.imageTop+'" style="cursor: hand;" onClick="pc.'+this.id+'.changeIntoImage(this,\''+ oneCell[1] +'\', \''+ oneCell[4] +'\', true);" >';
						this.elementsAllInfo[i+';'+j] = oneCell.join('#');
					};
					
				};
				t += '</td>';
			};
			t += '</tr>';
		};
	};
	t += '</table>';
	this.setHTML(t);
};

componentMemory.prototype.checkAnswer = function() 
{ 
	return(this.parseCheckAnswer(this));
};

componentMemory.prototype.parseCheckAnswer = function(obj) 
{
	if(!obj) return(false);
	for (var i=0; i < this.rowsNumber; i++){
		for (var j=0; j < this.colsNumber; j++){
			if (this.twoDimArr[i][j] != -1) return false;
		};
	};	
	return true;
};

componentMemory.prototype.getFontSize = function(value, format)
{
	var fontSizesPX = new Array(0,10,13,16,18,24,32,48);
	var fontSizesPT = new Array(0, 8,10,12,14,18,24,36);
	var pxSize = fontSizesPT[parseInt(value)];
	if (format == "px") {
		pxSize = fontSizesPX[parseInt(value)];
	}
	return pxSize;
};

componentMemory.prototype.getPairsIndex = function(arr) 
{
	var indexArr = new Array();
	for (var i=0; i < arr.length; i++){
		var oneCell = arr[i].split("#!#");
		indexArr[indexArr.length] = oneCell[0];
	};
	return indexArr;
};

componentMemory.prototype.changeIntoText = function(text, popup, indI, indJ, noChange) 
{
	var cellObj = document.getElementById('replace_cell_' + indI+';'+indJ);
	if ((this.locked && noChange ) || this.checkCardOpen(cellObj)) return;	
	
	if (this.timer == null){
		var fontSize = this.getFontSize(this.fontSize, 'px');
		if (this.howManyOpen == 0) {
			this.rememberOpen[0] = cellObj;
			this.rememberTexts[0] = text;
			this.rememnerPopups[0] = popup;
			this.rememnerSequence[0] = 'text';
			cellObj.innerHTML = '<font style="font-size:' + fontSize + '; color:' + this.fontColor + '; font-family:' + this.fontFamily + '">'+ text +'</font>';				
			this.howManyOpen = 1;
		}else{
			var correct = this.checkIsCorrect(this.rememberOpen[0], cellObj);
			this.rememberOpen[1] = cellObj;
			this.rememberTexts[1] = text;
			this.rememnerPopups[1] = popup;
			this.rememnerSequence[1] = 'text';
			cellObj.innerHTML = '<font style="font-size:' + fontSize + '; color:' + this.fontColor + '; font-family:' + this.fontFamily + '">'+ text +'</font>';
			if (!correct) {
				this.timer = setTimeout('pc.'+this.id+'.setHidden('+this.id+');', this.hideWrongPairTimeInterval);
			}else{
				this.setMatchPair(this.rememberOpen[0], this.rememberOpen[1]);
				if (page.all[popup]) page.all[popup].show();
			}
			this.howManyOpen = 0;
		};
	};
};

componentMemory.prototype.checkCardOpen = function(obj) 
{
	if (this.rememberOpen[0]) 
	{
		if (this.rememberOpen[0].id == obj.id)  return true;//klikam na to samo co przed chwila otworzylem
	}
	
	var splited = obj.id.split("_");
	var objId = splited[2].split(";");
	
	return (this.twoDimArr[objId[0]][objId[1]] == -1);
}

componentMemory.prototype.changeIntoImage = function(obj,  image, popup, noChange) 
{
	if ((this.locked && noChange ) || this.checkCardOpen(obj)) return;	
	
	if (this.timer == null){
		obj.src = this.resourcesURL + image;
		if (this.howManyOpen == 0) {
			this.rememberOpen[0] = obj;
			this.rememnerPopups[0] = popup;
			this.rememnerSequence[0] = 'image';
			this.howManyOpen = 1;			
		}else{
			var correct = this.checkIsCorrect(this.rememberOpen[0], obj);
			this.howManyOpen = 0;
			this.rememberOpen[1] = obj;
			this.rememnerPopups[1] = popup;
			this.rememnerSequence[1] = 'image';
			if (!correct) {
				this.timer = setTimeout('pc.'+this.id+'.setHidden('+this.id+');', this.hideWrongPairTimeInterval);
			}else{
				this.setMatchPair(this.rememberOpen[0], this.rememberOpen[1]);
				if (page.all[popup]) page.all[popup].show();
			};
		};
	};
};

componentMemory.prototype.getAnswer = function()
{
	var result = "";
	var sep = "";
	for (var i=0; i < this.openCards.length; i++){
		if (result != "") sep = '||';
		result += sep + this.openCards[i];
	};
	return(result);
}

componentMemory.prototype.setAnswer = function(valStr, showCorrect)
{
	if (typeof(showCorrect) == 'undefined' && typeof(valStr) == 'undefined') showCorrect = true;
	//if (valStr == "" || typeof(valStr) == 'undefined' || !showCorrect) return; //pg2- && !!!!
	if (valStr == "" || typeof(valStr) == 'undefined') return; //pg2- removed '!showCorrect' - it's decided in PlayerInterface
	var arr = valStr.split('||');
	var cardID = 0;
	var objImg = null;
	var cardInfoArr = '';
	
	for(var i=0; i < arr.length; i++){
		for (var k=0; k < this.rowsNumber; k++){
			for (var j=0; j < this.colsNumber; j++){
				cardID = this.twoDimArr[k][j];
				if ((cardID == arr[i]) || showCorrect){
					objImg = document.getElementById('image_cell_'+k+';'+j);
					cardInfoArr = this.elementsAllInfo[k+';'+j].split('#');
					if (objImg){
						if (this.displayImage){
							this.changeIntoImage(objImg, cardInfoArr[1], cardInfoArr[4], false);
						}else if (this.displayText){
							if (cardInfoArr[3] != 'OnlyImage') {
								this.changeIntoText(cardInfoArr[2], cardInfoArr[4], IDInd[0], IDInd[1], false);
							}else{
								this.changeIntoImage(objImg, cardInfoArr[1], cardInfoArr[4], false);
							};
						}else if (this.displayDiffImage){
							if (cardInfoArr[3] != 'OnlyDiffImage') {
								this.changeIntoImage(objImg, cardInfoArr[5], cardInfoArr[4], false);
							}else{
								this.changeIntoImage(objImg, cardInfoArr[1], cardInfoArr[4], false);
							};
						};
					};
				};
			};
		};	
	};
};

componentMemory.prototype.setMatchPair = function(oneObj, twoObj)
{
	var splited = oneObj.id.split("_");
	var firstObjId = splited[2].split(";");
	splited = twoObj.id.split("_");
	var secondObjId = splited[2].split(";");
	
	this.openCards.push(this.twoDimArr[firstObjId[0]][firstObjId[1]]);
	
	this.twoDimArr[firstObjId[0]][firstObjId[1]] = -1;
	this.twoDimArr[secondObjId[0]][secondObjId[1]] = -1;
	
	this.rememberOpen = new Array();
};

componentMemory.prototype.checkIsCorrect = function(oneObj, twoObj)
{
	var splited = oneObj.id.split("_");
	var firstObjId = splited[2].split(";");
	splited = twoObj.id.split("_");
	var secondObjId = splited[2].split(";");
	
	var temp1 = this.twoDimArr[firstObjId[0]][firstObjId[1]];
	var temp2 = this.twoDimArr[secondObjId[0]][secondObjId[1]];
	
	if (this.twoDimArr[firstObjId[0]][firstObjId[1]] == this.twoDimArr[secondObjId[0]][secondObjId[1]]) {
		return true;
	}else {
		return false;
	};
};

componentMemory.prototype.setHidden = function(name, hidden)
{
	for(var i=0; i < page.children.length; i++){
		if (page.children[i].id == name.id) {
			var arr   = page.children[i].rememberOpen;
			var texts = page.children[i].rememberTexts;
			var sequence = page.children[i].rememnerSequence;
			var popup = page.children[i].rememnerPopups;
			if (arr){
				for (var i=0; i < 2; i++){
					if (sequence[i] == 'text'){
						var splited = arr[i].id.split("_");
						var objId = splited[2].split(";");
						arr[i].innerHTML = '<img id=image_cell_'+objId[0]+';'+objId[0]+' src="'+this.resourcesURL + this.imageTop+'" style="cursor: hand;" onClick="pc.'+this.id+'.changeIntoText(\''+ texts[i] +'\', \''+ popup[i] +'\', '+objId[0]+','+objId[1]+', true );">';
					}else{
						arr[i].src = this.resourcesURL + this.imageTop;
					}
				};
				this.timer = null;
			}
			break;
		};
	};
	this.rememberOpen = new Array();
};

componentMemory.prototype.makeDoubleArray = function(singleArrFirst, singleArrSecond)
{
	var length = singleArrFirst.length
	for (var i=0; i < length; i++){
		if (this.displayImage){		
			var replaced = singleArrFirst[i];
		}else if (this.displayDiffImage){
			var replaced = singleArrFirst[i].replace('OnlyImage', 'OnlyDiffImage');
		}else if (this.displayText){
			var replaced = singleArrFirst[i].replace('OnlyImage', 'OnlyText');
		}
		singleArrFirst[singleArrFirst.length] = replaced;
		this.pairsArr[this.pairsArr.length] = singleArrSecond[i];
	};
	return singleArrFirst;
};

componentMemory.prototype.arrIntoTwoDimArr = function(singleArr)
{
	var index = 0;
	for (var i=0; i < this.rowsNumber; i++){
		for (var j=0; j < this.colsNumber; j++){
			this.twoDimArr[i][j] = singleArr[index];
			index++;
		}
	};
	return this.twoDimArr;
};

componentMemory.prototype.setRandomArray = function(itemArr)
{
	for (var i=0; i < itemArr.length; i++){
		j = parseInt(Math.random()*(itemArr.length));
		var temp = itemArr[j];
		var temp2 = this.pairsArr[j];
		itemArr.splice(j,1);		
		this.pairsArr.splice(j,1);		
		itemArr.splice(0,0,temp);		
		this.pairsArr.splice(0,0,temp2);		
	};
	return itemArr;
};


componentMemory.prototype.interactionsCorrectResponsesCount = function()
{
	return 1;
};

componentMemory.prototype.interactionsCorrectResponses = function()
{
	var result = ""
	var sep = ""
	for (var k in this.elementsAllInfo){
		if (result != "") sep ="||";
		result += sep + k+'#'+this.elementsAllInfo[k]+'#-1';
	};	
	return(result);
};

componentMemory.prototype.interactionsStudentResponse = function()
{
	return this.getAnswer();
};

componentMemory.prototype.interactionsCorrectResponsesAsText = function()
{
	var result = ""
	var sep = ""
	for (var k in this.elementsAllInfo){
		if (result != "") sep ="||";
		result += sep + k+'#'+this.elementsAllInfo[k]+'#-1';;
	};	
	return(result);
};

componentMemory.prototype.interactionsStudentResponseAsText = function()
{
	return this.getAnswer();
};
