(function($) {
	var id_editable=new Array;

	$.fn.editableWbw = function(options) {
		$(this).mouseover(function(){	
			className=$(this).attr('class').split(' ');		
	
			if(typeof id_editable[$(this).attr('id')+'_'+className[1]+'_'+className[2]]=='undefined'){			
				if(className[3]=='select')
					loadurl=DIR+controller+'/editableLoadSelect/'+className[1];
				else
					loadurl='';

				$(this).editable(DIR+controller+'/editable', {
					id         : 'data['+className[1]+'][id]',
					name       : 'data['+className[1]+']['+className[2]+']',
					type       : className[3],
					submit     : '<img height="16" width="16" border="0" title="Salva" alt="Salva" src="'+DIR+'img/save.png" />',
					cancel     : '<img height="16" width="16" border="0" title="Annulla" alt="Annulla" src="'+DIR+'img/close.png" />',
					tooltip    : 'Clicca qui per modificare',
					width      : '70%',
					indicator  : '<img src="'+DIR+'img/indicator.gif">',
					placeholder: '&nbsp;',
					loadurl    : loadurl,
/*					loadtext   : '<img src="'+DIR+'img/indicator.gif">',*/
/*					loadtext   : 'Caricamento in corso...',*/
					onblur     : 'ignore',
					callback   : editableCallback,
					height_iniz: $(this).height()			
				});

				id_editable[$(this).attr('id')+'_'+className[1]+'_'+className[2]]=1;	
			}	
	    });
       
		$(this).mouseover(function() { 
			$(this).css('background-color', '#CCEFD6');
	    });
	
	    $(this).mouseout(function() { 
			$(this).css('background', 'none');
	    });
	    
	    $(this).click(function() { 
			$(this).css('background', 'none');
			$(this).css('white-space', 'nowrap');
			
			$(this).unbind('mouseover');
			
			if(className[3]=='textarea')
				$(this).css('height','55px');
			else
				$(this).css('height','16px');
	    });
   	}

	function editableCallback(data, settings){
// 		$(this).animate({height: 'auto'});
 
        json=eval(("(" + data + ")"));
        
 		$(this).html(json.value);
   		
  		if(parseInt(json.err_code)>0){       	
        	$error='<div class="error">';
        	$error+=json.err;
        	$error+='</div>';
        	
         	$(this)
         		.prepend($error)
	        	.children('.error').animate({
		        		opacity:1.0
		        	},
		        	3000
	        	).animate({
		        		opacity: 'toggle', 
		        		height: 'toggle'
		        	},
		        	1500,
		        	'linear',
		        	function(){       		
						$(this).parent().html(json.value);
		        	}
	       	);
        }
           
		$(this).mouseover(function() { 
			$(this).css('background-color', '#CCEFD6');
	    });
	
	    $(this).mouseout(function() { 
			$(this).css('background', 'none');
	    });
    }      
    
	$.editable.addInputType(
		'datetime', {
	    element : function(settings, original) {

			var input = $('<input>');
			if (settings.width  != 'none') { input.width(settings.width);  }
			if (settings.height != 'none') { input.height(settings.height); }

			//input[0].setAttribute('autocomplete','off');
			input.attr('autocomplete','off');
			input.css('width','80px');
			
			$(this).append(input);
			$(this).find('input').datepicker(
					$.extend(
						{}, 
						$.datepicker.regional["it"],
						{
							dateFormat: "dd/mm/yy",
							showStatus: true, 
							showOn: "both",
							showAnim: "fadeIn",
							buttonImage: DIR+"img/calendar.gif",
							buttonImageOnly: true
						}
					)
				);
			
			return(input);	
	    }
	});
/*        
	$.editable.addInputType(
		'checkbox', {
	    element : function(settings, original) {
			var input = $('<input type="hidden">');
			
			$(this).append(input);
			
			var input = $('<input type="checkbox">');
			
			$(this).append(input);
			
			return(input);	
	    },
		content : function(string, settings, original) {
		    $(':input:eq(0)', this).val(0);		    
		    $(':input:eq(0)', this).attr('name',settings.name);

		    $(':input:eq(1)', this).val(1);
		    
		    if(string!='No')
		    	$(':input:eq(1)', this).attr('checked','checked');

//			console.log(settings);
		}
	});
 */       
	$.editable.addInputType(
		'checkbox', {
	    element : function(settings, original) {
			var input = $('<select><option value="1">Sì</option><option value="0">No</option></select>');
			
			$(this).append(input);
			
			return(input);	
	    },
		content : function(string, settings, original) {
		    if(string=='No')
		    	$('select option[value=0]', this).attr('selected','selected');
		    else
		    	$('select option[value=1]', this).attr('selected','selected');

//			console.log(settings);
		}
	});
	
	$.editable.types.defaults.reset=function(settings, original) {          	    
		$(this).parent().animate({height: settings.height_iniz});

		original.reset();
		
		$(original).mouseover(function() { 
			$(original).css('background-color', '#CCEFD6');
	    });
	
	    $(original).mouseout(function() { 
			$(original).css('background', 'none');
	    });
	};
 })(jQuery);
