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

componentMatchSentence = function(name) 
{
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.inArea = null;	
	this.baskets = new Array();	
	this.texts = new Array();		
	this.groupsID = new Array();		
	this.dropAudioComp = null;
	
	this.interactions = true;
	this.interactionsType = "matching";		
};

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

componentMatchSentence.prototype.onBaseCreate = function(obj) {
	this.captureMouseEvents();
};

componentMatchSentence.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 (this.enableFlashPlayer) {
		var e = new componentFlashAudioPlayer;
		e.myParent = this;
		e.id = this.id + 'dropAudioComp';
		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.dropAudioComp = page.addChild(e);
	} else {	
		var audioElement = new componentAudio;
		audioElement.myParent = this;
		audioElement.id = this.id+'dropAudioComp'; 
		audioElement.src = '';
		audioElement.pvWMP64 = false;
		audioElement.pvWMP7up = false;
		audioElement.step = -1;
		audioElement.editorPaint = this.editorPaint;
		this.dropAudioComp = page.addChild(audioElement);		
		audioElement.allowNextStep = true;
	}
	var contentArr = this.content.split('||');
	
	var tempX = this.x + 3;
	var tempY = this.y + 3;	
	
	var maxWidth = parseInt((this.w-this.verticalSpaceBetweenElements)/2 )-3;
	var maxHeight = parseInt((this.h-10 - this.horizotalSpaceBetweenElements*(contentArr.length-1))/contentArr.length);
	
	var index = 0;
	var text = '';

	if (this.basketBorderColor == 'transparent') {
		var border = '';
	}else{
		var border = 'border: 1px solid '+this.basketBorderColor+';';
	};	
	
	var text = '<table cellspacing="0" cellpadding="0" height="'+eval(this.h-2*this.padding)+'px" width="'+eval(this.w-2*this.padding)+'px">';
	for(var i=0; i < contentArr.length; i++){
		text += '<tr>';
		for(var j=0; j < 2; j++){
			var groupArr = contentArr[j].split('#!#');
			if (j != 0){
				text += '<td style="width:'+this.verticalSpaceBetweenElements+'px;"></td>';
			};
			text += '<td id="'+this.id+'_DDB_'+index+'" style="'+border+' background: '+this.basketBckColor+'; width:'+eval(maxWidth-4)+'px; height:'+maxHeight+'px; font-size:0px;">.</td>';
			index++;
		};		
		text += '</tr>';
		text += '<tr><td colspan="3" height="'+this.horizotalSpaceBetweenElements+'px" ></td></tr>';
	};
	text += '</table>';	
	this.text = text;		
	
	tempX = this.x + 3;
	tempY = this.y + 3;	
	index = 0;

	for(var i=0; i < contentArr.length; i++){
		var groupWord = contentArr[i].split('#!#');
		for(var j=0; j < 2; j++){
			var ddelementText = new __componentText;
			ddelementText.myParent = this; 
			ddelementText.id = this.id+'_DDE_'+index; 
			ddelementText.x = tempX;
			ddelementText.y = tempY;
			ddelementText.x_backup = ddelementText.x;
			ddelementText.y_backup = ddelementText.y;			
			ddelementText.startX = ddelementText.x;
			ddelementText.startY = ddelementText.y;
			ddelementText.w = maxWidth;
			ddelementText.h = maxHeight;
			ddelementText.insideBasketId = "";
			ddelementText.text = groupWord[j];
			ddelementText.align = "center";
			ddelementText.valign = 'middle';		
			ddelementText.font.family = this.fontFamily;
			ddelementText.font.size = this.fontSize;		
			ddelementText.font.color = this.fontColor;		
			ddelementText.borderWidth = 1;
			ddelementText.borderColor = this.elementBorderColor;
			ddelementText.padding = 0;
			ddelementText.bgColor = this.elementBckColor;		
			ddelementText.cursor = this.elementCursor;		
			ddelementText.canDragging = true;
			ddelementText.lastBasket = false;
			ddelementText.pair = 'pair_'+i;
			ddelementText.basketRow = '';
			ddelementText.solved = false;
			ddelementText.orgZ = ddelementText.z = this.z+1+i;
			ddelementText.dropAudio = groupWord[2+j];
			
			ddelementText.onCreate = this.onCreateText;
			ddelementText.beginDrag = this.beginDragText;
			ddelementText.endDrag = this.endDragText;
			ddelementText.onMove = this.mouseMoveText;		
			ddelementText.baseOnPaint = ddelementText.onPaint;
			ddelementText.onPaint = this._onPaint;									
			
			page.addChild(ddelementText);		
			this.texts.push( ddelementText );
			
			tempX += maxWidth;	
			
			if (j == 1) {
				tempX = this.x + 3;
				tempY = (this.y + 3) + (i+1)*maxHeight + (i+1)*this.horizotalSpaceBetweenElements;
			};
			index++;
		};
	};	
};

