
var Search = new Class({
	
	Implements: Options,
	
	options: {
		link: null,
		text: "Google Custom Search",
		form: null
	}, 
	
	initialize: function(options){
		this.setOptions(options);
		this.events();
	},
	
	events: function(){
		this.options.link.addEvent("click", this.toggle.bindWithEvent(this));
		/* this.options.link.addEvent("click", function(){
			if (this.getParent().hasClass("search-active")) {
				this.getParent().removeClass("search-active");
			} else {
				$$("#header li", "#header h1").filter(function(item){
					return !item.hasClass("search");
				}).setStyle("visibility", "hidden");
				this.getParent().addClass("search-active");
			}
		}); */
		this.options.form.getElement("input[name=q]").addEvent("focus", this.emptyText.bindWithEvent(this));
		this.options.form.getElement("input[name=q]").addEvent("blur", this.fillText.bindWithEvent(this));
	},
	
	show: function(event){
		if (event) {
			event.stop();
			if (this.options.form.getStyle("display") == "block") {
				return;
			}
		}
		this.options.form.setStyle("display", "block");
		//this.options.form.getElement("input[name=q]").focus();
		this.options.link.addClass("active");
	},
	
	hide: function(event){
		if (event) {
			event.stop();
		}
		this.options.form.setStyle("display", "none");
		this.options.link.removeClass("active");
	},
	
	emptyText: function(event){
		var input = this.options.form.getElement("input[name=q]");
		if (input.get("value") == this.options.text) {
			input.value = "";
		}
	},
	
	fillText: function(event){
		var input = this.options.form.getElement("input[name=q]");
		if (input.get("value") == "") {
			input.value = this.options.text;
		}
	},
	
	toggle: function(event){
		if (this.options.form.getStyle("display") == "none") {
			this.show(event);
		} else {
			this.hide(event);
		}
	}
});