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

componentMarkImagePart = function(name)
{
	//inherited
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.images = new Array();
	this.buttons = new Array();
	
	this.compAudio = null;
	this.warningAudio = null;
	this.partsFoundCounts = 0;
	
	this.componentStep = 0;
	
	this.timer = null;
};

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

componentMarkImagePart.prototype.onCreate = function()
{
	//this.captureMouseEvents();
	if ( PageInterface.GetValue("state") != psDesigning && lms && this.disableOnlineOfflineMix != false ) 
	{
		this.resourcesStatus 	= lms.resourcesStatus();
		this.resourcesURL 		= lms.resourcesURL();
	} else this.resourcesURL = resourcesURL;

	var elemArr = this.content.split('||');
	var audioArr = this.audioSteps.split('||');
	
	if (PageInterface.GetValue("state") == psDesigning) var bgColor = this.borderColorMouseClick;
		else var bgColor = 'transparent';	
	
	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 + 'WarningAudio';
		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.warningAudio = 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 audioElement = new componentAudio;
		audioElement.myParent = this;
		audioElement.id = this.id+'WarningAudio'; 
		audioElement.src = this.beepAudio;
		audioElement.pvWMP64 = false;
		audioElement.pvWMP7up = false;
		audioElement.step = -1;
		audioElement.editorPaint = this.editorPaint;
		this.warningAudio = page.addChild(audioElement);		
		audioElement.allowNextStep = true;	
		
		var audioButtonsArr = new Array(this.audioPlay, this.audioPause, this.audioStop, '_play_', '_pause_', '_stop_', 'Play', 'Pause', 'Stop');
	}
	
	for(var i=0; i < elemArr.length; i++){
		var elemValue = elemArr[i].split('#!#');	
		
		var elemImage = new __componentText;
		elemImage.myParent = this; 
		elemImage.id = this.id+'_IMAGE_'+i; 
		elemImage.x = this.x + parseInt(elemValue[1])+this.elemBorderWidth;
		elemImage.y = this.y + parseInt(elemValue[2])+this.elemBorderWidth;
		var newImg = new Image();
		newImg.src = this.resourcesURL + elemValue[0];
		elemImage.h = newImg.height+2*this.elemBorderWidth;
		elemImage.w = newImg.width+2*this.elemBorderWidth;
		elemImage.padding = 0;
		elemImage.correctClick = false;
		elemImage.borderWidth = this.elemBorderWidth;
		
		elemImage.borderColor = bgColor;
		
		elemImage.text = '<img src="'+this.resourcesURL + elemValue[0]+'" width="'+newImg.width+'" height="'+newImg.height+'">';
		
		elemImage.step = this.getStepNumer(audioArr, elemValue[0]);
		elemImage.onCreate = this.onCreateText;
		elemImage.eventMouseOver = this.onMouseOverImage;
		elemImage.eventMouseOut = this.onMouseOutImage;	
		elemImage.eventMouseDown = this.onMouseClickImage;
		elemImage.eventMouseUp = this.onMouseUpImage;
		
		this.images.push( elemImage );
		var img = page.addChild(elemImage);
	};
	
	var tempX = 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; 
		var imageTest = new Image();
		imageTest.src = this.resourcesURL + audioButtonsArr[i];
		image.w = imageTest.width;
		image.h = imageTest.height;
		image.x = this.x+5+tempX;
		image.y = this.y+this.h-image.h-5;
		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);
		
		tempX +=  image.w + 5;
	};	
};

componentMarkImagePart.prototype.onPaint = function() 
{
	this._getHTMLDef();
}

componentMarkImagePart.prototype._getHTMLDef = function() 
{
	var s = '';
		s += '<img id="' + this.id + '_contentObj" src="' + this.resourcesURL + this.src + '" width="100%" height="100%" border="0" />';
	this.setHTML(s);
}

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

componentMarkImagePart.prototype.getStepNumer = function(arr, image)
{
	for (var i=0; i < arr.length; i++){
		var splited = arr[i].split('#!#');
		if (splited[0] == image) return i;
	};
	return -1;
};

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

componentMarkImagePart.prototype.onMouseOverImage = function() 
{
	if (this.correctClick || this.myParent.locked) return;
	
	this.borderWidth = this.myParent.elemBorderWidth;
	this.borderColor = this.myParent.borderColorMouseOver;
	this.setBorder(this.myParent.borderColorMouseOver, this.myParent.elemBorderWidth);
};

componentMarkImagePart.prototype.onMouseOutImage = function() 
{
	this.borderWidth = this.myParent.elemBorderWidth;
	this.borderColor = 'transparent';
	this.setBorder('transparent', this.myParent.elemBorderWidth);
};

