$(function(){

	// Konstanter
    var ALL_TOOLTIPS = "table.kontaktformular td.tooltipcell";
    var INACTIVE_TOOLTIPCELL = "tooltipcell inactive";
    var ACTIVE_TOOLTIPCELL = "tooltipcell active";
    var TIMEOUT_SECS = 400;
	
	// Map, der holder referencer til alle timeouts i kø
    var timeouts = {};

	// Læg event-handlers på de felter, der har et tooltip ...
	$("table.kontaktformular div.tooltipwrapper")
		.hover(tooltipIconMouseoverHandler, tooltipIconMouseoutHandler)
		.closest("tr.formularrow")
		.find("input,textarea,select")
		.focus(inputfieldFocusHandler).blur(inputfieldBlurHandler);
 
    /****************************************************************
    ** Event type-specifikke event handlers                        **
    ****************************************************************/

	function tooltipIconMouseoverHandler() {
		showTooltipHandler($(this).closest("td.tooltipcell"));
	}
	
	function tooltipIconMouseoutHandler() {
		hideTooltipHandler($(this).closest("td.tooltipcell"));
	}
	
	function inputfieldFocusHandler() {
		showTooltipHandler($(this).closest("tr.formularrow").find("td.tooltipcell"));
	}

	function inputfieldBlurHandler() {
		hideTooltipHandler($(this).closest("tr.formularrow").find("td.tooltipcell"));
	}

    /****************************************************************
    ** Generelle event handlers                                    **
    ****************************************************************/

	function showTooltipHandler(tooltip) {
		hideAllTooltipsExcept(tooltip);
		tooltip.each(showTooltip);
	}

	function hideTooltipHandler(tooltip) {
		var timeoutId = window.setTimeout(hideTooltipCallback(tooltip), TIMEOUT_SECS);
		var tooltipId = tooltip.attr("id");

		if (isValidId(tooltipId)) {
			tooltipId = timeoutId
			tooltip.attr("id", timeoutId);
		}

		timeouts[tooltipId] = timeoutId;
	}

    /****************************************************************
    ** Lukning af tooltips (med forsinkelse)                       **
    ****************************************************************/

	function hideTooltipCallback(tooltip) {
		return function() {
			tooltip.each(hideTooltip);
		}
	}

	function hideAllTooltipsExcept(tooltip) {
		clearAlltimeouts();
		$(ALL_TOOLTIPS).not(tooltip).each(hideTooltip);
	}

	function clearAlltimeouts() {
		for (var timeout in timeouts) {
			window.clearTimeout(timeouts[timeout]);
		}
		timeouts = {};
	}

    /****************************************************************
    ** Funktioner til vis / skjul tooltip                          **
    ****************************************************************/
	
    function hideTooltip() {
		this.className = INACTIVE_TOOLTIPCELL;
	}
	
    function showTooltip() {
		this.className = ACTIVE_TOOLTIPCELL;
	}

    /****************************************************************
    ** Hjælpefunktioner                                            **
    ****************************************************************/
	
	function isValidId(id) {
		return id != undefined && id.length > 0;
	}
	
});
