/**
 * @author shrestha
 */

PHILIPS.namespace('PHILIPS.util'); 

PHILIPS.util.formHelper = new Class({
    Implements: [Options, Events],
    options: {
		CLASSES: {
			topLevelButton: "defaultText"
			
		},
		IDS: {
			
		},
		SELECTORS: {
			
		}
    },
	inputBoxes: [],
	submitButton: '',
	
    initialize: function(options) {
		
        this.setOptions(options);
		this._findElements();
		this._attachListeners();
		
    },
	
		//----------------------------------------------------------------------
		// PROPERTIES
		//----------------------------------------------------------------------
		
	
	
		//----------------------------------------------------------------------
		// PRIVATE METHODS
		//----------------------------------------------------------------------
		
	_attachListeners: function() {
		this.inputBoxes.each(function(el, index) {

			el.addEvent('focus', this.setupButtons.bindWithEvent(this, el));
			el.addEvent('blur', this.setupMouseout.bindWithEvent(this, el));
			formTag = el.getParent('form');
			submitTag = formTag.getChildren('input[type=submit]');
			
			//Preventing empty searches or search with default text
			submitTag.addEvent('click', function(e){
				
				
				defaultField = e.target.getParent('form').getChildren('.defaultText')[0];
				
				
				defaultText = defaultField.get('rel');
				currentValue = defaultField.get('value').stripTags();
				currentValue = currentValue.tidy();
				currentValue = currentValue.replace(';', ' ');
				currentValue = currentValue.replace('?', ' ');
				currentValue = currentValue.replace('<', ' ');
				currentValue = currentValue.replace('>', ' ');
				currentValue = currentValue.replace('"', ' ');
				currentValue = currentValue.replace("'", ' ');
				currentValue = currentValue.replace("!", ' ');
				currentValue = currentValue.replace("@", ' ');
				currentValue = currentValue.replace("#", ' ');
				currentValue = currentValue.replace("$", ' ');
				currentValue = currentValue.replace("*", ' ');
				currentValue = currentValue.replace("=", ' ');
				currentValue = currentValue.replace("|", ' ');
				
				//stripping tags
				defaultField.set('value',currentValue);
				
				if(currentValue == defaultText || currentValue == ''){
					e.preventDefault();	
				}
				
			});
		}, this);
		
	},
		
	_findElements: function() {
		
		this.inputBoxes = $$('.' + this.options.CLASSES.topLevelButton);
		
		
	},
	
		//----------------------------------------------------------------------
		// EVENT HANDLERS
		//----------------------------------------------------------------------
	setupButtons: function(e,el){
			var currentValue = el.get('value');
			var defaultText = el.get('rel');
			
			if(currentValue == defaultText){
				el.set('value', '');
			}
		return false;
	},
	setupMouseout: function(e, el){
			var currentValue = el.get('value');
			var defaultText = el.get('rel');
			
			if(currentValue == ''){
				el.set('value', defaultText);
			}
		return false;
	},
	
	
		//----------------------------------------------------------------------
		// GETTER SETTERS
		//----------------------------------------------------------------------
	setSomeVariable: function() {
	
	},
	getSomeVariable: function() {
	
	}
	
});