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

componentCheckboxWithFeedbackMltp = function(name) 
{
	this.componentElement = componentElement;
	this.componentElement(name);

	this.preloadImg = new Image();
	this.preloadImg2 = new Image();
	this.preloading = false;
	this.preloaded = false;
	this.waitingTryCount = 0;

	this.resourcesStatus = "online";
	this.resourcesURL = "";

	this.isChanged = false;
	this.statusList = new Array();
	this.idList = new Array();

	this.correctAnswersList = "";
	this.interactions = true;
	this.interactionsType = "choice";
};

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

componentCheckboxWithFeedbackMltp.prototype.onCreate = function(obj)
{
	this.captureMouseEvents();

	if ( lms && this.disableOnlineOfflineMix != false ) 
	{
		this.resourcesStatus 	= lms.resourcesStatus();
		this.resourcesURL 		= lms.resourcesURL();
	} 
	else this.resourcesURL = resourcesURL;
};

componentCheckboxWithFeedbackMltp.prototype.checkAnswer = function() 
{ 
	return(this.parseCheckAnswer(this));
};


componentCheckboxWithFeedbackMltp.prototype.parseCheckAnswer = function(obj) 
{
	if(!obj) return(false);
	var splited = this.correctAnswersList.split('');
	for (var i=0; i < this.statusList.length; i++)
		if (splited[i] != this.statusList[i]) {
			return false;
		}
	return true;
};

componentCheckboxWithFeedbackMltp.prototype.getAnswer = function() 
{
	var result = '';
	var sep = '';

	for (var i=0; i < this.idList.length; i++){
		if (result != '') sep = '||';
		result += sep;
		result += this.idList[i] +';'+ this.statusList[i];
	};
	return result;
};

componentCheckboxWithFeedbackMltp.prototype.setAnswer = function(valStr, showCorrect)
{
	if ((typeof(valStr) == 'undefined' || valStr == '') && !showCorrect) return;
	if (valStr == '' && (typeof(showCorrect) == 'undefined' || showCorrect == '')) showCorrect = true;
	
	var splited = valStr.split("||");
	if (valStr == '' && !showCorrect) {
		for (var i=0; i < this.statusList.length; i++) this.statusList[i] = 0;	
		return;
	}
	if (!showCorrect){
		for (var i=0; i < splited.length; i++){
			var oneQuest = splited[i].split(';');
			var ind = this.findIndex(this.idList, oneQuest[0]);
			var obj = document.getElementById(this.id + '_img_' + ind);
			if (oneQuest[1] == '1'){
				this.statusList[ind] = 1;
				obj.src = this.resourcesURL + this.imgChecked;
			}else {
				this.statusList[ind] = 0;
				obj.src = this.resourcesURL + this.imgNotChecked;
			};	
		};
	}else{
		var changed = unquotedString(this.text);
		var arrOpt 	= changed.split("||");
		var splitedCorrAnswer = this.correctAnswersList.split('');
		
		for (var i = 0; i < arrOpt.length; i++) 
		{
			var one = arrOpt[i].split('#!#');
			var img = document.getElementById(this.id + '_img_' + i);
			if (one[1] == 1){
				img.src = this.resourcesURL + this.imgChecked;
				this.statusList[i] = 1;
			}else{
				img.src = this.resourcesURL + this.imgNotChecked;
				this.statusList[i] = 0;
			};
			var index = this.idList[i];
			var index = index.replace(/ /g, '');
			var splited = arrOpt[index].split("#!#");
			var name = splited[3].replace(/ /g, '');
			
			if (page.all[name]){
				this.showButton(i);
			}			
		};	
	};
};

componentCheckboxWithFeedbackMltp.prototype.findIndex = function(arr, value)
{
	for(var i=0; i < arr.length; i++){
		if (arr[i] == value) return i;
	};
	return 0;
};

componentCheckboxWithFeedbackMltp.prototype.getCorrectAnswersList = function(arr)
{
	this.correctAnswersList = '';
	for (var i = 0; i < arr.length; i++) {
		var splited = arr[i].split("#!#");
		var temp = splited[1].replace(/ /g, '');
		this.correctAnswersList += temp;
	};
};

