var $ = jQuery, // Access to jQuery
    log = CmdUtils.log; // Access to 'console.log';
    
// Setup custom jQuery selector:
$.expr[':'].hasContent = function(elem) {
    return !$(elem).text().match(/^$|^\s+$/);
};

// Return google search URL:
function getGoogURL(sTerm) {
    return 'http://www.google.com/search?btnI=I\'m+Feeling+Lucky&q='
           + encodeURIComponent('site:https://developer.mozilla.org/')
           + ' ' + sTerm;
}

// Create new command:
CmdUtils.CreateCommand({

    name: "MDC",
    
    takes: {"Search term": noun_arb_text},
    
    // This function will be run when a user types in something
    // You can show whatever you want in the preview box (pBlock):
    preview: function( pBlock, input ) {
    
        if (!input.text) return;
      	$(pBlock).html('Wait a second...');
        var URL = getGoogURL(input.text);
        
        $.get(URL, function(response){
            var content = $(response).find('#pageText');
            if (!content[0]||!$('pre.eval:eq(0)',content)[0]||!$('p:hasContent:eq(0)',content)[0]) {
                $(pBlock).html('Eek! Nothing found!');
                return;
            }
            var para = '<p>' + content.find('p:hasContent:eq(0)').html()
                     + '... ' + '<a style="text-decoration:underline;" href="'
                     + URL + '">More &raquo;</a></p>',
                codeEg = content.find('pre.eval:eq(0)');
            $(pBlock).html(para).append(codeEg);
        });
        
    },
    
    // This function will be run when the user presses enter
    // after entering a command:
    execute: function( input ) {
    
        if (!input.text) return;
        Utils.openUrlInBrowser(getGoogURL(input.text));
        
    }
    
});