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

componentMatchParagraph = function(name) 
{
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.inArea = null;
	
	this.elementHeight = 0;
	this.elementWidth = 0;	
	
	this.texts = new Array();
	this.buttons = new Array();
	
	this.compAudio = null;
	this.dropAudioComp = null;
	
	this.interactions = true;
	this.interactionsType = "matching";		
};

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

componentMatchParagraph.prototype.onBaseCreate = function(obj) 
{
	this.captureMouseEvents();
	
	if ( PageInterface.GetValue("state") != psDesigning && lms && this.disableOnlineOfflineMix != false ) 
	{
		this.resourcesStatus 	= lms.resourcesStatus();
		this.resourcesURL 		= lms.resourcesURL();
	} else this.resourcesURL = resourcesURL;	
};

componentMatchParagraph.prototype.onCreate = function(obj)
{
	var contentArr = this.content.split('||');
	
	var tempX = this.x + 3;
	var tempY = this.y + 5;	
	var imageHeigth = 0;
	var imageWidth = 0;	
	
	if (this.enableFlashPlayer) {
		var e = new componentFlashAudioPlayer;
		e.myParent = this;
		e.id = this.id + 'Audio';
		e.step = -1;
		e.src = this.flashAudioPlayerSrc;
		e.x = 0;
		e.y = 0;
		e.w = 1;
		e.h = 1;
		e.loop = false;
		e.flashVars = "";//"playIdx=0&autoStart=true";
		this.compAudio = page.addChild(e);
		
		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 = "";//"playIdx=0&autoStart=true";
		this.dropAudioComp = page.addChild(e);
	} else {
		var audioElement = new componentAudio;
		audioElement.myParent = this;
		audioElement.id = this.id+'Audio'; 
		audioElement.src = this.src;
		audioElement.pvWMP64 = false;
		audioElement.pvWMP7up = false;
		audioElement.step = -1;
		audioElement.editorPaint = this.editorPaint;
		this.compAudio = page.addChild(audioElement);
		audioElement.allowNextStep = true;
		
		var audioElement = new componentAudio;
		audioElement.myParent = this;
		audioElement.id = this.id+'dropAudioComp'; 
		audioElement.src = this.src;
		audioElement.pvWMP64 = false;
		audioElement.pvWMP7up = false;
		audioElement.step = -1;
		audioElement.editorPaint = this.editorPaint;
		this.dropAudioComp = page.addChild(audioElement);
		audioElement.allowNextStep = true;
	}
	
	if (this.playAudio == 'click') {
		var audioButtonsArr = new Array(this.audioPlay, this.audioPause, this.audioStop, '_play_', '_pause_', '_stop_', 'Play', 'Pause', 'Stop');		
		for (var i=0; i < 3; i++){
			var image = new componentImage;
			image.myParent = this; 
			image.id = this.id+audioButtonsArr[i+3]+i; 
			image.x = tempX;
			image.y = tempY;
			var imageTest = new Image();
			imageTest.src = this.resourcesURL + audioButtonsArr[i];
			image.w = imageTest.width;
			image.h = imageTest.height;
			image.src = audioButtonsArr[i];
			image.cursor = 'hand';
			
			image.captureMouseEvents = this.captureMouseEvents;
			var funct = 'this.onMouseClickImage'+audioButtonsArr[i+6];
			image.eventClick = eval(funct);
			
			page.addChild(image);
			this.buttons.push(image);
			
			
			if (image.h > imageHeigth) imageHeigth = image.h;
			tempX +=  image.w + 5;
		};
		imageHeigth += 10;
	};	
	
	if (this.basketBorderColor == 'transparent') {
		var border = '';
	}else{
		var border = 'border: 1px solid '+this.basketBorderColor+';';
	};	
	
	var maxWidth = this.w-6;
	var maxHeight = parseInt((this.h-imageHeigth-5-(this.spaceBetweenElements*(contentArr.length-1)))/contentArr.length);
	
	this.elementHeight = maxHeight;
	this.elementWidth = maxWidth;		
	
	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>';
		text += '<td id="'+this.id+'_DDB_'+i+'" style="'+border+' background: '+this.basketBckColor+'; width:'+maxWidth+'px; height:'+maxHeight+'px; margin: 0px; font-size:'+this.fontSize+'px; font-size:0px;">.</td>';
		text += '</tr>';
		text += '<tr>';
		text += '<td style="width:'+maxWidth+'px; height="'+this.spaceBetweenElements+'px"></td>';
		text += '</tr>';
	};
	this.text = text;	
	
	if (this.playAudio == 'click'){
		var backupText = this.text;
		var rows = '<table height="'+imageHeigth+'"><tr><td></td></tr></table>';
		
		this.text = rows + backupText;		
	};
	tempX = this.x + 3;
	tempY = this.y + imageHeigth + 3;		
	
	for(var i=0; i < contentArr.length; i++){
		var oneArr = contentArr[i].split('#!#');
		
		var ddelementText = new __componentText;
		ddelementText.myParent = this; 
		ddelementText.id = this.id+'_DDE_'+i; 
		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.destinationBasket = this.id+'_DDB_'+i;
		ddelementText.text = oneArr[0];
		ddelementText.align = this.align;
		ddelementText.valign = this.valign;		
		ddelementText.borderWidth = 1;
		ddelementText.borderColor = this.elementBorderColor;
		ddelementText.padding = 3;
		ddelementText.bgColor = this.elementBckColor;		
		ddelementText.cursor = this.elementCursor;		
		ddelementText.canDragging = true;
		ddelementText.lastBasket = false;
		ddelementText.solved = false;
		ddelementText.orgZ = ddelementText.z = this.z+1+i;
		ddelementText.dropAudio = oneArr[1];
		
		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 );
		
		tempY = tempY + maxHeight + this.spaceBetweenElements;
		//tempY = (this.y + 3) + (i+1)*maxHeight + imageHeigth + (i+1)*this.spaceBetweenElements;
	};
};

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

