{% macro menu_block(menu) %}
	{% for item1 in menu.get_list() %}
		{% if item1 | is_menu_visible %}
			{% if item1.childs %}
				<li class="dropdown">
				<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
				{{_(item1.name)}}<b class="caret"></b></a>
		        <ul class="dropdown-menu">
				{% for item2 in item1.childs %}
					{% if item2.childs %}
						<li class="dropdown">
                        	        <a tabindex="-1" href="{{item2.get_url()}}">
                        	        <i class="fa {{item2.icon}}"></i> - {{_(item2.name)}}</a>
						</li>
					{% else %}
						{% if item2.name == '-' %}
                       		<li class="divider"></li>
                    	{% else %}
							{% if item2 | is_menu_visible %}
								<li><a tabindex="-1" href="{{item2.get_url()}}">
								<i class="fa {{item2.icon}}"></i> - {{_(item2.name)}}</a></li>
							{% endif %}
						{% endif %}
					{% endif %}
				{% endfor %}
				</ul></li>
			{% else %}
				<li>			
				<a tabindex="-1" href="{{item1.get_url()}}">
            	<i class="fa {{item1.icon}}"></i> - {{_(item1.name)}}</a>
            	</li>
        	{% endif %}
        {% endif %}
    {% endfor %}
{% endmacro %}

{% macro locale_menu(languages) %}
{% set locale = session['locale'] %}
{% if not locale %}
	{% set locale = 'en' %}
{% endif %}
<li class="dropdown">
	<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
	<ul class="f16"><li class="flag {{languages[locale].get('flag')}}"></li></ul>
	</a>
	<ul class="dropdown-menu">
	<li class="dropdown">
		{% for lang in languages %}
			{% if lang != locale %}
        		<a tabindex="-1" href="/lang/{{lang}}?next={{request.path}}">
        		<ul class="f16"><li class="flag {{languages[lang].get('flag')}}"></li> - {{languages[lang].get('name')}}</ul>
        		</a>
        		{% endif %}
        	{% endfor %}
        </li>
        </ul>
</li>
{% endmacro %}       

{% macro navbar_block(menu, languages) %}
{% if menu.reverse %}
	<div class="navbar navbar-inverse" role="navigation">
{% else %}
	<div class="navbar" role="navigation">
{% endif %}
   <div class="container">
        <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                {% if baseapp.app_icon %}
                	<a class="brand" href="{{url_for('%s.%s' % (baseapp.indexview.endpoint, baseapp.indexview.default_view))}}">
                	<img src="{{baseapp.app_icon}}" height="50px" width="100%">
                	</a>
                {% else %}
                	<span class="navbar-brand">
                	<a href="{{url_for('%s.%s' % (baseapp.indexview.endpoint, baseapp.indexview.default_view))}}">
                	{{ baseapp.app_name }}
                	</a>
                	</span>
                {% endif %}
        </div>
        <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                        {{ menu_block(menu)}}
                </ul>
                <ul class="nav navbar-nav navbar-right">
                {{ locale_menu(languages) }}
                {% if not current_user.is_anonymous() %}
				<li><a href="/logout"><i class="fa fa-sign-out"></i>{{_("Logout")}}</a></li>
                                <li><a class="pull-right" href="/users/userinfo"><i class="fa fa-user"></i> {{g.user.get_full_name()}}</a></li>
                {% else %}
                                <li><a href="/login"><i class="fa fa-sign-in"></i>{{_("Login")}}</a></li>
                {% endif %}
                </ul>
        </div>
   </div>
</div>

{% endmacro %}
