if ( typeof(Libero) == "undefined" ) {
	Libero = {
		Version: '1.0.0'
    };
}

// test prototype library
if ( typeof(Prototype) == "undefined" ) {
	var Class = {
		create: function() {
			return function() {
				this.initialize.apply(this, arguments);
			}
		}
	};
	Object.extend = function(destination, source) {
		for (var property in source) {
			destination[property] = source[property];
  		}
		return destination;
	};
}

// Create search class
Libero.Search = Class.create();
Libero.Search.prototype = {

	// constructor
	initialize: function() {
		var params = Object.extend({
			form_el: 'search',
			engine_el : 'search_engine',
			query_el: 'search_query',
			class_query_empty: 'sn-search-form-empty',
			class_query: 'sn-search-form',
			initial_query : undefined,
			engine_drivers: {
				video: {
					action: 'http://video.libero.it/app/search',
					method: 'get',
					on_empty_query: function (q) { document.location.href = 'http://video.libero.it'; return false }
				},
				blog: {
					action: 'http://blog.libero.it/ricerca_blog.php',
					on_empty_query: function (q) { document.location.href = 'http://blog.libero.it'; return false }
				},
				persone: {
					action: 'http://digiland.libero.it/ricerca_persone.php',
					on_empty_query: function (q) { document.location.href = 'http://digiland.libero.it'; return false },
					empty_message: 'Interessi o nickname'
				},
				siti: {
					action: 'http://digiland.libero.it/ricerca_siti.php',
					on_empty_query: function (q) { document.location.href = 'http://digiland.libero.it'; return false }
				},
				foto: {
					action: 'http://foto.libero.it/cercafoto.php',
					on_empty_query: function (q) { document.location.href = 'http://foto.libero.it'; return false }
				},
				tutti: {
					action: 'http://digiland.libero.it/ricerca_tutti.php',
					on_empty_query: function (q) { document.location.href = 'http://digiland.libero.it'; return false },
					empty_message: 'Persone, Blog, Siti'
				}
			} 

		}, arguments[0]);
		this.params = params;
		if ( typeof(Prototype) == "undefined" ) {
			this.form = document.getElementById(this.params.form_el);
			this.q = document.getElementById(this.params.query_el);
			this.engine = document.getElementById(this.params.engine_el);
		}
		else {
			this.form = $(this.params.form_el);
			this.q = $(this.params.query_el);
			this.engine = $(this.params.engine_el);
		}
		
		// pre-select engine
		for ( var i = 0; i < this.engine.length; i++ ) {
			if ( this.engine[i].value == this.params.engine ) {
				this.engine.selectedIndex = i;
				break;
			}
		}
		this.query = this.params.initial_query == undefined ? this.q.value : this.params.initial_query;	
		this.init();
		
		
	},

	// init and onChange callback
	blur: function () {
		if ((this.params.engine_drivers[this.engine[this.engine.selectedIndex].value].empty_message == undefined )|| (this.q.value != this.params.engine_drivers[this.engine[this.engine.selectedIndex].value].empty_message)) {
			this.query = this.q.value; //autocompletamento
		}
		if (this.query.length == 0) {
			this.set_default();
		}
	},

	// init and onChange callback
	change: function () {
		if ( this.query.length == 0 ) {
			this.set_default();
		}
	},

	// init and onChange callback
	init: function () {
		if ( this.query.length > 0 ) {
			this.q.value = this.query;
			this.q.className = this.params.class_query;
		} else {
			this.set_default();
		}
	},

	set_default: function () {
		if ( this.params.engine_drivers[this.engine[this.engine.selectedIndex].value].empty_message != undefined ) {
			this.q.className = this.params.class_query_empty;
			this.q.value = this.params.engine_drivers[this.engine[this.engine.selectedIndex].value].empty_message;
		} else {
			this.q.value = '';
		}
	},

	// onKeyUp callback
	keyup: function () {
		this.query = this.q.value;
	},

	// onFocus callback
	focus: function () {
		this.q.className = this.params.class_query;
		if (this.q.value != this.query) {
			this.q.value = this.query;
		}
	},

	// form submit action
	search: function () {

		// Get/set a valid engine 
		var engine = this.engine[this.engine.selectedIndex].value;
		if ( this.params.engine_drivers[engine] == undefined )
			engine = this.params.default_engine;
	
		// Set form properties
		if ( this.params.engine_drivers[engine].action != undefined )
			this.form.action = this.params.engine_drivers[engine].action;
		if ( this.params.engine_drivers[engine].target != undefined )
			this.form.target = this.params.engine_drivers[engine].target;
		else
			this.form.target = '_self';
		if ( this.params.engine_drivers[engine].method != undefined && this.params.engine_drivers[engine].method.match(/^get|post$/i) )
			this.form.method = this.params.engine_drivers[engine].method;
		
		// Check query value
		if ((this.params.engine_drivers[this.engine[this.engine.selectedIndex].value].empty_message != undefined )&& (this.q.value == this.params.engine_drivers[this.engine[this.engine.selectedIndex].value].empty_message)) {
			this.q.value = this.query;
		}
		if ( this.q.value.match(/^\s*$/)) {
			if (typeof this.params.on_empty_query_default == 'function' ) {
				return this.params.on_empty_query_default(this.q);
			}
			if (typeof this.params.engine_drivers[engine].on_empty_query == 'function' ) {
				return this.params.engine_drivers[engine].on_empty_query(this.q);
			}
		}
	}
};

