if(jQuery) (function(){
	
	jQuery.fn.extend({
		
		cssSelect: function(handler)
		{
			$(this).each( function() {
				var options = {};
				var array_opt;
				array_opt = $('#'+this['id']+' > option');
				select_id = $(this).attr('id');
				select_name = $(this).attr('name');
				select_class = $(this).attr('class');
				
				var html = "";
				var option = "";
				var intestazione_select = "";
				var contenuto_select = "";
				var valueSelected = "";
				var textSelected = "";				
				 
				select_over = "";
				select_opened = "";
				selected_index = false;
			
					
				function repl(text)
				{
					text = text.replace('[img]','<img ');
					text = text.replace('[/img]',' />');
					text = text.replace('[span]','<span ');
					text = text.replace('[/span]','</span>');
					text = text.replace('[font]','<font ');
					text = text.replace('[/font]','</font>');
					return text;
				}

				for(i=0;i<array_opt.length;i++)
				{
					text = repl(array_opt[i].text);
					if(array_opt[i].selected == true) 
					{
						selecd = select_class+'_con_sel'; 
						valueSelected = array_opt[i].value;
						textSelected = text;
					} else { 
						selecd = '';
					}
					
				option += '<div class="'+select_class+'_con_out '+selecd+'" idp="'+select_id+'"  act="'+select_id+'" cla="'+select_class+'" value="'+array_opt[i].value+'" style=""><span>'+text+'</span></div>';
				}
					
				intestazione_select += '<div id="'+select_id+'_cont_val_act" idp="'+select_id+'" cla="'+select_class+'" class="'+select_class+'_act" act="'+select_id+'" value="'+valueSelected+'" style="position:absolute; top:0; left:0; z-index:100;"><span>'+textSelected+'</span></div>';

				contenuto_select = '<div tipo="select" idp="'+select_id+'" id="'+select_id+'_cont_val" cla="'+select_class+'" class="'+select_class+'_cont_val" act="'+select_id+'" style="position:absolute; display:none; z-index:200;">'+option+'</div>';
				

				
				
				html = '<div id="'+select_id+'_f" class="'+select_class+'" cla="'+select_class+'" act="'+select_id+'" style="position:relative;">';
				html += intestazione_select;
				html += contenuto_select;
				html += '</div><input type="hidden" id="'+select_id+'" cla="'+select_class+'" name="'+select_name+'" value="'+valueSelected+'" />';
				$(this).replaceWith(html);
				
				/********************************************/
				
			});
			
			var div_cla, div_act;
			
					$('.select2, .select3').click(function(){
					
					div_cla = $(this).attr('cla'); 
					div_act = $(this).attr('act');
					
					$('#'+div_act+'_cont_val').fadeIn( function() {

						$('#'+div_act+'_f').mouseleave(function(){							   
							$('#'+div_act+'_cont_val').fadeOut();
						});																  
						
					
						$('.'+div_cla+'_con_out').mouseover(function(){
							$(this).addClass($(this).attr('cla')+'_con');
						});	
						
						$('.'+div_cla+'_con_out').mouseout(function(){
							$(this).removeClass($(this).attr('cla')+'_con');
						});
						
						$('#'+div_act+'_cont_val > div').click(function(){
							$('#'+div_act+'_cont_val > div').removeClass(div_cla+'_con_sel');
							$(this).addClass(''+div_cla+'_con_sel');			
							
							$('#'+div_act).attr({value: $(this).attr('value')});
							$('#'+div_act+'_cont_val_act').html($(this).html());
							$('#'+div_act+'_cont_val').fadeOut();
							return false;
						});						
						
					});
					
					
					});
				
			}		
	});
})(jQuery);
