/**
 * Functions for the FastSearchTool
 *
 * @copyright 	copyright (c) 2008 by GMX GmbH
 * @author 		matthias kurte <mkurte at gmx-gmbh dot de>
 */


// Init FastSearchTool-Namespace
var fastSearchTool = new Object;


// Start initial Functions
poma.addLoadEvent(function() {
	fastSearchTool.init();
});


// Intitial Function
fastSearchTool.init = function() {
}


//FastSearchTool Functions

fastSearchTool.handle = {

	//Handle the Tool
	handleTool: function(event) {
		if(poma.dom.$id("fastsearch-tool").style.display == "block") { // Is it shown?
			//this.hide(); // hide
			if (window.getSelection) { // It is shown, but the user selected another text, display at new position
				this.show(event);
			} else if (document.getSelection) {
				this.show(event);
			} else if (document.selection) {
				this.show(event);
			} else {
				this.hide();
			}
		} else { // It is not shown, so lets display it
			this.show(event);
		}
	},

	show: function(event) {
		//Mouse koordinates
		var xMouse = 0;
		var yMouse = 0;
		xMouse = (window.event) ? document.documentElement.scrollLeft+window.event.clientX : event.pageX;
		yMouse = (window.event) ? document.documentElement.scrollTop+window.event.clientY : event.pageY;

		var text = new String;
		if (window.getSelection) {
			text += window.getSelection();
		} else if (document.getSelection) {
			text += document.getSelection();
		} else if (document.selection) {
			text += document.selection.createRange().text;
		} else {
			return;
		}
		if (text != "" && text != poma.dom.$id("fastsearch-input").value) {
			// only if the selection was in one of the following elements, the search gets triggered
			if(text.length <= 30) { //maximum 30 characters marked to open the tool
				var doIt = false;
				$ui("#grid h1, #grid h2, #grid h3, #grid p, #grid li").each(function() {
					if ($ui(this).is(":contains('" + text + "')")) doIt = true;
				});
				if (!doIt) return false;
				this.open(text, xMouse, yMouse);
			} else {
				if(poma.dom.$id("fastsearch-tool").style.display == "block") {
					this.hide();
				}
			}
		}
	},

	hide: function() {
		poma.dom.$id("fastsearch-tool").style.display = "none";
		poma.dom.$id("fastsearch-input").value = "";
	},

	open: function(text, xMouse, yMouse) { //open the tool at the right position
		poma.dom.$id("fastsearch-tool").style.display = "block";
		// add a little bit space between box and curser
		xMouse = xMouse + 10;
		yMouse = yMouse + 10;
		poma.dom.$id("fastsearch-tool").style.top = yMouse + "px";
		poma.dom.$id("fastsearch-tool").style.left = xMouse + "px";
		poma.dom.$id("fastsearch-input").value = text;
	},

	getFormTarget: function(form) {

		var hostFieldDomain = poma.dom.$id("fastsearch-domain");
		var hostFieldWikipedia = poma.dom.$id("fastsearch-wikipedia");
        var hostFieldWeb = poma.dom.$id("fastsearch-web");
        var hostFieldProducts = poma.dom.$id("fastsearch-products");

		if(hostFieldWeb.checked) { // normal search
			form.webRb.value = '';
			poma.dom.$id("fastsearch-mc").value = fastsearchMc["web"];
			form.action = fastsearchparams["formaction"];
		} else if(hostFieldDomain.checked) { // search on domain
			hostFieldDomain.value = location.hostname.substring(location.hostname.indexOf('.')+1);
			form.webRb.value = "custom";
			poma.dom.$id("fastsearch-mc").value = fastsearchMc["site"];
			form.action = fastsearchparams["formaction-host"];
		} else if(hostFieldProducts.checked) { // product search
			poma.dom.$id("fastsearch-product-search").value = poma.dom.$id("fastsearch-input").value;
			form.webRb.value = "";
			poma.dom.$id("fastsearch-mc").value = fastsearchMc["products"];
			form.action = fastsearchparams["formaction-products"];
		} else if(hostFieldWikipedia.checked) { // wiki search
			poma.dom.$id("fastsearch-wikipedia-search").value = poma.dom.$id("fastsearch-input").value;
			form.webRb.value = "";
			poma.dom.$id("fastsearch-mc").value = fastsearchMc["lexikon"];
			form.action = fastsearchparams["formaction-lexikon"];
		}
	}
}
