function js_clock( date_id, time_id )
{
    var days = ['воскресенье', 'понедельник', 'вторник', 'среда', 'четверг', 'пятница', 'суббота'];
    var months = ['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря'];

    var f_num = function(num)
    {
        return num < 10 ? '0' + num : num;
    }

    var date_elem = $('#'+date_id);
    var time_elem = $('#'+time_id);

    var update = function()
    {
        try
        {
            var d = new Date();
            date_elem.text( $.sprintf("%s, %s %s %s",
                days[d.getDay()], d.getDate(), months[d.getMonth()], d.getFullYear() )
            );
            time_elem.text( $.sprintf("%s:%s:%s",
                f_num(d.getHours()), f_num(d.getMinutes()), f_num(d.getSeconds()) )
            );
        }
        catch (e)
        {
            //alert(e.message);
        }
    }

    update();
    setInterval( update, 500 );
}

function initTopMenu()
{
    var closeMenuInfo = {}

    var showDropDownMenu = function()
    {
        var $this = $(this);

        var item =  $this.is('.nav-item') ? $this : $this.parent('.nav-item');
        var title = $('.nav-item-title', item);
        var btn   = $('.nav-submenu-button', item);

        var submenuId = item.attr('id') + '_submenu';
        var smenu = $('#' + submenuId);

        var _smenu = smenu.get(0);
        if ( _smenu )
        {
            if ( !_smenu._initialized )
            {
                _smenu._initialized = true;
                smenu.bind('mouseleave', $.bind(null, closeMenu, submenuId));
                smenu.bind('mouseenter', $.bind(null, cancelClosing, submenuId));
                $('.nav-sm-item', smenu).hoverToggleClass('nav-sm-item-over');
            }

            var size = title.offset();
            size.width = btn.offset().left + btn.width() - size.left;
            size.top  += 24;

            smenu.css('top', size.top)
                 .css('left', size.left)
                 .css('z-index', 1000)
                 .css('width', size.width);

            smenu.show(500);
        }
    }

    var closeMenu = function( submenuId )
    {
        var f = function()
        {
            $('#'+submenuId).hide(200);
            closeMenuInfo[submenuId] = null;
        };

        cancelClosing( submenuId );
        closeMenuInfo[submenuId] = setTimeout( f, 500 );
    }

    var cancelClosing = function( submenuId )
    {
        if ( closeMenuInfo[submenuId] )
        {
            clearTimeout( closeMenuInfo[submenuId] );
            closeMenuInfo[submenuId] = null;
        }
    }

    $('#navigation .nav-item').each( function()
    {
        $this = $(this);
        $this.bind( 'mouseleave', $.bind(null, closeMenu, this.id + '_submenu') );
        $this.bind( 'mouseenter', $.bind(null, cancelClosing, this.id + '_submenu') );

        var title = $('.nav-item-title', this);
        var btn = $('.nav-submenu-button', this);

        btn.css('display', 'block');
        $this.parent('li').css('display', 'block');

        var mouseOut = function() {
            title.removeClass('nav-item-title-over');
            if ( btn.size() )
            {
                btn.removeClass('nav-submenu-button-over');
            }
        };

        var mouseOver = function() {
            title.addClass('nav-item-title-over');
            if ( btn.size() )
            {
                btn.addClass('nav-submenu-button-over');
                ($.bind(this, showDropDownMenu))();
            }
        };

        $this.hover( mouseOver, mouseOut );
    } );

};

function showAlert( title, message )
{
    $(document.body).append(
        $.sprintf('<div id="alert-box" title="%s">%s</'+'div>', title, message)
    );
    var alert = $("#alert-box");
    alert.dialog(
    {
        position:     'center',
        resizable:    false,
        modal:        true,

        close: function() { $(this).dialog('destroy').remove(); },

        overlay:
        {
            backgroundColor: '#000',
            opacity: 0.2
        },

        buttons:
        {
            'OK': function() { $(this).dialog('destroy').remove(); }
        }
    });
    alert.show();
}