componentCheckboxWithFeedbackMltp.prototype.onPaint = function(obj)
{
	var changed = unquotedString(this.text);
	var arrOpt 	= changed.split("||");
	var arrLength = arrOpt.length;

	this.getCorrectAnswersList(arrOpt);
	var width 	= this.w - this.borderWidth*2;
	var height 	= this.h - this.borderWidth*2;  
	var t = '<table border="0" style="width:' + width + 'px; height:' + height + 'px;" id="choice_tab_' + this.id + '" >';

	if (PageInterface.GetValue("state") == psDesigning) {
		for (var i = 0; i < arrLength; i++) 
		{
			var splited = arrOpt[i].split("#!#");
			
			t += '<tr><td style="width: '+this.button_w+' px;">';
			t += '<img src="' + this.resourcesURL + this.button_srcOut +'"></td>';
			t += '<td style="width: '+this.imageWidth+' px;">';
			var temp = splited[1].replace(/ /g, '');
			if (temp == '0') t += '<img src="' + this.resourcesURL + this.imgNotChecked;
				else t += '<img src="' + this.resourcesURL + this.imgChecked;
			t += '" id="' + this.id + '_img_' + i.toString() + '"></td>';
			t += '<td style="color:' + this.fontColor + '; font-family:' + this.fontFamily + '; font-size:' + this.fontSize + ';" align="left">';
			t += splited[2] + '</td></tr>';
		}
	}else{
		if (this.randAns){
			var randomed = this.setRandomArray(arrOpt);
			var a ='';
			for (var i = 0; i < arrLength; i++) 
			{
				this.statusList[i] = 0;
				var splited = randomed[i].split("#!#");
				this.idList[i] = splited[0];
				t += '<tr><td valign="middle" align="center" style="width: '+this.button_w+' px;" >';
				t += '<div id='+this.id+'_div_button_'+i+' style="position: relative; visibility: hidden; cursor: hand"';
				t += ' onMouseOver="pc.'+this.id+'.mouseOver('+i+');" onMouseOut="pc.'+this.id+'.mouseOut('+i+');">';
				t += '<img id='+this.id+'_button_'+i+' src="' + this.resourcesURL + this.button_srcOut +'" onClick="pc.'+this.id+'.showFeedback(\''+splited[3]+'\');"></td>';
				t += '</div>'
				t += '<td style="width: '+this.imageWidth+' px;">';
				t += '<img src="' + this.resourcesURL  + this.imgNotChecked + '" id=' + this.id + '_img_' + i.toString() + ' onclick="pc.'+this.id+'.setImage(\''+this.id+'_img_'+i+'\','+i+',\''+splited[3]+'\');">';
				//t += '</td><td style="color:' + this.fontColor + '; font-family:' + this.fontFamily + '; font-size:' + this.fontSize + ';" align="left" onclick="pc.'+this.id+'.setImage(\''+this.id+'_img_'+i+'\','+i+',\''+splited[3]+'\');">';
				t += '</td><td style="color:' + this.fontColor + '; font-family:' + this.fontFamily + '; font-size:' + this.fontSize + ';" align="left">';
				t += splited[2] + '</td></tr>';
			}
		}else{
			for (var i = 0; i < arrLength; i++) 
			{
				this.statusList[i] = 0;
				var splited = arrOpt[i].split("#!#");
				this.idList[i] = splited[0].replace(' ', '');			
				t += '<tr><td valign="middle" align="center" style="width: '+this.button_w+' px;" >';
				t += '<div id='+this.id+'_div_button_'+i+' style="position: relative; visibility: hidden; cursor: hand"';
				t += ' onMouseOver="pc.'+this.id+'.mouseOver('+i+');" onMouseOut="pc.'+this.id+'.mouseOut('+i+');">';
				t += '<img id='+this.id+'_button_'+i+' src="' + this.resourcesURL + this.button_srcOut +'" onClick="pc.'+this.id+'.showFeedback(\''+splited[3]+'\');"></td>';
				t += '</div>'
				t += '<td style="width: '+this.imageWidth+' px;">';
				t += '<img src="' + this.resourcesURL  + this.imgNotChecked + '" id=' + this.id + '_img_' + i.toString() + ' onclick="pc.'+this.id+'.setImage(\''+this.id+'_img_'+i+'\','+i+',\''+splited[3]+'\');">';
				//t += '</td><td style="color:' + this.fontColor + '; font-family:' + this.fontFamily + '; font-size:' + this.fontSize + ';" align="left" onclick="pc.'+this.id+'.setImage(\''+this.id+'_img_'+i+'\','+i+',\''+splited[3]+'\');">';
				t += '</td><td style="color:' + this.fontColor + '; font-family:' + this.fontFamily + '; font-size:' + this.fontSize + ';" align="left">';
				t += splited[2] + '</td></tr>';
			}
		}
	}
	t += '</table>';
	this.setHTML(t);
	
	this.onRepaint();
};

