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

componentQuestionToTextAudio = function(name) 
{
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.elementHeight = 0;
	
	this.texts = new Array();
	this.checks = new Array();
	
	this.compAudio = null;
	
	this.buttons = new Array();
};

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

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

componentQuestionToTextAudio.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 + '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 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);	
		audioElement.allowNextStep = true;
		
		var audioButtonsArr = new Array(this.audioPlay, this.audioPause, this.audioStop, '_play_', '_pause_', '_stop_', 'Play', 'Pause', 'Stop');
	}
	
	var tempX = this.x + 3;
	var tempY = this.y + 3;		
	var imageHeigth = 0;
	var imageWidth = 0;	
	var btnLength = audioButtonsArr.length/3;
	
	for (var i=0; i < btnLength; i++){
		
		var image = new componentImage;
		image.myParent = this; 
		image.id = this.id+audioButtonsArr[i+btnLength]+i; 
		image.x = tempX;
		var imageTest = new Image();
		imageTest.src = this.resourcesURL + audioButtonsArr[i];
		if (this.audioButtonsAbove){
			image.y = tempY;
		}else{
			image.y = this.y + this.h - imageTest.height-3;
		}		
		image.w = imageTest.width;
		image.h = imageTest.height;
		image.src = audioButtonsArr[i];
		image.cursor = 'hand';
		
		image.captureMouseEvents = this.captureMouseEvents;
		image.captureMouseEvents = this.captureMouseEvents;
		var funct = 'this.onMouseClickImage'+audioButtonsArr[i+btnLength*2];
		image.eventClick = eval(funct);
		
		page.addChild(image);
		this.buttons.push(image);
		
		if (image.h > imageHeigth) imageHeigth = image.h;
		tempX +=  image.w + 5;
	};
	
	if (this.audioButtonsAbove){
		var backupText = this.text;
		var rows = '<table height="'+eval(imageHeigth+10)+'"><tr><td></td></tr></table>';
		this.text = rows + backupText;
	};

	var sizeDiv = '<span id="'+this.id+'sizeDiv" style="visibility: hidden; position: absolute;"></span>';
	
	var ob = document.getElementById(this.id);
	ob.innerHTML += sizeDiv;
	
	var elem = document.getElementById(this.id+"sizeDiv");

	var tempX = this.x + 3;
	if (this.showText){
		var tempY = this.y + 15 + this.getTextHeight(elem, this.text, this.font.family, this.font.size);
	}else{
		var tempY = this.y + 15;
		this.text = '';
	};
	var imagesWidth = this.getImagesWidth(this.radioOn, this.radioOff)+20;
	var questAndAnsArr = this.content.split('!@@!');

	this.elementHeight = (this.h - tempY - 5)/(questAndAnsArr.length-1);
	var questArr = questAndAnsArr[0].split('##');
	var answArr = questAndAnsArr[1].split('##');
	//var imgWidthHeightArr = this.getImageWidthHeight(this.checkOn);
	var imgWidthHeightArr = [this.checkW, this.checkH];	
	
	for(var i=0; i < questArr.length; i++){
		var oneQuestArr = questArr[i].split("||");
		var questHeight = this.getTextHeight(elem, oneQuestArr[1], this.answFontFamily, this.answFontSize);
		
		var sentText = new __componentText;
		sentText.myParent = this; 
		sentText.id = this.id+'_QUEST_'+i; 
		sentText.x = tempX;
		sentText.y = tempY;
		sentText.w = this.w;
		sentText.h = questHeight;
		sentText.text = oneQuestArr[1];
		sentText.borderWidth = 0;
		sentText.padding = 0;
		
		page.addChild(sentText);
		this.texts.push(sentText);
		tempY += sentText.h;
		
		var oneAnswArr = answArr[i].split("&&");
		for(var j=0; j < oneAnswArr.length; j++){
			var splitAns = oneAnswArr[j].split("#!#");
			var questHeight = this.getTextHeight(elem, splitAns[1], this.answFontFamily, this.answFontSize);
			
			if (imgWidthHeightArr[1] > questHeight) {
				var elemHeight = imgWidthHeightArr[1]
			} else {
				var elemHeight = questHeight;
			};
			
			var checkElem = new componentCheckBox;
			checkElem.myParent = this; 
			checkElem.id = this.id+'_CHECK_'+i; 
			checkElem.x = tempX + 10;
			checkElem.y = tempY;
			checkElem.w = this.w - 10;
			checkElem.h = elemHeight;
			checkElem.borderWidth = 0;
			checkElem.borderWidth = 1;
			checkElem.padding = 0;
			checkElem.group = this.id+'group_'+i;			
			checkElem.textOffset = 0;			
			checkElem.padding = 1;			
			checkElem.paddingTopText = 0;
			
			if (PageInterface.GetValue("state") == psDesigning) checkElem.correctAnswer = (splitAns[0] == 1);
			else checkElem.correctAnswer = splitAns[0];
			checkElem.checked = 0;
			checkElem.imgChecked = this.checkOn;
			checkElem.imgNotChecked = this.checkOff;
			checkElem.imgCheckedCorrectSrc = this.correctCheckOn;
			checkElem.imgNotCheckedCorrectSrc = this.correctCheckOff;
			checkElem.imgCheckedIncorrectSrc = this.incorrectCheckOn;
			checkElem.imgNotCheckedIncorrectSrc = this.incorrectCheckOff;
			checkElem.parseCheckAnswer = this.__parseCheckAnswer;
			checkElem.changeChecked = this.__changeChecked;
			
			checkElem.showCorrectAnswerIndicator = this.showCorrectAnswerIndicator;				
			checkElem.showAnswerIndicatorAnyTime = this.showAnswerIndicatorAnyTime;				
			
			checkElem.text.w = this.w - 10 - imgWidthHeightArr[0];	
			checkElem.text.h = imgWidthHeightArr[1];
			checkElem.text.text = splitAns[1];			
			checkElem.imgn.src = this.checkOff;
			checkElem.imgn.w = imgWidthHeightArr[0];
			checkElem.imgn.h = imgWidthHeightArr[1];	
			
			checkElem.imgc.src = this.checkOn;
			checkElem.imgc.w = imgWidthHeightArr[0];
			checkElem.imgc.h = imgWidthHeightArr[1];					
			
			this.checks.push(checkElem);
			var obj = page.addChild(checkElem);
			
			tempY += elemHeight + 5;
		};
	};
};

