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

componentWordDefinition = function(name) 
{
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.passed = false;
	this.basketsCount = 0;
	this.componentX = 0;
	this.componentY = 0;
	
	//this.sentence = null;
	this.inputs = new Array();
	this.texts = new Array();
};

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

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

componentWordDefinition.prototype.componentWordDefinitionBaseOnCreate = componentWordDefinition.prototype.onCreate;
componentWordDefinition.prototype.onCreate = function(obj) {
	this.componentWordDefinitionBaseOnCreate();
	
	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="width: '+eval(this.w-200)+' ; visibility: hidden; position: absolute; font-family:' + this.font.family
	+ '; color:' + this.font.color + ';"></span>';
	
	var ob = document.getElementById(this.id);
	ob.innerHTML += sizeDiv;

	var match = this.text.match(/\{[^\{]+\}/gi);
	
	var baskets = "";
	baskets += '<font style="font-family:' + this.font.family;
	baskets += '; font-size:' + this.getFontSize(this.font.size, "px") + 'px; color:' + this.font.color + ';';
	baskets += ' align="right">';
	baskets += this.text
	baskets += '</font>';

	//textBackup = this.text;
	textBackup  = this.clearHTMLTags(this.text);
	
	for (var i=0; i < match.length; i++) {
		// Czysty tekst bez nawiasow
		onlyText = match[i].substr(1,match[i].length-2);
		
		baskets = baskets.replace(match[i], "<span id='_"+this.id+"_"+i+"_highlight' style='background-color:" + this.selectedColor + "; font-size:" + this.getFontSize(this.font.size, "px") + "px; color:" + this.font.color + "; font-family:" + this.font.family + ";'>"+onlyText+"</span>");
	};
	
	this.text = baskets;
	var wordsArr = this.wordAndDefinition.split('||');	
	
	var maxHeight = 0;
	var fontSize = this.getFontSize(this.font.size, "px");
	
	var inputX = this.x + this.w - 190;
	var textX = this.x + 5;
	var tempY = this.y + this.h - 5;
	
	wordsArr = this.jointDwoArray(wordsArr, match);
	
	var randomIndexes = this.randomElements(wordsArr);
	var sizeDiv = document.getElementById(this.id+"sizeDiv");
	
	for (var i = wordsArr.length-1; i > -1; i--){
		var correct = wordsArr[i].split('#!#');
		correct[0] = this.clearHTMLTags(correct[0]);
		if (textBackup.indexOf('{'+correct[0]+'}') > -1){
			var textDimArr = this.getTextHeightWidth(sizeDiv, correct[1]);
			
			tempY -= eval(textDimArr[0] + 22);
			
			var input = new componentInput;
			input.parentText = this; 
			input.id = this.id+'_Inp_'+randomIndexes[i];
			input.x = inputX;
			input.y = tempY;
			input.w = 190;
			input.h = 22;
			input.caseSensitive = this.caseSensitive;
			input.maxLength = -1;
			input.fieldType = 'text';
			input.inputBgColor = '#ffffff';
			input.borderColor = '#000000';
			input.fontFamily = 'Arial';
			input.fontSize = 2;
			input.fontColor = '#000000';
			input.textAlign = 'left';
			input.solved = false;
			input._setHTMLDef = this.__setHTMLDef;	
			input.checkAnswer = this.__checkAnswer;	
			input.onRepaint = this.__onRepaint;	
			
			input.rePaint = this.rePaint;			
			var inputComp = page.addChild(input);			
			inputComp.correctAnswer = correct[0];
			inputComp.showCorrectAnswerIndicator = this.showCorrectAnswerIndicator;
			inputComp.correctColor = this.elementCorrectBckColor;
			inputComp.incorrectColor = this.elementInCorrectBckColor;						
			this.inputs.push( inputComp );
			
			var textTemp = new __componentText;
			textTemp.parentText = this; 
			textTemp.id = this.id+'_Def_'+randomIndexes[i]; 
			if (typeof(correct[1]) == 'undefined') correct[1] = '';
			textTemp.text = correct[1];
			textTemp.x = textX;
			textTemp.y = tempY;
			textTemp.w = eval(this.w-200);
			textTemp.h = textDimArr[0]+3;
			textTemp.padding = 3;
			
			textTemp.borderWidth = 1;
			textTemp.padding = 0;
			textTemp.borderColor = this.definitionBorderColor;
			textTemp.bgColor = this.definitionBckgColor;
			
			var compText = page.addChild(textTemp);
			
			this.texts.push( textTemp );
		};
	};
};

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

componentWordDefinition.prototype.getTextHeightWidth = function(elem, text) 
{
	var maxWidth = 0;
	var maxHeight = 0;
	
	if (elem) {
		elem.innerHTML = text;
		//elem.style.lineHeight = this.lineHeight;
		
		//elem.style.fontFamily = this.fontFamily;
		//elem.style.fontSize = this.getFontSize(this.fontSize, 'px');
		width = elem.offsetWidth;
		height = elem.offsetHeight;
	};
	
	if ( width > maxWidth ) maxWidth = width;
	if ( height > maxHeight ) maxHeight = height;
	return [maxHeight, maxWidth];
};

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

componentWordDefinition.prototype.clearHTMLTags = function(inHTML) 
{
	var regEx
	var outText
	regEx = /<[^>]*>/g 
	
	outText = inHTML.replace(regEx, "")
	
	regEx = /&nbsp;/g
	
	outText = outText.replace(regEx, "")
	
	return outText;
};

componentWordDefinition.prototype.jointDwoArray = function(words, match) 
{
	for (var i = 0; i < match.length; i++){
		var found = false;
		for (var j = 0; j < words.length; j++){
			var oneRow = words[j].split('#!#');
			var word = match[i].substr(1,match[i].length-2)
			oneRow[0] = this.clearHTMLTags(oneRow[0]);
			word = this.clearHTMLTags(word);
			
			if (oneRow[0] == word)
				found = true;
		};
		if (!found) words[words.length] = match[i].substr(1,match[i].length-2);
	};
	
	return words;
};

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

componentWordDefinition.prototype.randomElements = function(textArr) 
{
	var indexes = new Array();
	var texts = new Array();
	
	for (var i=0; i < textArr.length; i++) {
		indexes.push(i);
		texts.push( textArr[i] );
	};

	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 < textArr.length; i++) {
		textArr[i] = texts[ randomIndexes[i] ];
	};
	
	return randomIndexes;
};

