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

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

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

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

componentFillTextTopAudio.prototype.componentFillTextTopAudioBaseOnCreate = componentFillTextTopAudio.prototype.onCreate;
componentFillTextTopAudio.prototype.onCreate = function(obj) {
	this.componentFillTextTopAudioBaseOnCreate();
	
	if ( PageInterface.GetValue("state") != psDesigning && lms && this.disableOnlineOfflineMix != false ) 
	{
		this.resourcesStatus 	= lms.resourcesStatus();
		this.resourcesURL 		= lms.resourcesURL();
	} else this.resourcesURL = resourcesURL;
	
	var sizeDiv = '<span id="'+this.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.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 = "";
		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 = "";
		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);		
	}
	
	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(this.id+"sizeDiv")) {
				document.getElementById(this.id+"sizeDiv").innerHTML = onlyText;
				width = document.getElementById(this.id+"sizeDiv").offsetWidth;
			}
			if ( width > this.maxWidth ) this.maxWidth = width;
		}
		width = this.maxWidth + this.elementPadding;
	}
	
	var baskets = this.text;
	var lastTopX = tempX;
	var lastBottomX = tempX;
	var spanHeight = eval(height);
	
	for (var i=0; i<this.basketsCount; i++) {
		// Czysty tekst bez nawiasow
		var ddeText = match[i].substr(1,match[i].length-2);
		onlyText = this.getTextOnly(ddeText);
		
		// tymczasowy element do ustalania szerokosci i wysokosci ddelementow
		if (document.getElementById(this.id+"sizeDiv")) {
			document.getElementById(this.id+"sizeDiv").innerHTML = ddeText;
			height = document.getElementById(this.id+"sizeDiv").offsetHeight;// + this.elementPadding;
			if (this.dynamicWidth) width = document.getElementById(this.id+"sizeDiv").offsetWidth;
		};
		
		if (this.basketBorderColor == 'transparent'){
			var borderStyle = '0px';
		}else{
			var borderStyle = '1px solid '+this.basketBorderColor;
		};		
		
		var ddeText = match[i].substr(1,match[i].length-2);
		
		//baskets = baskets.replace(match[i], "<span id='_"+this.id+"_"+i+"_basket' style='border: "+borderStyle+"; width: " + eval(width+this.elementPadding) + "px; height: " + eval(height+2)+ "px; margin: 0px; font-size: 0pt; line-height: " + getFontSize(this.font.size, "px") + "px;'></span>");
		
		baskets = baskets.replace(match[i], "<textarea name='"+this.id+"_"+onlyText+"' id='_"+this.id+"_"+i+"_basket' style='background: #ffffff; border: "+borderStyle+"; width: " + eval(width+this.elementPadding) + "px; height:" + eval(height+2)+ "px; padding-right: "+width+"px; overflow: hidden;' disabled></textarea>");	
		
		// 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 {
				tempY = this.y + 2 + this.borderWidth;
				tempX += tempWidth + 8 + (this.elementPadding-3);
			}
		}else{ 
			tempY = this.y + 2 + this.borderWidth;//+ (this.elementPadding-3);
			tempX += tempWidth + this.borderWidth;
		};
		
		// powoladnie ddelementow
		var textTemp = new __componentText;
		textTemp.parentText = this; 
		textTemp.id = this.id+'_DDE_'+i; 
		textTemp.text = ddeText;//match[i].substr(1,match[i].length-2);
		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.basketName = this.id+"_"+onlyText;
		textTemp.solved = false;
		textTemp.orgZ = textTemp.z = this.z+i;
		textTemp.insideBasketId = "";
		textTemp.cursor = this.ddelementCursor;
		textTemp.hide = this.__hide;
		textTemp.baseOnPaint = textTemp.onPaint;
		textTemp.onPaint = this._onPaint;						
		
		// Szerokosc poprzedniego ddelementu
		tempWidth = width;
		
		// dodanie ddelementu na strone
		page.addChild(textTemp);
		
		// dodanie do tablicy ddelementow
		this.texts.push( textTemp );
	};
	
	this.valign = 'bottom';
	
	this.text = baskets;
	if (PageInterface.GetValue("state") != psDesigning) this.randomDDElements();
	
	this.componentX = this.x;
	this.componentY = this.y;
};
		
