/**
 * form_base
 * base form class provides methods used for forms
 *
 * @author Rocco Janse, <rocco[at]efocus.nl>
 * @copyright eFocus, www.efocus.nl
 * @version 0.5 alpha
 */
 
 var Form_Base = new Class({
 
 	Implements: Options,
 	
 	// options
 	options: {
 		forms: [],
 		trigger: 'custom',
 		sendOnEnter: false,
 		antiBotString: 'aNtIbOtStRiNg!'
 	}, 	
 	
 	initialize: function(options) {
		
		var self = this;
		
		// apply options
		this.setOptions(options);
		
		// fix IE6 hover bug
		if (Browser.Engine.trident && (Browser.Engine.version <= 4)) {
			this.fixHovers();
		}
		
		// set & remove default value in text inputs
		this.defaultInputText();
		
		// set input fields
		this.handleInputs();

 	},

	/**
	 * handleInputs
	 * creates custom input fields
	 *
	 * @return void
	 */
	 
	handleInputs: function() {

		// scope
		var self = this;

		this.options.forms.each(function(form) {
		
			// get inputs
			var inputs = form.getElements('input.'+self.options.trigger);
			if(!inputs) return;
			
			inputs.each(function(input) {
				var styles = self.getElementStyles(input);
				
		 		// create container to fix 'running backgrounds' 
		 		// when input exeedes inputbox width
		 		var container = new Element('div').setStyles(styles);
		 		//container.addStyle('float', 'left');
		 		
		 		var clone = input.clone(true, true).setStyles({
		 			'background': 'none',
					'border': 'none'
		 		});

				// add events to clone
				clone.addEvents({
					'focus': function(event) {
						event.stop();
						self.toggleFieldFocus(container);
					},
					'blur': function(event) {
						event.stop();
						self.toggleFieldFocus(container);
					},
					'keyup': function(event) {
						event.stop();
						self.setAntiBotSecurity();
					}
				});

				// insert clone into container
		 		clone.inject(container);
		 		
		 		// container replaces original input
		 		container.replaces(input);

			});
		});
	},

 	/**
 	 * getElementStyles
 	 * fetches element styles from stylesheet
 	 *
 	 * @param object element
 	 * @return object styles
 	 */
	
 	getElementStyles: function(element) {
 		if(!element) return;

		// fetch styles from element 		
 		var styles = element.getStyles(
 			'position',
 			'top',
 			'left',
 			'margin', 
 			'padding', 
 			'border-top', 
 			'border-right', 
 			'border-bottom', 
 			'border-left', 
 			'width', 
 			'height', 
 			'display',
 			'color',
 			'background-image', 
 			'background-position', 
 			'background-repeat', 
 			'background-color'
 		);
		return styles;
 	},

 	/**
 	 * handleFieldFocus
 	 * handles field focus to add focus style and
 	 * and to actually invoke submit on enter if set
 	 *
 	 * @param object element which has focus
 	 * @return void
 	 */
	
	toggleFieldFocus: function(element) {
 		if(!element) return;
		
		// add focus class
		element.toggleClass('focus');	

	},	
	
	/**
	 * defaultInputText
	 *
	 * toggles default text in text inputfields
	 *
	 * @author Klaas Dieleman <klaas{AT}efocus.nl>
	 * @return void
	 */
	 
	defaultInputText: function() {
		var inputfields = document.getElements('input').filter(function(item) { return item.getProperty('type') == 'text'});
		
		inputfields.each(function(item) {
			item.defaultText = item.value;
			
			item.addEvents({
				'focus': function() {
					if (item.value == item.defaultText) item.value = '';
					item.removeClass('default');
				},
				'blur': function() {
					if (item.value == '') {
						item.value = item.defaultText;
						item.addClass('default');
					}
				}
			});
		});
	},
	
 	/**
 	 * setAntiBotSecurity
 	 * inserts a hidden security field in the form to
 	 * prevent bot spam
 	 *
 	 * @return void
 	 */
 	
 	setAntiBotSecurity: function() {		
 		// scope
 		var self = this;
 		
 		// if field already exist, return
 		if ($('antispam')) return;
 		
 		// insert antibot hidden field
 		this.options.forms.each(function(form) {
 			input = new Element('input', {
 						type: 'hidden',
 						id: 'antispam',
 						name: 'antispam',
 						value: self.options.antiBotString
 					});	
 			if (input) {
 				input.inject(form);
 			}
 		});
 	},
 	
 	/**
	 * fixHover
	 * fixes IE6 hover bug for a.formbuttons
	 *
	 * @return void
	 */
 	
 	fixHovers: function() {
 		var buttons = $$('a.formbutton');
 		if (!buttons) return;
 		
 		buttons.each(function(button) {
 			button.addEvents({
 				'mouseenter': function() {
 					this.addClass('hover');
 				},
 				'mouseleave': function() {
 					this.removeClass('hover');
 				}
 			});
 		});
 	}
 });