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

componentFindWrongWord = function(name) 
{
	this.componentElement = componentElement;
	this.componentElement(name);
	
	this.spaceBetween = 0;
	this.elementHeight = 0;
	this.elementWidth = 0;
	this.texts = new Array();
	this.buttons = new Array();
	this.isLocked = false;
	
	this.interactions = true;
	this.interactionsType = "matching";		
};

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

componentFindWrongWord.prototype.onBaseCreate = function(obj) {
	this.captureMouseEvents();
};

componentFindWrongWord.prototype.onCreate = function(obj)
{
	if ( PageInterface.GetValue("state") != psDesigning && lms && this.disableOnlineOfflineMix != false ) 
	{
		this.resourcesStatus 	= lms.resourcesStatus();
		this.resourcesURL 		= lms.resourcesURL();
	} else this.resourcesURL = resourcesURL;
	
	var maxHeight = 25;
	var wordaArr = this.choiceValues.split('||');
	
	var tempX = this.x + 3;
	var tempY = this.y + 3;		
	var imageHeigth = 0;
	var imageWidth = 0;
	
	var correctAns = this.changeFromInt(this.correctAnswer);
	
	this.spaceBetween = (wordaArr.length-1)*2;			
	this.elementWidth = ((this.w - imageWidth - 5) - (this.spaceBetween*(wordaArr.length-1)+6))/wordaArr.length;
	this.elementHeight =  eval(this.h - imageHeigth - 12);
	
	for(var i=0; i < wordaArr.length; i++){
		var ddelementText = new __componentText;
		ddelementText.myParent = this; 
		ddelementText.id = this.id+'_CHECK_'+i; 
		ddelementText.x = tempX;
		ddelementText.y = tempY;
		ddelementText.w = this.elementWidth;
		ddelementText.h = this.elementHeight;
		ddelementText.text = wordaArr[i];
		ddelementText.valign = 'middle';
		ddelementText.align = this.fontAlign;
		ddelementText.font.size = this.fontSize;
		ddelementText.font.color = this.fontColor;
		ddelementText.font.family = this.fontFamily;
		
		ddelementText.borderWidth = 1;
		ddelementText.padding = 0;
		ddelementText.borderColor = this.elementBorderColor;
		ddelementText.bgColor = this.elementBckColor;		
		ddelementText.cursor = this.elementCursor;		
		ddelementText.checked = false;
		ddelementText.isCorrect = this.isInArray(correctAns, i);
		ddelementText.solved = false;
		
		ddelementText.onCreate = this.onCreateText;
		ddelementText.eventMouseDown = this.onMouseClickText;
		ddelementText.baseOnPaint = ddelementText.onPaint;
		ddelementText.onPaint = this._onPaint;								
		
		page.addChild(ddelementText);
		this.texts.push(ddelementText);
		
		tempX += this.elementWidth + this.spaceBetween;
		tempY = this.y + 3; 
	};	
	if (this.randAns == 1) this.randomElements(this.texts);
};

componentFindWrongWord.prototype.onCreateText = function(text) 
{
	this.captureMouseEvents();
};

componentFindWrongWord.prototype._onPaint = function() 
{
	if (this.locked) {
		if (this.solved) {
			this.setBgColor(this.myParent.elementCorrectBckColor);
		}else { 
			this.setBgColor(this.myParent.elementInCorrectBckColor);
		}
		return;
	}
	this.baseOnPaint();
}

componentFindWrongWord.prototype.editorPaint = function()
{
	return '';
}

componentFindWrongWord.prototype.onPaint = function() 
{
	if (this.locked) return;

	for (var i=0; i < this.texts.length; i++) {
		this.texts[i].setBorder(this.elementBorderColor, 1);
		this.texts[i].obj.style.background = this.elementBckColor;		
		this.texts[i].checked = false;
		this.texts[i].solved = false; //pg2-tes
	};
};

componentFindWrongWord.prototype.onRepaint = function(obj) 
{
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator)
	{
		this.colorMyElements(); //pg2-new colour of answers
		
		/*for (var i=0; i < this.texts.length; i++) { //pg2-removed
			if ((this.texts[i].checked && this.texts[i].isCorrect) || (!this.texts[i].checked && !this.texts[i].isCorrect)){	
				this.texts[i].setBorder(this.elementCorrectBorderColor, 1);				
				this.texts[i].obj.style.background = this.elementCorrectBckColor;
			}else{ 
				this.texts[i].setBorder(this.elementInCorrectBorderColor, 1);						
				this.texts[i].obj.style.background = this.elementInCorrectBckColor;
			};
		};*/
	};
	return;
};

