
$(function()
{

    var $topSearchText = $('#top-search-text');
    $topSearchText.focus(function()
    {
        var val = $topSearchText.val();
        if (val == '' || val == 'Что будем искать?')
        {
            $topSearchText.val('');
            $topSearchText.removeClass('top-search-text-empty');
        }
    }).blur(function()
    {
        var val = $topSearchText.val();
        if (val == '' || val == 'Что будем искать?')
        {
            $topSearchText.val('Что будем искать?');
            $topSearchText.addClass('top-search-text-empty');
        }
    }).blur();

    var $topSearchSelect = $('#top-search-select');
    var $topSearchSelectOptions = $('#top-search-select-options');
    $topSearchSelect.hover(function()
    {
        $topSearchSelectOptions.fadeIn('fast');
    }, function()
    {
        $topSearchSelectOptions.fadeOut('fast');
    });

    $('#top-search-select-options a').click(function()
    {
        var $this = $(this);
        $('#top-search-select-value').val($this.attr('href').substr(1));
        $('#top-search-select-current').html($this.text());
        $topSearchSelectOptions.fadeOut();
        return false;
    });

    $('.events-calendar .with_tip').hover(function()
    {
        var $this = $(this);
        var $tip = $this.children('.tip');
        $tip.css('top', ($this.offset().top + $this.outerHeight() - 10) + 'px');
        var left = $this.offset().left - $tip.outerWidth() + 10;
        if (left < 0)
        {
            left = 0;
        }
        $tip.css('left', left + 'px');
        $tip.fadeIn('fast');
    }, function()
    {
        $(this).children('.tip').fadeOut('fast');
    });



    var hscrollInterval = null;
    var hscrollIntervalMs = 20;
    var hscrollIntervalPx = 10;

    function hscrollStop()
    {
        if (hscrollInterval)
        {
            clearInterval(hscrollInterval);
            hscrollInterval = null;
        }
    }

    function hscrollScroll($scrollBlock, direction)
    {
        $scrollBlock.scrollLeft($scrollBlock.scrollLeft() + direction * hscrollIntervalPx);
    }

    function hscrollStartScroll($button, direction)
    {
        if (!$button.data('scrollBlock'))
        {
            $button.data(
                'scrollBlock',
                $button.parents('.hscroll-btns').find('.hscroll-block')
            );
        }
        var $scrollBlock = $button.data('scrollBlock');
        hscrollScroll($scrollBlock, direction);
        hscrollInterval = setInterval(function()
        {
            hscrollScroll($scrollBlock, direction);
        }, hscrollIntervalMs);
    }

    $('.hscroll-btn-left a').mousedown(function()
    {
        hscrollStartScroll($(this), -1);
        return false;
    }).mouseup(function()
    {
        hscrollStop();
        return false;
    }).mouseout(function()
    {
        hscrollStop();
        return false;
    }).click(function()
    {
        return false;
    });

    $('.hscroll-btn-right a').mousedown(function()
    {
        hscrollStartScroll($(this), 1);
        return false;
    }).mouseup(function()
    {
        hscrollStop();
        return false;
    }).mouseout(function()
    {
        hscrollStop();
        return false;
    }).click(function()
    {
        return false;
    });

//    hscrollScroll($('.hscroll-block'), 1);

});
