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

componentSayingDialogue = function(name)
{
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.buttons = new Array();	
	this.recButtons = new Array();	
	this.audioButtons = new Array();	
	this.activeX = new Array();	
	this.audiosArr = new Array();	
	
	this.activeXObj = null;
	this.audioNumer = 0;
	
	this.maxSentenceToSay = 0;
	this.choosenPerson = '';
	this.buttonsStatus = 'off';	
	this.warningText = null;
	
	this.iTimerID = null
	this.activeActiveXObj = null;
	
	this.componentState = 0; //0 - play; 1 - record; 2 - play recorded
};

componentSayingDialogue.prototype = new __componentText();
componentSayingDialogue.prototype.type = "componentSayingDialogue";

componentSayingDialogue.prototype.componentSayingDialogueBaseonCreate = componentSayingDialogue.prototype.onCreate;
componentSayingDialogue.prototype.onCreate = function(obj) 
{
	this.componentSayingDialogueBaseonCreate();
	
	if ( PageInterface.GetValue("state") != psDesigning && lms && this.disableOnlineOfflineMix != false ) 
	{
		this.resourcesStatus 	= lms.resourcesStatus();
		this.resourcesURL 		= lms.resourcesURL();
	} else this.resourcesURL = resourcesURL;
	
	if (PageInterface.GetValue("state") != psNormal){
		var textTemp = new __componentText;
		textTemp.parentText = this; 
		textTemp.id = this.id+'_warning';
		textTemp.text = this.textDesign;
		textTemp.align = "center";		
		textTemp.x = this.x + 0;
		textTemp.y = this.y + this.h - 75;
		textTemp.w = this.w;
		textTemp.h = this.h;
		
		page.addChild(textTemp);
		this.warningText = textTemp;	
	};	
	
	var sizeDiv = '<span id="'+this.id+'sizeDiv" style="visibility: hidden; position: absolute;"></span>';
	
	var ob = document.getElementById(this.id);
	ob.innerHTML += sizeDiv;
	
	var sentencesArr = this.content.split('||');
	var personWidth = 0;
	var personHeight = 0;
	
	this.maxSentenceToSay = sentencesArr.length;
	
	for (var i=0; i < sentencesArr.length; i++){
		var stepArr = sentencesArr[i].split('#!#');
		var sizeDiv = document.getElementById(this.id+'sizeDiv');
		if (sizeDiv){
			sizeDiv.innerHTML = stepArr[0];
			sizeDiv.style.fontFamily = 'Arial';
			sizeDiv.style.fontSize = '13';
			var width = sizeDiv.offsetWidth;				
			var height = sizeDiv.offsetHeight;				
		};
		if (width > personWidth) personWidth = width;
		if (height > personHeight) personHeight = height;
	};
	
	var text = '';
	text += '<table border="0" cellspacing="0" cellpadding="2" width="100%">';
	for (var i=0; i < sentencesArr.length; i++){
		var stepArr = sentencesArr[i].split('#!#');
		
		if (this.textBorderColor == 'transparent'){
			var defStyle = '0px ';
		}else{
			var defStyle = 'solid 1px '+this.textBorderColor;
		};		
		
		text += '<tr>'
		text += '<td style="width:1px;" align="center" valign="top" >'
		text += '<div style="position: relative; width:'+eval(personWidth+20)+'; border: '+defStyle+'; background: '+this.textBckgColor+'; font-family:Arial; font-size: 13px; font-color: #000000;">';
		text += stepArr[0];
		text += '</div>';
		text += '</td>';
		text += '<td style="width:10px">';
		text += '</td>';
		
		if (i == this.audioNumer ){
			text += '<td id="'+this.id+'_TD_'+i+'" align="left" valign="top" style="width:'+eval(this.w-eval(personWidth+20)-10)+'; border: '+defStyle+';background: '+this.sayingSentenceColor+'; font-family:Arial; font-size: 13px; font-color: #000000;"';

			text += ' sentFor="'+stepArr[0]+'" >';
		}else{
			text += '<td id="'+this.id+'_TD_'+i+'" align="left" valign="top" style="width:'+eval(this.w-eval(personWidth+20)-10)+'; border: '+defStyle+';background: '+this.sentenceColor+'; font-family:Arial; font-size: 13px; font-color: #000000;"';
			text += ' sentFor="'+stepArr[0]+'" >';
		};
			
		text += stepArr[1];
		text += '</td>'		
		text += '</tr>'
		text += '<tr>'
		text += '<td height="10px">'		
		text += '</td>'		
		text += '</tr>'
		
		if (PageInterface.GetValue("state") == psNormal){
			this.addActiveX(i, stepArr[2]);
		};		
	};
	
	if (PageInterface.GetValue("state") != psDesigning) {
		var vis = 'hidden';
	}else{
		var vis = 'inherit';
	}
	
	text += '<tr>';
	text += '<td colspan="3" align="center" valign="middle">';
	text += '<span id="'+this.id+'_textButtons" style="visibility: '+vis+';border: '+defStyle+'; background: '+this.textInfoBckgColor+'; font-family:Arial; font-size: 13px; font-color: #000000;">';
	text += this.textAboveButtons;
	text += '</span>';
	text += '</td>'		
	text += '</tr>'	
	
	text += '</table>';	
	this.text = text;
	this._setHTMLDef(this.text);

	var perArr = this.personsName.split('|');
	
	var buttonsWidth = parseInt(eval(personWidth+20)*(perArr.length-1));
	var buttonsSpace = parseInt(buttonsWidth + 10*(perArr.length-2));
	
	var tempX = this.x + parseInt(this.w/2) - parseInt(buttonsSpace/2);
	
	this.addPersonsButtons(tempX, personWidth, personHeight, perArr)
	
	if (PageInterface.GetValue("state") == psNormal){
		this.addRecordButtons();
		this.addAudioButtons();
	};
	this.setFilesIntoActiveX();
};

