/**
 * Side Search Form Widget Script
 * @author Bogumil Wrona
 * @copyright Cre8 New Media
 */

document.observe('dom:loaded', initSearchPropertyForm);

function initSearchPropertyForm()
{
	new DefVal('search_keyword', 'Enter Keyword, Location or Postcode');
}

var DefVal = Class.create(
{
	initialize: function(fieldID, defaultVal) 
	{
		this.field = $(fieldID);
		this.fieldID = fieldID;
		this.defaultVal = defaultVal;
		
		this.isPasswordType = (this.field.type == 'password') ? true : false;
		if(Prototype.Browser.IE) this.isPasswordType = false;
		
		if(this.field.value == '') this.setDefVal();
		
		this.startObserving();
		
	},
	clear: function() 
	{
		this.field.value = '';		
		if(this.isPasswordType) this.field.type = 'password';
	},
	setDefVal: function() 
	{
		this.field.value = this.defaultVal;
		if(this.isPasswordType) this.field.type = 'text';
	},
	isDefVal: function(){
		return (this.field.value.strip() == this.defaultVal);
	},
	fieldOnFocus: function()
	{
		if(this.isDefVal()) this.clear();
	},
	fieldOnBlur: function()
	{
		if(this.field.value.strip() == '') this.setDefVal();
	},
	stopObserving: function()
	{
		Event.stopObserving(this.field, 'focus');
		Event.stopObserving(this.field, 'blur');
	},
	startObserving: function()
	{
		Event.observe(this.field, 'focus', this.fieldOnFocus.bindAsEventListener(this, true));
		Event.observe(this.field, 'blur', this.fieldOnBlur.bindAsEventListener(this, true));
	}
});