// Определяет произошло ли событие e вне блока block
/*function utils_isEventOut(block, e) {
    return $(e.target).closest(block).length == 0;
}*/

// Функция предзагрузки изображений
// В качестве атрибутов ссылки на изображения или массив с изображениями
function utils_preloadImages() {
    var images = (typeof arguments[0] == 'object') ? arguments[0] : arguments;
    for (var i = 0; i < images.length; i++) {
        $("<img>").attr("src", images[i]);
    }
}


/* Форма авторизации */
function authPopup_init(title, theme) {
    if (arguments[0]==undefined) title = '';
    if (arguments[1]==undefined) theme = 'light';

    var popup = $('.b-authpopup');
    popup.removeClass('b-authpopup-light b-authpopup-dark').addClass('b-authpopup-'+theme);
    popup.find('.title').remove();
    popup.css('z-index', '1001');
    
    if (title!='')
        popup.prepend('<div class="title">'+title+'</div>');

}

var showAuthPopup = function(el, title, offset) {

        theme = '';
        if ($('.b-toppanel').hasClass('b-toppanel-popupDark')) theme = 'dark';
        if ($('.b-toppanel').hasClass('b-toppanel-popupLight')) theme = 'light';

        authPopup_init(title, theme);

        var block = $('.b-authpopup');
        //var el = $(this);

        // Скрывает блок
        function hide() {
            block.hide();
            el.removeClass('active');
            $(document).unbind('mouseup', fnOutHide);
            $('.close', block).unbind('click', fnCloseClickHide);
        }

        // Скрывает блок при клике вне меню
        var fnOutHide = function(e) {
           /* if (utils_isEventOut(el, e) && utils_isEventOut(block, e))
                hide();
	   */
            if (!$(e.target).parents('.b-authpopup').length) {
                    hide();
		  }

        };

        // Скрывает блок при клике на крестике
        var fnCloseClickHide = function(e) {
            hide();
            return false;
        };

        // Показать/скрыть блок
        if (block.css('display')!='none') {

            hide();

        } else {

            $(this).addClass('active');
            if (offset) {
                block.css({top: offset.top, left: offset.left, right: ''}).show();
            } else {
                block.css({
                    'right': $('BODY').width() - $('.b-toppanel .l-base').offset().left - $('.b-toppanel .l-base').width() + 20 + 'px',
                    'top': '24px',
                    'left': ''
                }).show();
            }
            
            // При клике на крестик - скрывать
            $('.close', block).click(fnCloseClickHide);

            // При клике вне блока - скрывать
            $(document).mouseup(fnOutHide);

        }

        return false;
    }

/* Общая панель */
$(document).ready(function(){
    var more = $('.b-toppanel .menu-more');
    if (more.length) {
        var block = $('.b-toppanel .more-popup');
        var a = more.find('A');
        // "Другие сервисы" - Показать/скрыть блок
        a.click(function(){
            var el = $(this);

            // Скрывает блок
            function hide() {
                block.hide();
                el.removeClass('active');
                $(document).unbind('mouseup', fnOutHide);
            }

            // Скрывает блок при клике вне меню
            var fnOutHide = function(e) {
                /*if (utils_isEventOut(el, e) && utils_isEventOut(block, e))
                    hide();*/

		  if (!$(e.target).parents('.b-authpopup').length) {
                    hide();
		  }
            };

            // Показать/скрыть блок
            if (block.css('display')!='none') {

                hide();

            } else {

                $(this).addClass('active');
                block.show();

                // При клике вне блока - скрывать
                $(document).mouseup(fnOutHide);

            }

            return false;
        });

        // "Другие сервисы" - Инициализация
        block.css('left', a.offset().left - $('.b-toppanel .l-base').offset().left - 12 + 'px');

    }

    // Форма авторизации - Показать/скрыть
    $('.b-toppanel .user-menu .login A').click(function() {
        return showAuthPopup($(this));
    });

});

// Заглушка для трупов
$(function(){
    $('.test').click(function(){
        alert('Извините, функция находится в стадии тестирования.')

        return false;
    });
});

