(function($) {
	$.fn.autocompleteWbw = function(options) {
		controllerAuto=$(this).attr('class').match('ctrl_(.*)')[1];
		options.field=$(this).attr('name').match('data\\[.*\\]\\[(.*)\\]')[1];

		parentClass=$(this).parent().attr('class')
			.replace('cercaAjax ','')
			.replace('aggiungiTagAjax ','')
			.replace(' habtm','')
			.replace(' belongsTo','')
		;
		
		tipo=$(this).parent().attr('class').match('.*'+parentClass+' (.*)')[1];
		
		extra={
			field:options.field,
			parentClass:parentClass,
			tipo:tipo
		};
		
		if(options.model!=undefined)
			extra=$.extend({model:options.model},extra);
				
		$(this).autocomplete(
			DIR+''+controllerAuto+'/autoComplete',
			{
				minChars: 2,
				lineSeparator: "^",
				cacheLength: 10,
				matchContains: true,
				extraParams:extra,
				/*formatItem: $.fn.autocompleteWbw.formatItem,*/
				autoFill: false,
				selectFirst: false
			}
		);
		
		if(options.add==1){
			$(this).result(
				function(event, item) {
					$.fn.tagAjax.addTag({el:this,input_value:item[0],model:options.model,parentClass:item[2]});
				}
			);
		}else{
			$(this).result(
				function(event, item) {
					$.fn.tagAjax.addTagResponse({nome:item[0],id:item[1],parentClass:item[2],tipo:item[3]});
				}
			);
		}

	}
})(jQuery);


