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

componentTesterExercise = function(name)
{
	this.componentElement = componentElement;
	this.componentElement(name);
	
	this.text = this.addChild(new __componentText(null));
	this.button = this.addChild(new componentButton(null));
	this.popupTitle = this.addChild(new __componentText(null));
	this.input = this.addChild(new componentInput(null));
	this.input.borderColor = '#000000';
	this.input.fontFamily = 'Arial';
	this.input.fontSize = '2';
	this.input.fontColor = '#000000';
	this.input.textAlign = 'left';	
	this.input.maxLength = '';
	this.input.fieldType = 'text';
	
	this.buttonVisible = true;
	this.randomedQuestions = new Array();
	this.currentCorrectAnswer = "";
	this.correctRandomCount = 0;
	this.randomCount = 0;
	this.wasClicked = false;
};

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

componentTesterExercise.prototype.onCreate = function(obj)
{
	if (!this.execution) {
		this.text.p_checkMarkers = this.p_checkMarkers;
		this.text.p_checkPNG = this.p_checkPNG;
		this.text.pngFix = this.pngFix;
		
		this.button.p_checkMarkers = this.p_checkMarkers;
		this.button.p_checkPNG = this.p_checkPNG;
		this.button.pngFix = this.pngFix;
		
		this.popupTitle.p_checkMarkers = this.p_checkMarkers;
		this.popupTitle.p_checkPNG = this.p_checkPNG;
		this.popupTitle.pngFix = this.pngFix;
		
		this.input.p_checkMarkers = this.p_checkMarkers;
		this.input.p_checkPNG = this.p_checkPNG;
		this.input.pngFix = this.pngFix;
	}
	
	if (this.buttonVisible) {
		this.button.stopPropagation = false;
		this.button.onClick = function(e) {
			this.parent.btnClick();
		}
	}
	
	if (this.questionText.indexOf("%s") == -1) this.questionText += ': "%s"';
	var tmpArr = this.listValues.split("#|#");
	
	this.baseLangArr = tmpArr[0].split("||");
	this.trnsLangArr = tmpArr[1].split("||");
	
	if (this.randomCount > this.baseLangArr.length) this.randomCount = this.baseLangArr.length;
	
	if (this.baseLangArr.length > 0) this.randomQuestion();
	else this.text.text = this.questionText;
};

componentTesterExercise.prototype.onPaint = function(obj)
{
	this.popupTitle.setVisible(this.popupTitleVisible);
	this.button.setVisible(this.buttonVisible);
	
	this.style.cursor = this.cursor;
	this.text.cursor = this.cursor;
	this.button.cursor = "pointer";
	
	this.popupTitle.font.bold = true;
	this.popupTitle.font.family = this.textFontFamily;
	this.popupTitle.font.size = this.textFontSize;
	this.popupTitle.font.color = this.textFontColor;
	
	var bor = (is.ns4) ? 0 : this.borderWidth;
	var bor2 = bor * 2;
	var of = this.padding;
	var of2 = of * 2;
	var pth = (this.popupTitleVisible) ? this.popupTitle.h : 0;
	var buttonH = (this.buttonVisible) ? (this.button.h + of) : 0;
	
	this.text.x = this.text.y = of;
	this.text.w = this.w / 2;
	this.text.y = of + pth;
	this.text.h = this.h - bor2 - of2 - pth;
	this.popupTitle.x = 0;
	this.popupTitle.y = 0;
	this.popupTitle.w = this.w - bor2;
	
	this.input.x = this.text.x + this.text.w + 10;
	this.input.y = this.text.y;
	this.input.w = Math.round(this.w / 3) * 2; //pg2-round & *2
	this.input.h = 25;
	
	if (this.buttonVisible) {
		if (this.buttonAlign == "left") {
			this.button.x = of;
			this.button.y = this.h - bor2 - of - buttonH;
		} else if(this.buttonAlign == "center") {
			this.button.x = (this.w - bor2 - of2) / 2 - (this.button.w / 2);
			this.button.y = this.h - bor2 - of - buttonH;
		} else {
			this.button.x = this.w - bor2 - of - this.button.w;
			this.button.y = this.h - bor2 - of - this.button.h;
		}
	}
};

componentTesterExercise.prototype.componentTesterExerciseShow = componentTesterExercise.prototype.show;
componentTesterExercise.prototype.show = function() 
{
	this.componentTesterExerciseShow();
	this.text.show();
	this.input.show();
}

componentTesterExercise.prototype.btnClick = function() {
	this.wasClicked = true;
	this.checkIsCorrectAnswer();
	
	if (this.randomedQuestions.length >= this.randomCount) {
		this.button.setVisible(false);
		this.popupTitle.setVisible(false);
		this.input.setVisible(false);
		
		var txt = this.summary;
		if (this.checkAnswer()) txt = txt.replace("%s", this.popupCorrect);
		else txt = txt.replace("%s", this.popupIncorrect);
		txt = txt.replace("%d", this.getAnswerPercentage()+' %');
		txt = txt.replace('%d', this.percentageValue+' %');
		this.text.text = txt;
		this.text.orgText = txt; //pg2
		this.text.paint();
	} else this.randomQuestion();
};

componentTesterExercise.prototype.getAnswerPercentage = function()
{
	return(Math.floor((100 / this.randomCount) * this.correctRandomCount));
};

componentTesterExercise.prototype.checkAnswer = function()
{
	return(this.getAnswerPercentage() >= this.percentageValue);
};

componentTesterExercise.prototype.getAnswer = function()
{
	return(!this.ignoreAnswer) ? this.randomCount : 0;
};

componentTesterExercise.prototype.checkIsCorrectAnswer = function()
{
	this.popupTitle.visual = false;
	var userAns = this.input.getAnswer();
	var currCorrAns = this.currentCorrectAnswer;
	
	if (!this.caseSensitive){
		userAns = userAns.toLowerCase();
		currCorrAns = currCorrAns.toLowerCase();
	};	
	if (userAns == currCorrAns) {
		this.correctRandomCount++;
		this.popupTitle.text = this.popupCorrect;
	} else {
		this.popupTitle.text = this.popupIncorrect;
	};	
	if (this.showQuestionPopup) {
		this.popupTitle.visual = true;
		this.popupTitle.paint();
	}
	
	this.input.clearValue();
};

componentTesterExercise.prototype.inRandomedQuestions = function(v)
{
	for (var i=0; i<this.randomedQuestions.length; i++) {
		if (this.randomedQuestions[i] == v) return true;
	}
	return false;
};

componentTesterExercise.prototype.getRandomNo = function()
{
	return Math.round(Math.random() * (this.randomCount - 1));
};

componentTesterExercise.prototype.randomQuestion = function()
{
	var rand = this.getRandomNo();
	while (this.inRandomedQuestions(rand)) {
		rand = this.getRandomNo();
	}
	
	this.randomedQuestions[this.randomedQuestions.length] = rand;
	
	var txt = this.questionText.replace("%s", this.baseLangArr[rand]);
	txt = txt.replace('%i', '');
	
	this.text.text = txt;
	this.text.orgText = txt; //pg2
	this.text.paint();
	if (typeof(this.trnsLangArr) != 'undefined'){
		this.currentCorrectAnswer = this.trnsLangArr[rand];
		this.input.focus();
	}
};

componentTesterExercise.prototype.isReady = function()
{
	return(this.button.isReady());
}