componentSayingDialogue.prototype.setFilesIntoActiveX = function()
{
	if (PageInterface.GetValue("state") != psDesigning) {
		for (var i=0; i < this.activeX.length; i++) {
			this.activeX[i].onPaint();
			var activeX = eval('document.'+this.id+'activeX'+i+'_obj');
			if (activeX)
				activeX.WavPattern = this.resourcesURL + this.activeX[i].audioFile;
		};	
	};	
};

componentSayingDialogue.prototype.addActiveX = function(ind, text)
{
	var activeXElem = new __componentText;
	// var activeXElem = new componentElement;
	activeXElem.parentText = this; 
	activeXElem.id = this.id+'activeX'+ind;

	activeXElem.x = 0;
	activeXElem.y = 0;
	activeXElem.w = 1;
	activeXElem.h = 1;
	
	activeXElem.text = this.getActiveXHTMLDef(ind, text);			
	activeXElem.index = ind;			
	activeXElem.audioFile = text;
	//activeXElem.getHTMLDef = this.getActiveXHTMLDef;
	activeXElem.onCreate = this.onCreateActiveX;
	activeXElem.onPaint = this.onPaintActiveX;
	activeXElem.specificRemove = this.__specificRemove;
	
	page.addChild(activeXElem);	
	this.activeX.push( activeXElem );
	
	this.audiosArr.push( text );
};

componentSayingDialogue.prototype.addPersonsButtons = function(X, buttonW, buttonH, arr)
{
	for (var i=0; i < arr.length-1; i++){
		var button = new __componentText;
		button.myParent = this; 
		button.id = this.id+'_button_'+i;
		button.x = X;
		button.y = this.y + this.h - eval(buttonH+20);
		button.w = eval(buttonW+20);
		button.h = eval(buttonH+10);
		button.step = 0;		
		button.text = arr[i];
		button.align = "center";
		button.valign = "middle";		
		button.font.family = 'Arial';
		button.font.size = '2';
		button.font.color = "*#000000"; 			
		button.borderWidth = 1;
		button.bgColor = this.buttonBckgColor;
		button.borderColor = this.textBorderColor;
		button.cursor = 'hand';
		
		button.onCreate = this.onCreateText;
		button.captureMouseEvents = this.captureMouseEvents;
		button.eventMouseDown = this.onMouseClickText;
		
		page.addChild(button);
		this.buttons.push( button);	
		
		X += button.w + 10;
	};
};

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

componentSayingDialogue.prototype.onMouseClickText = function()
{
	this.myParent.choosenPerson = this.text;
	this.myParent.showHidePersonsButtons('hidden');
	this.myParent.audioNumer = -1;
	this.myParent.componentState = 1;
	this.myParent.playSentence();
};

