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

componentQuestionToText = function(name) 
{
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.elementHeight = 0;
	
	this.texts = new Array();
	this.checks = new Array();
	
	this.interactions = true; //pg2-dg -do not depend on checkboxes virtual component (group) and its interactions. Handle it by yourself
	this.interactionsType = "matching";
};

componentQuestionToText.prototype = new __componentText;
componentQuestionToText.prototype.type = "componentQuestionToText";

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

componentQuestionToText.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 sizeDiv = '<span id="'+this.id+'sizeDiv" style="visibility: hidden; position: absolute;"></span>';
	
	var ob = document.getElementById(this.id);
	ob.innerHTML += sizeDiv;
	
	var elem = document.getElementById(this.id+"sizeDiv");

	var tempX = this.x + 3;
	var tempY = this.y + 15 + this.getTextHeight(elem, this.text, this.font.family, this.font.size);
	var imagesWidth = this.getImagesWidth(this.radioOn, this.radioOff)+20;
	var questAndAnsArr = this.content.split('!@@!');

	this.elementHeight = (this.h - tempY - 5)/(questAndAnsArr.length-1);
	var questArr = questAndAnsArr[0].split('##');
	var answArr = questAndAnsArr[1].split('##');
	//var imgWidthHeightArr = this.getImageWidthHeight(this.checkOn);
	var imgWidthHeightArr = [this.checkW, this.checkH];
	
	this.correctAnswer = '';
	
	var checksCount = 0;
	
	for(var i=0; i < questArr.length; i++){
		var oneQuestArr = questArr[i].split("||")
		var questHeight = this.getTextHeight(elem, oneQuestArr[1], this.answFontFamily, this.answFontSize);
		
		var sentText = new __componentText;
		sentText.myParent = this; 
		sentText.id = this.id+'_QUEST_'+i; 
		sentText.x = tempX;
		sentText.y = tempY;
		sentText.w = this.w;
		sentText.h = questHeight;
		sentText.text = oneQuestArr[1];
		sentText.borderWidth = 0;
		sentText.padding = 0;
		
		page.addChild(sentText);
		this.texts.push(sentText);
		tempY += sentText.h;
		
		var oneAnswArr = answArr[i].split("&&");
		for(var j=0; j < oneAnswArr.length; j++){
			var splitAns = oneAnswArr[j].split("#!#");
			var questHeight = this.getTextHeight(elem, splitAns[1], this.answFontFamily, this.answFontSize);
			
			if (imgWidthHeightArr[1] > questHeight) {
				var elemHeight = imgWidthHeightArr[1]
			} else {
				var elemHeight = questHeight;
			};
			
			var checkElem = new componentCheckBox;
			checkElem.myParent = this;
			
			checkElem.dontCreateGroupComponent = true; //pg2-dg -do not use separate virtual group component. Create separate interaction
			
			//checkElem.id = this.id+'_CHECK_'+i; //pg2-replaced with below ones
			checkElem.id = this.id + '_CHECK_' + checksCount++;
			//checkElem.optionText = splitAns[1];
			
			checkElem.x = tempX + 10;
			checkElem.y = tempY;
			checkElem.w = this.w - 10;
			checkElem.h = elemHeight;
			checkElem.borderWidth = 0;
			checkElem.borderWidth = 1;
			checkElem.padding = 0;
			//checkElem.group = this.id+'group_'+i;			
			checkElem.textOffset = 0;			
			checkElem.padding = 1;			
			checkElem.paddingTopText = 0;
			
			if (PageInterface.GetValue("state") == psDesigning) checkElem.correctAnswer = (splitAns[0] == 1);
			else checkElem.correctAnswer = splitAns[0];
			checkElem.checked = 0;
			checkElem.imgChecked = this.checkOn;
			checkElem.imgNotChecked = this.checkOff;
			checkElem.imgCheckedCorrectSrc = this.correctCheckOn;
			checkElem.imgNotCheckedCorrectSrc = this.correctCheckOff;
			checkElem.imgCheckedIncorrectSrc = this.incorrectCheckOn;
			checkElem.imgNotCheckedIncorrectSrc = this.incorrectCheckOff;
			checkElem.parseCheckAnswer = this.__parseCheckAnswer;
			checkElem.changeChecked = this.__changeChecked;
			
			checkElem.showCorrectAnswerIndicator = this.showCorrectAnswerIndicator;				
			checkElem.showAnswerIndicatorAnyTime = this.showAnswerIndicatorAnyTime;				
			
			checkElem.text.w = this.w - 10 - imgWidthHeightArr[0];	
			checkElem.text.h = imgWidthHeightArr[1];
			checkElem.text.text = splitAns[1];			
			checkElem.text.font.family = this.answFontFamily;
			checkElem.text.font.size = this.answFontSize;
			checkElem.text.font.color = this.answFontColor; 
			
			checkElem.imgn.src = this.checkOff;
			checkElem.imgn.w = imgWidthHeightArr[0];
			checkElem.imgn.h = imgWidthHeightArr[1];	
			
			checkElem.imgc.src = this.checkOn;
			checkElem.imgc.w = imgWidthHeightArr[0];
			checkElem.imgc.h = imgWidthHeightArr[1];
			
			checkElem.group = this.id; //pg2-dg -add sub-component to this component group
			
			if (checkElem.correctAnswer == '1') {
				if (this.correctAnswer != '') this.correctAnswer += '||';
				this.correctAnswer += (checksCount-1) + '';
			}
			
			this.checks.push(checkElem);
			var obj = page.addChild(checkElem);
			
			tempY += elemHeight + 5;
		};
	};
};

