'Widget renderer
@param field: the Field
@param value: the current value
@param attributes: the HTML attributes for the widget'

def __call__(self, field, value, **attributes):

    T = current.T
    if ('_id' in attributes):
        input_id = attributes['_id']
    else:
        input_id = str(field).replace('.', '_')
    s3 = current.response.s3
    formstyle = s3.crud.formstyle
    if ((not callable(formstyle)) or isinstance(formstyle('', '', '', ''), tuple)):
        widgetstyle = self._formstyle
    else:
        widgetstyle = formstyle
    controls = TAG[''](A(T('Select    from    Registry'), _id=('%s-select' % input_id), _class='action-btn'), A(T('Remove    Selection'), _id=('%s-clear' % input_id), _class='action-btn    hide', _style='padding-left:15px;'), A(T('Edit    Details'), _id=('%s-edit' % input_id), _class='action-btn    hide', _style='padding-left:15px;'), DIV(_class='throbber    hide', _style='padding-left:85px;'))
    controls = widgetstyle(('%s-select-row' % input_id), '', controls, '')
    controls.add_class(('box_top' if self.select_existing else 'hide'))
    s3db = current.s3db
    ctable = s3db[self.component]
    (prefix, resourcename) = self.component.split('_', 1)
    autocomplete = self.autocomplete
    if autocomplete:
        ac_field = ctable[autocomplete]
        widget = S3AutocompleteWidget(prefix, resourcename=resourcename, fieldname=autocomplete, link_filter=self.link_filter)
        selector = widgetstyle(('%s-autocomplete-row' % input_id), LABEL(('%s:    ' % ac_field.label), _class='hide', _id=('%s-autocomplete-label' % input_id)), widget(field, value), '')
        selector.add_class('box_top')
    else:
        widget = OptionsWidget.widget(field, None, _class='hide', _id=('dummy_%s' % input_id))
        label = LABEL(('%s:    ' % field.label), _class='hide', _id=('%s-autocomplete-label' % input_id))
        hidden_input = INPUT(_id=input_id, _class='hide')
        selector = widgetstyle(('%s-autocomplete-row' % input_id), label, TAG[''](widget, hidden_input), '')
        selector.add_class('box_top')
    fields = [f for f in ctable if ((f.writable or f.readable) and (not f.compute))]
    request = current.request
    if (field.name in request.post_vars):
        selected = request.post_vars[field.name]
    else:
        selected = None
    if selected:
        for f in fields:
            requires = (f.requires or [])
            if (not isinstance(requires, (list, tuple))):
                requires = [requires]
            [r.set_self_id(selected) for r in requires if hasattr(r, 'set_self_id')]
    (labels, required) = s3_mark_required(fields)
    if required:
        s3.has_required = True
    form = SQLFORM.factory(table_name=self.component, labels=labels, formstyle=formstyle, upload='default/download', separator='', *fields)
    formrows = []
    append = formrows.append
    for formrow in form[0]:
        if (not formrow.attributes['_id'].startswith('submit_record')):
            if hasattr(formrow, 'add_class'):
                formrow.add_class(('box_middle    embedded-%s' % input_id))
            append(formrow)
    formrows = TAG[''](formrows)
    divider = widgetstyle('', '', DIV(_class='subheading'), '')
    divider.add_class('box_bottom    embedded')
    appname = request.application
    if s3.debug:
        script = 's3.ui.embeddedcomponent.js'
    else:
        script = 's3.ui.embeddedcomponent.min.js'
    script = ('/%s/static/scripts/S3/%s' % (appname, script))
    if (script not in s3.scripts):
        s3.scripts.append(script)
    url = ('/%s/%s/%s/' % (appname, prefix, resourcename))
    options = {'ajaxURL': url, 'fieldname': input_id, 'component': self.component, 'recordID': str(value), 'autocomplete': (True if autocomplete else False)}
    post_process = s3db.get_config(self.link, 'post_process')
    if post_process:
        try:
            pp = (post_process % input_id)
        except TypeError:
            pp = post_process
        options['postprocess'] = pp
    script = ("$('#%(input)s').embeddedComponent(%(options)s)" % {'input': input_id, 'options': json.dumps(options)})
    s3.jquery_ready.append(script)
    return TAG[''](controls, selector, formrows, divider)
