/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */
 
componentQuestionToBlanks = function(name)
{
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.correctValues = new Array(new Array(), new Array());
	this.complete = false;
	this.addInactive = false;
	
	this.texts = new Array();
	
	this.compAudio = null;
	this.clickAudio = null;
	
	this.iTimerID == null;
};

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

componentQuestionToBlanks.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;
	
	if (PageInterface.GetValue("state") != psDesigning) {
		// this.text = this.textBackup;
		this.text = this.setButtonsActions(this.text);
		this.getCorrectValues(this.inputsCorrectValues);
	}
	this.generateQuestions();
	this.generateCompAudio();
};

componentQuestionToBlanks.prototype.generateCompAudio = function()
{
	if (this.enableFlashPlayer) {
		var e = new componentFlashAudioPlayer;
		e.myParent = this;
		e.id = this.id + 'Audio';
		e.step = -1;
		e.src = this.flashAudioPlayerSrc;
		e.x = 0;
		e.y = 0;
		e.w = 1;
		e.h = 1;
		e.loop = false;
		e.flashVars = "";
		this.compAudio = page.addChild(e);
		
		var e = new componentFlashAudioPlayer;
		e.myParent = this;
		e.id = this.id + 'clickAudio';
		e.step = -1;
		e.src = this.flashAudioPlayerSrc;
		e.x = 0;
		e.y = 0;
		e.w = 1;
		e.h = 1;
		e.loop = false;
		e.flashVars = "";
		this.clickAudio = page.addChild(e);		
	} else {
		var audioElement = new componentAudio;
		audioElement.myParent = this;
		audioElement.id = this.id+'Audio'; 
		audioElement.src = this.src;
		audioElement.pvWMP64 = false;
		audioElement.pvWMP7up = false;
		audioElement.step = -1;
		audioElement.editorPaint = this.editorPaint;		
		this.compAudio = page.addChild(audioElement);
		
		var audioElement = new componentAudio;
		audioElement.myParent = this;
		audioElement.id = this.id+'clickAudio'; 
		audioElement.src = this.src;
		audioElement.pvWMP64 = false;
		audioElement.pvWMP7up = false;
		audioElement.step = -1;
		audioElement.editorPaint = this.editorPaint;
		this.clickAudio = page.addChild(audioElement);	
	}
};

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

componentQuestionToBlanks.prototype.componentQuestionToBlanksBaseOnPaint = componentQuestionToBlanks.prototype.onPaint;
componentQuestionToBlanks.prototype.onPaint = function() 
{
	if (this.locked) return;
	
	this.componentQuestionToBlanksBaseOnPaint();	
};

componentQuestionToBlanks.prototype.generateQuestions = function()
{
	var sizeDiv = '<span id="'+this.id+'sizeDiv" style="width: '+eval(this.w-200)+' ; visibility: hidden; position: absolute; font-family:' + this.font.family
	+ '; font-size:' + this.getFontSize(this.font.size, "px") + 'px; color:' + this.font.color + ';"></span>';
	
	var ob = document.getElementById(this.id);
	ob.innerHTML += sizeDiv;
	
	var questArr = this.questionToInputs.split('||');
	
	var textX = this.x + 10;
	var tempY = this.y + this.h - 10;
	
	for (var i = 0; i < questArr.length; i++){
		var wordsArr = questArr[i].split('#!#');
		var textTemp = new __componentText;
		textTemp.parentText = this; 
		textTemp.id = this.id+'_Quest_'+i; 
		textTemp.text = wordsArr[1];
		textTemp.w = this.w;
		textTemp.h = this.getTextHeight(wordsArr[1], 'Arial', 13) + 5;
		textTemp.x = textX;
		textTemp.y = tempY - textTemp.h;				
		textTemp.borderWidth = 1;
		textTemp.padding = 0;
		textTemp.cursor = 'hand';
		textTemp.clickAudio = wordsArr[3];
		
		textTemp.onCreate = this.onCreateText;
		textTemp.captureMouseEvents = this.captureMouseEvents;
		textTemp.eventClick = this.onMouseClickText;		
		
		this.texts.push( textTemp );
		page.addChild(textTemp);
		
		tempY -= textTemp.h + 5;
	};
	if (PageInterface.GetValue("state") != psDesigning) this.randomElements();
};

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

