var open_sel=new Array();
$.fn.select = function(){

	$('.select').each(function(i,elem){
		id_select=Math.floor(Math.random()*1000);
		$(elem)[0].id="id"+id_select;
		open_sel["id"+id_select]=0;
	});
	
	//$('.select div').hide();
	
	var on=0;
	var click=0;
	$('*').click(function(){
		click=1;
		if ($(this).attr('class')=='select') {on=1;}
	});
	
	setInterval(function(){
		if ((on==0) && (click==1)) {
			on=0;
			click=0;
			for (var i in open_sel)
			{
				$("#"+i).find('div').slideUp(150);
				$("#"+i).css('background-position','top left');
				open_sel[i]=0;
			}
		}
		else {on=0;click=0;}
	},1);
	

	$('.select').click(function(){
		id_select=$(this)[0].id;
		if (open_sel[id_select]==0)
		{
			th=$(this);
			$(this).find('div').slideDown(150);
			$(th).css('background-position','bottom left');
			open_sel[id_select]=1;
			$(this).find('input').focus();
		}
		else
		{
			th=$(this);
			$(this).find('div').slideUp(150,function(){$(th).css('background-position','top left');});
			open_sel[id_select]=0;
		}
		for (var i in open_sel)
		{
			if (i!=id_select)
			{
				$("#"+i).find('div').slideUp(150,function(){$("#"+i).css('background-position','top left');});
				
			}
		}	
	});
	
	
	$('.select div span').mousemove(function(){
		$(this).attr('class','active');
	});
	$('.select div span').mouseout(function(){
		$(this).attr('class','');
	});
	$('.select div span').click(function(){	
		$(this).parents().parents('div:first').find('.first span').html($(this).html());
		$(this).parents().parents('div:first').find('input').val($(this).html());
		setTimeout(function(){
			$(this).parents().parents('div:first').find('input').blur();
		},500);
	});
}
