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

componentSolveText = function(name) {
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.passed = false;
	this.basketsCount = 0;
	this.componentX = 0;
	this.componentY = 0;	
	
	this.interactions = true;
	this.interactionsType = "matching";			
};

componentSolveText.prototype = new __componentText;
componentSolveText.prototype.type = "componentSolveText";
componentSolveText.prototype.onBaseCreate = function(obj) {
	this.captureMouseEvents();
};

componentSolveText.prototype.componentSolveTextBaseOnCreate = componentSolveText.prototype.onCreate;
componentSolveText.prototype.onCreate = function(obj) {
	this.componentSolveTextBaseOnCreate();
	if (PageInterface.GetValue("state") != psDesigning) {
		var sizeDiv = '<span id="sizeDiv" style="visibility: hidden; position: absolute; font-family:' + this.font.family
		+ '; font-size:' + getFontSize(this.font.size, "px") + 'px; color:' + this.font.color + ';"></span>';
		
		var ob = document.getElementById(this.id);
		ob.innerHTML += sizeDiv;

		var width = 0;
		var height = 0;
		var tempX = this.x+this.borderWidth+1;
		var tempY = this.y + this.h - 1;
		var tempWidth = 0;
		var onlyText;
		
		this.texts = new Array();

		var match = this.text.match(/\{[^\{]+\}/gi);
		
		this.basketsCount = match.length;
		this.dynamicWidth = false;
		this.maxWidth = 0;
		if (!this.dynamicWidth) 
		{
			for (var i=0; i<this.basketsCount; i++) {
				// Czysty tekst bez nawiasow
				onlyText = match[i].substr(1,match[i].length-2);
				
				// tymczasowy element do ustalania szerokosci i wysokosci ddelementow
				if (document.getElementById("sizeDiv")) {
					document.getElementById("sizeDiv").innerHTML = onlyText;
					width = document.getElementById("sizeDiv").offsetWidth;
				}
				if ( width > this.maxWidth ) this.maxWidth = width;
			}
			width = this.maxWidth + this.elementPadding;
		}

		for (var i=0; i<this.basketsCount; i++) {
			// Czysty tekst bez nawiasow
			var ddeText = match[i].substr(1,match[i].length-2);			
			
			// tymczasowy element do ustalania szerokosci i wysokosci ddelementow
			if (document.getElementById("sizeDiv")) {
				document.getElementById("sizeDiv").innerHTML = ddeText;
				height = document.getElementById("sizeDiv").offsetHeight;// + this.elementPadding;
				if (this.dynamicWidth) width = document.getElementById("sizeDiv").offsetWidth;
			};
			
			onlyText = ddeText.replace(/'/ig, '');
			onlyText = onlyText.replace(/"/ig, '');
			onlyText = onlyText.replace(/ /ig, '');					
			
			this.text = this.text.replace(match[i], "<textarea name='"+this.id+"_"+onlyText+"' id='_"+this.id+"_"+i+"_basket' style='background: white; border: 1px solid black; width: " + eval(width+this.elementPadding) + "px; height: " + eval(height+2)+ "px; padding-right: "+eval(width-1)+"px; overflow: hidden;' disabled></textarea>"); //pg2-padding-right - added "-1", 'cause in Firefox (>2.0) displaying wasn't ok
			
			// Uustalenie pozycji x,y elementu (pod komponentem glownym)
			if (i !=0) {
				if ( (tempX + tempWidth + width + 2) >  (this.x + this.w) ) {
					tempX = this.x + this.borderWidth + 1;
					tempY -= height + 8 + (this.elementPadding-3);
				}else tempX += tempWidth + 8 + (this.elementPadding-3);
			}else { 
				tempY -= height + 2 + this.borderWidth + (this.elementPadding-3);
				tempX += tempWidth;
			};
			
			// powoladnie ddelementow
			var textTemp = new __componentText;
			textTemp.parentText = this; 
			textTemp.id = this.id+'_DDE_'+i; 
			textTemp.text = match[i].substr(1,match[i].length-2);
			textTemp.clearText = onlyText;
			textTemp.x = tempX;
			textTemp.y = tempY;
			textTemp.x_backup = tempX;
			textTemp.y_backup = tempY;
			textTemp.startX = tempX;
			textTemp.startY = tempY;

			if (!this.dynamicWidth) {
				textTemp.w = width+this.elementPadding;
				textTemp.h = height+2;//+this.elementPadding;
			};
			
			textTemp.canDragging = true;
			textTemp.borderWidth = 1;
			textTemp.borderColor = this.elementBorderColor;
			textTemp.padding = 0;
			textTemp.bgColor = this.elementBckColor;
			textTemp.step = -1;
			textTemp.align = "center";
			textTemp.valign = 'middle';
			textTemp.font = this.font;
			
			textTemp.onCreate = this.onCreateText;
			textTemp.beginDrag = this.beginDragText;
			textTemp.endDrag = this.endDragText;
			textTemp.onMove = this.mouseMoveText;
			
			textTemp.basketsCount = this.basketsCount; 
			textTemp.destinationBasketId = "_"+this.id+"_"+i+"_basket";
			textTemp.destinationBasketName = onlyText;
			textTemp.clearText = onlyText;
			textTemp.basketName = this.id+"_"+onlyText;
			textTemp.solved = false;
			textTemp.orgZ = textTemp.z = this.z+i;
			textTemp.insideBasketId = "";
			textTemp.insideBasketName = '';
			textTemp.cursor = this.ddelementCursor;
			
			// Szerokosc poprzedniego ddelementu
			tempWidth = width;
			
			// dodanie ddelementu na strone
			page.addChild(textTemp);
			
			// dodanie do tablicy ddelementow
			this.texts.push( textTemp );
		}
		this.randomDDElements();
	}
	var content = this.findContentObj();
	content.innerHTML = this.text;
	
	this.componentX = this.x;
	this.componentY = this.y;
};

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

componentSolveText.prototype.onLoad = function() {
}

componentSolveText.prototype.componentSolveTextBaseOnPaint = componentSolveText.prototype.onPaint;
componentSolveText.prototype.onPaint = function() {
	this.componentSolveTextBaseOnPaint();
	
	var newX = this.componentX - this.x;
	var newY = this.componentY - this.y; 
	// var newX = 0;
	// var newY = 0;
	
	
	this.componentX = this.x;
	this.componentY = this.y;
	
	for (var i=0; i<this.basketsCount; i++) {
		this.texts[i].x = this.texts[i].x_backup;
		this.texts[i].y = this.texts[i].y_backup;
		this.texts[i].startX = this.texts[i].x_backup;
		this.texts[i].startY = this.texts[i].y_backup;
		
		this.texts[i].insideBasketId = '';
		this.texts[i].solved = false;
		
		this.texts[i].startX -= newX;
		this.texts[i].startY -= newY;
		
		this.texts[i].x -= newX;
		this.texts[i].y -= newY;
		
		this.texts[i].setX(this.texts[i].x);
		this.texts[i].setY(this.texts[i].y);	
		
		this.texts[i].obj.style.border = this.elementBorderColor + " solid 1px";
		this.texts[i].obj.style.background = this.elementBckColor;		
	};
}

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

componentSolveText.prototype.findPosX = function (obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

componentSolveText.prototype.findPosY = function(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

componentSolveText.prototype.mouseMoveText = function(e) {
};

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

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

		this.texts[i].startX = this.texts[i].x;
		this.texts[i].startY = this.texts[i].y;
		this.texts[i].startY = this.texts[i].x_backup;
		this.texts[i].startY = this.texts[i].y_backup;
	};
};

componentSolveText.prototype.beginDragText = function() {
	/**
	var obj = document.getElementById(this.destinationBasketId);
	obj.style.borderColor = "red";
	*/
	this.setZIndex(1000);
};

componentSolveText.prototype.endDragText = function(e) 
{
	this.setZIndex(this.orgZ);

	var ddbasket = document.getElementById(this.destinationBasketId);
	var ddelement = document.getElementById(this.id);
	/** ddbasket.style.borderColor = "black"; */
	
	var middlePoint = { 
		x: (ddelement.offsetLeft + ddelement.offsetWidth/2), 
		y: (ddelement.offsetTop + ddelement.offsetHeight/2)
	};
	
	for (var i=0; i < this.basketsCount; i++){
		var ddbasket = document.getElementById("_"+this.parentText.id+"_"+i+"_basket");
		
		var left = this.parentText.findPosX(ddbasket);
		var right = this.parentText.findPosX(ddbasket) + ddbasket.offsetWidth;
		var top = this.parentText.findPosY(ddbasket);
		var bottom = this.parentText.findPosY(ddbasket) + ddbasket.offsetHeight;
		
		if ( middlePoint.x > left && middlePoint.x < right && middlePoint.y > top && middlePoint.y < bottom ) {
			this.inArea = ddbasket;
			break;
		}else this.inArea = false; 
	};
	
	if (this.inArea && !this.parentText.checkIsFilled(this, this.inArea.id)){
		this.parentText.setIntoBasket(this, ddelement, this.inArea)
	}else {
		this.parentText.moveToStart(this, ddelement);
	};
};

componentSolveText.prototype.moveToStart = function(obj, elem) 
{
	obj.solved = false;
	obj.x = obj.startX;
	obj.y = obj.startY;
	obj.insideBasketId = '';
	elem.style.left = obj.startX;
	elem.style.top = obj.startY;
};

componentSolveText.prototype.setIntoBasket = function(sender, dde, obj) 
{
	var x = sender.parentText.findPosX(obj)+sender.parentText.borderWidth;
	var y = sender.parentText.findPosY(obj)+sender.parentText.borderWidth;

	dde.style.left = x;
	dde.style.top = y;
	
	sender.x = x;
	sender.y = y;
	sender.insideBasketId = obj.id;
	sender.insideBasketName = obj.name.replace(this.id+"_", "");
	//sender.solved = (obj.name == sender.text);
	//sender.solved = (obj.id == sender.destinationBasketId);
	sender.solved = (obj.name == sender.basketName);
};

componentSolveText.prototype.checkIsFilled = function(obj, destBasket) 
{
	for (var i=0; i < this.basketsCount; i++) {
		if (this.texts[i].id != obj.id && this.texts[i].insideBasketId == destBasket){
			if (this.replaceElements){
				this.moveToStart(this.texts[i], this.texts[i]);	
				return false;
			}else{
				this.moveToStart(obj, obj);	
				return true;
			}
		}
	}	
	return false;
};

componentSolveText.prototype.onRepaint = function() {
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator)
	{
		for (var i=0; i < this.basketsCount; i++) {
			if (!this.texts[i].solved) {
				if (this.showCorrectAnswerIndicator == true) {
					this.texts[i].obj.style.border = this.elementInCorrectBorderColor + " solid 1px";
					this.texts[i].obj.style.background = this.elementInCorrectBckColor;
				}
			}
			else if (this.showCorrectAnswerIndicator == true) { 
				this.texts[i].obj.style.border = this.elementCorrectBorderColor + " solid 1px";
				this.texts[i].obj.style.background = this.elementCorrectBckColor;
			}
		}
	}
}

componentSolveText.prototype.getAnswer = function(obj)
{
	var result = '';
	var sep = '';
	for (var i=0; i < this.basketsCount; i++) {
		if (result != '') sep = '#!#';
		result += sep + this.texts[i].id +'||'+this.texts[i].insideBasketId 
	}
	return(result);
};

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

	if (valInt != '' && !showCorrect){
		var splited = valInt.split('#!#');
		for (var i=0; i < splited.length; i++){
			var elem = splited[i].split('||');
			var ddelement = document.getElementById(elem[0]);
			var ddbasket = document.getElementById(elem[1]);
			var j=-1;
			do{
				j++;
			}while (this.texts[j].id != elem[0] && j < this.basketsCount)
			
			if (ddbasket) this.setIntoBasket(this.texts[j], ddelement, ddbasket);		
		};
	}else{
		for (var i=0; i < this.basketsCount; i++) {
			var ddelement = document.getElementById(this.texts[i].id);
			var ddbasket = document.getElementById(this.texts[i].destinationBasketId);
			
			this.setIntoBasket(this.texts[i], ddelement, ddbasket);
		};
	};
};

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

componentSolveText.prototype.parseCheckAnswer = function(index)
{
	var result = true;
	for (var i=0; i < this.basketsCount; i++) {
		if (!this.texts[i].solved) {
			if (this.returnWhenIncorrect && page.countAnswers != 0){
				this.moveToStart(this.texts[i], this.texts[i]);	
			};
			result = false;
		}
	}
	return result;
};

componentSolveText.prototype.onLock = function() {
	for (var i=0; i < this.basketsCount; i++) {
		this.texts[i].canDragging = false;
	}
};

componentSolveText.prototype.onUnLock = function() {
	for (var i=0; i < this.basketsCount; i++) {
		this.texts[i].canDragging = true;
	}
};

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

componentSolveText.prototype.interactionsCorrectResponses = function(index)
{
	var result = '';
	var sep = '';
	for (var i=0; i < this.basketsCount; i++) {
		if (result != '') sep = '#!#';
		result += sep + this.texts[i].id +'||'+this.texts[i].destinationBasketId; 
	}
	return(result);
};

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

componentSolveText.prototype.interactionsCorrectResponsesAsText = function(index)
{
	var result = '';
	var sep = '';
	for (var i=0; i < this.basketsCount; i++) {
		if (result != '') sep = '#!#';
		result += sep + this.texts[i].text +'||'+this.texts[i].destinationBasketName; 
	}
	return(result);
};

componentSolveText.prototype.interactionsStudentResponseAsText = function()
{
	var result = '';
	var sep = '';
	for (var i=0; i < this.basketsCount; i++) {
		if (result != '') sep = '#!#';
		result += sep + this.texts[i].text +'||'+this.texts[i].insideBasketName; 
	}
	return(result);
};

