;jQuery.fn.inputblur = function(){
    this.each(function(){
        var startText;
        $(this).focus(function(){
            if (typeof(startText) == 'undefined'){
                startText = $(this).val();
            }
            if ($(this).val() == startText) {
                $(this).val('');
            }
        }).blur(function(){
            if ($(this).val() == '') {
                $(this).val(startText);
            }
        });
    });
};

$(function(){
    /* Text inputs: start tekst weghalen of terugzetten, onfocus/onblur */
    $('#comment-text, #keyword, #text').inputblur();

    /* Categorieen: in- en uitklappen */
    $('#categorybox li').click(function(e){
        var target = $(e.target);
        if (false == target.is('#categorybox li ul li a')){
            if ($(this).attr('id') == 'active') {
                $('#active').attr('id', null);
            } else {
                $('#active').attr('id', null);
                $(this).attr('id', 'active');
            }
            return false;
        }
    });

    /* Poll: stem resultaten met ajax ophalen na het stemmen */
    $('#pollform input').change(function(){
        $.get(
            '/poll-stem/' + $(this).val(), // url
            null,                          // data
            function(response){            // callback
                $('#pollform').remove();
                $('#poll').append(response);
            }
        );
    });

    /* Categorie filter: categorie wisselen na keuze uit de dropdown */
    $('#viewcategory').change(function(){
        window.location = '/filtercategory/' + $(this).val();
    });
});