componentMatchParagraph.prototype.editorPaint = function()
{
	return '';
}

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

componentMatchParagraph.prototype.componentMatchParagraphBaseOnPaint = componentMatchParagraph.prototype.onPaint;
componentMatchParagraph.prototype.onPaint = function() 
{
	if (this.locked) return;

	this.componentMatchParagraphBaseOnPaint();
	
	this.randomDDElements(this.texts);
	this.setElementIntoBasketsAtRandom(this.texts);
};

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

componentMatchParagraph.prototype.flashPlayerPlay = function()
{
	if (this.compAudio != null && this.compAudio.type == "componentFlashAudioPlayer") {
		this.compAudio.flashVars = "playIdx=0&autoStart=true";
		this.compAudio.onPaint();
	}
};

componentMatchParagraph.prototype.onMouseClickImagePlay = function()
{
	if (this.enableFlashPlayer) {
		this.myParent.flashPlayerPlay();
	} else {
		this.myParent.compAudio.actionMediaPlay();
	}
};

componentMatchParagraph.prototype.onMouseClickImagePause = function()
{
	this.myParent.compAudio.actionMediaPause();
};

componentMatchParagraph.prototype.onMouseClickImageStop = function()
{
	this.myParent.compAudio.actionMediaStop();
};

componentMatchParagraph.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);
			}
		}
	}
};

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

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

		arr[i].startX = arr[i].x;
		arr[i].startY = arr[i].y;
		arr[i].startY = arr[i].x_backup;
		arr[i].startY = arr[i].y_backup;	
	
		arr[i].x = sizes[randomIndexes[i]].x;
		arr[i].y = sizes[randomIndexes[i]].y;
	};
};

componentMatchParagraph.prototype.setElementIntoBasketsAtRandom = function(elemArr) 
{
	var contentArr = this.content.split('||');
	
	for (var i=0; i < this.texts.length; i++) {
		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)
		};		
		for(var j=0; j < contentArr.length; j++){
			var ddbasket = document.getElementById(this.id+'_DDB_'+j);
			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);
				break;
			};
		};
	};
};

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

componentMatchParagraph.prototype.getAudioIndex = 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[1] == audioFile) {
			result = i + 1;
			break;
		}
	}
	return result;
};