componentWordDefinition.prototype.__setHTMLDef = function(valStr) 
{
	valStr = (valStr ? valStr : "");
	
	var t = '<form name="' + this.id + 'Form" id="' + this.id + 'Form">';
	t += '<input id="' + this.id + '_contentObj" name="' + this.id + 'Input" type="' + this.fieldType + '" value="' + valStr + '" ';
	if (this.maxLength != -1) { 
		t += 'maxlength="' + this.maxLength + '" ';
	};
	if (is.ns4) {
		var cols;
		if (this.w == 0) cols = 0;
		else {
			if (this.w < 25) cols = 1;
			else cols = 1 + (parseInt((this.w - 18) / 8));
		}
		t += ' size="' + cols + '"';
	};
	t += this._getStyleDef();
	t += ' onkeyup="pc.'+this.parentText.id+'.rePaint(\''+this.id+'\');"';
	t += '>';
	t += '</form>';
	
	this.setHTML(t);
};

componentWordDefinition.prototype.rePaint = function(name) 
{
	for (var i=0; i < this.inputs.length; i++){
		if (this.inputs[i].id == name){
			if (this.inputs[i].solved){
				var input = document.getElementById(name+"_contentObj");
				if (input) {
					input.style.background = 'transparent';
					this.inputs[i].solved = false;
				};
			};
		};
	};
};

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

componentWordDefinition.prototype.__onRepaint = function() 
{
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator) {
		var splitStr = "||";
		var actAnswer = this.getAnswer().toString();
		var ans = "";
		var arrCorrAns = new Array();
		arrCorrAns = this.correctAnswer.toString().split(splitStr);
		
		if (!this.caseSensitive) {
			actAnswer = actAnswer.toLowerCase();
		};
		
		for (var i = 0; i < arrCorrAns.length; i++) {
			ans = (this.caseSensitive) ? arrCorrAns[i]:arrCorrAns[i].toLowerCase();
			var input = document.getElementById(this.id+"_contentObj");
			if (input){
				if (actAnswer == ans) {
					input.style.background = this.correctColor;
					this.solved = true;
				}else{
					input.style.background = this.incorrectColor;
					this.solved = true;
				}
			}
		};	
	};	
}

componentWordDefinition.prototype.__checkAnswer = function() 
{
	var splitStr = "||";
	var actAnswer = this.getAnswer().toString();
	var ans = "";
	var arrCorrAns = new Array();
	arrCorrAns = this.correctAnswer.toString().split(splitStr);
	
	if (!this.caseSensitive) {
		actAnswer = actAnswer.toLowerCase();
	};
	
	for (var i = 0; i < arrCorrAns.length; i++) {
		ans = (this.caseSensitive) ? arrCorrAns[i]:arrCorrAns[i].toLowerCase();
		if (actAnswer == ans) {
			if (this.parentText.showAnswerIndicatorAnyTime && this.parentText.showCorrectAnswerIndicator) {
				var input = document.getElementById(this.id+"_contentObj");
				if (input) {
					input.style.background = this.correctColor;
					this.solved = true;
				}
			};
			return (true);
		}else{
			if (this.parentText.showAnswerIndicatorAnyTime && this.parentText.showCorrectAnswerIndicator) {
				var input = document.getElementById(this.id+"_contentObj");
				if (input) {
					input.style.background = this.incorrectColor;
				}
			};
		}
	};
	return (false);
};
