componentQuestionToBlanks.prototype.getAudioIndex = function(audioFile)
{
	var result = -1;
	var contentArr = this.questionToInputs.split('||');
	for(var i=0; i < contentArr.length; i++){
		var oneArr = contentArr[i].split('#!#');
		if (oneArr[3] == audioFile) {
			result = i + 1;
			break;
		}
	}
	return result;
};

componentQuestionToBlanks.prototype.onMouseClickText = function()
{
	if (this.clickAudio == '' || this.clickAudio == 'undefined' || typeof(this.clickAudio) == 'undefined')
		return;
	
	if (this.parentText.enableFlashPlayer) {
		this.parentText.compAudio.flashVars = "playIdx="
		+ this.parentText.getAudioIndex(this.clickAudio) + "&autoStart=true&playOnce=true";
		this.parentText.compAudio.show();
	} else {
		this.parentText.compAudio.setUrl(this.parentText.resourcesURL + this.clickAudio);
		this.parentText.compAudio.actionMediaPlay();
	};
};

componentQuestionToBlanks.prototype.playAudioPlay = function()
{
	if (this.audioSrc == '' || this.audioSrc == 'undefined' || typeof(this.audioSrc) == 'undefined')
		return;
		
	if (this.enableFlashPlayer) {
		this.clickAudio.flashVars = "playIdx=0&autoStart=true&playOnce=true";
		this.clickAudio.show();
	} else {
		this.clickAudio.setUrl(this.resourcesURL + this.audioSrc);
		this.clickAudio.actionMediaPlay();
	};		
};

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