componentMarkImagePart.prototype.onMouseUpImage = function()
{
	if (!this.correctClick) {
		this.eventMouseOver();
	}else{
		this.eventMouseOut();
	}
}

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

componentMarkImagePart.prototype.onMouseClickImage = function() 
{
	if (this.myParent.locked) return;

	if (this.step == this.myParent.componentStep){
		this.myParent.componentStep += 1;
	}else{
		if (this.myParent.playBeepAudio == 1 && this.myParent.componentStep < this.myParent.images.length){
			if (this.myParent.enableFlashPlayer) {
				this.myParent.warningAudio.flashVars = "playIdx=0&autoStart=true&playOnce=true";
				this.myParent.warningAudio.show();
				var time = 1000;
			} else {
				this.myParent.warningAudio.actionMediaPlay();
				if (this.myParent.replayAudio) {
					var player = document.getElementById(this.myParent.compAudio.id + "MediaPlayer");
					var time = parseInt(parseFloat(player.Duration) * 1000);
				};				
			};			
			this.myParent.timer = window.setTimeout('pc.' + this.myParent.id + '.replayMedia();', time);
		}
		return;
	};
	
	this.borderWidth = this.myParent.elemBorderWidth;
	this.borderColor = this.myParent.borderColorMouseClick;
	this.setBorder(this.myParent.borderColorMouseClick, this.myParent.elemBorderWidth);	
	
	this.myParent.loadNextAudio(this.step);
	this.correctClick = true;
	
	if (this.myParent.playAudioAfterClick) {
		if (this.myParent.enableFlashPlayer) {
			var flVars = this.myParent.compAudio.flashVars;
			//this.compAudio.flashVars = "playIdx=0&autoStart=true&playOnce=true";
			if (flVars.indexOf('playIdx=-1') == -1){
				this.myParent.compAudio.show();			
			}else{
				this.myParent.compAudio.flashVars = "";
				this.myParent.compAudio.show();			
			}
		} else {
			this.myParent.compAudio.actionMediaPlay();
		};		
	};
};

componentMarkImagePart.prototype.getAudioIndex = function(audioFile)
{
	var result = -1;
	var contentArr = this.audioSteps.split('||');	
	for(var i=0; i < contentArr.length; i++){
		var oneArr = contentArr[i].split('#!#');
		if (oneArr[1] == audioFile) {
			result = i+1;
			break;
		}
	}
	return result;
}

componentMarkImagePart.prototype.loadAudioToFlash = function(lastStep)
{
	this.compAudio.flashVars = "playIdx="+lastStep+"&autoStart=true&playOnce=true"
}

componentMarkImagePart.prototype.loadNextAudio = function(lastStep)
{
	var audioArr = this.audioSteps.split('||');
	if (this.enableFlashPlayer) {
		if (++lastStep < audioArr.length){
			var oneArr = audioArr[lastStep].split('#!#');
			this.loadAudioToFlash(this.getAudioIndex(oneArr[1]));
		}else{
			this.loadAudioToFlash(-1);
		};
		this.partsFoundCounts++;
	}else{
		var res = false;
		var player = document.getElementById(this.id+'AudioMediaPlayer');
		if (player) {			
			if (++lastStep < audioArr.length){
				var oneArr = audioArr[lastStep].split('#!#');
				player.url = this.resourcesURL + oneArr[1];
			}else{
				player.url = this.resourcesURL;	
			};
			this.partsFoundCounts++;
		};
	}
};

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

componentMarkImagePart.prototype.parseCheckAnswer = function()
{
	var audioArr = this.audioSteps.split('||');
	return (this.partsFoundCounts >= audioArr.length);
};

componentMarkImagePart.prototype.onMouseClickImagePlay = function()
{
	if (this.myParent.enableFlashPlayer) {
		this.myParent.compAudio.show();
	} else {
		this.myParent.compAudio.actionMediaPlay();
	};	
};

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

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

componentMarkImagePart.prototype.onShow = function() 
{
	for (var i=0; i < this.images.length; i++) {
		this.images[i].show();
	};
	for (var i=0; i < this.buttons.length; i++) {
		this.buttons[i].show();
	};		
	var audioArr = this.audioSteps.split('||');
	var oneArr = audioArr[0].split('#!#');
	
	if (this.enableFlashPlayer) {
		this.loadAudioToFlash(this.getAudioIndex(oneArr[1]));
	}else{
		var player = document.getElementById(this.id+'AudioMediaPlayer');
		if (player) {
			player.url = this.resourcesURL + oneArr[1];
			this.componentStep = 0;
		};
	}
};

componentMarkImagePart.prototype.onFree = function()
{
	window.clearTimeout(this.timer);
};
