var Ihm_Abstract_Effect = Class.create(
{
	initialize: function (options)
	{
		this.options = {
				screen_containerId: 'systemOverlay'
		};

		Object.extend(this.options, options || {});
	},

	startScreenEffect: function ()
	{
		if($J('#'+this.options.screen_containerId).is(':animated')) {
			$J('#'+this.options.screen_containerId).stop(true, true);
		}

		if(!$(this.options.screen_containerId).visible())
		{
			$(this.options.screen_containerId).setStyle({backgroundColor:this.options.screenEffect_backgroundColor, opacity:0}).show();
			
			$J('#'+this.options.screen_containerId).fadeTo(
					this.options.screenEffect_duration*1000,
					this.options.screenEffect_opacity,
					this.options.screenAction_start_afterFinish || Prototype.emptyFunction
			);
		}
	},

	stopScreenEffect: function ()
	{
		if($J('#'+this.options.screen_containerId).is(':animated')) {
			$J('#'+this.options.screen_containerId).stop(true, true);
		}

		if(!Prototype.Browser.IE)
		{
			if($(this.options.screen_containerId).visible()) {
				$J('#'+this.options.screen_containerId).fadeOut(this.options.screenEffect_duration*1000, this.options.screenAction_stop_afterFinish || Prototype.emptyFunction);
			}
		}
		else {
			$(this.options.screen_containerId).hide();
			(this.options.screenAction_stop_afterFinish || Prototype.emptyFunction)();
		}
	}
}
);

var Ihm_Abstract_Human = Class.create(Ihm_Abstract_Effect,
{
	keyboardActionBindAsEventListener: undefined,


	initialize: function ($super, options)
	{
		$super(options);
	},

	startIhmEffect: function ()
	{
		if($J('#'+this.options.ihm_containerId).is(':animated')) {
			$J('#'+this.options.ihm_containerId).stop(true, true);
		}

		this.centerIhm();

		$(this.options.ihm_containerId).setOpacity(0).show();
		$J('#'+this.options.ihm_containerId).fadeTo(this.options.ihmEffect_duration*1000, this.options.ihmEffect_opacity);
	},

	centerIhm: function ()
	{
		var element = $(this.options.ihm_containerId);
		element.setStyle({marginTop:-(element.getHeight()/2)+'px', marginLeft:-(element.getWidth()/2)+'px'});
	},

	stopIhmEffect: function ()
	{
		if($J('#'+this.options.ihm_containerId).is(':animated')) {
			$J('#'+this.options.ihm_containerId).stop(true, true);
		}

		if(!Prototype.Browser.IE) {
			$J('#'+this.options.ihm_containerId).fadeOut(this.options.ihmEffect_duration*1000);
		}
		else {
			$(this.options.ihm_containerId).hide();
		}
	},

	ihmAction_start_internal_afterFinish: function ()
	{
		var ihmContainerId = $(this.options.ihm_containerId);
		ihmContainerId.setStyle({marginTop:-(ihmContainerId.getHeight()/2)+'px'});
	},

	stopByEvent: function (event)
	{
		var response = (event.element().readAttribute('response') === 'true') ? (true) : (false);
		this.stop(response);
	},
	
	stopByKeyboard: function (response)
	{
		this.stop(response);
	},

	enableKeyboardObserving: function ()
	{
		if(Object.isUndefined(this.keyboardActionBindAsEventListener)) {
			this.keyboardActionBindAsEventListener = this.keyboardAction.bindAsEventListener(this);
			document.observe('keydown', this.keyboardActionBindAsEventListener);
		}
	},

	disableKeyboardObserving: function ()
	{
		if(!Object.isUndefined(this.keyboardActionBindAsEventListener)) {
			document.stopObserving('keydown', this.keyboardActionBindAsEventListener);
			this.keyboardActionBindAsEventListener = undefined;
		}
	},

	keyboardAction: function (event)
	{
		switch(event.keyCode)
		{
			case undefined:
				return;
			case Event.KEY_ESC:
			case event.DOM_VK_ESCAPE:
				var response = false;
				break;
			case Event.KEY_RETURN:
			case event.DOM_VK_ENTER:
				var response = true;
				break;
		}

		if(!Object.isUndefined(response)) {
			event.stop();
			this.stopByKeyboard(response);
		}
	}
}
);