componentQuestionToBlanks.prototype.randomElements = function() 
{
	var indexes = new Array();
	var sizes = new Array();
	for (var i=0; i < this.texts.length; i++) {
		indexes.push(i);
		sizes.push( 
			{ 
				x: this.texts[i].x, 
				y: this.texts[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 < this.texts.length; i++) {
		this.texts[i].x = sizes[ randomIndexes[i] ].x;
		this.texts[i].y = sizes[ randomIndexes[i] ].y;
	};
};

componentQuestionToBlanks.prototype.getTextHeight = function(text, font, size)
{
	if (document.getElementById(this.id+"sizeDiv")) {
		document.getElementById(this.id+"sizeDiv").innerHTML = text;
		document.getElementById(this.id+"sizeDiv").style.fontFamily = font;
		document.getElementById(this.id+"sizeDiv").style.fontSize = size;			
		var height = document.getElementById(this.id+"sizeDiv").offsetHeight;
	};
	return height;
};

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

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

componentQuestionToBlanks.prototype.setButtonsActions = function(text)
{
	text = text.replace(/InputOnKeyUp/g,  		 'onKeyup="pc.'+this.id+'.keyUp(this);"');
	text = text.replace(/specialID/g, this.id);	
	text = text.replace(/value=/g, '');
	return text;
};

componentQuestionToBlanks.prototype.onRepaint = function(index)
{
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator)
	{
		var inputsValues = this.inputsCorrectValues.split("||");
		
		for (var i=0; i < inputsValues.length; i++){
			if (inputsValues != ''){
				var splited = inputsValues[i].split('#!#');
				var name = splited[0].replace('specialID', '');
				var obj = document.getElementById(this.id+name);	
				var corrValues = splited[1].split('#');
				
				for (var j=0; j < corrValues.length; j++){		
					if (this.isInArray(obj.value, corrValues)){
						obj.style.background = this.correctColor;
					}else{
						obj.style.background = this.incorrectColor;
					};
				};
			};
		};
	}else if (page.solved == true){
		var inputsValues = this.inputsCorrectValues.split("||");
		for (var i=0; i < inputsValues.length; i++){
			if (inputsValues != ''){
				var splited = inputsValues[i].split('#!#');
				var name = splited[0].replace('specialID', '');
				var obj = document.getElementById(this.id+name);	
				obj.style.background = this.backgroundInputColor;
			};
		};
	};
};

componentQuestionToBlanks.prototype.isInArray = function(value, arr)
{
	for(var i=0; i < arr.length; i++){
		if (value == arr[i]) return true;
	}
	return false;
};


componentQuestionToBlanks.prototype.lock = function(index)
{
	var arr = this.inputsCorrectValues.split("||");
	var found = 0;
	var counter = 0;
	do{
		var obj = document.getElementById(this.id+'inputInsideText_'+counter);
		var addButton = document.getElementById(this.id+'bttonAddInput_'+counter);
		if (obj)  {
			obj.disabled = true;
			if (addButton) this.hideAddButton(addButton, 'out');
			found++;
		};
		counter++;
	}while (found < arr.length && counter < 100)
	this.locked = true;	
};

componentQuestionToBlanks.prototype.unlock = function()
{
	var arr = this.inputsCorrectValues.split("||");
	var found = 0;
	var counter = 0;
	do{
		var obj = document.getElementById(this.id+'inputInsideText_'+counter);
		var addButton = document.getElementById(this.id+'bttonAddInput_'+counter);
		if (obj)  {
			obj.disabled = false;
			if (addButton) this.hideAddButton(addButton, 'in');
			found++;
		};
		counter++;
	}while (found < arr.length && counter < 100)
	
	this.complete = false;
	this.locked = false;
};

componentQuestionToBlanks.prototype.checkAnswer = function(index)
{
	if (this.showAnswerIndicatorAnyTime && this.showCorrectAnswerIndicator)
		this.colorMyInputs();
		
	if (this.parseCheckAnswer(this)){
		this.playAudioPlay();
		return true;
	}else{
		return false;
	}
};

componentQuestionToBlanks.prototype.colorMyInputs = function(index)
{
	var inputsValues = this.inputsCorrectValues.split("||");
	
	for (var i=0; i < inputsValues.length; i++){
		if (inputsValues != ''){
			var splited = inputsValues[i].split('#!#');
			var name = splited[0].replace('specialID', '');
			var obj = document.getElementById(this.id+name);		
			var corrValues = splited[1].split('#');
			
			for (var j=0; j < corrValues.length; j++){	
				if (this.isInArray(obj.value, corrValues)){
					obj.style.background = this.correctColor;
				}else{
					obj.style.background = this.incorrectColor;					
				}
			};			
		};
	};
};

componentQuestionToBlanks.prototype.parseCheckAnswer = function(index)
{
	var inputsValues = this.inputsCorrectValues.split("||");
	var result = true;
	for (var i=0; i < inputsValues.length; i++){
		if (inputsValues != ''){
			var splited = inputsValues[i].split('#!#');
			var name = splited[0].replace('specialID', '');
			var obj = document.getElementById(this.id+name);		
			var corrValues = splited[1].split('#');
			
			for (var j=0; j < corrValues.length; j++){		
				if (this.isInArray(obj.value, corrValues)){
					result = true;
					break;				
				}else{
					result = false;
				};
			};			
			if (!result) return result;
		};
	};
	return result;
};

componentQuestionToBlanks.prototype.setInfoButtons = function(index)
{

	var arr = this.inputsCorrectValues.split("||");
	var found = 0;
	do{
		var obj = document.getElementById(this.id+'bttonAddInput_'+found);
		if (obj)  {
			obj.src = resourcesURL + this.buttonInfo_srcOut;
			obj.style.visibility = 'inherit';
		};
		found++;
	}while (found < arr.length)
};

componentQuestionToBlanks.prototype.hideAddButton = function(obj, action)
{

	if (obj) {
		obj.name = 'hide';
		if (action == 'out'){
			this.mouseOutAdd(obj);
		}else{
			this.mouseOverAdd(obj);
		};
	}
};

componentQuestionToBlanks.prototype.checkIsCorrectValue = function(value, index)
{
	var inputValue = value;
	var splitedValue = inputValue.split('');
	var result = false;
	for (var i=0; i < this.correctValues[index].length; i++){
		var correct = this.correctValues[index][i].split('');
		if (value == '') return this.correctValues[index][i];
		for (var j=0; j < splitedValue.length; j++){
			if (splitedValue[j] == correct[j]){
				result = true;
			}else {
				result = false;
				break;
			};
			if (j == eval(splitedValue.length-1) && result) return this.correctValues[index][i];
		};
	};
	return '';
};

componentQuestionToBlanks.prototype.getCorrectValues = function(text)
{
	var value = text;
	var str ='';
	var inputIdAndValues = value.split('||');
	for (var i=0; i < inputIdAndValues.length; i++){
		var values = inputIdAndValues[i].split('#!#');
		var idArr = values[0].split('_');
		this.correctValues[idArr[1]] = values[1].split('#');
		str += idArr[1] + ' | ';
	};
	return ;
};

componentQuestionToBlanks.prototype.keyUp = function(keyUpObj)
{
	if (this.complete) 
	var name = '';
	
	name = keyUpObj.id.toString();
	var id = name.split('_');
	
	var addButton = document.getElementById(this.id+'bttonAddInput_'+id[1]);
	if (keyUpObj){
		var correctValue = this.checkIsCorrectValue(keyUpObj.value, id[1]);	
		if (correctValue != ''){
			this.setColor(keyUpObj, this.correctColor);
		}else{
			this.setColor(keyUpObj, this.incorrectColor);
		};
	};
	//if (this.checkAnswer()) this.playAudioPlay();
};

componentQuestionToBlanks.prototype.getAnswer = function()
{

	var arr = this.inputsCorrectValues.split("||");
	var result = '';
	var sep = '';
	
	for(var i=0; i < arr.length; i++){
		var one = arr[i].split('#!#');
		var objName = one[0].replace('specialID', this.id);
		var obj = document.getElementById(objName);
		if (result != '') sep = '||';
		
		if (obj){
			result += sep + obj.id + '#!#' + obj.value;
		};
	};
	return result;
};

componentQuestionToBlanks.prototype.setAnswer = function(valStr, showCorrect)
{	
	if ((typeof(valStr) == 'undefined' || valStr == '') && !showCorrect) return;
	if (valStr == '' && (typeof(showCorrect) == 'undefined' || showCorrect.toString() == '')) showCorrect = true;
	
	if (!showCorrect){
		var arr = valStr.split("||");
		for(var i=0; i < arr.length; i++){
			var one = arr[i].split('#!#');
			var obj = document.getElementById(one[0]);
			
			if (obj){
				obj.value = one[1];
			};
		};		
	}else{
		var arr = this.inputsCorrectValues.split("||");
		for(var i=0; i < arr.length; i++){
			var one = arr[i].split('#!#');
			var objName = one[0].replace('specialID', this.id);
			var obj = document.getElementById(objName);
			
			if (obj){
				var values = one[1].split('#');
				obj.value = values[0];
				this.setColor(obj, this.correctColor);
			};
		};
	};
	this.onRepaint();		
};

componentQuestionToBlanks.prototype.setColor = function(obj, color)
{
	if (this.automaticCheck){
		obj.style.background = color;
	}else if (this.showAnswerIndicatorAnyTime) obj.style.background = 'transparent';
};

componentQuestionToBlanks.prototype.componentQuestionToBlanksBaseUnselectable = componentQuestionToBlanks.prototype.unselectable;
componentQuestionToBlanks.prototype.unselectable = function()
{
	this.componentQuestionToBlanksBaseUnselectable();
	
	var arr = this.inputsCorrectValues.split("||");
	var result = '';
	var sep = '';
	
	for(var i=0; i < arr.length; i++){
		var one = arr[i].split('#!#');
		var objName = one[0].replace('specialID', this.id);
		var obj = document.getElementById(objName);
		if (result != '') sep = '||';
		
		if (obj){
			obj.unselectable = "";
		};
	};
};

componentQuestionToBlanks.prototype.onFree = function() {

	if (this.iTimerID != null) 
		window.clearTimeout(this.iTimerID);
}










