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

componentCheckboxWithFeedback = function(name) {
	//inherited
	this.componentElement = componentElement;
	this.componentElement(name);
	
	this.isChanged = false;
	
	this.text = this.addChild(new __componentText(null));
	this.imgc = this.addChild(new __componentImage(null));
	this.imgc.visible = false;
	this.imgc.onClick = this.checkClick;
	this.imgn = this.addChild(new __componentImage(null));
	this.imgn.visible = false;
	this.imgn.onClick = this.checkClick;
	
	this.buttonWHY = this.addChild(new componentRollover(null));
	this.buttonWHY.onClick = this.showFeedback;
	this.buttonWHY.stopPropagation = true;
	
	this.imgChecked = "";
	this.imgNotChecked = "";
	
	this.interactions = true;
	this.interactionsType = "choice";	
};

componentCheckboxWithFeedback.prototype = new componentElement();
componentCheckboxWithFeedback.prototype.type = "componentCheckboxWithFeedback";

componentCheckboxWithFeedback.prototype.onCreate = function(obj)
{
	this.captureMouseEvents();
	// dont forget to define onMouseDown if captureMouseEvents()
	this.text.captureMouseEvents();
	this.imgc.captureMouseEvents();
	this.imgn.captureMouseEvents();

	if (PageInterface.GetValue("state") == psDesigning || PageInterface.GetValue("state") == psPreview) this.checked = this.correctAnswer;
	this.addToGroup();
	
	//ustawienie grafik ze wzgledu na edytory
	this.imgChecked = this.imgc.src;
	this.imgNotChecked = this.imgn.src;
    if (this.imgCheckedCorrectSrc == "") this.imgCheckedCorrectSrc = this.imgc.src;
    if (this.imgCheckedIncorrectSrc == "") this.imgCheckedIncorrectSrc = this.imgc.src;
    if (this.imgNotCheckedCorrectSrc == "") this.imgNotCheckedCorrectSrc = this.imgn.src;
    if (this.imgNotCheckedIncorrectSrc == "") this.imgNotCheckedIncorrectSrc = this.imgn.src;
};

componentCheckboxWithFeedback.prototype.onPaint = function(obj)
{
 	if (PageInterface.GetValue("state") == psDesigning) this.buttonWHY.show();
		else this.buttonWHY.hide();
		
	this.imgc.visible = this.checked;
	this.imgn.visible = !this.checked;
	
	this.text.x = parseInt(this.imgc.w) + parseInt(this.textOffset) + parseInt(this.padding) + this.buttonWHY.w;
	this.text.y = this.paddingTopText;
	this.text.w = parseInt(this.w) - parseInt(this.imgc.w) - parseInt(this.textOffset) - (parseInt(this.padding) * 2);
	this.text.h = parseInt(this.h) - (parseInt(this.padding) * 2) - this.paddingTopText;
	
	if (this.checkVAlign == "bottom") {
		var xp = this.padding + this.buttonWHY.w;
		var yp = parseInt(this.h) - parseInt(this.imgc.h) - parseInt(this.padding) - 2 + this.paddingTopImage;
	} else if (this.checkVAlign == "middle") {
		var xp = this.padding + this.buttonWHY.w ;
		var yp = parseInt(this.h)/2 - parseInt(this.imgc.h)/2 + parseInt(this.padding) + this.paddingTopImage;
	} else {
		var xp = this.padding + this.buttonWHY.w;
		var yp = this.padding + this.paddingTopImage + 2;
	}
	this.imgc.x = this.imgn.x = xp;
	this.imgc.y = this.imgn.y = yp;
	
	this.onRepaint();
};

componentCheckboxWithFeedback.prototype.onRepaint = function(obj)
{
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator) {
		if (this.checkAnswer()) {
			this.imgc.src = this.imgCheckedCorrectSrc;
			this.imgn.src = this.imgNotCheckedCorrectSrc;
		} else {
			this.imgc.src = this.imgCheckedIncorrectSrc;
			this.imgn.src = this.imgNotCheckedIncorrectSrc;
		}
		if (page.all[this.anotherPopup]) this.buttonWHY.show();
	}
	else
	{
		this.imgc.src = this.imgChecked;
		this.imgn.src = this.imgNotChecked;
	}
	
	this.imgc.paint();
	this.imgn.paint();
}

componentCheckboxWithFeedback.prototype.checkClick = function(obj) {
	if (!this.parent.locked) {
		if (!this.parent.invokeEvent("Change")) {
			this.parent.isChanged = true;
			this.parent.checked = !this.parent.checked;
			this.parent.paint();
		}
		if (this.parent.checkAnswer()) {
			this.parent.invokeEvent("ChangeCorrectAnswer");
		} else {
			this.parent.invokeEvent("ChangeIncorrectAnswer");
		}
		//this.invokeEvent("Click");
	}
};

// examine if answer is correct
componentCheckboxWithFeedback.prototype.checkAnswer = function() 
{
	return(this.parseCheckAnswer(this));
};

componentCheckboxWithFeedback.prototype.checkIsAnswer = function() {
	return this.checked;
};