componentMatchParagraph.prototype.playDropAudio = function(audioFile) 
{
	if (audioFile == "") return;
	var res = false;
	
	if (this.enableFlashPlayer) {
		this.compAudio.flashVars = "playIdx="
			+ this.getAudioIndex(audioFile) + "&autoStart=true&playOnce=true";
		this.compAudio.show();
		res = true;
	} else {
		var player = document.getElementById(this.id+'dropAudioCompMediaPlayer');
		if (player) {
			player.url = this.resourcesURL + audioFile;
			if (this.dropAudioComp.pvWMP7up) {
				if (player.controls) res = player.controls.play();
			} else if (this.dropAudioComp.pvWMP64) {
				res = player.Play();
			};
		};
	}
	return res;
};	

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

componentMatchParagraph.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('||');
	
	for(var j=0; j < contentArr.length; j++){
		var ddbasket = document.getElementById(this.myParent.id+'_DDB_'+j);
		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;
			break;
		}else this.inArea = null; 
	};
	var lastbasket = this.lastBasket;
	
	if (this.inArea){	
		if (lastbasket != this.inArea.id){
			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, ddbasket);
			};
		}else{
			this.myParent.moveToStart(this, ddelement);
		};
	}else this.myParent.moveToStart(this, ddelement);
};

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

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

componentMatchParagraph.prototype.moveToStart = function(sender, elem) 
{
	sender.setZIndex(sender.orgZ);
	sender.x = sender.startX;
	sender.y = sender.startY;
	elem.style.left = sender.startX;
	elem.style.top = sender.startY;
};

componentMatchParagraph.prototype.setIntoBasket = function(sender, dde, obj) 
{
	if (sender.myParent.showAnswerIndicatorAnyTime){
		sender.setBorder(sender.myParent.elementBorderColor, 1);
		sender.setBgColor(sender.myParent.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.y = y;
	sender.startX = x;
	sender.startY = y;
	sender.insideBasketId = obj.id;	
	sender.solved = (sender.destinationBasket == obj.id);	
	sender.lastBasket = obj.id;
	
	//if (this.checkAudioRun()) this.runAudio();
};

componentMatchParagraph.prototype.checkAudioRun = function(obj, destBasket) 
{
	for (var i=0; i < this.texts.length; i++) {
		if (!this.texts[i].solved) return false;
	};
	return true;
}

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

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

componentMatchParagraph.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);
};

componentMatchParagraph.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();		
};

componentMatchParagraph.prototype.checkAnswer = function() 
{
	if (this.showAnswerIndicatorAnyTime && this.showCorrectAnswerIndicator) this.colorMyElements();
	
	if (this.parseCheckAnswer(this)) {
		this.runAudio();
		return true;
	}else{		
		return false;
	};
	
	//return (this.parseCheckAnswer(this));
};

componentMatchParagraph.prototype.colorMyElements = function(index)
{
	for (var i=0; i < this.texts.length; i++) {
		if (this.texts[i].solved){	
			this.texts[i].setBorder(this.elementCorrectBorderColor, 1);
			this.texts[i].setBgColor(this.elementCorrectBckColor);
		}else{
			this.texts[i].setBorder(this.elementInCorrectBorderColor, 1);
			this.texts[i].setBgColor(this.elementInCorrectBckColor);
		}
	};
};

componentMatchParagraph.prototype.parseCheckAnswer = function(index)
{
	var result = true;
	for (var i=0; i < this.texts.length; i++) {
		if (!this.texts[i].solved) return false;
	};
	return true;
};

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

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

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

componentMatchParagraph.prototype.runAudio = function() 
{	
	if (this.playAudio != 'play') return;
	
	var res = false;
	
	if (this.enableFlashPlayer) {
		this.compAudio.flashVars = "playIdx=0&autoStart=true&playOnce=true";
		this.compAudio.show();
		res = true;
	} else {	
		var player = document.getElementById(this.id+'AudioMediaPlayer');
		var audio = document.getElementById(this.id+'Audio');
		
		if (player) {
			player.url = this.resourcesURL + this.src;
			if (this.compAudio.pvWMP7up) {
				if (player.controls) res = player.controls.play();
			} else if (this.compAudio.pvWMP64) {
				res = player.Play();
			};
		};
	}
	return res;
};

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

componentMatchParagraph.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.texts[i].destinationBasket;
	}
	return(result);
};

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

componentMatchParagraph.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.texts[i].destinationBasket;
	}
	return(result);
};

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