componentQuestionToTextAudio.prototype.onRepaint = function() 
{
	//po to aby nie zmieniala sie czcionka pytania po nacisnieci na OK
	return;
}

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

componentQuestionToTextAudio.prototype.__changeChecked = function(obj) 
{
	if (!this.locked) {
		if (!this.invokeEvent("Change")) {
			this.isChanged = true;
			this.checked = !this.checked;
			this.paint();
		}
	}
};

componentQuestionToTextAudio.prototype.__parseCheckAnswer = function() 
{
	var isCorrect = false;
	var actAnswer = this.getAnswer().toString();
	var arrCorrAns = new Array();
	
	if(typeof(this.correctAnswer)!="undefined"){
		if (actAnswer == this.correctAnswer) isCorrect = true;
		if (this.showAnswerIndicatorAnyTime && this.showCorrectAnswerIndicator){
			if (isCorrect){
			this.imgc.src = this.imgCheckedCorrectSrc;
			this.imgn.src = this.imgNotCheckedCorrectSrc;			
			this.imgc.paint();
			this.imgn.paint();
			}else{
				this.imgc.src = this.imgCheckedIncorrectSrc;
				this.imgn.src = this.imgNotCheckedIncorrectSrc;			
				this.imgc.paint();
				this.imgn.paint();
			}
		}
	}
	return(isCorrect);
};

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

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

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


componentQuestionToTextAudio.prototype.checkIsCorrect = function(arr, ind)
{
	for (var i=0; i < arr.length; i++){
		if (ind == arr[i]) return 1;
	};
	return 0;
};

componentQuestionToTextAudio.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 eval(pxSize+2);
};

componentQuestionToTextAudio.prototype.getTextHeight = function(obj, text, fontFamily, fontSize)
{
	obj.innerHTML = text;
	obj.style.fontFamily = fontFamily;
	obj.style.lineHeight = this.lineHeight;
	obj.style.fontSize = this.getFontSize(fontSize, 'px');
	var height = obj.offsetHeight;
	
	return height;
}

componentQuestionToTextAudio.prototype.getImageWidthHeight = function(imageName)
{
	var newIng = new Image();
	newIng.src = this.resourcesURL + imageName;
	
	return [newIng.width, newIng.height];
}

componentQuestionToTextAudio.prototype.getImagesWidth = function(radioOn, radioOff)
{
	var width = 0;
	
	var newImg = new Image();
	newImg.src = this.resourcesURL + radioOn;
	width = newImg.width;	
	
	return width;
}

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