componentMatchSentence.prototype.editorPaint = function() 
{
	return "";
}

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

componentMatchSentence.prototype.componentMatchSentenceBaseResetElement = componentMatchSentence.prototype.resetElement;
componentMatchSentence.prototype.resetElement = function()
{
	this.componentMatchSentenceBaseResetElement();
	for (var i=0; i < this.texts.length; i++) {
		this.texts[i].setBorder(this.elementBorderColor, 1);
	};
}

componentMatchSentence.prototype._onPaint = function() 
{
	if (this.locked) {
		if (this.solved) {
			this.setBgColor(this.myParent.elementCorrectBckColor);
		}else { 
			this.setBgColor(this.myParent.elementInCorrectBckColor);
		}
		return;
	}
	this.baseOnPaint();
}

componentMatchSentence.prototype.componentMatchSentenceBaseOnPaint = componentMatchSentence.prototype.onPaint;
componentMatchSentence.prototype.onPaint = function() 
{
	if (this.locked) return;

	this.componentMatchSentenceBaseOnPaint();
	if (PageInterface.GetValue("state") != psDesigning) {
		this.randomDDElements();	
	}
	this.setElementIntoBasketsAtRandom();
};

componentMatchSentence.prototype.onRepaint = function() 
{
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator)
	{
		for (var i=0; i < this.texts.length; i++) {
			if (!this.texts[i].solved) {
				this.texts[i].setBorder(this.elementInCorrectBorderColor, 1);
				this.texts[i].setBgColor(this.elementInCorrectBckColor);				
			}else { 
				this.texts[i].setBorder(this.elementCorrectBorderColor, 1);
				this.texts[i].setBgColor(this.elementCorrectBckColor);				
			}
		}
	}
};

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

componentMatchSentence.prototype.randomDDElements = 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,
				startX: this.texts[i].startX,
				startY: this.texts[i].startY		
			}
		);
	};
	
	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;
		this.texts[i].startX = sizes[ randomIndexes[i] ].startX;
		this.texts[i].startY = sizes[ randomIndexes[i] ].startY;
	};
};

componentMatchSentence.prototype.onShow = function() 
{
	for (var i=0; i < this.baskets.length; i++) {
		this.baskets[i].show();
	};
	for (var i=0; i < this.texts.length; i++) {
		this.texts[i].show();
	};	
};

componentMatchSentence.prototype.setElementIntoBasketsAtRandom = function() 
{
	var contentArr = this.content.split('||');
	var found = false;
	
	for (var i=0; i < this.texts.length; i++) {
		found = false;
		this.texts[i].show();
		var ddelement = document.getElementById(this.texts[i].id);
		var middlePoint = { 
			x: (ddelement.offsetLeft + ddelement.offsetWidth/2), 
			y: (ddelement.offsetTop + ddelement.offsetHeight/2)
		};		
		var index = 0;
		for(var l=0; l < contentArr.length; l++){
			for(var k=0; k < 2; k++){
				var ddbasket = document.getElementById(this.id+'_DDB_'+index);
				var left = this.findPosX(ddbasket);
				var right = left + ddbasket.offsetWidth;
				var top = this.findPosY(ddbasket);
				var bottom = top + ddbasket.offsetHeight;		
				
				if ( middlePoint.x > left && middlePoint.x < right && middlePoint.y > top && middlePoint.y < bottom ) {
					this.setIntoBasket(this.texts[i], ddelement, ddbasket)
					found = true;
					break;
				};
				index++;
			};
			if (found) break;
		};
	};
};

