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

componentMarkSentence = function(name) 
{
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.elementHeight = 0;
	
	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";
};

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

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

componentMarkSentence.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 = 15 + this.getTextHeight(elem, this.text);
	var imagesWidth = this.getImagesWidth(this.radioOn, this.radioOff)+20;
	
	var sentArr = this.choiceValues.split('||');
	var corrArr = this.correctAnswer.split('||');
	
	this.elementHeight = (this.h - tempY - 5)/sentArr.length;
	var imgArr = this.getImageWidthHeight(this.checkOn);
	var imgArr = [this.checkW, this.checkH]	
	
	for(var i=0; i < sentArr.length; i++){
		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; 
		checkElem.x = tempX;
		checkElem.y = this.y + tempY;		
		checkElem.w = this.w;
		checkElem.h = this.elementHeight;
		checkElem.textOffset = 0;
		checkElem.padding = 0;
		checkElem.paddingTopText = 0;
		checkElem.cursor = this.cursor;
		checkElem.text.x = imgArr[0];
		checkElem.text.y = this.y + tempY;	
		checkElem.text.w = this.w - imagesWidth - 20;
		checkElem.text.h = this.elementHeight;//this.getTextWidth(elem, sentArr[i]);
		checkElem.text.text = sentArr[i];
		checkElem.text.font.color = this.font.color;
		checkElem.text.font.family = this.font.family;
		checkElem.text.font.size = this.font.size;
		checkElem.align = "left";
		checkElem.valign = 'top';		
		checkElem.padding = 0;
		var isCorrect = this.checkIsCorrect(corrArr, i);
		checkElem.correctAnswer = (isCorrect == 1) ? 1 : 0;
		if (PageInterface.GetValue("state")==psDesigning || PageInterface.GetValue("state")==psPreview) checkElem.checked = checkElem.correctAnswer;
		checkElem.showCorrectAnswerIndicator = this.showCorrectAnswerIndicator;
		
		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.changeChecked = this.__changeChecked;		
		checkElem.parseCheckAnswer = this.__parseCheckAnswer;		
		
		checkElem.imgn.src = this.checkOff;
		checkElem.imgn.w = imgArr[0];
		checkElem.imgn.h = imgArr[1];	
		
		checkElem.imgc.src = this.checkOn;
		checkElem.imgc.w = imgArr[0];
		checkElem.imgc.h = imgArr[1];

		checkElem.group = this.id; //pg2-dg -add sub-component to this component group
		
		this.checks.push(checkElem);
		var objRodio = page.addChild(checkElem);
	
		tempY += this.elementHeight;//sentText.h;
	};	
};

componentMarkSentence.prototype.onRepaint = function()
{
	return ;
}

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

componentMarkSentence.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;
};

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

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

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

componentMarkSentence.prototype.onShow = function() 
{
	for (var i=0; i < this.checks.length; i++) 
	{
		this.checks[i].show();
		this.checks[i].text.show();
	};
};

componentMarkSentence.prototype.__changeChecked = function(obj) 
{
	if (!this.locked) {
		this.isChanged = true;
		this.checked = !this.checked;
		this.paint();
	};
};

componentMarkSentence.prototype.__parseCheckAnswer = function() 
{
	if(this.correctAnswer == this.getAnswer()){
		if (this.myParent.showAnswerIndicatorAnyTime && this.myParent.showCorrectAnswerIndicator){
			this.imgc.src = this.imgCheckedCorrectSrc;
			this.imgn.src = this.imgNotCheckedCorrectSrc;
			
			this.imgc.paint();
			this.imgn.paint();
		};
		return true;
	}else{
		if (this.myParent.showAnswerIndicatorAnyTime && this.myParent.showCorrectAnswerIndicator){
			this.imgc.src = this.imgCheckedIncorrectSrc;
			this.imgn.src = this.imgNotCheckedIncorrectSrc;
			
			this.imgc.paint();
			this.imgn.paint();
	};
	}
	return false;
};
//-------------------------------------------------------------------
//pg2-dg -define your own methods
//-------------------------------------------------------------------
componentMarkSentence.prototype.parseCheckAnswer = function(obj)
{
	return this.checkAnswer();
};

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

componentMarkSentence.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;
}

componentMarkSentence.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) {};
	}
}

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

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

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

componentMarkSentence.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;
};

componentMarkSentence.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;
}
