//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Standings JS
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//Document Ready +++++
$(function() {		
	//Schedule Update Modal
	$("a.standings-edit").click(function(e) {
		e.preventDefault();
		$("#modal-standings").modal({ containerCss:{ width:505 }, opacity:50, overlayClose:true });
	});
	
	//Increment Values
	$("td.win a.plus,td.loss a.plus").live("click", function(e){
		e.preventDefault();
		var increment = $(this).prev().val();
		increment++;
		$(this).prev().val(increment);
	});
	//Decrement Values
	$("td.win a.minus,td.loss a.minus").live("click", function(e){
		e.preventDefault();
		var decrement = $(this).next().val();
		decrement--;
		$(this).next().val(decrement);
	});
	//Save Button in Modal
	$(".save-standings").click(function(e){
		e.preventDefault();
		//Hide prior success message
		$("#modal-standings .saved").hide();
		//Show the Spinner
		$("#modal-standings .loading").show();
		
		var dataToSend = $("#standings-admin input").serialize();
		
		//Send the Changes to the PHP script
		$.ajax({
			type: "POST",
		    	url: "ui/scripts/standings.php",
		    	data: dataToSend+"&direction=push",
			    success: function(msg){
				    	//Show the new table
					$("#standings tbody tr").remove();
					$("#standings tbody").append(msg);
					//Update the update table in the modal
					$.ajax({
						type: "POST",
					    	url: "ui/scripts/standings.php",
					    	data: dataToSend+"&direction=push&updater=yes",
						    success: function(msg){
							    	//Show the new update table
								$("#standings-admin tbody tr").remove();
								$("#standings-admin tbody").append(msg);
								//Hide the spinner
							    	$("#modal-standings .loading").hide();
							    	//Show success message
							    	$("#modal-standings .saved").show();
						    }
					 });
			    }
		 });
	});
});

