//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Statistics JS
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//Document Ready +++++
$(function() {		
	//Schedule Update Modal
	$("a.statistics-edit").click(function(e) {
		e.preventDefault();
		$("#modal-statistics").modal({ containerCss:{ width:620, height:440 }, opacity:50, overlayClose:true });
	});
	//Player Selector
	$("#whichPlayer li a").click(function(e) {
		e.preventDefault();
		
		//Remove Stats from last time (if any)
		$("ul#statistics").children().remove();  	
		//Show Instructions (if not already there)
		$("#instructions").show();
		$("#player-loading").show();
		//Hide the spinner
		$("#modal-statistics .loading").hide();
		//Hide the success message
		$("#modal-statistics .saved").hide();
		//Show Checked Player
		var whichPlayer = $("#whichPlayer li a.checked").text();
		//Go pull the stats for the selected player
		$.ajax({
			type: "POST",
		    	url: "ui/scripts/statistics.php",
		    	data: "whichPlayer="+whichPlayer+"&direction=pull",
			    success: function(msg){
			    	//Hide the instructions and spinner
				$("#instructions").hide();
				//Set the instruction text for future accesses
				$("#instructions p").text("Loading stats...");
				//Add the Stats
				$("ul#statistics").append(msg);	
				//Show Save Button
				$(".save-statistics").show();	    										    	
			    }
		 });
	});
	//Increment Values
	$("#statistics a.plus").live("click", function(e){
		e.preventDefault();
		var increment = $(this).prev().val();
		increment++;
		$(this).prev().val(increment);
	});
	//Decrement Values
	$("#statistics a.minus").live("click", function(e){
		e.preventDefault();
		var decrement = $(this).next().val();
		decrement--;
		$(this).next().val(decrement);
	});
	//Save Data
	$(".save-statistics").click(function(e) {
		e.preventDefault();
		//Show the spinner
		$("#modal-statistics .loading").show();
		var dataToSend = $("#statistics input").serialize();
		//Get the checked player
		var whichPlayer = $("#whichPlayer li a.checked").text();
		//Send the Changes to the PHP Script
		$.ajax({
			type: "POST",
		    	url: "ui/scripts/statistics.php",
		    	data: dataToSend+"&whichPlayer="+whichPlayer+"&direction=push",
			    success: function(msg){
			    	//Hide the spinner
			    	$("#modal-statistics .loading").hide();
			    	//Show the success message
			    	$("#modal-statistics .saved").show();								    	
			    }
		 });
	});
});

