/**
 * @author np
 */

	function moveAction(whatAction,whereTo,action,closedCommitmentID,actionID,authToken) {
		//var authToken='<%=form_authenticity_token%>';
		// ADD ACTION
		if (action == "add") {
			$('#add_action_'+actionID+'_button').hide();
			$('.close_commitment_'+actionID+'_button').hide();
			//$('#detail_action_'+actionID+'_button').hide();
			$('#reject_action_'+actionID+'_button').hide();
			$('#q_throbber_'+actionID).show().css('opacity','0.3');
		
			$.ajax({
   				type: "POST",
   				url: "/commitments/create/"+actionID,
   				data: "authenticity_token="+encodeURIComponent(authToken)+"&commitment[eco_action_id]="+actionID,
   				success: function(commit_id){
   				
				var thisAction = $(whatAction).html();
				$(whatAction).remove();
				$('#commitments_'+whereTo).prepend('<li id="commited_action_'+actionID+'"><span id="'+commit_id+'" class="committed_id">'+thisAction+'</span></li>');
				$('#q_throbber_'+actionID).hide().css('opacity','0');
				$('.no_actions_in_list').hide();
				$('#remove_action_'+actionID+'_button').fadeIn();
   				}
   				});		
		}
		
		
		// REJECT ACTION
		if (action == "reject") {
			var committed_id = $('.committed_id').attr('id');
			$(whatAction).hide();
			$.ajax({
   				type: "POST",
   				url: "/commitments/reject/"+committed_id,
   				data: "authenticity_token="+encodeURIComponent(authToken)+"&commitment[eco_action_id]="+actionID,
   				success: function(msg){
   				//alert('removed')
   				}
   				});	
			$('#rejected_action_'+actionID).remove();
		}		
		

		
		// REMOVE ACTION
		if (action == "remove") {
			var committed_id = $('.committed_id').attr('id');
			var thisAction = $(whatAction).html();
			$('#possible_actions_next_few_weeks').prepend('<li id="add_action_'+actionID+'"><span style="padding-right:20px;">'+thisAction+'</span></li>');
			$(whatAction).remove();
			$('#remove_action_'+actionID+'_button').hide();
			$('#add_action_'+actionID+'_button').fadeIn();
			$('.close_commitment_'+actionID+'_button').fadeIn();
			//$('#detail_action_'+actionID+'_button').fadeIn();
			$('#reject_action_'+actionID+'_button').fadeIn();
			$.ajax({
   				type: "POST",
   				url: "/commitments/destroy/"+committed_id,
   				data: "authenticity_token="+encodeURIComponent(authToken)+"&commitment[eco_action_id]="+actionID,
   				success: function(msg){
   				}
   				});	
			$('#commited_action_'+actionID).remove();
		}		
		
		
		
		// ACCEPT ACTION
		if (action == "accept") {
			var committed_id = $('.committed_id').attr('id');
			$(whatAction).hide();
			$.ajax({
   				type: "POST",
   				url: "/commitments/accept/"+committed_id,
   				data: "authenticity_token="+encodeURIComponent(authToken)+"&commitment[eco_action_id]="+actionID,
   				success: function(msg){
   				//alert('removed')
   				}
   				});	
			$('#rejected_action_'+actionID).remove();
		}
		
		
		// CLOSE ACTION
		if (action == "close") {
			$(whatAction+" a").remove();
			var thisAction = $(whatAction).html();
			$('#q_throbber_'+actionID).show().css('opacity','0.3');
			
			if (closedCommitmentID=='new') {
			$.ajax({
   				type: "POST",
   				url: "/commitments/create/"+actionID,
   				data: "authenticity_token="+encodeURIComponent(authToken)+"&commitment[eco_action_id]="+actionID,
   				success: function(commit_id){
   					$.ajax({
   					type: "POST",
   					url: "/commitments/close/"+commit_id,
   					data: "authenticity_token="+encodeURIComponent(authToken)+"&commitment[eco_action_id]="+actionID,
   					success: function(msg){
   						//alert('closed')
   						$('#q_throbber_'+actionID).hide().css('opacity','0');
   						$(whatAction).remove();
						$('#closed_commitment_null').remove();
						$('#closed_commitments').prepend('<li id="closed_action_'+closedCommitmentID+'"><span style="padding-right:20px;text-decoration:line-through;">'+thisAction+'</span></li>');

   						}
   					});	
   				}
   			});
			} else {
			$.ajax({
   				type: "POST",
   				url: "/commitments/close/"+closedCommitmentID,
   				data: "authenticity_token="+encodeURIComponent(authToken)+"&commitment[eco_action_id]="+actionID,
   				success: function(msg){
   					//alert('closed')
   					$(whatAction).remove();
					$('#closed_commitment_null').remove();
					$('#closed_commitments').prepend('<li id="closed_action_'+closedCommitmentID+'"><span style="padding-right:20px;text-decoration:line-through;">'+thisAction+'</span></li>');
   				}
   			});	

   		}
	}				
}	
	
	

		
			

