function urlencode(str) {
    return escape(str).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
}

var DicioWidget = {};

DicioWidget.requestData = function(l) {
    var palavra = jQuery('#dicio-palavra').val();
    var url = 'http://www.dicio.com.br/widgets/api.php?q=' + urlencode(palavra) + '&l=' + l;

    jQuery('.dicio-content').hide();
    jQuery('.dicio-loading').show();
    jQuery('.dicio-content').html('');

    jQuery.getJSON(url, '', DicioWidget.processData);
}

DicioWidget.processData = function(json) {
    var item;
    var html = '<h1>Busca por ' + json.data.word + '</h1>';
    switch(json.result) {
    case 'word':
        html = html + '<p>' + json.data.definition + '</p>';
        html = html + '<a href="' + json.data.link + '" target="_blank">Mais sobre esta palavra...</a>';
        break;
    case 'list':
        html = html + '<ul>';
        for(var i=0;i<json.data.items.length;i++) {
            item = json.data.items[i];
            html = html + '<li><a href="' + item.link + '" target="_blank">' + item.word + '</a></li>';
        }
        html = html + '</ul>';
        html = html + '<a href="' + json.data.link + '" target="_blank">Ver mais palavras...</a>';
        break;
    case 'empty':
        html = html + '<p>Não foram encontradas palavras na busca por <strong>' + json.data.word + '</strong>.</p>';
        break;
    }
    jQuery('.dicio-content').html(html);
    jQuery('.dicio-loading').hide();
    jQuery('.dicio-content').show();
}