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

componentMarkSentenceAudio = function(name) 
{
	this.componentElement = componentElement;
	this.componentElement(name);
	
	this.spaceBetween = 0;
	this.elementHeight = 0;
	this.elementWidth = 0;
	this.texts = new Array();
	this.buttons = new Array();
	this.compAudio = null;	
	this.isLocked = false;
	
	this.interactions = true;
	this.interactionsType = "choice";
};

componentMarkSentenceAudio.prototype = new componentElement;
componentMarkSentenceAudio.prototype.type = "componentMarkSentenceAudio";

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

componentMarkSentenceAudio.prototype.onCreate = function(obj)
{
	var maxHeight = 25;
	var wordaArr = this.choiceValues.split('||');
	
	var tempX = this.x + 3;
	var tempY = this.y + 3;		
	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 = "";
		this.compAudio = page.addChild(e);
		var audioButtonsArr = new Array(this.audioPlay, this.audioStop, '_play_', '_stop_', 'Play', 'Stop');
	} 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 audioButtonsArr = new Array(this.audioPlay, this.audioPause, this.audioStop, '_play_', '_pause_', '_stop_', 'Play', 'Pause', 'Stop');
	}
	
	var correctAns = this.changeFromInt(this.correctAnswer);
	
	if (this.showAudioButtons){
		var btnLen = audioButtonsArr.length/3;
		
		for (var i=0; i < btnLen; i++){
			var image = new componentImage;
			image.myParent = this; 
			image.id = this.id+audioButtonsArr[i+btnLen]+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.onCreate = this.onCreateText;
			var funct = 'this.onMouseClickImage'+audioButtonsArr[i+btnLen*2];
			image.eventClick = eval(funct);
			
			page.addChild(image);
			this.buttons.push(image);
			
			if(this.buttonsOrientation == 'horizontal'){
				if (image.h > imageHeigth) imageHeigth = image.h;
				tempX +=  image.w + 5;
			}else{
				if (image.w > imageWidth) imageWidth = image.w;
				tempY +=  image.h + 5;
			};
		};
		if(this.buttonsOrientation == 'horizontal'){
			tempX = this.x + 3;
			tempY += imageHeigth + 5;	
		}else{
			tempX += imageWidth + 5;
			tempY = this.y + 3;
		};	
	};
	
	if(this.buttonsOrientation == 'horizontal'){
		this.spaceBetween = (wordaArr.length-1)*2;
		this.elementHeight = ((this.h - imageHeigth-5) - (this.spaceBetween*(wordaArr.length-1)+6))/wordaArr.length;
		this.elementWidth = this.w-6;
	}else{
		this.spaceBetween = (wordaArr.length-1)*2;
		this.elementHeight = (this.h - (this.spaceBetween*(wordaArr.length-1)+6))/wordaArr.length;
		this.elementWidth = eval(this.w - imageWidth - 12);
	};		
	
	for(var i=0; i < wordaArr.length; i++){
		var ddelementText = new __componentText;
		ddelementText.myParent = this; 
		ddelementText.id = this.id+'_CHECK_'+i; 
		ddelementText.x = tempX;
		ddelementText.y = tempY;
		ddelementText.w = this.elementWidth;
		ddelementText.h = this.elementHeight;
		ddelementText.text = wordaArr[i];
		ddelementText.valign = 'middle';
		ddelementText.align = this.fontAlign;
		ddelementText.font.size = this.fontSize;
		ddelementText.font.color = this.fontColor;
		ddelementText.font.family = this.fontFamily;
		
		ddelementText.borderWidth = 1;
		ddelementText.padding = 0;
		ddelementText.borderColor = this.elementBorderColor;
		ddelementText.bgColor = this.elementBckColor;		
		ddelementText.cursor = this.elementCursor;		
		ddelementText.checked = false;
		ddelementText.isCorrect = this.isInArray(correctAns, i);
		ddelementText.solved = false;
		
		ddelementText.onCreate = this.onCreateText;
		ddelementText.eventMouseDown = this.onMouseClickText;
		ddelementText.baseOnPaint = ddelementText.onPaint;
		ddelementText.onPaint = this._onPaint;							
		
		page.addChild(ddelementText);
		this.texts.push(ddelementText);
		
		if (this.showAudioButtons){
			if(this.buttonsOrientation == 'horizontal'){
				tempX = this.x + 3;
				tempY += this.elementHeight + this.spaceBetween;
			}else{
				tempX = this.x + 3 + imageWidth + 5;	
				tempY += this.elementHeight + this.spaceBetween;
			};
		}else{			
			tempX = this.x + 3;	
			tempY += this.elementHeight + this.spaceBetween;
		};
	};	
	if (this.randAns == 1) this.randomElements(this.texts);
};

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

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

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

