$(function(){
	$(document).click(function(){
		$('div.select .options').hide();
	});
	
	var pSelects = $('div.select');
	pSelects.each(function(index){
		pSelect = $(this);
		pOptionsDiv = pSelect.find('.options');
		pTitle = pSelect.find('.title');
		pTitle.click(function(e){
			pTitle.next('.options').show();
			e.stopPropagation();
		});
		rSelect = $(pSelect.find('select')[0]);
		selectedIndex = rSelect.get(0).selectedIndex;
		rOptions = rSelect.find('option');
		rOptions.each(function(index){
			div = $(document.createElement('div'));
			text = $(this).text();
			eval("div.text(text).click(function(e){\
				rSelect[0].selectedIndex = "+index+";\
				pTitle.text($($(this)[0]).text());\
			});");
			pOptionsDiv.append(div);
			if (index == selectedIndex) pTitle.append(text);
		});
	});
});
