$(document).ready(function(){
                      
    // Fake a click on region search input when icon is clicked
    $('#region-search').click( function () {
    	console.log('lol');
    	$('#school-search .submit').click();
    })
    
    // Nav menu
    $('nav ul.sf-menu').superfish({ 
            delay:       1000,                          // one second delay on mouseout 
            animation:   {opacity:'show'},  			// fade-in and slide-down animation 
            speed:       'medium',                        // faster animation speed 
            autoArrows:  true,                          // disable generation of arrow mark-up 
            dropShadows: true,                          // disable drop shadows
            pathClass:   'active',
            pathLevels:  2 
        });  
    
    // setup form placeholders
    $('input').each(function(index, element)
    {
        $(element).attr('value', $(element).attr('placeholder'));
        
    }).focus(function(event)
    {
        var element = $(event.target);
        if ($(element).attr('value') == $(element).attr('placeholder'))
        {
            $(element).attr('value', '');
        }
    }).blur(function(event)
    {
        var element = $(event.target);
        if ($(element).attr('value') == '')
        {
            $(element).attr('value', $(element).attr('placeholder'));
        }
    });

    var event_id = false;
    var event_element = false;
    // setup menu navigation
    $('nav ul.menu > li').each(function(index, el)
    {
        var element = $(el);

        element.hover(function(event)
        {
            event_id = false;
            event_element = element;
            $('nav ul.menu > li ul').hide();
            element.find('ul').show().css('position', 'absolute');
        
        }, function (event)
        {
            var my_event_id = Math.random();
            event_id = my_event_id;
            
            setTimeout(function () {
                           if (event_element != element || event_id == my_event_id)
                           {
                               element.find('ul').hide();
                           }
                           
                           if (my_event_id == event_id)
                           {
                               $('nav ul.menu > li.active ul').show();
                           }
                       }, 1500);
        });
    });
                      
    $('article h1, article h2 a, #region h3').each(function ()
    {
        var text = $(this).text().trim().split(' ');
        text[0] = '<span class="first">' + text[0] + "</span>";
        $(this).html(text.join(' '));
    });
                      
    $('aside ul.images').each(function (index, el)
    {
        var images = $('li', el);
        images.hide().first().show();
        
        show_next = function (el)
        {
            $(el).hide();
            var next = images.get(index + 1); 
            
            if (next)
            {
                $(next).show();
            }
            else
            {
                $(images.get(0)).show();
            }

        };
        
        $('li', el).each(function (index, el)
                         {
                             $(el).click(function (event)
                                         {
                                             show_next(el);
                                         });
                         });
    });
    
});
                  