componentMarkSentenceAudio.prototype.onRepaint = function(obj) 
{
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator)
	{
		for (var i=0; i < this.texts.length; i++) {
			if ((this.texts[i].checked && this.texts[i].isCorrect) || (!this.texts[i].checked && !this.texts[i].isCorrect)){
				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;
};

componentMarkSentenceAudio.prototype.onMouseClickImagePlay = function()
{
	if (this.myParent.enableFlashPlayer) {
		this.myParent.compAudio.flashVars = "playIdx=0&autoStart=true&playOnce=true";
		this.myParent.compAudio.show();
	} else {
		this.myParent.compAudio.actionMediaPlay();
	};	
};

componentMarkSentenceAudio.prototype.onMouseClickImagePause = function()
{
	if (this.myParent.enableFlashPlayer) {
		this.myParent.compAudio.flashVars = "";
		this.myParent.compAudio.show();
	} else {
		this.myParent.compAudio.actionMediaPause();
	};	
};

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

componentMarkSentenceAudio.prototype.isInArray = function(str, index) 
{
	var arr = str.split('||');
	for (var i=0; i < arr.length; i++){
		if (arr[i] == index) return true;
	};
	return false; 
};

componentMarkSentenceAudio.prototype.changeFromInt = function(str) {
	if (str == null || typeof(str) == "undefined" || str == "null" || str == "0") return "";

	var arrTemp = "", brk = "";
	var nr = 0;

	for (var i=0; i<str.length; i++) {
		nr += Math.pow(10,(str.length-i-1))*str.charAt(i);
	}

	while ( 1 ) {
		arrTemp += nr % 2;
		if ( (nr == 0) || (nr == 1) ) {
			break;
		}
		nr = Math.floor(nr / 2);  	
	}

	str = "";
	for (i=0; i<arrTemp.length; i++)
		if (arrTemp.charAt(i) == '1') {
			str += brk + i.toString();
			brk = "||";
		}
	return str;
};

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

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

componentMarkSentenceAudio.prototype.randomElements = 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
			}
		);
	};
	
	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].startX = arr[i].x;
		arr[i].startY = arr[i].y;
	
		arr[i].x = sizes[randomIndexes[i]].x;
		arr[i].y = sizes[randomIndexes[i]].y;
	};
};

componentMarkSentenceAudio.prototype.onMouseClickText = function() 
{
	if (this.myParent.isLocked) return;
	
	this.checked = !this.checked;
	if (this.checked){
		this.bgColor = this.myParent.elementBorderColorClick;
	}else {
		this.bgColor = this.myParent.elementBckColor;
	}
	this.obj.style.border = this.myParent.elementBorderColor + " solid 1px";
	
	this.paint();
};

componentMarkSentenceAudio.prototype.getAnswer = function() 
{
	var valStr = '';
	var sep = '';
	 
	for (var i=0; i < this.texts.length; i++) {
		if (valStr != '') sep = '||'
		valStr += sep + this.texts[i].id + '##';
		valStr += (this.texts[i].checked)?1:0;
	};
	return(valStr);
};