componentQuestionToText.prototype.onRepaint = function() 
{
	//po to aby nie zmieniala sie czcionka pytania po nacisnieci na OK
	return;
}

componentQuestionToText.prototype.__changeChecked = function(obj) 
{
	if (!this.locked) {
		if (!this.invokeEvent("Change")) {
			this.isChanged = true;
			this.checked = !this.checked;
			this.paint();
		}
	}
};

componentQuestionToText.prototype.__parseCheckAnswer = function() 
{
	var isCorrect = false;
	var actAnswer = this.getAnswer().toString();
	var arrCorrAns = new Array();
	
	if(typeof(this.correctAnswer)!="undefined"){
		if (actAnswer == this.correctAnswer) isCorrect = true;
		if (this.showAnswerIndicatorAnyTime && this.showCorrectAnswerIndicator){
			if (isCorrect){
			this.imgc.src = this.imgCheckedCorrectSrc;
			this.imgn.src = this.imgNotCheckedCorrectSrc;			
			this.imgc.paint();
			this.imgn.paint();
			}else{
				this.imgc.src = this.imgCheckedIncorrectSrc;
				this.imgn.src = this.imgNotCheckedIncorrectSrc;			
				this.imgc.paint();
				this.imgn.paint();
			}
		}
	}
	return(isCorrect);
};

componentQuestionToText.prototype.checkIsCorrect = function(arr, ind)
{
	for (var i=0; i < arr.length; i++){
		if (ind == arr[i]) return 1;
	};
	return 0;
};

componentQuestionToText.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 eval(pxSize+2);
};

componentQuestionToText.prototype.getTextHeight = function(obj, text, fontFamily, fontSize)
{
	obj.innerHTML = text;
	obj.style.fontFamily = fontFamily;
	obj.style.lineHeight = this.lineHeight;
	obj.style.fontSize = this.getFontSize(fontSize, 'px');
	var height = obj.offsetHeight;
	
	return height;
}

componentQuestionToText.prototype.getImageWidthHeight = function(imageName)
{
	var newIng = new Image();
	newIng.src = this.resourcesURL + imageName;
	
	return [newIng.width, newIng.height];
}

componentQuestionToText.prototype.getImagesWidth = function(radioOn, radioOff)
{
	var width = 0;
	
	var newImg = new Image();
	newImg.src = this.resourcesURL + radioOn;
	width = newImg.width;	
	
	// newImg.src = this.resourcesURL + radioOff;	
	// width += newImg.width;
	
	return width;
}

componentQuestionToText.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.checks.length; i++) {
			this.checks[i].show();
			this.checks[i].text.show();
		};
	};
};
//-------------------------------------------------------------------
//pg2-dg -define your own methods
//-------------------------------------------------------------------
componentQuestionToText.prototype.parseCheckAnswer = function(obj)
{
	return this.checkAnswer();
};

componentQuestionToText.prototype.checkAnswer = function()
{
	var result = true;
	result = (this.correctAnswer == this.getAnswer());
	
	return result;
}

componentQuestionToText.prototype.getAnswer = function()
{
	var result = "";
	for (var i=0; i<this.checks.length; i++) {
		var obj = this.checks[i];
		if (obj && obj.checked) {
			if (result != '') result += '||';
			result += i + '';
		}
	}
	
	return result;
}

componentQuestionToText.prototype.setAnswer = function(valStr, showCorrect)
{
	if ((typeof(valStr) == 'undefined' || valStr == '') && !showCorrect) return;
	if (valStr == '' && (typeof(showCorrect) == 'undefined' || showCorrect.toString() == '')) showCorrect = true;
	
	var answers;
	if (!showCorrect) {
		answers = valStr.split(',');
	} else {
		answers = this.correctAnswer.split('||');
	}
	
	for (var i=0; i<answers.length; i++) {
		try {
			var number = parseInt(answers[i]);
			var obj = this.checks[number];
			if (obj && typeof(obj.setAnswer) == 'function') obj.setAnswer(1);
		} catch(e) {};
	}
}

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

componentQuestionToText.prototype.interactionsCorrectResponses = function(index)
{
	var result = this.correctAnswer;
	while (result.indexOf('||') > -1) result = result.replace('||', ',');
	return result;
};

componentQuestionToText.prototype.interactionsStudentResponse = function()
{
	var result = this.getAnswer();
	while (result.indexOf('||') > -1) result = result.replace('||', ',');
	return result;
};

componentQuestionToText.prototype.interactionsCorrectResponsesAsText = function(index)
{
	var result = "";
	var answersList = this.correctAnswer.split('||');
	for (var i=0; i<answersList.length; i++) {
		var answerIndex = answersList[i];
		var checkName = this.id + '_CHECK_' + answerIndex;
		
		for (var j=0; j<this.checks.length; j++) {
			var obj = this.checks[j];
			if (obj && obj.id == checkName) {
				if (result != '') result += ',';
				var answer = obj.text.text;
				answer = answer.replace(new RegExp(',', "g"), '');
				result += escape(answer);
				break;
			}
		}
	}
	
	return result;
};

componentQuestionToText.prototype.interactionsStudentResponseAsText = function()
{
	var result = "";
	var answersList = this.getAnswer().split('||');
	for (var i=0; i<answersList.length; i++) {
		var answerIndex = answersList[i];
		var checkName = this.id + '_CHECK_' + answerIndex;
		
		for (var j=0; j<this.checks.length; j++) {
			var obj = this.checks[j];
			if (obj && obj.id == checkName) {
				if (result != '') result += ',';
				var answer = obj.text.text;
				answer = answer.replace(new RegExp(',', "g"), '');
				result += escape(answer);
				break;
			}
		}
	}
	
	return result;
}