componentFindWrongWord.prototype.isInArray = function(str, index) 
{
	var arr = str.split('||');
	for (var i=0; i < arr.length; i++){
		if (arr[i] == index) return true;
	};
	return false; 
};

componentFindWrongWord.prototype.changeFromInt = function(str) {
	if (str == null || typeof(str) == "undefined" || str == "null" || str == "0") return "";

	var arrTemp = "", brk = "";
	var nr = 0;

	for (var i=0; i<str.length; i++) {
		nr += Math.pow(10,(str.length-i-1))*str.charAt(i);
	}

	while ( 1 ) {
		arrTemp += nr % 2;
		if ( (nr == 0) || (nr == 1) ) {
			break;
		}
		nr = Math.floor(nr / 2);  	
	}

	str = "";
	for (i=0; i<arrTemp.length; i++)
		if (arrTemp.charAt(i) == '1') {
			str += brk + i.toString();
			brk = "||";
		}
	return str;
};

componentFindWrongWord.prototype.onShow = function() 
{
	if (PageInterface.GetValue("state") != psDesigning) {
		for (var i=0; i < this.texts.length; i++) {
			this.texts[i].show();
		};
		for (var i=0; i < this.buttons.length; i++) {
			this.buttons[i].show();
		};
	};
};

componentFindWrongWord.prototype.get_random = function(from, to) {
    var ranNum = Math.floor( Math.random() * (to-from+1) ) + from;
    return ranNum;
};

componentFindWrongWord.prototype.randomElements = function(arr) 
{
	var indexes = new Array();
	var sizes = new Array();
	
	for (var i=0; i<arr.length; i++) {
		indexes.push(i);
		sizes.push( 
			{ 
				x: arr[i].x, 
				y: arr[i].y
			}
		);
	};
	
	var randomIndexes = new Array();
	while( indexes.length != 0 ) {
		randomIndexes.push( indexes.splice( this.get_random(0, indexes.length-1),1 ) );
	};
	for (var i=0; i<arr.length; i++) {
		arr[i].x = sizes[ randomIndexes[i] ].x;
		arr[i].y = sizes[ randomIndexes[i] ].y;

		arr[i].startX = arr[i].x;
		arr[i].startY = arr[i].y;
	
		arr[i].x = sizes[randomIndexes[i]].x;
		arr[i].y = sizes[randomIndexes[i]].y;
	};
};

componentFindWrongWord.prototype.removeColours = function() 
{
	for (var i=0; i < this.texts.length; i++) {
		this.texts[i].obj.style.background = this.elementBckColor;	
		this.texts[i].checked = false;
	};
}

componentFindWrongWord.prototype.onMouseClickText = function() 
{
	if (this.myParent.isLocked) return;
	if (this.myParent.onlyOneClicked){
		this.myParent.removeColours();
	}
	
	this.checked = !this.checked;
	this.setBorder(this.borderColor, 1);	
	if (this.checked){
		this.obj.style.background = this.myParent.elementBorderColorClick;				
	}else {
		this.obj.style.background = this.myParent.elementBckColor;				
	};
};

componentFindWrongWord.prototype.getAnswer = function() 
{
	var valStr = '';
	var sep = '';
	 
	for (var i=0; i < this.texts.length; i++) {
		if (valStr != '') sep = '||'
		valStr += sep + this.texts[i].id + '##';
		valStr += (this.texts[i].checked)?1:0;
	};
	return(valStr);
};

