/**
 * (c) by aheadware.com
 */
"use strict";

var app = app || {};
app.gui = app.gui || {};

/**
 * Navbar and its items
 */
app.gui.Navbar = (function()
{
    var $navRoot,
        $notificationBadge,
        $notificationDropdown,
        $messageBadge,
        $messageDropdown;

    return {
        init : function()
        {
            $navRoot = app.core.View.getBody().find( 'nav' ).first();
            $messageBadge = $navRoot.find( '[data-id="nav-span-message-badge"]' ).first();
            $messageDropdown = $navRoot.find( '[data-id="nav-div-message-dropdown"]' ).first();
            $notificationBadge = $navRoot.find( '[data-id="nav-span-notification-badge"]' ).first();
            $notificationDropdown = $navRoot.find( '[data-id="nav-div-notification-dropdown"]' ).first();

            // Init with empty values
            this.setNotificationDropdown( [] );
            this.setMessageDropdown( [] );
        }
        ,
        /**
         *
         * @param notifications[<app.model.Notification>]
         */
        setNotificationDropdown : function( notifications )
        {
            var cntNew = 0;

            if ( notifications && notifications.length > 0 )
            {
                for ( var ni = 0; ni < notifications.length; ni ++ )
                {
                    if ( true === notifications[ ni ].isUnread()  )
                    {
                        cntNew++;
                    }
                }
            }

            if ( 0 === cntNew )
            {
                //$notificationBadge.html( "0" );
                $notificationBadge.removeClass( 'visible' );
                $notificationBadge.addClass( 'invisible' );
            }
            else
            {
                //$notificationBadge.html( "" + cntNew );
                $notificationBadge.removeClass( 'invisible' );
                $notificationBadge.addClass( 'visible' );
            }

            $notificationDropdown.html(
                app.core.View.getTemplate( 'gui-navbar-notification-dropdown', { notifications: notifications || [] } )
            );
        }
        ,
        setMessageDropdown : function( messages )
        {
            if ( messages && messages.length > 0 )
            {
                $messageBadge.removeClass( 'invisible' );
            }
            else
            {
                $messageBadge.addClass( 'invisible' );
            }

            $messageDropdown.html(
                app.core.View.getTemplate( 'gui-navbar-message-dropdown', { messages: messages || [] } )
            );
        }
    }
})();