componentSayingDialogue.prototype.addAudioButtons = function()
{
	var oneThird = parseInt(this.w/3);
	
	for (var i=0; i < 3; i++){
		var button = new componentImage;
		button.myParent = this; 
		
		button.captureMouseEvents = this.captureMouseEvents;
		
		switch (i) {
		   case 0 :
					button.id = this.id+'_play_recorded';
					button.w = this.srcPlayRecordedW;
					button.h = this.srcPlayRecordedH;
					button.x = this.x + oneThird - button.w;
					button.src = this.srcPlayRecorded;
					button.eventMouseDown = this.onMouseClickAudioPlayRecorded;
				break;
		   case 1 :
					button.id = this.id+'_play_pattern';
					button.w = this.srcPlayPatternW;
					button.h = this.srcPlayPatternH;				
					button.x = this.x + oneThird + parseInt(oneThird/2) - parseInt(button.w/2);
					button.src = this.srcPlayPattern;
					button.eventMouseDown = this.onMouseClickAudioPlayPattern;
				break;
		   default :
					button.id = this.id+'_record_again';
					button.w = this.srcRecordAgainW;
					button.h = this.srcRecordAgainH;				
					button.x = this.x + oneThird*2;
					button.src = this.srcRecordAgain;
					button.eventMouseDown = this.choseAgainPerson;		   
				break;
		};
		
		button.y = this.y + this.h - eval(button.h+20);
		button.cursor = 'hand';
		
		page.addChild(button);
		this.audioButtons.push( button);	
	};	
};

componentSayingDialogue.prototype.addRecordButtons = function()
{
	for (var i=0; i < 2; i++){
		var button = new componentImage;
		button.myParent = this; 
		
		button.captureMouseEvents = this.captureMouseEvents;
		
		if (i == 0){
			button.id = this.id+'_record_start';
			button.w = this.srcStartRecordW;
			button.h = this.srcStartRecordH;				
			button.x = this.x + parseInt(this.w/2) - 10 - button.w;
			button.src = this.srcStartRecord;
			button.eventMouseDown = this.onMouseClickStartRecord;		
		}else{
			button.id = this.id+'_record_stop';
			button.w = this.srcStopRecordW;
			button.h = this.srcStopRecordH;					
			button.x = this.x + parseInt(this.w/2) + 10;
			button.src = this.srcStopRecord;
			button.eventMouseDown = this.onMouseClickStopRecord;		
		};
		button.y = this.y + this.h - eval(button.h+20);
		button.cursor = 'hand';	
	
		page.addChild(button);
		this.recButtons.push( button);	
	};	
};

componentSayingDialogue.prototype.isReady = function()
{
	if (PageInterface.GetValue("state") == psNormal){
		var isLoaded = false;
		for (var i=0; i < this.recButtons.length; i++)
		{
			var isLoaded = this.recButtons[i].isReady();
			if (!isLoaded) return false;
		};	
		for (var i=0; i < this.audioButtons.length; i++)
		{
			var isLoaded = this.audioButtons[i].isReady();
			if (!isLoaded) return false;
		};
		
		this.isLoaded = isLoaded;
		return this.isLoaded;
	}else{
		return true;
	};
};

componentSayingDialogue.prototype.onMouseClickAudioPlayRecorded = function()
{
	this.myParent.componentState = 2;
	this.myParent.hidePlayAudioButtons();
	this.myParent.audioNumer = -1;
	this.myParent.playSentence();	
	this.myParent.buttonsStatus = 'off';
};

componentSayingDialogue.prototype.onMouseClickAudioPlayPattern = function()
{	
	this.myParent.componentState = 0;
	this.myParent.hidePlayAudioButtons();
	this.myParent.audioNumer = -1;
	this.myParent.playSentence();	
	this.myParent.buttonsStatus = 'off';
};

componentSayingDialogue.prototype.choseAgainPerson = function()
{
	this.myParent.hidePlayAudioButtons();
	this.myParent.showHidePersonsButtons('show');
	this.myParent.buttonsStatus = 'choose';
}

componentSayingDialogue.prototype.onMouseClickStartRecord = function()
{
	if (typeof(this.myParent.activeActiveXObj.RecordNew) != 'undefined')
		this.myParent.activeActiveXObj.RecordNew();
	this.myParent.buttonsStatus = 'record';
};

