if(typeof(Control) == "undefined")
	var Control = {};

Control.DecorateSearch = Class.create();

Control.DecorateSearch.prototype = {
	
	initialize: function(element){
		
		this.agent = navigator.userAgent.toLowerCase();
		this.element = $(element);
		this.message = this.element.value;
		this.standInElement = document.createElement('input');
		this.width = parseInt(this.element.style.width.replace('px',''));
		
		// Create wrapper
		this.wrapper = document.createElement('div');
		this.wrapper.style.width = this.width + "px";
		Element.addClassName(this.wrapper,'SearchWrapper');
		this.element.parentNode.replaceChild(this.standInElement,this.element);
		
		if(this.agent.match(/AppleWebKit/i)){
			
			// Safari
			this.element.type = 'search';
			this.element.setAttribute('results','5');
			
			// Append elements
			this.wrapper.appendChild(this.element);
		
		}
		else {
			
			// Everything else
			this.element.style.width = (this.width - 30) + "px";

			// Left
			this.left = document.createElement('span');
			Element.addClassName(this.left,'Left');
			
			// Right
			this.right = document.createElement('span');
			Element.addClassName(this.right,'Right');
			
			// Append elements
			this.wrapper.appendChild(this.left);
			this.wrapper.appendChild(this.element);
			this.wrapper.appendChild(this.right);
						
		}
		
		// Get attachment element		
		Event.observe(this.element,'blur',this.blurred.bindAsEventListener(this));
		Event.observe(this.element,'focus',this.focused.bindAsEventListener(this));
		this.blurred();
		
		if(this.standInElement)
			this.standInElement.parentNode.replaceChild(this.wrapper,this.standInElement);
		
	},
	
	blurred: function(){
		if(this.message.length > 0){
			if(this.element.value.length == 0)
				this.element.value = this.message;
				
			if(this.element.value == this.message)
				Element.addClassName(this.element,'Blurred');
		}
	},
	
	focused: function(){
		if(this.element.value == this.message)
			this.element.value = '';
			
		Element.removeClassName(this.element,'Blurred');
	}
	
}