/**
*  Live Music Hall FrontEnd
*  
*  @author Philipp Steingrebe <philipp@steingrebe.de>
*  @changed 20.09.2011 
*/


lmhFrontEnd = new Class({
	initialize : function(){
		this.invokeForms();
	},
	invokeForms : function(){
		$$('.divMailFormFrame').each(function(mailform){
			var mailFormID = mailform.get('id');
			var errorBlock = new Element('div').inject(mailform, 'top');
			var inputs = mailform.getElements('.valueFlip');
			inputs.each(function(input){
				new ieValueFlip(input, input.get('value'));
			});
			mailform.getElements('.mailFormSubmit').addEvent('click', function(){
				var invalid = false;
				var values = {
					id : mailFormID
				}
				inputs.each(function(input){
					var value = input.get('value');
					if(value === false){
						invalid = true;
						input.addClass('invalidInput');
					}
					else
						input.removeClass('invalidInput');
					
					values[input.get('name')] = value;
				});
				
				if(invalid)
					return;
				
				new Request.JSON({
					url : 'ajax/ajax.mailform.submit.php',
					onSuccess : function(json){
						if(json.error)
							errorBlock.set('html', json.error);
						else
							mailform.set('html', json.msg);
					}
				}).post(values);
			});
		});
	}
});

ieValueFlip = new Class({
	initialize : function(element, flip){
		this.element = element;
		this.flip = flip || element.get('value');
		
		this.element.set('value', flip);
		
		this.element.addEvents({
			'focus' : this.clear.bind(this),
			'blur'  : this.check.bind(this)
		});

		this.element.get = (function(prop){
			var property = Element.Properties.get(prop);
			value =  (property && property.get) ? property.get.apply(this, Array.slice(arguments, 1)) : this.getProperty(prop);
			return (value == flip) ? false : value;
		}).bind(this.element);

	},
	check : function(){
		if(this.element.get('value') == '')
			this.element.set('value', this.flip);
	},
	clear : function(){
		if(this.element.get('value') == false)
			this.element.set('value', '');
	}
});

window.addEvent('domready', function(){
	new lmhFrontEnd();
});
