$(document).ready(function(){    
/**
 * Toggle search bar filter in header.
 ***********************************************************************/

 /* Inserts toggle button directly into HTML. */
    $('<a id=search-toggle>Blogs/Posts</a>').insertBefore('#simple-search .user-input');
    
    // Toggle between sites and posts for the radio button
    
    var searchtype = $('#simple-search input[checked="checked"]').attr('value');
    switch ( searchtype ) {
        case 'posts': 
            $('#search-toggle').attr('class', 'search-type'); 
            break;
        case 'sites':
        default:
            searchtype = 'sites';
            $('#search-toggle').attr('class', ''); 
            break;
    }

    // Replace the radio buttons with a hidden field which will be easier to manipulate in javascript
    $("#simple-search .radio-btns label").remove();
    $("#simple-search .radio-btns").after("<input type='hidden' name='return' value='" + searchtype +"' />"); 
    

 /* Adds class that specifies background image, and swaps out input value. */                    
    $('a#search-toggle').click(function(){
        var defaultText = { 'blogsearch' : 'Search for blogs...',
                            'postsearch' : 'Search for posts...' };
        $(this).toggleClass('search-type');   
        
        // Toggle between default text, but don't change if it's already been changed to something else
        if ( $(this).hasClass('search-type') && $('#header input:text').val() == defaultText.blogsearch)
            $('#header input:text').val(defaultText.postsearch);
        else if ( $('#header input:text').val() == defaultText.postsearch )
            $('#header input:text').val(defaultText.blogsearch);
        
        // Toggle between sites and posts for the radio button
        switch( $('#simple-search input[name="return"]').attr('value') ) {
            case 'sites':
                $('#simple-search input[value="sites"]').attr('value', 'posts');
                break;
            case 'posts':
                $('#simple-search input[value="posts"]').attr('value', 'sites');
                break;
        }
    });


/**
 * Toggle list of top movers in sidebar.
 ***********************************************************************/

/* Show fallers list. */
    $('#toggle-fallers').click(function(){ 
        $('#top-fallers').show();
        $('#top-risers').hide();
        $('#risers-fallers').css({'background-position':'0 -26px'});
        return false;
    });

/* Show risers list. */
    $('#toggle-risers').click(function(){
        $('#top-risers').show();
        $('#top-fallers').hide();
        $('#risers-fallers').css({'background-position':'0 0'});
        return false;
    });


/**
 * Hide/Show search result filters
 ***********************************************************************/
    $('.refine').toggle(
        function () {
            $(this).html('Hide refine options');                
            $('.search-refine').show('fast');
            $('.refine-submit').show('fast');
        },
        function () {
            $(this).html('Click to refine this search');
            $('.search-refine').hide('fast');
            $('.refine-submit').hide('fast');
        }            
    );
/**
 * Subchannel select menu (in channels)
 ***********************************************************************/
    $('#more-blogs select').change(function(){
        if ( this.value == '' ) return;
        window.location = window.location.protocol
                        + '//'
                        + window.location.host
                        + this.value
                        ;
    });
});