Framework.ActionStateMessage = Class.create({
	
	wrapperElement: null,
	contentElement: null,
	options: null,
	counter: 0,
	
	debug: function(m)
	{
		// debug(m);
	},
	
	initialize: function(options)
	{
		this.debug("new Framework.ActionStateMessage();");
		
		this.options = Object.extend({
			'closeClass': 'closeActionStateMessage'
		}, options);
	},
	
	show: function(type, message, options)
	{
		this.debug("Framework.ActionStateMessage.show()");
		this.debug(type);
		this.debug(message);
		this.debug(options);
		
		options = Object.extend({
			time: 3,
			sticky: false,
			width: 450
		}, options);
		
		var closeButton =  "<img src=\"" + Framework.Configuration.get('staticURL') + "/s/i/wrapper/blank.gif\" class=\"icon iconClose " + this.options.closeClass + "\" alt=\"\" />";
		this._setContent(closeButton + message);
		
		this.debug('actionstatemessage options:');
		this.debug(options);
		
		if (options.onShowFunction) 
		{
			options.onShowFunction(this.wrapperElement, options);
		}
		
		var classType = type.charAt(0).toUpperCase() + type.substr(1).toLowerCase();
		this.divElement.setClassData('actionStateMessage', classType, { glue: "" });
		this.divElement.setStyle({ 'width': options.width + 'px' });
		this.contentElement.setStyle({ 'width': (options.width - 130) + 'px' });
		this.wrapperElement.show();
		
		// to lock
		this.counter = this.counter + 1;
		var counter = this.counter;
		
		if (!options.sticky)
		{
			this.setHideTimeout(options, counter);
		}
		
		// On hover: pause timer, cancel animation + reinit timer when leaving
		
		this.wrapperElement.observe('mouseenter', function () {
			window.clearTimeout(this.timer);
		}.bind(this));
		
		this.wrapperElement.observe('mouseleave', function () {
			if (!options.sticky)
			{
				this.setHideTimeout(options, counter);
			}
		}.bind(this));
		
		this.wrapperElement.observe('mouseover', function () {
			window.clearTimeout(this.timer);
			if (this.effect)
			{
				this.effect.cancel();
			}
			this.wrapperElement.show();
			this.wrapperElement.setStyle({ opacity: 1 });
		}.bind(this));
	},
	
	setHideTimeout: function(options, counter)
	{
		if (this.timer)
		{
			window.clearTimeout(this.timer);
		}
		
		this.timer = window.setTimeout((function() {
			this.hide({ 
				effect: true, 
				counter: counter 
			});
		}).bind(this), (options.time * 1000));
	},
	
	onClick: function(event)
	{
		if (event.findElement('img.' + this.options.closeClass))
		{
			this.hide();
			event.stop();
		}
	},

	hide: function(options)
	{
		this.debug("Framework.ActionStateMessage.hide()");
		
		options = Object.extend({
			effect: false,
			effectDuration: 2,
			counter: undefined
		}, options);
		
		if (typeof options.counter !== "undefined")
		{
			if (options.counter !== this.counter)
			{
				this.debug("Framework.ActionStateMessage.hide() cancelled, because new message shown.");
				return false;
			}
		}
		if (options.effect && Effect)
		{
			this.effect = new Effect.Fade(this.wrapperElement, { 
				duration: options.effectDuration,
				afterFinish: (function() {
//					this._setContent("");
				}).bind(this)
			});
		}
		else
		{
			if (this.wrapperElement)
			{
				this.wrapperElement.hide();
			}
			
//			this._setContent("");
		}
	},
	
	hideStatusMessages: function()
	{
		$$('.statusMessage').invoke('remove');
	},
	
	_setContent: function(message)
	{
		this.debug("Framework.ActionStateMessage._setContent()");
		
		if (!this.wrapperElement)
		{
			this._createElement();
		}
		
		this.contentElement.update(message); 
	},
	
	_createElement: function()
	{
		this.debug("Framework.ActionStateMessage._createElement()");
		
		if (!this.wrapperElement)
		{
			this.divElement = new Element('div', { 'class' : 'clearfix actionStateMessage' });
			this.contentElement = new Element('div', { 'class' : 'text' });
			this.iconElement = new Element('div', { 'class' : 'actionStateIcon' });
			this.divElement.insert(this.iconElement);
			this.divElement.insert(this.contentElement);
			this.wrapperElement = $('actionStateWrapper');
			
			this.wrapperElement.update(this.divElement);
		
			this.contentElement = $(this.wrapperElement).down('div.text');
			this.wrapperElement.observe('click', this.onClick.bind(this));
		}
	},
	
	_eoo: true
	
});

Framework.ActionStateMessage = new Framework.ActionStateMessage();
