jQuery.fn.textdropdown = function() {
  with ($(this)) {
    with (next('ul:first')) {
      find('li').click(function() {
        $(this).parent().prev('.textdropdown-outer').find('input:first').attr('value', $(this).html());
        $(this).parent().hide();
      });
      hide();
    }

    keypress(function() {
      $(this).parent().next('ul:first').hide();
    });

    change(function() {
      $(this).parent().next('ul:first').hide();
    });

    wrap('<div class="textdropdown-outer" style="width: ' + width() + 'px; height: ' + (height() + 4) + 'px"></div>');
    var btn = parent().prepend('<div class="textdropdown-btn">&nbsp;</div>').find('.textdropdown-btn');
    width(width() - btn.width() - 5);
	css('border', 'none');

    btn.click(function() {
      var p = parent();
      with (p.next('ul:first')) {
        css('position', 'absolute');
        css('width',    p.width());
        css('left',     p.position().left);
        css('top',      p.position().top + p.height() + 1);
        toggle();
      }
    });
  }
}