componentMatchSentence.prototype.getDropAudioIndex = function(audioFile)
{
	var result = -1;
	var contentArr = this.content.split('||');
	for(var i=0; i < contentArr.length; i++){
		var oneArr = contentArr[i].split('#!#');
		if (oneArr[3] == audioFile) {
			result = i*2+1;
			break;
		}else if (oneArr[2] == audioFile) {
			result = i*2;
			break;
		}
	}
	return result;
};

componentMatchSentence.prototype.playDropAudio = function(audioFile) 
{
	if (audioFile == '' || audioFile == 'undefined' || typeof(audioFile) == 'undefined')
	return;
	
	if (this.enableFlashPlayer) {
		this.dropAudioComp.flashVars = "playIdx="
		+ this.getDropAudioIndex(audioFile) + "&autoStart=true&playOnce=true";
		this.dropAudioComp.show();
	} else {
		this.dropAudioComp.setUrl(this.resourcesURL + audioFile);
		this.dropAudioComp.actionMediaPlay();
	};	
};

componentMatchSentence.prototype.mouseMoveText = function(e) 
{
	return;
};

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

componentMatchSentence.prototype.endDragText = function(e) 
{
	var ddbasket = document.getElementById(this.destinationBasketId);
	var ddelement = document.getElementById(this.id);
	/** ddbasket.style.borderColor = "black"; */
	
	this.myParent.playDropAudio(this.dropAudio);
	
	var middlePoint = { 
		x: (ddelement.offsetLeft + ddelement.offsetWidth/2), 
		y: (ddelement.offsetTop + ddelement.offsetHeight/2)
	};
	
	var contentArr = this.myParent.content.split('||');
	var found = false;
	var index = 0;
	
	for(var j=0; j < contentArr.length; j++){
		for(var i=0; i < 2; i++){			
			var ddbasket = document.getElementById(this.myParent.id+'_DDB_'+index);
			var left = this.myParent.findPosX(ddbasket);
			var right = left + ddbasket.offsetWidth;
			var top = this.myParent.findPosY(ddbasket);
			var bottom = top + ddbasket.offsetHeight;		
			
			if ( middlePoint.x > left && middlePoint.x < right && middlePoint.y > top && middlePoint.y < bottom ) {
				this.inArea = ddbasket;
				found = true;
				break;
			}else this.inArea = null; 
			
			index++;
		};
		if (found) break;
	};
	var lastbasket = this.lastBasket
	
	if (this.inArea && this.inArea.id != this.lastBasket){	
		var nameObjFiled = this.myParent.findElementInBasket(this, this.inArea.id);
		if(nameObjFiled){
			this.myParent.setIntoBasket(this, ddelement, this.inArea);
			
			var ddelement = document.getElementById(nameObjFiled.id);
			var ddbasket = document.getElementById(lastbasket);
			this.inArea = ddbasket;
			this.myParent.setIntoBasket(nameObjFiled, ddelement, this.inArea);
		};
	}else this.myParent.moveToStart(this, ddelement);
};

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

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

componentMatchSentence.prototype.moveToStart = function(sender, elem) 
{	
	sender.setZIndex(sender.orgZ);

	sender.solved = false;
	sender.x = sender.startX;
	sender.y = sender.startY;
	elem.style.left = sender.startX;
	elem.style.top = sender.startY;
};

componentMatchSentence.prototype.setIntoBasket = function(sender, dde, obj) 
{
	if (sender.myParent.showAnswerIndicatorAnyTime){
		sender.setBorder(this.elementBorderColor, 1);
		sender.setBgColor(this.elementBckColor);				
	};
	sender.setZIndex(sender.orgZ);
	
	var x = this.findPosX(obj);
	var y = this.findPosY(obj);

	dde.style.left = x;
	dde.style.top = y;
	
	sender.x = x;
	sender.startX = x;
	sender.y = y;
	sender.startY = y;
	sender.insideBasketId = obj.id;
	
	var idArr = obj.id.split(this.id+'_DDB_');
	sender.basketRow = 'row_'+parseInt(eval(idArr[1]/2));
	sender.lastBasket = obj.id;
};

