/**
*	Reemplaza Select por DIV

El campo SELECT se transforma a DIV de la siguiente manera.


*/
(function($) {
$.fn.customSelect = function(options) {
	var opt = {
		ident: $(this).attr('id')
	}; 
    $.extend(opt, options);
	
	
	var ident = $(this).attr('id');

	return this.each(function() {
		var hasfocus=0;	
		obj = $(this);  
		obj.after("<div id=\""+ident+"_selectoptions\"> </div>");
		obj.find('option').each(function(i){ 
			$("#"+ident+"_selectoptions").append("<div id=\"" + $(this).attr("value") + "\" class=\""+ident+
												"_selectitems\"><img src=\"" + 	this.title + "\" /><span>" 
												+ $(this).html() + "</span></div>");
		});
		
		obj.before("<input type=\"hidden\" id=\""+ident+"\" value =\"\" name=\"" + 
					this.name + "\" class=\""+ident+"_customselect\"/>"+
					"<input type=\"text\" id=\""+ident+"_iconselect\" value=\"" + this.title +
					"\" readonly=\"\" /><div id=\""+ident+"_iconselectholder\"> </div>").remove();
					
		$("#"+ident+"_iconselect").click(function(){
			$("#"+ident+"_iconselectholder").toggle(100);
		});
		$("#"+ident+"_iconselectholder").append( $("#"+ident+"_selectoptions")[0] );
		$("."+ident+"_selectitems").mouseover(function(){
				$(this).addClass(""+ident+"_hoverclass");
		});
		$("."+ident+"_selectitems").mouseout(function(){
		$(this).removeClass(""+ident+"_hoverclass");
		});
		$("."+ident+"_selectitems").click(function(){
			$("."+ident+"_selectedclass").removeClass(""+ident+"_selectedclass");
			$(this).addClass(""+ident+"_selectedclass");
			var thisselection = $(this).find('span').text();
			$("."+ident+"_customselect").val(this.id);
			//se carga la info al campo oculto
			$("#"+ident+"_iconselect").get(0).value = thisselection.replace(/<img[^>]*>/gi, "");
			$("#"+ident+"_iconselectholder").hide(100)
		});
		
		
		//Por si estan encima de la lista
		$("#"+ident+"_iconselectholder").mouseover(function(){
			hasfocus=1;
		});
		$("#"+ident+"_iconselect").mouseover(function(){
			hasfocus=1;
		});
		$("#"+ident+"_iconselectholder").mouseout(function(){
			hasfocus=-1;
		});
		$("#"+ident+"_iconselect").click(function(){
			hasfocus=-1;
		});
		
		$("#"+ident+"_iconselect").blur(function() {
			if(hasfocus!=-1){
				//alert('si hasfocus: '+hasfocus)
				
			}else{
				//alert('si hasfocus: '+hasfocus)
				$("#"+ident+"_iconselectholder").hide(100);
				
			}
		
		});

	
  });  
 

  
}
})(jQuery)