componentCheckboxWithFeedbackMltp.prototype.onRepaint = function(obj) 
{
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator)
	{
		var changed = unquotedString(this.text);
		var arrOpt 	= changed.split("||");
		var splitedCorrAnswer = this.correctAnswersList.split('');
		for (var i = 0; i < arrOpt.length; i++) 
		{
			var img = document.getElementById(this.id + '_img_' + i);
			if (splitedCorrAnswer[i] != this.statusList[i]){
				if (this.statusList[i] == 1){
					img.src = this.resourcesURL + this.imgCheckedIncorrectSrc;
				}else{
					img.src = this.resourcesURL + this.imgNotCheckedIncorrectSrc;
				};
			}else{
				if (this.statusList[i] == 1){
					img.src = this.resourcesURL + this.imgCheckedArrow;
				}else{
					img.src = this.resourcesURL + this.imgNotCheckedArrow;
				}
			};
			var index = this.idList[i];
			var index = index.replace(/ /g, '');
			var splited = arrOpt[index].split("#!#");
			var name = splited[3].replace(/ /g, '');
			if (page.all[name]){
				this.showButton(i);
			}			
		};
	};
	return true;
};

componentCheckboxWithFeedbackMltp.prototype.showButton = function(index) 
{
	var obj = document.getElementById(this.id+'_div_button_'+index);
	obj.style.visibility = 'inherit';
};

componentCheckboxWithFeedbackMltp.prototype.mouseOver = function(index)
{
	var obj = document.getElementById(this.id+'_button_'+index);
	obj.src = this.resourcesURL + this.button_srcIn;
};

componentCheckboxWithFeedbackMltp.prototype.mouseOut = function(index)
{
	var obj = document.getElementById(this.id+'_button_'+index);
	obj.src = this.resourcesURL + this.button_srcOut;
};

componentCheckboxWithFeedbackMltp.prototype.setImage = function(id , i, popup) 
{
	if (this.locked) return;	
	var obj = document.getElementById(id);
	
	if (obj)
		if (this.statusList[i] == 1){
			this.statusList[i] = 0;
			obj.src = this.resourcesURL + this.imgNotChecked;
		}else {
			this.statusList[i] = 1;
			obj.src = this.resourcesURL + this.imgChecked;
		};
		if (this.showFeedbackAfterClick) {
			//var obj = document.getElementById(this.id+'_div_button_'+i);
			if (page.all[popup]) page.all[popup].show();
		};	
};

componentCheckboxWithFeedbackMltp.prototype.setRandomArray = function(questArray)
{
	this.correctAnswer = '';
	var corrArray = this.correctAnswersList.split('');
	for (var i=0; i < questArray.length; i++){
		j = parseInt(Math.random()*(questArray.length));
		var temp = questArray[j];
		var temp2 = corrArray[j];
		questArray.splice(j,1);
		corrArray.splice(j,1);
		j = parseInt(Math.random()*(questArray.length));
		questArray.splice(j,0,temp);
		corrArray.splice(j,0,temp2);		
	};
	this.correctAnswersList = '';
	var sep = '';
	for (var i=0; i < corrArray.length; i++){
		var idArr = questArray[i].split('#!#');
		this.correctAnswersList += corrArray[i];
		if (this.correctAnswer != '') sep = '||';
		this.correctAnswer += sep + idArr[0]+';'+corrArray[i];
	};
	return questArray;
};

componentCheckboxWithFeedbackMltp.prototype.showFeedback = function(name)
{
	var name = name.replace(/ /g, '');
	if (page.all[name]){
		page.all[name].show();
	};
	return;
};

componentCheckboxWithFeedbackMltp.prototype.preload = function()
{
	this.preloading = true;
	this.preloadImg.src = this.resourcesURL + this.imgChecked;
	this.preloadImg2.src = this.resourcesURL + this.imgNotChecked;
};

componentCheckboxWithFeedbackMltp.prototype.isReady = function()
{
	if (this.imgChecked == "" || this.imgChecked == "empty" || this.imgNotChecked == "" || this.imgNotChecked == "empty"
	    || PageInterface.GetValue("state") == psDesigning || PageInterface.GetValue("state") == psPreview) {
		return true;
	} else {
		if (!this.preloaded) {
			if (!this.preloading) {
				this.preload();
			}
			
			this.preloaded = this.preloadImg.complete && this.preloadImg2.complete;
			if (!this.preloaded) this.waitingTryCount++;
			if (this.waitingTryCount > 50) {
				this.preloaded = true;
				this.waitingTryCount = 0;
				return true;
			}
			return this.preloaded;
		} else return true;
	}
};

componentCheckboxWithFeedbackMltp.prototype.checkIsChanged = function() {
	return(this.isChanged);
};

componentCheckboxWithFeedbackMltp.prototype.interactionsCorrectResponsesCount = function()
{
	var answerCount = 1;
	return answerCount;
};

componentCheckboxWithFeedbackMltp.prototype.interactionsCorrectResponses = function()
{
	return this.correctAnswersList;
};

componentCheckboxWithFeedbackMltp.prototype.interactionsCorrectResponsesAsText = function()
{
	return this.correctAnswersList;
};

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

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