var topsearchbox_hidden = true;
var topsearchbox_selected_eng = 'portal';
var topsearchbox_input_empty = true;

function topsearchbox_toggle() {
    if(topsearchbox_hidden) {
        topsearchbox_show();
    } else {
        topsearchbox_hide();
    }
    return false;
}

function topsearchbox_hide() {
    $('.topsearchbox_selector').hide();
    topsearchbox_hidden = true;
    return false;
}

function topsearchbox_show() {
    $('.topsearchbox_selector').show();
    topsearchbox_hidden = false;
    return false;
}

function topsearchbox_portal() {
    topsearchbox_selected_eng = 'portal';
    $('form[name="searchForm"]').attr('action', '/dieta/szukaj/')
    $('form[name="searchForm"]').attr('method', 'get')
    $('#topsearchbox_input').attr('name', 'SPHRASE');
    topsearchbox_refresh();
    topsearchbox_hide();
    return false;
}

function topsearchbox_forum() {
    topsearchbox_selected_eng = 'forum';
    $('form[name="searchForm"]').attr('action', '/grupy_wsparcia/search.php?do=process')
    $('form[name="searchForm"]').attr('method', 'post')
    $('#topsearchbox_input').attr('name', 'query');
    topsearchbox_refresh();
    topsearchbox_hide();
    return false;
}

function topsearchbox_refresh() {
    if(topsearchbox_input_empty) {
        var filler_text;
        switch(topsearchbox_selected_eng) {
            case 'forum':
                filler_text = $('#topsearchbox_engine_forum').text();
                break;
            case 'portal':
            default:
                filler_text = $('#topsearchbox_engine_portal').text();
                break;
        }
        $('#topsearchbox_input').val( filler_text);
    }
}

function topsearchbox_submit() {
    if(topsearchbox_input_empty) {
        alert('Podaj frazę, którą chcesz wyszukać');
        return false;
    } else {
        document.searchForm.submit();
        return false;
    }
}

$(document).ready(function() {

    $('#topsearchbox_input').focus(function() {
        if(topsearchbox_input_empty) {
            $(this).val('');
            $(this).css('color','#666666');
        }
    });

    $('#topsearchbox_input').blur(function() {
        if($(this).val()=='') {
            $(this).css('color','#aaaaaa');
            var filler_text;
            switch(topsearchbox_selected_eng) {
                case 'forum':
                    filler_text = $('#topsearchbox_engine_forum').text();
                    break;
                case 'portal':
                default:
                    filler_text = $('#topsearchbox_engine_portal').text();
                    break;
            }
            $(this).val(filler_text)
            topsearchbox_input_empty = true;
        } else {
            $(this).css('color','#666666');
            topsearchbox_input_empty = false;
        }

    });

});
