(function($) {
	$.fn.dropify = function(options) {
		return this.each(function() {
			var hideDropList = function(e) {
				$list.slideUp(100, function() {
					$wrapper.removeClass('active');
				});
				$(document).unbind('click.dropify', hideDropList);
			};

			var $list = $(this).clone().addClass('dropified');
			var $wrapper = $(document.createElement('div')).addClass('dropzone');
			var $listContainer = $(document.createElement('div')).addClass('droplist');
			var $selectedLink = $list.find('a.selected').first();

			if ($selectedLink.length === 0) {
				$selectedLink = $list.find('a:first');
			}

			$selectedLink.parent('li').remove();

			$selectedLink.click(function(e) {
				if ($list.is(':visible')) {
					hideDropList();
				} else {
					$wrapper.addClass('active');
					$list.slideDown(100);
					$(document).one('click.dropify', hideDropList);
				}

				return false;
			});

			$selectedLink.appendTo($wrapper);
			$list.appendTo($listContainer);
			$listContainer.appendTo($wrapper);
			$(this).replaceWith($wrapper);
		});
	};
})(jQuery);
