// Let's write our own noun-type
var noun_type_wps_funcs = {
    _name: "function",
    
    suggest: function suggest(text, html, makeSuggestion) {

        var suggestions = [CmdUtils.makeSugg(text)];
        jQuery.ajax({
            dataType: 'jsonp',
            jsonp: 'jsonp_callback',
            url: 'http://api.wpseek.com/',
            data: ({ method: 'wordpress.getsimilar', s: text, limit: 5 }),
            cache: true,
            success: function suggestFuncs( j ) {
                var i, results, result;
                for (i = 0; i < 4; i++) {
                    result = j.wps.entries[ i ];
                    //CmdUtils.log( result.title );
                    makeSuggestion( CmdUtils.makeSugg( result.name ) );
                }
            }
        });
        // currently limited by Ubiquity to at most five
        return suggestions;
    }
}

CmdUtils.CreateCommand({
    /*name: "wpseek",
    synonyms: ["wps"],*/
	names: ['wpseek', 'wps'],
    /*takes: {"function": noun_type_wps_funcs},*/
	arguments: [ {role: 'object', nountype: noun_type_wps_funcs, label: 'function'} ],
    description: "Lookup WordPress Functions or Template Tags with wpseek.com",
    icon: "http://s.wordpress.org/favicon.ico?2",
    license: "GPL",
    homepage: "http://wpseek.com/",
    author: { name: "Oliver Schloebe", email: "office AT wpseek.com"},
    help: "Try it out: type &quot;wpseek bloginfo&quot;.",
  
    preview: function(pblock, args) {
        pblock.innerHTML = '<h2 style="background:url(http://wpseek.com/ubiquity/ubi_heading.png) left center no-repeat; padding-left:38px;">Loading ...</h2>';
        var wpssearchText = jQuery.trim( args.object.text );
        if (wpssearchText.length > 2) {
            jQuery.ajax({
                dataType: 'jsonp',
                jsonp: 'callback',
                url: 'http://api.wpseek.com/',
                data: ({ method: 'wordpress.getfunction', s: wpssearchText }),
                cache: true,
                error: function() {
                    pblock.innerHTML = "Whoops, error loading JSON data. Sorry.";
                },
                success: function(j) {
                    if( j.wps[0].status == 'error' ) {
					pblock.innerHTML = '<h2 style="background:url(http://wpseek.com/ubiquity/ubi_heading.png) left center no-repeat; padding-left:38px;">' + j.wps[0].status + ' ...</h2>';
                        //pblock.innerHTML = j.wps[0].message;
                        return;
                    }
                    if( typeof j.wps[0].name == 'undefined' ) {
                        var wpsfuncname = 'Search for ' + term.text;
                    } else {
                        var wpsfuncname = j.wps[0].name;
                    }
                    if( typeof j.wps[0].type == 'undefined' ) {
                        var wpsfunctype = '';
                    } else {
                        var wpsfunctype = j.wps[0].type;
                    }
                    pblock.innerHTML = '<h2 style="background:url(http://wpseek.com/ubiquity/ubi_heading.png) left center no-repeat; padding-left:38px;">' + wpsfuncname + ' <span style="font-weight:200;"><em>' + wpsfunctype + '</em></span></h2>';
                    pblock.innerHTML += '<p style="background:#333;padding:6px;border:solid 1px #777;-moz-border-radius:10px;">' + j.wps[0].descriptionHtml.replace('href="', 'href="http://codex.wordpress.org') + '</p>';
                    pblock.innerHTML += '<p>More information at: <a href="' + encodeURI(j.wps[0].codexURI) + '" target="_blank">' + j.wps[0].codexURI + '</a></p>';
                    pblock.innerHTML += '<p>Hit Enter key to browse to <a href="http://wpseek.com/' + encodeURI(j.wps[0].name) + '/" target="_blank">http://wpseek.com/' + j.wps[0].name + '/</a></p>';
                    pblock.innerHTML += '<hr />';
                    pblock.innerHTML += '<p><a href="#" id="wpscodesnippets" class="wpslinks" style="background:#444; -moz-border-radius:10px; padding:5px; font-size:80%; text-decoration:none;">Code Snippets</a> <a href="#" id="wpsdiscussions" class="wpslinks" style="background:#444; -moz-border-radius:10px; padding:5px; font-size:80%; text-decoration:none;">Forum discussions</a></p>';
                    pblock.innerHTML += '<div id="wpsresults"></div>';
                  
                    // Some js event handlers
                    jQuery('a#wpscodesnippets', pblock).click(function() {
                        jQuery('div#wpsresults', pblock).html( 'Loading ...' );
                        jQuery.ajax({
                           dataType: 'jsonp',
                           jsonp: 'callback',
                           url: 'http://api.wpseek.com/',
                           data: ({ method: 'wordpress.getsnippets', s: wpssearchText, limit: 2 }),
                           cache: true,
                           success: function( jc ) {
                              var k, coderesult, codeHtml;
                              codeHtml = '';
                              for (k = 0; k <= 1; k++) {
                                 coderesult = jc.wps.entries[ k ];
                                 if( coderesult.file != '' ) {
                                    var wpscodepatt = coderesult.snippet.match(/function/gi);
                                    if( wpscodepatt ) {
                                       codeHtml += '<a href="' + coderesult.link + '" target="_blank">' + coderesult.file + '</a><br /><pre style="overflow:auto; border:solid 1px #444;">' + coderesult.snippet + '</pre>';
                                    } else {
                                       codeHtml += coderesult.file + '<br /><pre style="overflow:auto; border:solid 1px #444;">' + coderesult.snippet + '</pre>';
                                    }
                                 }
                              }
                              jQuery('div#wpsresults', pblock).html( codeHtml );
                           }
                        });
                    });
                    jQuery('a#wpsdiscussions', pblock).click(function() {
                        jQuery('div#wpsresults', pblock).html( 'Loading ...' );
                        jQuery.ajax({
                           dataType: 'jsonp',
                           jsonp: 'callback',
                           url: 'http://api.wpseek.com/',
                           data: ({ method: 'wordpress.gettopics', s: wpssearchText, limit: 5 }),
                           cache: true,
                           success: function( jf ) {
                              var k, forumresult, forumHtml;
                              forumHtml = '<ul>';
                              for (k = 0; k <= 4; k++) {
                                 forumresult = jf.wps.entries[ k ];
                                 if( forumresult.title != '' ) {
                                    forumHtml += '<li><a href="' + forumresult.link + '" target="_blank">' + forumresult.title + '</a><br /><em>' + forumresult.pubDate + '</em></li>';
                                 }
                              }
                              forumHtml += '</ul>';
                              jQuery('div#wpsresults', pblock).html( forumHtml );
                           }
                        });
                    });
                    jQuery('a.wpslinks', pblock).click(function() {
                        jQuery('a.wpslinks', pblock).css('background', '#444444');
                        jQuery(this).css('background-color', '#666666');
                    });
                }
            });
        } else {
            pblock.innerHTML = "Lookup WordPress Functions or Template Tags with wpseek.com";
            return;
        }
    },
    execute: function(args) {
        Utils.openUrlInBrowser('http://wpseek.com/' + encodeURI( args.object.text ) + '/');
    }
})