//------------------------ скрывание верхнего меню ----------------------------//
$(function() {
    scrolledMainMenu();
    $('div.toolsWrapper div.quickSearch input.search').textareaSuggestion().keydown(function(e) {
        if (e.which || e.keyCode) {
            if ((e.which == 13) || (e.keyCode == 13))
                searchClick();
        }
    }
    );

});

function scrolledMainMenu()
{
    if($('.main-menu .hiddenBlock').length == 0){return;}

    $window = $(window);
    $mainMenu=$('.main-menu');
    $hideMenu = $('.hiddenBlock');
    $headerWrapper = $('.headerWrapper');
    
    $window.scroll(function()
    {
        var $wsct = $window.scrollTop();
        if($wsct > 43 && (!$mainMenu.attr('pinned') || $mainMenu.attr('pinned') == 0))
        {
            $hideMenu.css('display', 'none');
            $headerWrapper.css('position', 'fixed');
        }
        else
        {
            $hideMenu.css('display', 'block');
            $headerWrapper.css('position', 'fixed');
        }
    });

    $('.menu-line').hover(function(){
        $hideMenu.css('display','block');
    });

    $mainMenu.hover(function(){}, function()
    {
        if(!$mainMenu.attr('pinned') || $mainMenu.attr('pinned') == 0)
        {
            if($window.scrollTop() > 43)
            {
                $hideMenu.css('display','none');
            }
        }
    });

    $mainMenu.click(function()
    {        
        if($(this).attr('pinned') && $(this).attr('pinned') == 1)
        {
            $(this).attr('pinned', '0');
        }
        else
        {
            $(this).attr('pinned', '1');
        }
        
        return true;
    });

}

//----------------- запуск поиска через поле в верхнем меню -------------------//
function searchClick()
{
    var tbxSearch = $('div.toolsWrapper div.quickSearch input.search');
    if (tbxSearch.val().trim() != "" && !tbxSearch.hasClass('suggestion'))
        __doPostBack("", tbxSearch[0].id);
}

//--------------------- окно feedback`а ---------------------------------------//
//показывает форму сообщения об ошибке

var makeIncampusTxt;
var groupAdminTxt;

function blockPopUpShow(str)
{
    overDraw = true;
    
    if (str == null) 
    {
        str = "";
        $('#popUpHolderDiv p:first').show();
        $('#popUpHolderDiv p:last').hide();
        $('#divAction a.button-Red').html(makeIncampusTxt);
    }
    else
    {
        $('#popUpHolderDiv p:first').hide();
        $('#popUpHolderDiv p:last').show();
        $('#divAction a.button-Red').html(groupAdminTxt);
    }
    
    var editor = $('#popUpHolderDiv textarea');
    editor.val(str);
    
    blockPopUpCenter();

    $('#popUpFeedbackForm').show();
    $('#popUpFeedbackFormDiv').show();

    if (str == "") editor.focus();

    $(document).keydown(function(e) {
        if (e.which == 27) {
            blockPopUpHide();
        }
    }
    );

    return 0;     
}

//скрывает форму сообщения об ошибке
function blockPopUpHide()
{
    overDraw = false;
    $('#popUpFeedbackForm').hide();
    $('#popUpFeedbackFormDiv').hide();

    return 0;
}

function blockPopUpCenter()
{
    var element = $('#popUpFeedbackForm');
    element.css("position","absolute").
        css("top", ( $(window).height() - element.height() ) / 2+$(window).scrollTop() + "px").
        css("left", ( $(window).width() - element.width() ) / 2+$(window).scrollLeft() + "px");
}
// -------------------------------------------------------------------
$.fn.textareaSuggestion = function() {
    return this.each(
        function() {
            function isSuggestion(o) {
                return o.val().length > 0 && o.attr('suggestion') == o.val() || o.val().length == 0;
            }

            if (isSuggestion($(this))) {
                $(this).val($(this).attr('suggestion')).addClass('suggestion');
            }

            $(this)
            .click(function() {
                if ($(this).hasClass('suggestion')) {
                    $(this).removeClass('suggestion').val('');
                }
            })
            .blur(function() {
                if (isSuggestion($(this))) {
                    $(this).val($(this).addClass('suggestion').attr('suggestion'));
                }
            });

        }
    );
}

// --------------------------------------------------------------------------------------------
function getEventKeyCode(event) {
    var key = 0;
    if (event) {
        key = event.keyCode;
    }
    else if (window.event)
        key = window.event.keyCode; //IE
    else
        key = event.which; //firefox
    return key;
}
// --------------------------------------------------------------------------------------------
String.prototype.replaceAll = function(search, replace) {
    return this.split(search).join(replace);
}

