﻿var openNav = "";
var timeout = 500;
var hide = new Array();
var timeouts = new Array();

function SetHeight(id, changeid) {
    var height = document.getElementById(id).offsetHeight;
    document.getElementById(changeid).style.height = (height - 1) + 'px';
}

function SmallBlend(id) {
    if (openNav == "") {
        openNav = id;
        SwapClass(id, 'navelemhover');
        document.getElementById(id + 'sub').style.display = 'block';
    } else {
        if (openNav != id) {
            SwapClass(id, 'navelemhover');
            document.getElementById(id + 'sub').style.display = 'block';
            SwapClass(openNav, 'navelem');
            document.getElementById(openNav + 'sub').style.display = 'none';
            openNav = id;
        }
    }
}

function ClearTimeouts() {
    //Clear all pending timeouts
    for (var i = timeouts.length - 1; i >= 0; i--) {
        window.clearTimeout(timeouts[i]);
        timeouts.pop();
    }
}

function Blend(id) {
    //Show part of the navigation hierarchy
    ClearTimeouts();            //Show --> No hide --> clear timeout
    RemoveElementToHide(id);    //remove current element from hide array -> Should be shown not hidden
    document.getElementById(id).style.display = 'block';    //Show
    SetHeight(id, id + '_bg');    //Configure height of the element
}

function BlendSub(id, subid) {
    //Show part of the navigation hierarchy
    ClearTimeouts();            //Show --> No hide --> clear timeout

    //remove elements from hide array -> Should be shown not hidden
    RemoveElementToHide(id);
    RemoveElementToHide(subid);

    //Show elements
    document.getElementById(id).style.display = 'block';
    document.getElementById(subid).style.display = 'block';

    //Configure height of the elements
    var height = document.getElementById(id).offsetHeight;
    var subheight = document.getElementById(subid).offsetHeight;
    if (height < subheight) {
        SetHeight(subid, id);
    } else {
        SetHeight(id, subid);
    }
    SetHeight(id, id + '_bg');
    SetHeight(subid, subid + '_bg');

    //Hide other shown menus
    HideMenus();
}
function BlendTimeout() {
    //Clear pending timeouts
    ClearTimeouts();
    //Create new timeout and add to pending timeouts
    timeouts.push(window.setTimeout('HideMenus()', timeout));
}

function RemoveElementToHide(id) {
    var hideRemoved = new Array();
    for (var i = 0; i < hide.length; i++) {
        if (hide[i] != id)
            hideRemoved.push(hide[i]);
    }
    hide = hideRemoved;
}

function AddElementToHide(id) {
    hide.push(id);
    BlendTimeout();
}

function HideMenus() {
    for (var i = hide.length - 1; i >= 0; i--) {
        document.getElementById(hide[i]).style.display = 'none';
        hide.pop();
    }
}

function SwapClass(id, strclass) {
    document.getElementById(id).className = strclass;
}
var currentMenuItemId;
function HoverMenu(id, bool) {
    var items = document.getElementById('mainnavigation').getElementsByTagName('div')
    for (var i = 0; i < items.length; i++) {
        items[i].style.display = 'none';
        if (items[i].className == 'current' && !bool)
            items[i].style.display = 'block';
    }
    if (bool && id != '') {
        document.getElementById(id).style.display = 'block';
    } else {
        document.getElementById(currentMenuItemId).style.display = 'block';
    }

}

