$(document).ready(function(){
    // Search
    $('input[type="search"]').each(function(){
        $(this).val('');
    }).bind('input', function(){
        var query = $(this).val().toLowerCase();
        var ul    = $(this).nextAll('ul');

        ul.find('li').each(function(){
            var title = $(this).children('a').text().toLowerCase();

            if (query === '' || title.indexOf(query) !== -1) {
                $(this).css('display', '');
            } else {
                $(this).css('display', 'none');
            }
        });
    });

    // Block links
    $('body > section > aside > ul > li').bind('click', function(event){
        event.preventDefault();

        window.location.href = $(this).find('a.more').attr('href');
    });

    // Print
    $('p.print-page > a').bind('click', function(event){
        event.preventDefault();

        window.print();
    });

    // Captcha reload
    $('form .captcha-row > div > img').bind('click', function(event){
        var uri = window.location.href;
        var img = $(this);

        $.post(uri, {newCaptcha: true}, function(data){
            img.attr('src', data.captchaUri);
            img.next('input[type="hidden"]').val(data.captchaId);
        });
    });

    // Placeholder
    if (!Modernizr.input.placeholder) {
        $('[placeholder]').each(function(){
            if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')) {
                $(this).val($(this).attr('placeholder'));
            }

            $(this).focus(function(){
                if ($(this).val() == $(this).attr('placeholder')) {
                    $(this).val('');
                    $(this).removeClass('placeholder');
                }
            }).blur(function(){
                if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')) {
                    $(this).val($(this).attr('placeholder'));
                    $(this).addClass('placeholder');
                }
            });
        });

        $('[placeholder]').closest('form').submit(function(){
            $(this).find('[placeholder]').each(function(){
                if ($(this).val() == $(this).attr('placeholder')){
                    $(this).val('');
                }
            })
        });
    }
});

