/*
 * Copyright ďż˝ 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */

componentAudioOnOff = function(name)
{
	//inherited	
	this.__componentImage = __componentImage;
	this.__componentImage(name);
	
	this.buttonState = 1;
};

componentAudioOnOff.prototype = new __componentImage();
componentAudioOnOff.prototype.type = "componentAudioOnOff";

componentAudioOnOff.prototype.componentAudioOnOffBaseOnCreate = componentAudioOnOff.prototype.onCreate;
componentAudioOnOff.prototype.onCreate = function(obj)
{
	this.componentAudioOnOffBaseOnCreate();

	this.captureMouseEvents();

    this.resourcesURL = unescape(this.resourcesURL);
	
	this.imagesNameArr = [ this.step1Image, this.step2Image, this.step3Image, this.step1ImageON, this.step2ImageON, this.step3ImageON ];
	this.getPreloadImagesArr();	
};

componentAudioOnOff.prototype.onPaint = function(obj)
{
	if (PageInterface.GetValue("state") == psDesigning)
	{
		this.setImage(1);
	}
	else
	{
		this.setImage(this.buttonState);
	}
};

componentAudioOnOff.prototype.onLoad = function()
{
	for (var i = 1; i < 4; i++)
	{
		if (eval("this.step"+i) && 
			(eval(eval("this.step"+i+"Condition")) || eval("this.step"+i+"Condition") == ""))
		{
			this.buttonState = i;
			break;
		}
	}
	if (!this.step3 && this.buttonState != 2 || this.buttonState == 1) {
		eval(eval("this.step3Action"));
	}
	this.setImage(this.buttonState);
};

componentAudioOnOff.prototype.update = function()
{
	for (var i = 1; i < 4; i++)
	{
		if (eval("this.step"+i) && 
			(eval(eval("this.step"+i+"Condition")) || eval("this.step"+i+"Condition") == ""))
		{
			this.buttonState = i;
			break;
		}
	}
	if (!this.step3 && this.buttonState != 2 || this.buttonState == 1) {
		eval(eval("this.step3Action"));
	}
	this.setImage(this.buttonState);
};

componentAudioOnOff.prototype.changeOn = function(buttonState)
{
	var image = document.getElementById(this.id + '_img');
	var imageSrc = eval("this.step"+buttonState+"ImageON");
	
	this.setValue(this.resourcesURL + imageSrc);	
	//image.src = this.resourcesURL + imageSrc;
};

componentAudioOnOff.prototype.changeOut = function(buttonState)
{
	var image = document.getElementById(this.id + '_img');
	var imageSrc = eval("this.step"+buttonState+"Image");
	
	this.setValue(this.resourcesURL + imageSrc);	
	//image.src = this.resourcesURL + imageSrc;
};

componentAudioOnOff.prototype.runAudioInThisStep = function(e)
{
	if (this.runStepAudio){
		PageInterface.Execute("runStepAudio");		
	};
};

componentAudioOnOff.prototype.eventClick = function(e)
{
	this.invokeEvent('Click');
	
	if (PageInterface.GetValue("state") != psDesigning)
	{
		try
		{
			if (this.buttonState == 2) this.runAudioInThisStep();
			eval(eval("this.step"+this.buttonState+"Action"));
		}
		catch (e)
		{
			if (PageInterface.GetValue("state") != 3)
			{
				showMessage("Invalid command. Please check the script on the step number " + this.buttonState + ".");
			}else{
				showMessage(this.notify);				
			}
		}
		if (this.buttonState < 3) var i = this.buttonState + 1;
		else var i = 0;
		
		var checkedState = "this.step"+i;
		while (!eval(checkedState) || (!eval(eval(checkedState+"Condition")) && eval(checkedState+"Condition") != ""))
		{
			if (i < 3) i++;
			else i = 1;
			
			checkedState = "this.step"+i;
		}
		this.buttonState = i;
		this.setImage(this.buttonState);

		var p = getPlayer();
		if (p) {
			p.findTypeAndCallFunc(this.type, "update");
		}
	}
};

componentAudioOnOff.prototype.setImage = function(buttonState)
{
	if (lms) { //pg2-TPSA
		mediaStatus = lms.mediaStatus();
		if (mediaStatus == "online") {
			buttonState = 1;
		} else
		if (mediaStatus == "off") {
			buttonState = 2;
			
		} else
		if (mediaStatus == "textOnly") {
			buttonState = 3;
		}
		this.buttonState = buttonState;
	}
	
	var image = eval("this.step"+buttonState+"Image");
	var rolloverState = eval("this.step"+buttonState+"Rollover");

	if (rolloverState) 
	{
		var s =  '<img id="' + this.id + '_contentObj" src="' + this.resourcesURL + image + '" border="0" onMouseOver="page.all[\''+this.id+'\'].changeOn('+buttonState+')" onMouseOut="page.all[\''+this.id+'\'].changeOut('+buttonState+')" width="100%" height="100%"/>';
	}else{
		var s = '<img id="' + this.id + '_contentObj" src="' + this.resourcesURL + image + '" border="0" width="100%" height="100%"/>';	
	}
	this.setHTML(s);
};

componentAudioOnOff.prototype.onUnLoad = function()
{
	if (!this.rememberAudioState){
		lms.setMediaStatus('online');
	}
}
