﻿function HorizontalNavBar (currentView) {

    this.Init = function () {

        if (currentView == null || currentView == "") {
            if (currentView != "Error")
                alert('Error: currentView not implemented.');
            else
                currentView = "Home";
        }

        $('#NavBar > ul > li > a').each(function (i, elem) {
            if (isCurrentView(elem)) {
                $(elem).addClass('Active');
                return false;
            }
        });


        function isCurrentView(elem) {
            var ret = false;
            if ($(elem).attr('name') == currentView)
                ret = true;
            if ($(elem).parent().children('ul').length > 0)
                $(elem).parent().children('ul').children('li').children('a').each(function (i, e) {
                    if ($(e).attr('name') == currentView)
                        ret = true;
                });
            return ret;
        }

        var hideChildren, removeParentClass;

        $('#NavBar > ul > li > a').hover(function () {
            $(this).addClass('Active');
            if ($(this).parent().children('ul').length > 0) {
                $(this).parent().children('ul').children('li').children('a').addClass('Active');
                $(this).parent().children('ul').fadeIn(200).children('li').show().children('a').show();

            }
        }, function () {
            var elem = this;
            if (!isCurrentView(elem))
                removeParentClass = setTimeout(function () { $(elem).removeClass('Active'); }, 50);
            if ($(elem).parent().children('ul').length > 0) {
                hideChildren = setTimeout(function () { $(elem).parent().children('ul').fadeOut(200); }, 100);
                $(elem).parent().children('ul').hover(function () {
                    clearTimeout(hideChildren);
                    if (!isCurrentView(elem))
                        clearTimeout(removeParentClass);
                }, function () {
                    $(this).fadeOut(200);
                    if (!isCurrentView(elem))
                        $(elem).removeClass('Active');
                });
            }
        });
    }
        
}
