﻿var floatingDivTimeout = 400;   // The time the floating div will stay visible after the mouse leaves the trigger element
var floatingDivActive = false;  // Determines whether the user moved the mouse over the floating div
var floatingDivTimeoutId = -1;

function triggerElementMouseOver(floatingDivId, e)
{
    // if floating div is soon going to be hidden
    if (floatingDivTimeoutId != -1)
    {
        // Hidding it now
        hideFloatingDiv(floatingDivId);
    }
    
	// If floating div is hidden 
	if (document.getElementById(floatingDivId).style.visibility != "visible")
	{
        clearTimeout(floatingDivTimeoutId);
        floatingDivTimeoutId = -1;
        setElementVisibility (floatingDivId, true);
        return true;
    }
    
    return false;
}

function triggerElementMouseOut(triggerElement, floatingDivId, e)
{
    // The event MouseOut occurs also to all trigger element's child elements,
    // so making sure to execute the code only when the event is for the trigger element itself
    var current_mouse_target = null;
	
	if(e.toElement)
	{				
		current_mouse_target = e.toElement;
	}
	else if(e.relatedTarget)
	{				
		current_mouse_target = e.relatedTarget;
	}
	
	if( !is_child_of(triggerElement, current_mouse_target) && triggerElement != current_mouse_target )
	{
        floatingDivActive = false;  // not a must, state should already be false
        floatingDivTimeoutId = setTimeout("hideFloatingDiv('" + floatingDivId + "');", floatingDivTimeout);
        return true;
    }
    return false;
}

function floatingDivMouseOver()
{
    floatingDivActive = true;
}

function hideFloatingDiv(floatingDivId)
{
    if(!floatingDivActive)
    {
        setElementVisibility(floatingDivId, false);
        floatingDivActive = false;
    }
}



/*********************************************************************
 * No onMouseOut event if the mouse pointer hovers a child element 
 * *** Please do not remove this header. ***
 * This code is working on my IE7, IE6, FireFox, Opera and Safari
 * 
 * Usage: 
 * <div onMouseOut="fixOnMouseOut(this, event, 'JavaScript Code');"> 
 *		So many childs 
 *	</div>
 *
 * @Author Hamid Alipour Codehead @ webmaster-forums.code-head.com		
**/
function is_child_of(parent, child) {
	if( child != null ) {			
		while( child.parentNode ) {
			if( (child = child.parentNode) == parent ) {
				return true;
			}
		}
	}
	return false;
}
//function fixOnMouseOut(element, event, JavaScript_code) {
//	var current_mouse_target = null;
//	if( event.toElement ) {				
//		current_mouse_target 			 = event.toElement;
//	} else if( event.relatedTarget ) {				
//		current_mouse_target 			 = event.relatedTarget;
//	}
//	if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
//		eval(JavaScript_code);
//	}
//}
/*********************************************************************/

