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

componentMatchDotsAudio = function(name) 
{
	this.componentMatchDots = componentMatchDots;
	this.componentMatchDots(name);
	
	this.spaceBetween = 0;
	this.inArea = null;
	
	this.buttons = new Array();	
	this.textsLeft = new Array();	
	this.textsRight = new Array();	
	
	this.imagesLeft = new Array();	
	this.imagesRight = new Array();	
	
	this.workArea = null;
	
	this.compAudio = null;
	
	this.startPoint = null;
	this.buttonH = 0;
	
	this.interactions = true;
	this.interactionsType = "matching";	
	this.imagesNameArr = new Array();	
};

componentMatchDotsAudio.prototype = new componentMatchDots;
componentMatchDotsAudio.prototype.type = "componentMatchDotsAudio";

componentMatchDotsAudio.prototype.onBaseCreate = function(obj) 
{
	this.resourcesStatus = "online";
	this.resourcesURL = resourcesURL;
	if (PageInterface.GetValue("state") != psDesigning && lms && lms.name != "lmsOffline") {
		this.resourcesStatus = lms.resourcesStatus();
		if (this.disableOnlineOfflineMix) this.resourcesURL = resourcesURL;
		else this.resourcesURL = lms.resourcesURL();
	}
	this.resourcesURL = unescape(this.resourcesURL);
	
	this.imagesNameArr = [	this.dotOff, this.dotOn, this.dotCorrect, this.dotIncorrect, this.audioPlay, this.audioStop, this.audioPause ];
	this.getPreloadImagesArr();
};

componentMatchDotsAudio.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("||");
		
	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;	
	
	this.drawAudioButtons();
	
	var fontSize = this.getFontSize(this.fontSize, 'px');
	
	var newImg = new Image();
	newImg.src = this.resourcesURL +  this.dotOff;	
	
	var textWidthHeightArr = this.getMaxWidthHeight(contentArr, this.fontFamily, fontSize);
	this.buttonH = getImageHeight(this.audioPlay) + 4;
	var oneRowHeight = (this.h-this.buttonH-10*(contentArr.length-1))/contentArr.length;
	
	this.spaceBetween = parseInt((this.h -this.buttonH- oneRowHeight*contentArr.length)/(contentArr.length-1));
	
	var tempX = this.x + 2;
	var tempY = this.y + this.buttonH;
	
	var maxFreeSpace = this.w - textWidthHeightArr[0]*2 - 4;	
	
	var drawArea = new __componentText;
	drawArea.myParent = this; 
	drawArea.id = this.id+'_WorkAreaDIV'; 
	drawArea.x = tempX + textWidthHeightArr[0];
	drawArea.y = tempY;
	drawArea.h = this.h;
	drawArea.w = maxFreeSpace;
	drawArea.text = '<div id="'+this.id+'_WorkArea" style="position: relative; width:100%; height:100%" onmousemove="pc.'+this.id+'.mouseMoveWorkArea();" ><img src="'+this.resourcesURL+'__.hotspot.gif" width="'+maxFreeSpace+'px" height="'+this.h+'px" /></div>';
	drawArea.padding = 0;
	drawArea.onRepaint = this.__onRepaint;
	drawArea.baseOnPaint = drawArea.onPaint;	
	drawArea.onPaint = this.__onPaintWorkArea;		
	
	page.addChild(drawArea);
	this.workArea = drawArea;
	
	for(var i=0; i < contentArr.length; i++){
		var groupArr = contentArr[i].split('#!#');
		for(var j=0; j < groupArr.length; j++){
			var elementText = new __componentText;
			elementText.myParent = this; 
			elementText.id = this.id+'_TEXT_'+j+':'+i; 
			elementText.x = tempX + (this.w - textWidthHeightArr[0] - 4)*j;
			elementText.y = tempY;
			elementText.h = oneRowHeight;

			elementText.w = textWidthHeightArr[0];
			elementText.borderWidth = 1;
			elementText.padding = 0;
			elementText.borderColor = this.tableBorderColor;
			elementText.bgColor = this.headerBckColor;
			elementText.font.size = this.fontSize;
			elementText.font.family = this.fontFamily;
			elementText.font.color = this.fontColor;
			elementText.align = this.align;
			elementText.valign = 'middle';
			elementText.text = groupArr[j];
			elementText.pair = 'pair_'+i;
			page.addChild(elementText);		
			if (j % 2){			
				this.textsRight.push( elementText ); 
			}else{
				this.textsLeft.push( elementText ); 
			};
			
			var compRollover = new componentRollover;
			compRollover.myParent = this; 
			compRollover.id = this.id+'_IMAGE_'+i+':'+j; 
			compRollover.h = this.dotH;
			compRollover.w = this.dotW;			
			if (j % 2){			
				compRollover.x = tempX + textWidthHeightArr[0] + maxFreeSpace - compRollover.w;
			}else{
				compRollover.x = tempX + textWidthHeightArr[0];
			};			
			compRollover.y = tempY + (oneRowHeight/2-compRollover.h/2);
			compRollover.srcOut = this.dotOff;
			compRollover.srcIn = this.dotOn;
			compRollover.pair = 'pair_'+i;
			compRollover.side = 'side_'+j;
			compRollover.solve = false;
			compRollover.isLocked = false;
			compRollover.correctPointName = (j % 2) ? this.id+'_IMAGE_0:'+i : this.id+'_IMAGE_1:'+i
			compRollover.connectPoint = null;
			compRollover.repainted = false;
			compRollover.cursor = this.elementCursor;			
			compRollover.xArray = [ 0,0,0,0,0 ];
			compRollover.yArray = [ 0,0,0,0,0 ];
			
			compRollover.captureMouseEvents = this.captureMouseEvents;
			compRollover.eventClick = this.clickOnButton;
			
			compRollover.eventMouseOver = this.buttonEventMouseOver;
			compRollover.eventMouseOut = this.buttonEventMouseOut;
			compRollover.baseOnPaint = compRollover.onPaint;
			//compRollover.onPaint = this._onPaint;										
			
			page.addChild(compRollover);	
			if (j % 2){
				this.imagesRight.push( compRollover ); 
			}else{
				this.imagesLeft.push( compRollover ); 
			};			
		};			
		tempY += this.spaceBetween + oneRowHeight - 2;	
	};	

	document.getElementById(this.id+"sizeDiv").innerHTML = '';
};

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

componentMatchDotsAudio.prototype.drawAudioButtons = function()
{
	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);	
		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 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.captureMouseEvents = this.captureMouseEvents;
		var funct = 'this.onMouseClickImage'+audioButtonsArr[i+btnLen*2];
		image.eventClick = eval(funct);
		
		page.addChild(image);
		this.buttons.push(image);
		
		if (image.h > imageHeigth) imageHeigth = image.h;
		tempX +=  image.w + 5;
	};
	return imageHeigth;
};

componentMatchDotsAudio.prototype.onShow = function() 
{
	for (var i=0; i < this.textsRight.length; i++) {
		this.textsLeft[i].show();
		this.textsRight[i].show();
		this.imagesLeft[i].show();
		this.imagesRight[i].show();
	};
	for (var i=0; i < this.buttons.length; i++) {
		this.buttons[i].show();
	};		
	this.workArea.show()
};

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

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

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











