(function($) {
	$.fn.formAjaxWbw = function(options) {
		$(this).click(function(){
			$('#editBlock').animate({opacity: 'show', height: 'show'});
		
			id_row=$(this).attr('href').substr($(this).attr('href').search(/[0-9]/g));
			
			url_edit=$(this).attr('href')+'/isAjaxReq';
//			console.log(url_edit);
	//		alert(id_row);
			
			$('#editBlock .bloccoForm').html('<p class="response">CARICAMENTO DATI <img src="'+DIR+'img/ajax-loader.gif" align="middle" /></p>');
				
			$.fn.formAjaxWbw.getForm({url_edit:url_edit})
			
			return false;
		}); 
	}
	
	$.fn.formAjaxWbw.getForm = function(opts) {
		$.getJSON(
			opts.url_edit,
			{url_edit:opts.url_edit},
			getFormCallback
		);
	}

	function getFormCallback(data){
//		alert("Data Loaded: " + data);

//		console.log($('#editBlock .bloccoAlto img').attr('src').search('/ico_tabella_\d\.gif/'));
		$('#editBlock .bloccoAlto img').attr('src',$('#editBlock .bloccoAlto img').attr('src').replace('/ico_tabella_.*\.gif/','ico_tabella_66'+data.action+'.gif'));
		height_prima=$('#editBlock .bloccoForm').height()+$('#editBlock .bloccoAlto').height()+$('#editBlock .bloccoBasso').height();
		$('#editBlock').css('height',height_prima);
		
		$('#editBlock .bloccoForm').html(data.html_txt);
		
		height_dopo=$('#editBlock .bloccoForm').height()+$('#editBlock .bloccoAlto').height()+$('#editBlock .bloccoBasso').height();

		$('#editBlock').animate({height: height_dopo});
		

/*
		$('#editBlock form').ajaxForm({
			dataType:  'json',
			target: '#editBlock .bloccoForm', 
			url: data.url_edit,
			beforeSubmit:showRequest,
			success: showResponse
		}); 
*/	
	}
	
	$.fn.formAjaxWbw.showRequest = function(formData, jqForm, options)  { 
		pos_x=($('#editBlock .bloccoForm').width()-$('#editBlock .bloccoForm .loadingTxt').width())/2;
		pos_y=($('#editBlock .bloccoForm').height()-$('#editBlock .bloccoForm .loadingTxt').height())/2;
//		console.log(pos_x);
//		console.log(pos_y);
		
//			alert($('#editBlock .bloccoForm .loadingTxt').height()+' '+$('#editBlock .bloccoForm .loadingTxt').width());
//			$('#editBlock .bloccoForm .loadingTxt').css('width',$('#editBlock .bloccoForm').width()).css('height',$('#editBlock .bloccoForm').height()).show();
		$('#editBlock .bloccoForm .loadingTxt')
/*			.css('top',$('#editBlock .bloccoAlto').height())*/
			.css('padding-top',pos_y)
			.css('padding-bottom',pos_y)
			.css('padding-right',pos_x)
			.css('padding-left',pos_x).show()
		;
		
		return true;
	}

	$.fn.formAjaxWbw.showResponse = function(data, statusText)  { 
		if(statusText=='success'){
			$('#editBlock .bloccoForm').html(data.html_txt);
		
//			console.log(data);
			if(data.error==0){
				if(data.action=='del'){
					riga_el=".elenco>tbody>tr[id='riga"+data.id+"']";
					$(riga_el).remove();
		
					$(".elenco>tbody>tr").removeClass();		
		
					$(".elenco>tbody>tr:even").addClass('sfondoBianco');
					$(".elenco>tbody>tr:odd").addClass('sfondoColore');			
				}else{
					$.fn.formAjaxWbw.aggiornaRiga(data.controller,data.id,data.categoriaId,data.action);
				}
				
				height_dopo=$('#editBlock .bloccoForm').height()+$('#editBlock .bloccoAlto').height()+$('#editBlock .bloccoBasso').height();
				$('#editBlock').animate({height: height_dopo});
	
				$('#editBlock .bloccoForm').removeClass('loading');
	//				$('#editBlock .bloccoForm').html(responseText); 
				$('#editBlock')
					.animate({opacity: 1.0}, 3000)
					.animate({opacity: 'toggle', height: 'toggle'});
				/*$('#editBlock').slideUp('slow').fadeOut('slow');*/
			}
		}
	}
	
	$.fn.formAjaxWbw.aggiornaRiga = function(controller,id_row,categoriaId,action)  { 
		if(action!='add' && action!='dup'){
			riga_el=".elenco>tbody>tr[id='riga"+id_row+"']>td:first-child";

			$(riga_el).html('<img src="'+DIR+'img/ajax-loader-small.gif" width="10" />')
		}

		if(categoriaId!=null)
			cat_id='cat_id:'+categoriaId+'/';
		else
			cat_id='';
		
		$.ajaxSetup({
		    jsonp: null,
		    jsonpCallback: null
		});		
		
//		console.log(DIR+controller+'/row/'+id_row);
		$.getJSON(
			DIR+controller+'/row/'+cat_id+id_row,
			{action:action},
			$.fn.formAjaxWbw.aggiornaRigaCallback
		);
	}
	
	$.fn.formAjaxWbw.aggiornaRigaCallback = function(data)  { 	
		riga_el=".elenco>tbody>tr[id=riga"+data.id+"]";

		switch (data.action){
			case 'add':
			case 'dup':
				riga_el='.elenco>tbody>tr:first-child';
				
				if($(riga_el).length>0){
					class_riga=$(riga_el).get(0).className=='sfondoBianco'?'sfondoColore':'sfondoBianco';
					color_orig=$(riga_el).get(0).className=='sfondoBianco'?'#FEF4D5':'#FFFFFF';
				}else{
					class_riga='sfondoBianco';
					color_orig='#FFFFFF';	
				}
				
				if($(riga_el).attr('id')=='riga0')
					$('.elenco tbody>tr').replaceWith(data.html_txt);	
				else
					$('.elenco tbody').prepend(data.html_txt);	
			break;
			
			case 'edit':
//				class_riga=$(riga_el).get(0).className;
				color_orig=$(riga_el).get(0).className=='sfondoBianco'?'#FFFFFF':'#FEF4D5';
				
				$(riga_el).replaceWith(data.html_txt);	
			break;
			
			case 'del':
				$(riga_el).remove();
			break;
			
			default:
			break;
		}
		
//		console.log(riga_el);

//		$(riga_el+' td:first-child').addClass('latoSx');
//		$(riga_el+' td:last-child').addClass('latoDx');
		
//		$(riga_el).addClass(class_riga);	
		

		$(".elenco>tbody>tr").removeClass();		
		
		$(".elenco>tbody>tr:even").addClass('sfondoBianco');
		$(".elenco>tbody>tr:odd").addClass('sfondoColore');			

		$(riga_el).addClass('modificata');

		$(riga_el+' td:first-child img').remove();
		
		setTimeout('$(riga_el).removeClass(\'modificata\')', 4000);
/*
		$(riga_el+" td[class!='strumenti']")
			.animate({opacity: 1.0}, 2000)
//			.animate({backgroundColor:color_orig},2000)
		;
*/
/*
		$(riga_el+' .schedaRow').formAjaxWbw();	
		$(riga_el+' .editRow').formAjaxWbw();
		$(riga_el+' .deleteRow').formAjaxWbw();	
		$(riga_el+' .previewRow').formAjaxWbw();
		$(riga_el+' .schedaRiassuntivaRow').formAjaxWbw();
*/
//		console.log(riga_el+' a[class$=Row]');

		$(riga_el+' a[class$=Row]').unbind('click').nyroModal();
	}
})(jQuery);