componentFindWrongWord.prototype.setAnswer = function(valStr, showCorrect)
{
	//if ((typeof(valStr) == 'undefined' || valStr == '') && !showCorrect) return; //pg2-removed
	if (valStr == '' && (typeof(showCorrect) == 'undefined' || showCorrect.toString() == '')) showCorrect = true;
	
	if (showCorrect){
		for (var i=0; i < this.texts.length; i++) {
			if (this.texts[i].isCorrect){
				this.texts[i].checked = true;
				//pg2-rem
				//this.texts[i].bgColor = this.elementCorrectBckColor; //this.elementBorderColorClick; 
				//this.texts[i].paint();
				
				//pg2-new
				this.texts[i].obj.style.background = this.elementCorrectBckColor;
				this.texts[i].setBorder(this.elementCorrectBckColor, 1);
			}else{
				this.texts[i].checked = false;
				//pg2-rem
				//this.texts[i].bgColor = this.elementBckColor;
				//this.texts[i].paint();
				
				//pg2-new
				this.texts[i].obj.style.background = this.elementBckColor;
				this.texts[i].setBorder(this.elementBckColor, 1);
			}
		};
	} else {
		if (valStr == '') {
			for (var i=0; i < this.texts.length; i++) {
				this.texts[i].checked = false;
				this.texts[i].obj.style.background = this.elementBckColor;
				this.texts[i].setBorder(this.elementBckColor, 1);
			}
		} else {
		var arr = valStr.split('||');
			for (var i=0; i < arr.length; i++){
				var oneCheck = arr[i].split('##');
				var obj = document.getElementById(oneCheck[0]);
				
				/*
				var j = -1;
				do{
					j++;
				}while (this.texts[j].id != oneCheck[0] && j < this.texts.length)
				
				if (oneCheck[1] == 1){
					this.texts[j].checked = true;
					this.texts[j].bgColor = this.elementBorderColorClick;				
					this.texts[j].paint();				
				};*/
				
				if (oneCheck[1] == "1") { //pg2-new
					obj.owner.checked = true;
					obj.owner.obj.style.background = this.elementBorderColorClick;
					obj.owner.setBorder(this.elementBorderColorClick, 1);
					//obj.owner.paint();
				} else {
					obj.owner.checked = false;
					obj.owner.obj.style.background = this.elementBckColor;
					obj.owner.setBorder(this.elementBckColor, 1);
					//obj.owner.paint();
				}
			};
		};
	};
	this.onRepaint(); //pg2-rem
	return(valStr);
};

componentFindWrongWord.prototype.checkAnswer = function() 
{ 
	var result = this.parseCheckAnswer(this);
	
	if (this.showAnswerIndicatorAnyTime && this.showCorrectAnswerIndicator && page.countAnswers != 0) {
		this.colorMyElements();	
	}
	return result;
};

componentFindWrongWord.prototype.colorMyElements = function() 
{
	var result = true;
	for (var i=0; i < this.texts.length; i++) {
		/*if (this.texts[i].checked && this.texts[i].isCorrect){	
			this.texts[i].setBorder(this.elementCorrectBorderColor, 1);	
			this.texts[i].obj.style.background = this.elementCorrectBckColor;		
		}else{
			this.texts[i].setBorder(this.elementInCorrectBorderColor, 1);						
			this.texts[i].obj.style.background = this.elementInCorrectBckColor;		
			//this.texts[i].setBorder(this.elementBorderColor, 1);	
			//this.texts[i].obj.style.background = this.elementBckColor;		
			if (this.texts[i].checked) this.texts[i].checked = !this.texts[i].checked;
		}*/
		
		//pg2-new colour of answers
		if (this.texts[i].checked && this.texts[i].isCorrect){		
			this.texts[i].setBorder(this.elementCorrectBorderColor, 1);
			this.texts[i].obj.style.background = this.elementCorrectBckColor;			
		} else 
		if (this.texts[i].checked && !this.texts[i].isCorrect) {
			this.texts[i].setBorder(this.elementInCorrectBorderColor, 1);					
			this.texts[i].obj.style.background = this.elementInCorrectBckColor;		
		}

		
	};
};

componentFindWrongWord.prototype.parseCheckAnswer = function() 
{
	var result = true;
	for (var i=0; i < this.texts.length; i++) {
		if ((this.texts[i].checked && this.texts[i].isCorrect) || (!this.texts[i].checked && !this.texts[i].isCorrect)){
			this.texts[i].solved = true;
		}else {
			result = false;
			this.texts[i].solved = false;
		}
	};
	return result;
};

componentFindWrongWord.prototype.onLock = function() 
{
	this.isLocked = true;
	for (var i=0; i < this.texts.length; i++) {
		this.texts[i].locked = true;
	};
};

componentFindWrongWord.prototype.onUnLock = function() 
{
	this.isLocked = false;
	for (var i=0; i < this.texts.length; i++) {
		this.texts[i].locked = false;
	};
};

componentFindWrongWord.prototype.interactionsCorrectResponsesCount = function()
{
	var correctAns = this.changeFromInt(this.correctAnswer);
	return correctAns.length;
};

componentFindWrongWord.prototype.interactionsCorrectResponses = function(index)
{
	var valStr = '';
	var sep = '';
	 
	for (var i=0; i < this.texts.length; i++) {
		if (valStr != '') sep = '||'
		valStr += sep + this.texts[i].id + '##';
		valStr += (this.texts[i].isCorrect)?1:0;
	};
	return(valStr);
};

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

componentFindWrongWord.prototype.interactionsCorrectResponsesAsText = function(index)
{
	var valStr = '';
	var sep = '';
	 
	for (var i=0; i < this.texts.length; i++) {
		if (valStr != '') sep = '||'
		valStr += sep + this.texts[i].id + '##';
		valStr += (this.texts[i].isCorrect)?1:0;
	};
	return(valStr);
};

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