var SearchFocus = 
{
	/** previous document.onload value */
	old_onload: null,

	/** default language, used if the language cannot be extracted from the document uri */
	default_language: "English",

	/** "search" labels for every language */
	text_list: { Deutsch: "Suchen", English: "Search", Russisch: "&#1055;&#1086;&#1080;&#1089;&#1082;", Polnisch: "Szukaj", Francais: "Recherche" },

	/**
	 * Registers the object for initialization after document loading.
	 */
	register: function()
	{
		var self = this;
		self.old_onload = window.onload;
		window.onload = self.init;
	},
	
	/**
	 * Initializes the object.
     * This function is called as onload event for the document object.
	 * @param e		event object
	 */
	init: function(e)
	{
		var self = SearchFocus;
		if (self.old_onload)
		{
			var oldload = self.old_onload;
			self.old_onload	= null;
			oldload(e);
		}
		var search_input = document.getElementById("nf_addon_smallsearch_string");
		if (search_input)
		{
			search_input.onfocus = self.remove_label;
			search_input.onblur = self.set_label;
			self.set_label();
		}
	},

	/**
	 * Returns the "search" label, according to the current page language.
	 */
	search_label: function()
	{
		var self = this;
		// extract the language name from the document's uri
		var lang_result = /\/doc\/(.*)\/[0-9]+\//.exec(document.location.href);
		var lang = (lang_result ? lang_result[1] : self.default_language);

		// is the label string is entity encoded?
		if (self.text_list[lang].charAt(0)=='&' && self.text_list[lang].charAt(1)=='#')
		{
			// yes, decode it to unicode
			var name_utf16 = "";
			var divider = /&#([0-9]+);/g;
 			var result;
			while(result = divider.exec(self.text_list[lang]))
				name_utf16 += String.fromCharCode(parseInt(result[1]));
			return name_utf16;
		}
		return self.text_list[lang];
	},

	/**
	 * Removes the "search" label.
	 * This function is called for the onfocus event for the search input box.
	 * So, the label is set if the box gains the input focus and the search label is set.
	 */
	remove_label: function()
	{
		var self = SearchFocus;
		var search_input = document.getElementById("nf_addon_smallsearch_string");
		if (search_input && search_input.value==self.search_label())
			search_input.value = "";
	},

	/**
	 * Sets the "search" label.
	 * This function is called for the onblur event for the search input box.
	 * So, the label is set if the box loses the input focus and is empty.
	 */
	set_label: function()
	{
		var self = SearchFocus;
		var search_input = document.getElementById("nf_addon_smallsearch_string");
		if (search_input && search_input.value=="")
			search_input.value = self.search_label();
	}

};

SearchFocus.register();

