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

componentAudioOnOff = function(name)
{
	this.__componentImage = __componentImage;
	this.__componentImage(name);
	this.buttonState = 1;
	this.multiClickEventBlock = false;
};

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

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

componentAudioOnOff.prototype.componentAudioOnOffBaseOnCreate = componentAudioOnOff.prototype.onCreate;
componentAudioOnOff.prototype.onCreate = function(obj)
{
	this.componentAudioOnOffBaseOnCreate();
	this.resourcesURL = unescape(this.resourcesURL);
	this.imagesNameArr = [this.step1Image, this.step2Image, this.step3Image, this.step1ImageON, this.step2ImageON, this.step3ImageON];
	this.getPreloadImagesArr();
	this.fixOnClick();
};

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

componentAudioOnOff.prototype.onLoad = function()
{
	/* //pg2-audioOnOff_new-removed
	for (var i = 1; i < 4; i++) {
		try {
			if (eval("this.step" + i) && (eval(eval("this.step" + i + "Condition")) || eval("this.step" + i + "Condition") == "")) {
				this.buttonState = i;
				break;
			};
		} catch(e) { //pg2
			break;
		};
	}; */
	/* //pg2-audioOnOff_new-removed
	try {
		if (!this.step3 && this.buttonState != 2 || this.buttonState == 1) {
			eval(eval("this.step3Action"));
		};
	} catch(e) {}; */
	this.setImage(this.buttonState);
	this.updatePageElementsState(); //pg2-audioOnOff_new
};

componentAudioOnOff.prototype.update = function()
{
	/* //pg2-audioOnOff_new-removed
	for (var i = 1; i < 4; i++) {
		try {
			if (eval("this.step" + i) && (eval(eval("this.step" + i + "Condition")) || eval("this.step" + i + "Condition") == "")) {
				this.buttonState = i;
				break;
			};
		} catch(e) { //pg2
			break;
		};
	};*/
	/* //pg2-audioOnOff_new-removed
	try {
		if (!this.step3 && this.buttonState != 2 || this.buttonState == 1) {
			eval(eval("this.step3Action"));
		}
	} catch(e) {}; */
	this.setImage(this.buttonState);
	this.updatePageElementsState(); //pg2-audioOnOff_new
};

componentAudioOnOff.prototype.changeOn = function(buttonState)
{
	var image = document.getElementById(this.id + '_img');
	var imageSrc = eval("this.step" + buttonState + "ImageON");
	this.setValue(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);
};

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

componentAudioOnOff.prototype.eventClick = function(e){
	if (PageInterface.GetValue("state") != psDesigning) {
		if (is.ie) {
			if(this.multiClickEventBlock) return true;
			this.multiClickEventBlock = true;
			setTimeout("pc."+this.id+".multiClickEventBlock=false;",200);
		};
		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;

		//pg2-audioOnOff_new-removed, has caused errors
		//try {
			//while (!eval(checkedState) || (!eval(eval(checkedState + "Condition")) && eval(checkedState + "Condition") != "")) {
			//	if (i < 3) i++; else i = 1;
			//	checkedState = "this.step" + i;
			//};
		//} catch(e) {};

		this.buttonState = i;
		this.setImage(this.buttonState);

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

			//pg2-audioOnOff_new -change icon of page 'componentAudioOnOff' elements and within template too, also - allow flash components to play audio
			var naviFrame = p.findFrame('navigation', top); 
			if (naviFrame) {
				if (typeof(naviFrame.audioBtnMouse) == 'function') {
					try {
						naviFrame.audioBtnMouse('audioBtn', 'out');
					} catch(e) {};
				};
			};
			this.updatePageElementsState();
		};
	};
};

componentAudioOnOff.prototype.updatePageElementsState = function() //pg2-audioOnOff_new
{
	try {
		for (var i = 0; i < page.children.length; i++) {
			var o = page.children[i];
			if (o) {
				if (o.type == 'componentAudioOnOff' && o.id != this.id) {
					o.setImage(this.buttonState);
				} else
				if (o.type == 'componentFlash' || o.type == 'componentSimpleFlash') {
					var fp = document.getElementById(o.id + 'flashPlayer');
					if (fp) {
						try {
							if (this.buttonState == 1) {
								fp.flaFunctionID('turnOnSound');
							} else
							if (this.buttonState == 2 || this.buttonState == 3) {
								fp.flaFunctionID('turnOffSound');
							};
						} catch(e) {};
					};
				};
			};
		};
	} catch(e) {};
};

componentAudioOnOff.prototype.setImage = function(buttonState)
{
	if (buttonState-5 == 1) { //pg2-bsh -invoke from template
		buttonState = 1;
		this.buttonState = 1;
	} else
	if (buttonState-5 == 2) {
		buttonState = 2;
		this.buttonState = 2;
	} else
	if (buttonState-5 == 3) {
		buttonState = 3;
		this.buttonState = 3;
	} else
	if (lms) { //pg2-TPSA
		mediaStatus = lms.mediaStatus();
		if (mediaStatus == "online") {
			buttonState = 1;
		} else
		if (mediaStatus == "off") {
			if (this.step2Rollover) buttonState = 2; else buttonState = 3;
		} else
		if (mediaStatus == "textOnly") {
			if (this.step3Rollover) buttonState = 3; else buttonState = 2;
		};
		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" alt="'+this.altHint+'" 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" alt="'+this.altHint+'" src="' + this.resourcesURL + image + '" border="0" width="100%" height="100%"/>';
	};
	this.setHTML(s);
	this.fixOnClick();
};

componentAudioOnOff.prototype.fixOnClick = function()
{
	if (is.ie) {
		this.contentObj.onclick = function(e) {
			if (!e) var e = window.event;
			pc[this.owner.id].eventClick(e);
		};
	};
};

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