// Shows tooltip that can display various informations.
function showTooltip(x, y, orientation, type, content) {
	var noanim = false;
	if ($("#tooltip").length > 0) {
		$("#tooltip").hide().remove();
		noanim = true;
	}

	if (orientation != "bl" && orientation != "br" && orientation != "tl" && orientation != "tr") {
		orientation = "bl";
	}

	if (type != "error" && type != "") {
		type = "error";
	}

	$("body").prepend("<div id=\"tooltip\" class=\"p" + orientation + "\"><div class=\"pointer\"><div class=\"tooltip_content\"><div class=\"top\"><div class=\"tl\"></div><div class=\"t\"></div><div class=\"tr\"></div></div><div class=\"content " + type + "\"></div><div class=\"bottom\"><div class=\"bl\"></div><div class=\"b\"></div><div class=\"br\"></div></div></div></div></div>");
	$("#tooltip .content").append(content);
	$("#tooltip")
		.hide()
		.css("left", x.toString() + "px")
		.css("top", y.toString() + "px")
		.click(function(){return false;})
		.mouseover(function(){
			window.clearTimeout($(this).data("timeout"));
		})
		.mouseout(function(){
			$(this).data("timeout", window.setTimeout(hideTooltip, 500));
		})
		.data("timeout", window.setTimeout(hideTooltip, 5000));

	if (noanim) {
		$("#tooltip").show();
	} else {
		$("#tooltip").fadeIn("normal");
	}
}

// Hide the tooltip when inactivity occurs.
function hideTooltip() {
	$("#tooltip").fadeOut("normal", function(){
		$(this).remove();
	});
}

// Shows 'Vyhledat ...' text in the search, until it is modified.
function installSearchHider() {
	$("#SearchText")
		.focus(function(){
			if (!$(this).data("changed")) {
				this.value = "";
			}
		})
		.blur(function(){
			if (!$(this).data("changed")) {
				this.value = "Vyhledat ...";
			}
		})
		.keydown(function(){
			$(this).data("changed", true);
		})
		.attr("value", "Vyhledat ...")
		.data("changed", false)
		.blur();

	$("#SearchForm").submit(function(){
		if (!$("#SearchText").data("changed")) {
			var btn = $("#search .button").offset();
			showTooltip(btn.left + 5, btn.top + 22, "tr", "error", "Musíte zadat hledaný výraz.");
			return false;
		}
		return $("#SearchText").attr("value") != "";
	});
}

$(function(){
	installSearchHider();
});
