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

componentPairWords = function(name) 
{
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.spaceBetween = 0;
	this.inArea = null;
	this.texts = new Array();	
	this.compAudio = null;	
	this.dropAudioComp = null;

	this.interactions = true;
	this.interactionsType = "matching";		
	this.valign = "top";
};

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

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

componentPairWords.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;

	var unEscContentArr = unescape(this.content);
	var contentArr = unEscContentArr.split("||");
	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);	
		
		var audioElement = new componentAudio;
		audioElement.myParent = this;
		audioElement.id = this.id+'dropAudioComp'; 
		audioElement.src = this.audioDrop;
		audioElement.pvWMP64 = false;
		audioElement.pvWMP7up = false;
		audioElement.step = -1;
		audioElement.editorPaint = this.editorPaint;
		this.dropAudioComp = page.addChild(audioElement);		
	};
	
	this.spaceBetween = parseInt((this.w-6)/2);
	
	var sizeDiv = '<span id="'+this.id+'sizeDiv" style="visibility: hidden; position: absolute; font-family:' + this.fontFamily
	+ '; font-size:' + this.fontSize + 'px; color:' + this.fontColor + ';"></span>';
	
	var ob = document.getElementById(this.id);
	ob.innerHTML += sizeDiv;	
	
	var ddelementHeight = 0;
	var ddelementWidth = 0;
	
	for(var i=0; i < contentArr.length; i++){
		var groupWord = contentArr[i].split('#!#');
		var span = document.getElementById(this.id+"sizeDiv");
		
		if (span) {
			span.innerHTML = groupWord[1];
			span.style.width = this.spaceBetween;
			span.style.fontFamily = this.fontFamily;
			span.style.fontSize = this.getFontSize(this.fontSize, 'px');
			height = span.offsetHeight;
		};
		if ( height > ddelementHeight ) ddelementHeight = height;		
	};
	ddelementHeight += 3;
	
	var tempX = this.x + 2;
	var tempY = this.y + this.h;
	var text = '';	
	text += '<table border="0" collpadding="0" collspacing="0" width="100%">';
	text += '<colgroup>';
	text += '<col width="'+this.spaceBetween+'">';
	text += '<col width="'+this.spaceBetween+'">';
	text += '</colgroup>';
	
	for(var i=0; i < contentArr.length; i++){
		var groupArr = contentArr[i].split('#!#');
		
		text += '<tr height="'+eval(ddelementHeight+3)+'">';
		text += '<td valign="middle" style="border: 1px solid '+this.tableBorderColor+'; background: '+this.headerBckColor+'; font-family:'+this.fontFamily+'; font-size: '+this.getFontSize(this.fontSize, 'px')+'; color: '+this.fontColor+';">'+groupArr[0]+'</td>';
		text += '<td id="'+this.id+'_DDB_'+i+'" style="border: 1px solid '+this.tableBorderColor+';">&nbsp;</td>';
		text += '</tr>';
	
		var ddelementText = new __componentText;
		ddelementText.myParent = this; 
		ddelementText.id = this.id+'_DDE_'+i; 
		ddelementText.x = tempX;
		ddelementText.y = tempY - ddelementHeight - 3;
		ddelementText.x_backup = ddelementText.x;
		ddelementText.y_backup = ddelementText.y;
		ddelementText.startX = ddelementText.x;
		ddelementText.startY = ddelementText.y;		
		ddelementText.w = this.spaceBetween-3;
		ddelementText.h = ddelementHeight+3;
		ddelementText.destinationBasketId = this.id+'_DDB_'+i;
		ddelementText.insideBasketId = "";
		ddelementText.text = groupArr[1];
		ddelementText.font.family = this.fontFamily;
		ddelementText.font.size = this.fontSize;
		ddelementText.font.color = this.fontColor;
		ddelementText.align = "center";
		ddelementText.valign = 'middle';		
		ddelementText.borderWidth = 1;
		ddelementText.borderColor = this.elementBorderColor;
		ddelementText.padding = 0;
		ddelementText.bgColor = this.elementBckColor;		
		ddelementText.cursor = this.elementCursor;		
		ddelementText.canDragging = true;
		ddelementText.solved = false;
		ddelementText.orgZ = ddelementText.z = this.z+i;
		ddelementText.audioSrc = groupArr[2];
		ddelementText.dropAudio = groupArr[3];
		ddelementText.onCreate = this.onCreateText;
		ddelementText.beginDrag = this.beginDragText;
		ddelementText.endDrag = this.endDragText;
		ddelementText.onMove = this.mouseMoveText;	
		
		ddelementText.onMouseOver = this.onMouseOverText;		
		ddelementText.onMouseOut = this.onMouseOutText;		
		ddelementText.baseOnPaint = ddelementText.onPaint;
		ddelementText.onPaint = this._onPaint;							
		
		page.addChild(ddelementText);		
		this.texts.push( ddelementText ); 
		
		tempX += this.spaceBetween + 2;	
		
		var result = (i+1) % 2;
		var div = parseInt((i+1)/2);
		
		if (result == 0) {
			tempX = this.x + 2;
			tempY = ddelementText.y - 3;
		};
	};
	text += '</table>';
	this.text = text;

	if (PageInterface.GetValue("state") != psDesigning) this.randomDDElements(this.texts);
};

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

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

