/*
 * Copyright (C) 1995-2014 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */ 
componentFourStatesButton = function(name)
{
	//inherited
	this.__componentImage = __componentImage;
	this.__componentImage(name);
	this.buttonState = 1;
};

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

componentFourStatesButton.prototype.onBaseCreate = function(obj)
{
    this.resourcesURL = unescape(this.resourcesURL);

	this.captureMouseEvents();
};

componentFourStatesButton.prototype.componentFourStatesButtonBaseEventMouseOver = componentFourStatesButton.prototype.eventMouseOver;
componentFourStatesButton.prototype.eventMouseOver = function(e)
{
	try {
		var step = eval("this.step"+this.buttonState+"Rollover");
		if (step) {
			var overSrc = eval("this.step" + this.buttonState + "ImageON");
			if (overSrc) this.setValue(resourcesURL + overSrc);
		};
	} catch(e) {
		if (typeof(page) != "undefined" && page.state == psView) alert(this.id + '.eventMouseOver() eval execution error\nMessage:\n' + e.message); //pg2
	};
	return this.componentFourStatesButtonBaseEventMouseOver(e);
};

componentFourStatesButton.prototype.componentFourStatesButtonBaseEventMouseOut = componentFourStatesButton.prototype.eventMouseOut;
componentFourStatesButton.prototype.eventMouseOut = function(e)
{
	try {
		var standardSrc = eval("this.step" + this.buttonState + "Image");
		if (standardSrc) this.setValue(resourcesURL + standardSrc);
	} catch(e) {
		if (typeof(page) != "undefined" && page.state == psView) alert(this.id + '.eventMouseOut() eval execution error\nMessage:\n' + e.message); //pg2
	};
	return this.componentFourStatesButtonBaseEventMouseOut(e);
};

componentFourStatesButton.prototype.onLoad = function()
{
	try {
		for (var i = 1; i < 5; i++)	{
			if (eval(eval("this.step"+i+"Condition")) || eval("this.step"+i+"Condition") == "")	{
				this.buttonState = i;
				break;
			};
		};
		var overSrc = eval("this.step" + this.buttonState + "Image");
		this.setValue(resourcesURL + overSrc);
	} catch(e) {
		if (typeof(page) != "undefined" && page.state == psView) alert(this.id + '.onLoad() eval execution error\nMessage:\n' + e.message); //pg2
	};
};

componentFourStatesButton.prototype.onPaint = function(e)
{
	if (PageInterface.GetValue("state") == psDesigning) {
		var overSrc = eval("this.step1Image");
		this.setValue(resourcesURL + overSrc);
	};
};

componentFourStatesButton.prototype.componentFourStatesButtonBaseEventClick = componentFourStatesButton.prototype.eventClick;
componentFourStatesButton.prototype.eventClick = function(e)
{
	if (page.type != psDesigning) {
		var allowExecute = false; //execute action only if the step is enabled
		try {
			var str = "allowExecute = (this.step" + this.buttonState + " == true);"; //dynamically check if the stepN is enabled
			eval(str);
		} catch(e) {
			allowExecute = false;
		};
		try	{
			if (allowExecute) eval(eval("this.step" + this.buttonState + "Action")); //pg2-conditional
		} catch (e) {
			if (page.state != 3){
				showMessage("Invalid command. Please check the script on the step number " + this.buttonState + ".");
			} else {
				showMessage("This script work property after export.");
			};
		};

		try {
			if (this.buttonState < 4) var i = this.buttonState + 1; else var i = 1;

			var checkedState = "this.step"+i;
			while ( !eval(checkedState) || (!eval(eval(checkedState+"Condition")) && eval(checkedState+"Condition") != "") ) {
				if (i < 4) i++;
				else i = 1;
				checkedState = "this.step" + i;
			};

			this.buttonState = i;

			var step = eval("this.step"+this.buttonState+"Rollover");
			if (step) {
				var overSrc = eval("this.step" + this.buttonState + "ImageON");
				if (overSrc) this.setValue(resourcesURL + overSrc);
			} else {
				var overSrc = eval("this.step" + this.buttonState + "Image");
				this.setValue(resourcesURL + overSrc);
			};
		} catch(e) {
			//alert('ERROR: ' + e.message); //pg2-removed
			if (typeof(page) != "undefined" && page.state == psView) alert(this.id + '.eventClick() eval execution error\nMessage:\n' + e.message); //pg2
		};
	};
	return this.componentFourStatesButtonBaseEventClick(e);
};

componentFourStatesButton.prototype.setState = function(newState) //pg2-new, to allow do it via scripts on page, not necessarily in ascending (1by1) order
{
	if (newState < 1 || newState > 4) return;
	this.buttonState = newState;

	try {
		var step = eval("this.step" + this.buttonState + "Rollover");
		if (step) {
			var overSrc = eval("this.step" + this.buttonState + "ImageON");
			if (overSrc) this.setValue(resourcesURL + overSrc);
		} else {
			var overSrc = eval("this.step" + this.buttonState + "Image");
			this.setValue(resourcesURL + overSrc);
		};
	} catch(e) {
		if (typeof(page) != "undefined" && page.state == psView) alert(this.id + '.setState() eval execution error\nMessage:\n' + e.message); //pg2
	};

	var allowExecute = false; //execute action only if the step is enabled
	try {
		var str = "allowExecute = (this.step" + this.buttonState + " == true);"; //dynamically check if the stepN is enabled
		eval(str);
	} catch(e) {
		allowExecute = false;
	};

	try	{
		if (allowExecute) eval(eval("this.step" + this.buttonState + "Action"));
	} catch (e) {
		if (page.state != 3){
			showMessage("Invalid command. Please check the script on the step number " + this.buttonState + ".");
		} else {
			showMessage("This script work property after export.");
		};
	};
};

componentFourStatesButton.prototype.componentFourStatesButtonResetElement = componentFourStatesButton.prototype.resetElement;
componentFourStatesButton.prototype.resetElement = function () 
{
	this.componentFourStatesButtonResetElement();

	try {
		for (var i = 1; i < 5; i++)	{
			if (eval(eval("this.step"+i+"Condition")) || eval("this.step"+i+"Condition") == "")	{
				this.buttonState = i;
				break;
			};
		};
		var overSrc = eval("this.step" + this.buttonState + "Image");
		this.setValue(resourcesURL + overSrc);
	} catch(e) {
		if (typeof(page) != "undefined" && page.state == psView) alert(this.id + '.setState() eval execution error\nMessage:\n' + e.message); //pg2
	};
};