componentMatchSentence.prototype.findElementInBasket = function(obj, destBasket) 
{
	for (var i=0; i < this.texts.length; i++) {
		if (this.texts[i].id != obj.id && this.texts[i].insideBasketId == destBasket){
			return this.texts[i];
		};
	};
	return null;
};

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

componentMatchSentence.prototype.setAnswer = function(valStr, showCorrect)
{
	if ((typeof(valStr) == 'undefined' || valStr == '') && !showCorrect) return;
	if (valStr == '' && (typeof(showCorrect) == 'undefined' || showCorrect.toString() == '')) showCorrect = true;
	
	if (!showCorrect){
		var splited = valStr.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.texts.length)
			
			if (ddbasket) this.setIntoBasket(this.texts[j], ddelement, ddbasket);	
		};
	}else{
		var tempArr = new Array();
		for (var i=0; i < this.texts.length; i++) {		
			var ddbasket = document.getElementById(this.id+'_DDB_'+i);
			var ddelement = document.getElementById(this.texts[i].id);
			
			if (ddbasket && ddelement)
				this.setIntoBasket(this.texts[i], ddelement, ddbasket);
		};
	};
	this.onRepaint();		
};

componentMatchSentence.prototype.checkAnswer = function() 
{
	if (this.showAnswerIndicatorAnyTime && this.showCorrectAnswerIndicator) this.colorMyElements();
	return (this.parseCheckAnswer(this));
};

componentMatchSentence.prototype.colorMyElements = function(index)
{
	var result = true;
	var beforeBasketId = '';

	var result = true;
	for (var i=0; i < this.texts.length; i++) {
		if ((i % 2) == 0){
			beforeBasketId = this.texts[i].basketRow;
		}else{
			if (beforeBasketId == this.texts[i].basketRow){
				this.texts[i].setBorder(this.elementCorrectBorderColor, 1);
				this.texts[i].setBgColor(this.elementCorrectBckColor);
				this.texts[eval(i-1)].setBorder(this.elementCorrectBorderColor, 1);
				this.texts[eval(i-1)].setBgColor(this.elementCorrectBckColor);
				
			}else{
				this.texts[i].setBorder(this.elementInCorrectBorderColor, 1);
				this.texts[i].setBgColor(this.elementInCorrectBckColor);
				this.texts[eval(i-1)].setBorder(this.elementInCorrectBorderColor, 1);
				this.texts[eval(i-1)].setBgColor(this.elementInCorrectBckColor);
			}
		};
	};
	return result;
};

componentMatchSentence.prototype.parseCheckAnswer = function(index)
{
	var result = true;
	var beforeBasketId = '';

	var result = true;
	for (var i=0; i < this.texts.length; i++) {
		if ((i % 2) == 0){
			beforeBasketId = this.texts[i].basketRow;
		}else{
			if (beforeBasketId != this.texts[i].basketRow){
				result = false;
				this.texts[i].solved = false;
				this.texts[eval(i-1)].solved = false;
			}else{
				this.texts[i].solved = true;
				this.texts[eval(i-1)].solved = true;
			};
		};
	};
	return result;
};

componentMatchSentence.prototype.onLock = function() {
	for (var i=0; i < this.texts.length; i++) {
		this.texts[i].canDragging = false;
		this.texts[i].locked = true;
	};
};

componentMatchSentence.prototype.onUnLock = function() {
	for (var i=0; i < this.texts.length; i++) {
		this.texts[i].canDragging = true;
		this.texts[i].locked = false;
	};
};

componentMatchSentence.prototype.interactionsCorrectResponsesCount = function()
{
	var contentArr = this.content.split('||');
	return contentArr.length;
};

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

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

componentMatchSentence.prototype.interactionsCorrectResponsesAsText = function(index)
{
	var result = '';
	var sep = '';
	for (var i=0; i < this.texts.length; i++) {
		if (result != '') sep = '#!#';
		result += sep + this.texts[i].id +'||'+this.id+'_DDB_'+i;
	}
	return(result);
};

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










