﻿
    var closeTopNavTimer;
    var closeTopNavTimeout;
    
    var currentFloater	= null;
    var currentTdSelection = null;
    

    // open hidden layer
    function showDropdown(sender, articleId) {

        closeDropdown();
        unSelectMenu();
        
        
        hideDropdown();
        
        var coordinates = GetElementCoordinates(sender);
        
	    // close old layer
	    if(currentFloater)
	    {
	        currentFloater.style.display = "none";
	    }
	    
	    currentTdSelection = sender.parentNode.parentNode; 
	    
	    if(currentTdSelection.oldClassName==null)
	    {
	        currentTdSelection.oldClassName = currentTdSelection.className;
	    }
	    
        currentTdSelection.className = "node selected";
        
        currentFloater = document.getElementById("top_menu_" + articleId);
        
        currentFloater.style.visibility = "hidden";
        currentFloater.style.display = "none";
	    
	    
        currentFloater.style.left = (coordinates.bottomLeft.x -10) + "px";
        currentFloater.style.top = (coordinates.bottomLeft.y -20) + "px";
	    
	    if(coordinates.bottomLeft.x > 700)
        {
	        var ddmenItemCoordinates = GetElementCoordinates(currentFloater);
    	    
	        var widthOfMenu = GetWidth(currentFloater);
	        var widthOfButton = GetWidth(sender);
    	    
	        if(PixelPress_WindowDimensions.x < (ddmenItemCoordinates.topRight.x + (widthOfMenu-widthOfButton)))
	        {   
	            currentFloater.style.left = "0px";
	            currentFloater.style.top = (coordinates.bottomLeft.y -3) + "px";
    	        
	            widthOfMenu = GetWidth(currentFloater);
               
	            currentFloater.style.left = (coordinates.bottomRight.x) +4 - widthOfMenu + "px";
	        }
	        else
	        {
	            // Default Measure
	            // get new layer and show it
	        }
	    }
	    
        currentFloater.style.visibility = "visible";
	    currentFloater.style.display = "inline-block";    
	    
	    // Nothis is display until here

    }
    // close showed layer
    function closeDropdown()
    {
        if (currentFloater) {
            clearInterval(closeTopNavTimer);
            clearTimeout(closeTopNavTimeout);
            currentFloater.style.display = 'none';
        }
	    unSelectMenu();
	       
    }

    function noMenu() {
        closeDropdown();
        unSelectMenu();
    }
    
    
    function unSelectMenu()
    { 
        if(currentTdSelection != null)
	    {
	        if(currentTdSelection.oldClassName!=null)
	        {
	            currentTdSelection.className =  currentTdSelection.oldClassName.toString();
	            currentTdSelection.oldClassName = null;
	        }
	    }
	}

    // go close timer
    function hideDropdown()
    {
        closeTopNavTimer = setInterval(
            function()
            {
                if(currentFloater!=null && currentTdSelection!=null)
                {
                    // we have an active menu
                    if(!IsMouseOverElementCoordinates(currentFloater) && !IsMouseOverElementCoordinates(currentTdSelection))
                    {

                        closeTopNavTimeout = setTimeout(function()
                        {
                        
                            closeDropdown();
                            unSelectMenu();
                            clearTimeout(closeTopNavTimeout);
                            
                        }, 50);
                        
                        clearInterval(closeTopNavTimer);
                    }
                }
            }
        , 50);
    }

