var MENUITEM_SLIDE_DELAY = 2; 

/* Common JS */
function $$(strID)
{
    return document.getElementById(strID);
}

function str_replace(search, replace, subject)
{
    return subject.split(search).join(replace);
}

/* Menu */
function menuitem_OnClick(strID)
{
    var obj = $$(strID);
    
    if (obj.style.display == 'none')
    {
        if (typeof(obj.origHeight) == 'undefined')
        {
            obj.style.display = 'block';
            var h = obj.getBoundingClientRect().bottom - obj.getBoundingClientRect().top;
            obj.style.height = '0px';
            obj.origHeight = h;
        }
        
        if (obj.isSliding == false || typeof(obj.isSliding) == 'undefined')
        {   
            obj.isSliding = true;
            
            if (typeof(menuitem_OnClick.activeItem) != 'undefined')
            {
                menuitem_slideIn(menuitem_OnClick.activeItem, 0);
            }

            var objIcon = $$(str_replace('submenu_', 'icon_', obj.id));
            if (objIcon != null)
            {
                objIcon.src = 'img/icon_menu_up.png';
            }

            menuitem_OnClick.activeItem = obj;
            menuitem_slideOut(obj, obj.origHeight);
        }
    }
    else
    {                                               
        if (obj.isSliding == false || typeof(obj.isSliding) == 'undefined') // || obj == menuitem.activeItem 
        {
            obj.isSliding = true;
            menuitem_slideIn(obj, 0);
        }
    }
    
    return false;
}

function menuitem_slideOut(obj, targetHeight)
{

    if (typeof(obj.isSlidingIn) == 'undefined' || obj.isSlidingIn == false)
    {
        obj.style.display = 'block';
    
        var h = obj.getBoundingClientRect().bottom - obj.getBoundingClientRect().top;
        
        var aktuell = parseInt(str_replace('px', '', h.toString() ));
        var ziel    = parseInt(str_replace('px', '', targetHeight.toString() ));
        
        var hinzu = parseInt((ziel-aktuell) / MENUITEM_SLIDE_DELAY);
        if (hinzu == 0)
        {
            hinzu = 1;
        }
    
        if ( aktuell < ziel )
        {
            obj.style.height = (aktuell + hinzu) + 'px';
            window.setTimeout( function () { menuitem_slideOut(obj, targetHeight); }, 10); 
        }
        else
        {
            obj.isSliding = false;
        }
    }
}

function menuitem_slideIn(obj, targetHeight)
{
    obj.isSlidingIn = true;
    obj.style.display = 'block';

    var h = obj.getBoundingClientRect().bottom - obj.getBoundingClientRect().top;
    
    var aktuell = parseInt(str_replace('px', '', h.toString() ));
    var ziel    = parseInt(str_replace('px', '', targetHeight.toString() ));
    
    var hinzu = parseInt((aktuell) / MENUITEM_SLIDE_DELAY);
    if (hinzu == 0)
    {
        hinzu = 1;
    }

    if ( aktuell > ziel )
    {
        obj.style.height = (aktuell - hinzu) + 'px';
        window.setTimeout( function () { menuitem_slideIn(obj, 0); }, 10); 
    }
    else
    {
        var objIcon = $(str_replace('submenu_', 'icon_', obj.id));
        if (objIcon != null)
        {
            objIcon.src = 'img/icon_menu_down.png';
        }

        obj.style.display = 'none';
        obj.isSlidingIn = false;
        obj.isSliding = false;        
    }
}

