﻿//
// functionality for categories menu, on: Include/Modules/Categories.ascx
//

// consts
var menuWidth = 143;
var parentHeight = 21;

// vars
var subMenu;

// shows the sub-menu
function catalogParentOver(parentId, parentTable, maxParents, container) 
{
	subMenu = document.getElementById('divCatSubMenu_'+parentId);
	var pos = findPos(parentTable.id);

	// menu placement based on container
	var left;
	var top;
	switch (container) {
	    // 0-1: the left containers, show menu on the right
	    case 0:
	    case 1:
	        left = pos[0] - menuWidth + 'px';
	        top = pos[1] + 'px';
	        break;

	    // 2: the middle container, show menu on the bottom
	    case 2:
	        left = (pos[0] - 2) + 'px';
	        top = pos[1] + (parentHeight + 2) + 'px';	    
	        break;

	    // 0-1: the right containers, show menu on the left
	    case 3:
	    case 4:
	        left = pos[0] + menuWidth + 'px';
	        top = pos[1] + 'px';
	        break; 
	}

	// hide the rest of the children
	for (i = 0; i < maxParents; i++) {
	    if (i != parentId) {
	        hideCatalogSubMenu(i);
	        hideCatalogSubMenu('content_' + i);
	    }
	}

	subMenu = document.getElementById('divCatSubMenu_' + parentId);
	subMenu.style.left = left;
	subMenu.style.top = top;	
	subMenu.style.display = 'block';
	
}

// occurs when the mouse leaves the parent
function catalogParentOut(parentId, container, event) 
{
    subMenu = document.getElementById('divCatSubMenu_' + parentId);
    var mouseCoords = getMouseCoords(event);
	var eventX = mouseCoords[0];
	var eventY = mouseCoords[1];
	var pos = findPos(subMenu.id);
	var menuX = pos[0];
	var menuY = pos[1];
	var parentMenuRight = menuX + (menuWidth * 2);

	// hide submenu based on container
	switch (container) {
	    // 0-1, 3-4: the left and right containers
	    case 0:
	    case 1:
	    case 3:
	    case 4:	    
	        if (eventY <= menuY || eventY >= menuY + parentHeight || eventX <= menuX || eventX >= parentMenuRight) {
	            hideCatalogSubMenu(parentId);
	        }
	        break;

	    // 2: the middle container, menu shown on the bottom
	    case 2:
	        if (eventY <= menuY - parentHeight || eventX <= menuX || eventX >= menuX + menuWidth) {
	            hideCatalogSubMenu(parentId);
	        }
	        break;
	}
}

// occurs when the mouse leaves the sub menu
function catalogMenuOut(parentId, container, event) 
{
    subMenu = document.getElementById('divCatSubMenu_' + parentId);
    var mouseCoords = getMouseCoords(event);
    var eventX = mouseCoords[0];
    var eventY = mouseCoords[1];
    var pos = findPos(subMenu.id);
    var menuX = pos[0];
    var menuY = pos[1];
    var parentMenuRight = menuX + (menuWidth * 2);
    var menuHeight = subMenu.offsetHeight;

    // hide submenu based on container
    switch (container) {
        // 0-1: the left containers, menu shown on the right 
        case 0:
        case 1:
            if (eventY <= menuY || eventY >= menuY + menuHeight || eventX <= menuX || eventX >= parentMenuRight) {
                hideCatalogSubMenu(parentId);
            }
            break;

        // 2: the middle container, menu shown on the bottom
        case 2:
            if (eventY >= menuY + menuHeight || eventX <= menuX || eventX >= menuX + menuWidth) {
                hideCatalogSubMenu(parentId);
            }         
            break;

        // 0-1: the right containers, menu shown on the left  
        case 3:
        case 4:
            if (eventY <= menuY || eventY >= menuY + menuHeight || eventX <= menuX || eventX >= menuX + menuWidth) {
                hideCatalogSubMenu(parentId);
            }        
            break;
    }
    
    //alert( 'eventY=' + eventY + ' menuY=' + menuY + ' menuY + menuHeight=' + (menuY + menuHeight) );
}

// hides the sub-menu
function hideCatalogSubMenu(parentId) 
{
	subMenu = document.getElementById('divCatSubMenu_'+parentId);
	if (subMenu)
		subMenu.style.display = 'none';
}

