$(document).ready(function()
{
    initTopMenu();
    initTopMenuSearch();
    initVideoList();
});

function initTopMenu()
{
    var $menu = $('#top-menu');
    $menu.children('.menu-item')
         .click(function()
         {
            var link = $(this).find('a').attr('href');
            window.location.replace(link);
         })
         .hover(
            function()
            {
                $(this).addClass('menu-item-hover')
                       .find('.menu-icon')
                       .addClass('menu-icon-hover');
            },
            function()
            {
                $(this).removeClass('menu-item-hover')
                       .find('.menu-icon')
                       .removeClass('menu-icon-hover');
            }
         );
}

function initTopMenuSearch()
{
    var $search = $('#top-menu-search');
    var defaultValue = $search.val();

        $search.click(function(evt)
        {
            evt.preventDefault();
            evt.stopPropagation();
        })
        .focus(function(evt)
        {
            if ($search.val() == defaultValue)
            {
                $search.val('');
            }
        })
        .blur(function(evt)
        {
            if ($search.val().length == 0)
            {
                $search.val(defaultValue);
            }
        })
        .autoComplete({
           ajax: websiteUrl + 'service/autocomplete/',
           minChars: 2,
           onSelect: function(event, ui)
           {
               window.location.replace(websiteUrl + 'school/' + ui.data.school_id);
           }
       });
}

function initVideoList()
{
    var $videosWrapper = $('#videos-wrapper');
    if ($videosWrapper.length > 0)
    {
        $videosWrapper.children('.video').find('.screenshot, .info').hover(
            function()
            {
                $(this).parent().addClass('video-hover');
            },
            function()
            {
                $(this).parent().removeClass('video-hover');
            }
        )
        .click(function()
        {
            var gotoUrl = $(this).find('a').attr('href');
            window.location.replace(gotoUrl);
        });
    }
}