componentFillTextTopAudio.prototype.getTextOnly = function(text) 
{
	var onlyText = text.replace(/'/ig, '');
	onlyText = onlyText.replace(/"/ig, '');
	onlyText = onlyText.replace(/ /ig, '');
	
	return onlyText;
}

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

componentFillTextTopAudio.prototype.__hide = function() 
{
	return;
}

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

componentFillTextTopAudio.prototype.onLoad = function() {
}

componentFillTextTopAudio.prototype.editorPaint = function()
{
	return '';
}
	
componentFillTextTopAudio.prototype.componentFillTextTopAudioBaseOnPaint = componentFillTextTopAudio.prototype.onPaint;
componentFillTextTopAudio.prototype.onPaint = function() 
{
	if (this.locked) return;

	this.componentFillTextTopAudioBaseOnPaint();
	
	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].setBorder(this.elementBorderColor, 1);	
		this.texts[i].obj.style.background = this.elementBckColor;		
	};
}

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

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

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

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

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

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

componentFillTextTopAudio.prototype.playDropAudio = function(audioFile) 
{	
	if (this.enableFlashPlayer) {
		this.dropAudioComp.flashVars = "playIdx=1&autoStart=true&playOnce=true";
		this.dropAudioComp.show();
	} else {
		this.dropAudioComp.actionMediaPlay();
	};	
};	

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

componentFillTextTopAudio.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.parentText.playDropAudio();
	
	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);
	};
};

componentFillTextTopAudio.prototype.moveToStart = function(obj, elem) 
{
	if (obj.parentText.showAnswerIndicatorAnyTime){
		obj.setBorder(obj.parentText.elementBorderColor, 1);	
		obj.obj.style.background = obj.parentText.elementBckColor;
	};
	
	obj.solved = false;
	obj.x = obj.startX;
	obj.y = obj.startY;
	obj.insideBasketId = '';
	elem.style.left = obj.startX;
	elem.style.top = obj.startY;
};

componentFillTextTopAudio.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.solved = (obj.name == sender.text);
	//sender.solved = (obj.id == sender.destinationBasketId);
	sender.solved = (obj.name == sender.basketName);
	
	if (!this.playAudioAfterCheck && this.checkPlayAudio()) this.runAudio();
};

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

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

componentFillTextTopAudio.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].setBorder(this.elementInCorrectBorderColor, 1);	
					this.texts[i].obj.style.background = this.elementInCorrectBckColor;
				}
			}
			else if (this.showCorrectAnswerIndicator == true) { 
				this.texts[i].setBorder(this.elementCorrectBorderColor, 1);		
				this.texts[i].obj.style.background = this.elementCorrectBckColor;
			}
		}
	}
}

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

componentFillTextTopAudio.prototype.setAnswer = function(valStr, showCorrect)
{
	if ((typeof(valStr) == 'undefined' || valStr == '') && !showCorrect) return;
	if (valStr == '' && (typeof(showCorrect) == 'undefined' || showCorrect.toString() == '')) showCorrect = true;
	
	if (valStr != '' && !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.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);
			
			if (ddbasket && ddelement)
				this.setIntoBasket(this.texts[i], ddelement, ddbasket);
		};
	};
	this.onRepaint();	
};

componentFillTextTopAudio.prototype.checkAnswer = function() 
{
	if (this.parseCheckAnswer(this)) {
		if (this.playAudioAfterCheck) this.runAudio();
		return true;
	};
	return false;
};

componentFillTextTopAudio.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;
		}
		if (this.showAnswerIndicatorAnyTime && this.showCorrectAnswerIndicator){
			if (this.texts[i].solved){
				this.texts[i].setBorder(this.elementCorrectBorderColor, 1);	
				this.texts[i].obj.style.background = this.elementCorrectBckColor;
			}else{
				this.texts[i].setBorder(this.elementInCorrectBorderColor, 1);
				this.texts[i].obj.style.background = this.elementInCorrectBckColor;			
			}
		};
	}
	return result;
};

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

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

componentFillTextTopAudio.prototype.runAudio = function() 
{	
	if (this.enableFlashPlayer) {
		this.dropAudioComp.flashVars = "";
		this.dropAudioComp.show();	
		this.compAudio.flashVars = "playIdx=0&autoStart=true&playOnce=true";
		this.compAudio.show();
	} else {
		this.compAudio.setUrl(this.resourcesURL + this.src);
		this.compAudio.actionMediaPlay();
	};	
};

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

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

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

componentFillTextTopAudio.prototype.interactionsCorrectResponsesAsText = 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);
};

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