componentCheckboxWithFeedback.prototype.getAnswer = function() {
	return(!this.ignoreAnswer && this.checked) ? 1 : 0;
};

componentCheckboxWithFeedback.prototype.setAnswer = function(v, showCorrect) 
{
	if ((typeof(v) == 'undefined' || v == '') && !showCorrect) return;
	if (v == '' && (typeof(showCorrect) == 'undefined' || showCorrect == '')) showCorrect = true;

	this.checked = (v==1) ? true:false;
	this.paint();
	if (page.all[this.anotherPopup])this.buttonWHY.show();
};

componentCheckboxWithFeedback.prototype.isReady = function() {
	return(this.imgc.isReady() && this.imgn.isReady());
}

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

componentCheckboxWithFeedback.prototype.addToGroup = function() {
	var c = 1;
	for (var i = 0; i < page.children.length; i++) {
		if (page.children[i].type == "componentCheckboxWithFeedbackGroup") {
			c++;
			if (page.children[i].group == this.group) {
				return;
			}
		}
	}
	//var e = page.addChild(new componentCheckboxWithFeedbackGroup("checkboxgroup" + c));
	var e = page.addChild(new componentCheckboxWithFeedbackGroup(this.type+"Group"+c));
	e.globalIndex = this.globalIndex;
	e.group = this.group;
};

componentCheckboxWithFeedback.prototype.showFeedback = function()
{
	if (page.all[this.parent.anotherPopup])
	{
		page.all[this.parent.anotherPopup].show();
	};
	return;
}

///
/// componentCheckboxWithFeedbackGroup
///

componentCheckboxWithFeedbackGroup = function(name) {
	this.componentElement = componentElement;
	this.componentElement(name);
	this.group = "";
	this.isGroup = true;
	
	this.interactions = true;
	this.interactionsType = "choice";
}

componentCheckboxWithFeedbackGroup.prototype = new componentElement();
componentCheckboxWithFeedbackGroup.prototype.type = "componentCheckboxWithFeedbackGroup";

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

componentCheckboxWithFeedbackGroup.prototype.interactionsCorrectResponses = function(index)
{
	var res = "";
	var j = 0;
	for (var i = 0; i < page.children.length; i++) {
		var o = page.children[i];
		if (o.type == "componentCheckboxWithFeedback" && o.group == this.group) {
			res += (o.correctAnswer) ? "1":"0";
		}
	}
	return res;
}

componentCheckboxWithFeedbackGroup.prototype.interactionsCorrectResponsesAsText = function(index)
{
	var res = "";
	var j = 0;
	for (var i = 0; i < page.children.length; i++) {
		var o = page.children[i];
		if (o.type == "componentCheckboxWithFeedback" && o.group == this.group) {
			res += (o.correctAnswer) ? "1":"0";
		}
	}
	return res;
}

componentCheckboxWithFeedbackGroup.prototype.interactionsStudentResponse = function()
{
	var res = "";
	for (var i = 0; i < page.children.length; i++) {
		var o = page.children[i];
		if (o.type == "componentCheckboxWithFeedback" && o.group == this.group) {
			res += (o.checked) ? "1":"0";
		}
	}
	return res;
}

componentCheckboxWithFeedbackGroup.prototype.interactionsStudentResponseAsText = function()
{
	var res = "";
	for (var i = 0; i < page.children.length; i++) {
		var o = page.children[i];
		if (o.type == "componentCheckboxWithFeedback" && o.group == this.group) {
			res += (o.checked) ? "1":"0";
		}
	}
	return res;
}

componentCheckboxWithFeedbackGroup.prototype.getAnswer = function() {
	var groupAnswer="";
	for (var i = 0; i < page.children.length; i++) {
		if (page.children[i].type == "componentCheckboxWithFeedback") {
			if (page.children[i].group == this.group) {
				if(groupAnswer.length>0) groupAnswer +=':';
				groupAnswer += page.children[i].getAnswer();
			}
		}
	}
	return groupAnswer;
};

componentCheckboxWithFeedbackGroup.prototype.setAnswer = function(valStr, showCorrect)
{	
	var res = "";
	if(valStr==null || typeof(valStr)=="undefined") return;
	var elemAnswer = valStr.split(':');
	var cnt=0;
	for (var i = 0; i < page.children.length; i++) {
		var o = page.children[i];
		if (o.type == "componentCheckboxWithFeedback" && o.group == this.group) {			
			o.setAnswer(elemAnswer[cnt]);
			cnt++;
		}
	}
	return res;
}

componentCheckboxWithFeedbackGroup.prototype.parseCheckAnswer = function() {
	var groupParseCheckAnswer=true;
	
	for (var i = 0; i < page.children.length; i++) {
		if (page.children[i].type == "componentCheckboxWithFeedback") {
			if (page.children[i].group == this.group) {					
				if(!page.children[i].parseCheckAnswer(page.children[i])){
					groupParseCheckAnswer=false;
					break;
				}
			}
		}
	}
	return groupParseCheckAnswer;
};


/********************************************************************/
