/**
 * 
 */
var magicFormWithFocus;
FastInit.addOnLoad(function(){
	$$('form.magicform').each(function(el){
		new MagicForm(el);
	});
	
	document.observe('mousemove', function(event){
		var el = event.element();
		var form = el.up('form.magicform');
		if (form == undefined && el.hasClassName('magicform')) {
			form = el;
		}
		if (form) {
			if (magicFormWithFocus != form) {
				form.fire('magicform:onMouseEnter');
			}
			magicFormWithFocus = form;
		} else if (magicFormWithFocus) {
			magicFormWithFocus.fire('magicform:onMouseLeave');
			magicFormWithFocus = null;
		}
	});
});

var MagicForm = Class.create({
	container: null,
	_hasFocus: false,
	initialize: function(container) {
		this.container = container;
		
		var form = this;
		
		this.container.select('input[type="text"], textarea').each(function(el){
			el.addClassName('magicform');
			el.setOpacity(0.5);
		});
		
		/* Fix: input elements steals mouseenter from form container */
		//this.container.observe('mouseenter', this.onMouseEnter.bind(this));
		//this.container.observe('mouseleave', this.onMouseLeave.bind(this));
		this.container.observe('magicform:onBlur', this.onMouseLeave.bind(this));
		this.container.observe('magicform:onFocus', this.onFocus.bind(this));
		
		this.container.observe('magicform:onMouseEnter', this.onMouseEnter.bind(this));
		this.container.observe('magicform:onMouseLeave', this.onMouseLeave.bind(this));
		
		initPlaceholders(this.container);
	},
	onFocus: function() {
		this.container.select('input[type="text"], textarea').each(function(el){
			el.setOpacity(0.99);
		});
	},
	onMouseEnter: function(e) {
		if (this.hasFocus())
			return;
		this.container.select('input[type="text"], textarea').each(function(el){
			el.fade({from: 0.5, to: 0.99, duration: 0.2});
		});
	},
	onMouseLeave: function(e) {
		if (this.hasFocus())
			return;
		var context = this;
		context.container.select('input[type="text"], textarea').each(function(el){
			el.fade({from: 0.99, to: 0.5, duration: 0.2});
		});
	},
	hasFocus: function() {
		var check = false;
		this.container.select('input[type="text"], textarea').each(function(el){
			if (el.hasClassName('input-focus')) {
				check = true;
				throw $break;
			}
		});
		return check;
	}
});

var initPlaceholders = function(container) {
	if ($(container).placeholderInit === true)
		return;
	$(container).select('.placeholder').each(function(el){
		el.magicValue = el.getValue();
		el.observe('focus', function(e){
			if (!this.hasClassName('placeholder-sticky') && this.getValue() == this.magicValue) {
				this.setValue('');
			}
			this.addClassName('input-focus');
			this.up('form').fire('magicform:onFocus');
		});
		el.observe('blur', function(e){
			if (!this.hasClassName('placeholder-sticky') && this.getValue() == "")
				this.setValue(this.magicValue);
			this.removeClassName('input-focus');
			var context = this;
			// Fixes bug-ish behaviour in chrome
			setTimeout(function(){
				context.up('form').fire('magicform:onBlur');
			},200);
		});
		el.up('form').observe('form:beforesubmit',function(event){
			if (el.getValue() == el.magicValue)
				el.setValue("");
		});
		el.up('form').observe('form:aftersubmit',function(event){
			if (el.getValue() == "")
				el.setValue(el.magicValue);
		});
	});
	$(container).placeholderInit = true;
};

