var dropdown={
	last_opened_options:null,
	//////////////////////////////////////////////////////////////////////
	init:function(id){
		this.id = id;
		this.kill_old_methods();
		$('#'+this.id+' .dropdown')
			.bind('mousedown.dropdown',this.toggleOptions)
			.find('.option')
				.bind('mousedown.dropdown',this.selectOption)
		;
		
		//specially to IE
		$('#'+this.id+' .dropdown')
			.find('select:first')
				.bind('change.dropdown',
					function(){
						var selectedOptions=$(this).find('option:selected');
						$(this)
							.parents('.dropdown:first').find('input:first')
								.val(selectedOptions.attr('value').toString().trim())
						;
					}
				)
		;
		/////////////////
		$(document).unbind('mousedown.dropdown');
		$(document).bind('mousedown.dropdown',this.hideOptions);
	},
	
	kill_old_methods:function(){
		$('#'+this.id+' *').unbind('.dropdown');
	},
	
	openOptions:function(obj,event){
		var options=$(obj).find('.options');
		if($(event.target).hasClass('options')){
			options.show();
		}else{
			options.toggle();
		}
		if(options.css('display')=='none'){
			return false;
		}
		return true;
	},
	
	toggleOptions:function(event){
		dropdown.last_opened_options=this;
		if(!dropdown.openOptions(this,event)){
			$(this).removeClass('opened');
			return false;
		}
		$(this).addClass('opened');
		
		dropdown.highlight_selected_options(this);
	},
	
	highlight_selected_options:function(obj){
		var input=$(obj).find('input:first');
		$(obj).find('.option')
			.removeClass('selected')
			.filter(function(){
					if($(this).text().toString().trim()==input.val()){return true;}
					return false;
				})
				.addClass('selected');
		;
	},
	
	selectOption:function(){
		$(this)
			.addClass('selected')
			.parents('.dropdown:first').find('input:first')
				.val($(this).text().toString().trim())
		;
		$(this).parents('.dropdown:first').find('input').eq(1)
			.val($(this).find('input:first').val());
		;
		$(this).parents('.dropdown:first').find('input:first').trigger('change');
	},
	
	hideOptions:function(){
		$('.dropdown').not($(dropdown.last_opened_options)).each(function(){
			dropdown.hideOption(this);
		});
		dropdown.last_opened_options=null;
	},
	
	hideOption:function(dropdown){
		$(dropdown)
			.removeClass('opened')
			.find('.options')
				.hide()
		;
	}
};