componentSayingDialogue.prototype.onMouseClickStopRecord = function()
{
	if (typeof(this.myParent.activeActiveXObj.StopNew) != 'undefined')
		this.myParent.activeActiveXObj.StopNew();
	this.myParent.hideRecordButtons();
	this.myParent.playSentence();
	this.myParent.buttonsStatus = 'off';
};

componentSayingDialogue.prototype.onCreateActiveX = function(i, file)
{
	this._setHTMLDef(this.text);
}

componentSayingDialogue.prototype.onPaintActiveX = function(i, file)
{
	return;
};

componentSayingDialogue.prototype.getActiveXHTMLDef = function(i, file)
{
	var t = '';	
	t += '<OBJECT id="'+ this.id + 'activeX'+i+'_obj"';
	t += '  classid="clsid:9E05090D-A2E3-4240-990C-2B3FF1097453"';
	t += '  codebase="'+ this.resourcesURL +'axRecorderProj1.ocx#version=1,0,0,315"';
	t += '  width=5 height=5 align=center hspace=0 vspace=0 name="'+ this.id + 'activeX'+i+'_obj" >';
	t += '<param name="WavPattern" value="">';		
	t += '<param name="ResizeForm" value="">';		
	t += '</OBJECT>';
	
	return t;
};

componentSayingDialogue.prototype.onPaint = function(obj) 
{
	if (PageInterface.GetValue("state") != psNormal){
		this.warningText.show();
	}
	this.markSayingSentence(this.audioNumer);
	this.showHideButtons(this.audioNumer);
}

componentSayingDialogue.prototype.playFromBegin = function(obj) 
{
	this.stopDialogue();
	if (this.visible) this.startDialogue();
	var selectedObj = this.markSayingSentence(this.audioNumer);
}

componentSayingDialogue.prototype.onHide = function(obj) 
{
	this.stopDialogue();
}
	
componentSayingDialogue.prototype.onShow = function(obj) 
{
	this.startDialogue();
};

componentSayingDialogue.prototype.stopDialogue = function(number) 
{
	this.timerStop();
	var actieXObj = document.getElementById(this.id + 'activeX'+this.audioNumer+'_obj');
	if (actieXObj){
		actieXObj.StopPattern();
		actieXObj.StopNew();
	};
	this.activeXObj = null;
	this.activeActiveXObj = null;	
	this.audioNumer = 0;	
	this.choosenPerson = '';
	this.buttonsStatus = 'off';		
	this.showHidePersonsButtons('hide');
	this.hideRecordButtons();
	this.audioNumer = 0;
}

componentSayingDialogue.prototype.startDialogue = function(number) 
{
	var actieXObj = document.getElementById(this.id + 'activeX'+this.audioNumer+'_obj');

	if (actieXObj){
		this.playPattern(actieXObj);
		this.activeActiveXObj = actieXObj;
		this.iTimerID = setTimeout('pc.'+this.id+'.playSentence();', 500);
	}else{
		this.iTimerID = setTimeout('pc.'+this.id+'.onPaint();', 1000);
	};
}

componentSayingDialogue.prototype.showHideButtons = function(number) 
{
	switch (this.buttonsStatus){
		case 'choose' : 
				this.hideObjectsFromArr(this.recButtons);
				this.hideObjectsFromArr(this.audioButtons);
			break;
		case 'record' : 
				this.hideObjectsFromArr(this.audioButtons);
				this.hideObjectsFromArr(this.buttons);
			break;		
		case 'play' : 
				this.hideObjectsFromArr(this.recButtons);
				this.hideObjectsFromArr(this.buttons);
			break;	
		default : 
				this.hideObjectsFromArr(this.recButtons);
				this.hideObjectsFromArr(this.audioButtons);
				this.hideObjectsFromArr(this.buttons);
			break;
	};
};

componentSayingDialogue.prototype.hideObjectsFromArr = function(arr) 
{
	for (var i=0; i < arr.length; i++)
	{
		arr[i].hide();
	};
}

componentSayingDialogue.prototype.__specificRemove = function() 
{
	var obj = document.getElementById(this.id+'_obj');
	if (obj) {
		if (typeof(obj.DoUnload) != 'undefined')
			obj.DoUnload();
	};	
};