componentMarkSentenceAudio.prototype.setAnswer = function(valStr, showCorrect)
{
	if ((typeof(valStr) == 'undefined' || valStr == '') && !showCorrect) return;
	if (valStr == '' && (typeof(showCorrect) == 'undefined' || showCorrect.toString() == '')) showCorrect = true;
	
	var arr = valStr.split('||');
	if (showCorrect){
		for (var i=0; i < this.texts.length; i++) {
			if (this.texts[i].isCorrect){
				this.texts[i].checked = true;
				this.texts[i].bgColor = this.elementBorderColorClick;		
				this.texts[i].setBorder(this.elementBorderColor, 1);				
				this.texts[i].paint();				
			}else{
				this.texts[i].checked = false;
				this.texts[i].bgColor = this.elementBckColor;		
				this.texts[i].setBorder(this.elementBorderColor, 1);				
				this.texts[i].paint();							
			}
		};
	}else{
		for (var i=0; i < arr.length; i++){
			var oneCheck = arr[i].split('##');
			var obj = document.getElementById(oneCheck[0]);
			
			var j = -1;
			do{
				j++;
			}while (this.texts[j].id != oneCheck[0] && j < this.texts.length)
			
			if (oneCheck[1] == 1){
				this.texts[j].checked = true;
				this.texts[j].bgColor = this.elementBorderColorClick;				
				this.texts[j].setBorder(this.elementBorderColor, 1);
				this.texts[j].paint();				
			}else{
				this.texts[i].checked = false;
				this.texts[i].bgColor = this.elementBckColor;		
				this.texts[i].setBorder(this.elementBorderColor, 1);				
				this.texts[i].paint();							
			}
		};	
	};
	this.onRepaint();	
	return(valStr);
};

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

componentMarkSentenceAudio.prototype.parseCheckAnswer = function() 
{
	var result = true;
	for (var i=0; i < this.texts.length; i++) {
		if ((this.texts[i].checked && this.texts[i].isCorrect) || (!this.texts[i].checked && !this.texts[i].isCorrect)){
			if (this.showAnswerIndicatorAnyTime){
				this.texts[i].setBorder(this.elementCorrectBorderColor, 1);
				this.texts[i].obj.style.background = this.elementCorrectBckColor;		
			}
			this.texts[i].solved = true;			
		}else{
			if (this.showAnswerIndicatorAnyTime){
				this.texts[i].setBorder(this.elementInCorrectBorderColor, 1);
				this.texts[i].obj.style.background = this.elementInCorrectBckColor;
			}
			this.texts[i].solved = false;			
			result = false;
		}
	};
	return result;
};

componentMarkSentenceAudio.prototype.componentMarkSentenceAudioBaseResetElement = componentMarkSentenceAudio.prototype.resetElement;
componentMarkSentenceAudio.prototype.resetElement = function() 
{
	this.componentMarkSentenceAudioBaseResetElement();
	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].checked = false;
		this.texts[i].bgColor = this.elementBckColor;
	};
}

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

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

componentMarkSentenceAudio.prototype.interactionsCorrectResponsesCount = function()
{
	var correctAns = this.changeFromInt(this.correctAnswer);
	return correctAns.length;
};

componentMarkSentenceAudio.prototype.interactionsCorrectResponses = function(index)
{
	var valStr = '';
	var sep = '';
	 
	for (var i=0; i < this.texts.length; i++) {
		if (valStr != '') sep = '||'
		valStr += sep + this.texts[i].id + '##';
		valStr += (this.texts[i].isCorrect)?1:0;
	};
	return(valStr);
};

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

componentMarkSentenceAudio.prototype.interactionsCorrectResponsesAsText = function(index)
{
	var valStr = '';
	var sep = '';
	 
	for (var i=0; i < this.texts.length; i++) {
		if (valStr != '') sep = '||'
		valStr += sep + this.texts[i].id + '##';
		valStr += (this.texts[i].isCorrect)?1:0;
	};
	return(valStr);
};

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