componentPairWords.prototype.getAudioIndex = function(audioFile)
{
	var result = -1;
	var contentArr = unescape(this.content).split('||');
	for(var i=0; i < contentArr.length; i++){
		var oneArr = contentArr[i].split('#!#');
		if (oneArr[2] == audioFile) {
			result = i;// + 1;
			break;
		}
	}
	return result;
};

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

componentPairWords.prototype.onMouseOverText = function()
{
	if (this.audioSrc == '' || this.audioSrc == 'undefined' || typeof(this.audioSrc) == 'undefined')
		return;
	
	if (this.myParent.enableFlashPlayer) {
		this.myParent.compAudio.flashVars = "playIdx="
			+ this.myParent.getAudioIndex(this.audioSrc) + "&autoStart=true&playOnce=true";
		this.myParent.compAudio.show();
		res = true;
	} else {
		this.myParent.compAudio.setUrl(this.resourcesURL + this.audioSrc);
		this.myParent.compAudio.actionMediaPlay();
	}		
};

componentPairWords.prototype.onMouseOutText = function()
{
	if (this.myParent.enableFlashPlayer) {
		this.myParent.compAudio.flashVars = "";
		this.myParent.compAudio.show();
	} else {
		this.myParent.compAudio.actionMediaStop();
	}
};

componentPairWords.prototype.getFontSize = function(value, format)
{
	var fontSizesPX = new Array(0,10,13,16,18,24,32,48);
	var fontSizesPT = new Array(0, 8,10,12,14,18,24,36);
	var pxSize = fontSizesPT[parseInt(value)];
	if (format == "px") {
		pxSize = fontSizesPX[parseInt(value)];
	};	
	return pxSize;
};

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

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

componentPairWords.prototype.componentPairWordsBaseOnPaint = componentPairWords.prototype.onPaint;
componentPairWords.prototype.onPaint = function() 
{
	if (this.locked) return;
	
	this.componentPairWordsBaseOnPaint();
};

componentPairWords.prototype.onRepaint = function() 
{
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator)
	{
		for (var i=0; i < this.texts.length; 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;
			}
		}
	}
};

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

componentPairWords.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].startX = arr[i].x_backup;
		arr[i].startY = arr[i].y_backup;	
		arr[i].beginX = arr[i].x_backup;
		arr[i].beginY = arr[i].y_backup;	
	
		arr[i].x = sizes[randomIndexes[i]].x;
		arr[i].y = sizes[randomIndexes[i]].y;
	};
};

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

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

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

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

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

	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 unEscContentArr = unescape(this.myParent.content);
	var contentArr = unEscContentArr.split("||");
	
	for(var i=0; i < contentArr.length; i++){
		var groupWord = contentArr[i].split('#!#');
		var ddbasket = document.getElementById(this.myParent.id+'_DDB_'+i);
		
		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; 
	};
	
	if (this.inArea && !this.myParent.checkIsFilled(this, this.inArea.id)){
		this.myParent.setIntoBasket(this, ddelement, this.inArea)
	}else {
		this.myParent.moveToStart(this, ddelement);
	};
};

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

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

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

componentPairWords.prototype.setIntoBasket = function(sender, dde, obj) 
{
	var x = sender.myParent.findPosX(obj);
	var y = sender.myParent.findPosY(obj);

	var shift = 0;
	if (is.moz) shift = 1;
	dde.style.left = x + shift;
	dde.style.top = y + shift;
	
	sender.x = x;
	sender.y = y;
	sender.insideBasketId = obj.id;
	var nameArr = obj.id.split('#');
	sender.solved = (nameArr[0] == sender.destinationBasketId);
};

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

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

componentPairWords.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);		
				this.texts[j].setBorder(this.elementBorderColor, 1);
			}
		};
	}else{
		var tempArr = new Array();
		for (var i=0; i < this.texts.length; i++) {
			
			if (tempArr[this.texts[i].destinationBasketId])
				var row = tempArr[this.texts[i].destinationBasketId].length;
			else var row = 0;
			
			var ddelement = document.getElementById(this.texts[i].id);
			var ddbasket = document.getElementById(this.texts[i].destinationBasketId);
			this.setIntoBasket(this.texts[i], ddelement, ddbasket);
			this.texts[i].setBorder(this.elementBorderColor, 1);
		};
	};
	this.onRepaint();		
};

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

componentPairWords.prototype.parseCheckAnswer = function(index)
{
	var result = true;
	for (var i=0; i < this.texts.length; i++) {
		if (!this.texts[i].solved) result = false;
		
		if (this.showAnswerIndicatorAnyTime && this.showCorrectAnswerIndicator){
			if (this.texts[i].solved){
				this.texts[i].obj.style.border = this.elementCorrectBorderColor + " solid 1px";
				this.texts[i].obj.style.background = this.elementCorrectBckColor;
			}else{
				this.texts[i].obj.style.border = this.elementInCorrectBorderColor + " solid 1px";
				this.texts[i].obj.style.background = this.elementInCorrectBckColor;
			}
		};			
	};
	return result;
};

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

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

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

componentPairWords.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].destinationBasketId; 
	}
	return(result);
};

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

componentPairWords.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].destinationBasketId; 
	}
	return(result);
};

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