componentSayingDialogue.prototype.playSentence = function(obj)
{
	if (this.iTimerID == null) return;

	var isPlaying = false;	
	if (this.activeActiveXObj.GetWav1Status == 1 || this.activeActiveXObj.GetWav1Status == 2){
		isPlaying = true;
	}else if (this.activeActiveXObj.GetWav2Status == 1 || this.activeActiveXObj.GetWav2Status == 2){
		isPlaying = true;
	};
	
	if (!isPlaying){
		this.audioNumer++;
		
		var actieXObj = document.getElementById(this.id + 'activeX'+this.audioNumer+'_obj');
		if (actieXObj){
			var selectedObj = this.markSayingSentence(this.audioNumer);
			person = selectedObj.getAttribute("sentFor");
			
			this.activeActiveXObj = actieXObj;
			
			if (person == this.choosenPerson){
				if (this.componentState == 1){
					this.showRecordButtons();
				}else if (this.componentState == 2){
					this.playNew(actieXObj);
					this.iTimerID = setTimeout('pc.'+this.id+'.playSentence();', 500);
				}else{
					this.playPattern(actieXObj);
					this.iTimerID = setTimeout('pc.'+this.id+'.playSentence();', 500);
				};
			}else{
				this.playPattern(actieXObj);
				this.iTimerID = setTimeout('pc.'+this.id+'.playSentence();', 500);
			};
		};
		if (this.audioNumer+1 > this.maxSentenceToSay) {
			if (this.choosenPerson == ''){
				this.showHidePersonsButtons('show');
			}else{
				this.markSayingSentence(-1);
				this.showPlayAudioButtons();
			};
		};
	}else{
		this.iTimerID = setTimeout('pc.'+this.id+'.playSentence()', 500);
	};
};

componentSayingDialogue.prototype.showHidePersonsButtons = function( visi ) 
{
	for (var i=0; i < this.buttons.length; i++) {
		if (visi == 'show'){
			this.buttons[i].show();
			this.buttons[i].setBorder(this.textBorderColor, 1);
		}else
			this.buttons[i].hide();
	};			
	var infoText = document.getElementById(this.id+'_textButtons');
	if (infoText) {
		if (visi == 'show'){
			infoText.style.visibility = 'inherit';
			this.buttonsStatus = 'choose';	
		}else{
			infoText.style.visibility = 'hidden';
			this.buttonsStatus = 'off';	
		}
	};
	this.markSayingSentence(-1);
};

componentSayingDialogue.prototype.showPlayAudioButtons = function() 
{
	for (var i=0; i < this.audioButtons.length; i++) {
		this.audioButtons[i].show();
	};
	this.buttonsStatus = 'play';
};

componentSayingDialogue.prototype.hidePlayAudioButtons = function() 
{
	for (var i=0; i < this.audioButtons.length; i++) {
		this.audioButtons[i].hide();
	};		
	this.buttonsStatus = 'off';	
};

componentSayingDialogue.prototype.showRecordButtons = function() 
{
	for (var i=0; i < this.recButtons.length; i++) {
		this.recButtons[i].show();
	};			
	this.buttonsStatus = 'record';	
};

componentSayingDialogue.prototype.hideRecordButtons = function() 
{
	for (var i=0; i < this.recButtons.length; i++) {
		this.recButtons[i].hide();
	};			
	this.buttonsStatus = 'off';	
};

componentSayingDialogue.prototype.markSayingSentence = function(markID) {
	var sentencesArr = this.content.split('||');
	var obj = null;
	
	for (var i=0; i < sentencesArr.length; i++){
		var sentence = document.getElementById(this.id+'_TD_'+i);
		if (i == markID){
			sentence.style.background = this.sayingSentenceColor;
			obj = sentence;
		}else{
			sentence.style.background = this.sentenceColor;
		};
	};
	return obj;
};

componentSayingDialogue.prototype.playPattern = function(obj)
{
	if (obj) {
		if (typeof(obj.PlayPattern) != 'undefined')
			obj.PlayPattern();
		return true;
	};		
	return false;
};

componentSayingDialogue.prototype.playNew = function(obj)
{
	if (obj) {
		if (typeof(obj.PlayNew) != 'undefined')
			obj.PlayNew();
		return true;
	};		
	return false;	
};

componentSayingDialogue.prototype.timerStop = function() 
{
	if (this.iTimerID != null) {
		window.clearTimeout(this.iTimerID);
		this.iTimerID = null;
	}
}

componentSayingDialogue.prototype.onFree = function() 
{
	this.timerStop();
}

