//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Schedule JS
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//Document Ready +++++
$(function() {		
	//Schedule Update Modal
	$("a.schedule-edit").click(function(e) {
		e.preventDefault();
		$("#modal-schedule").modal({ opacity:50, overlayClose:true });
	});
	//Schedule Add Modal
	$("a.schedule-add").click(function(e) {
		e.preventDefault();
		$("#modal-schedule-add").modal({ containerCss:{ width:445, height:408 }, opacity:50, overlayClose:true });
		$("input").blur().removeClass("focus"); //Remove default focus after modal launch
	});

	//Set the text on page load
	$("#hr").val("hr");
	$("#min").val("min");
	
	//Text style toggle on focus
	$("#hr").focus(function(){
		if($(this).val() == "hr" || $(this).val() == ""){ $(this).val(""); $(this).addClass("focus"); }
	});	
	$("#hr").blur(function(){
		if($(this).val() == ""){ $(this).val("hr"); $(this).removeClass("focus"); } else{ $(this).addClass("focus"); }
	});
	$("#min").focus(function(){
		if($(this).val() == "min" || $(this).val() == ""){ $(this).val(""); $(this).addClass("focus"); }
	});	
	$("#min").blur(function(){
		if($(this).val() == ""){ $(this).val("min"); $(this).removeClass("focus"); } else{ $(this).addClass("focus"); }
	});

	//Remove Game Ajax ----------------------------------------------------------------
	$("#modal-schedule td.remove a").click(function(e) {
		e.preventDefault();
		//Get the ID of the game to remove
		var theRow = $(this).parent().parent("tr").attr("class");
		var theNewRow = theRow.split("-");
		var removeID = theNewRow[1];
		//Show spinner
		$("#modal-schedule .loading").show();		
		
		//Send the removal to the PHP script
		$.ajax({
			type: "POST",
		    	url: "ui/scripts/schedule.php",
		    	data: "direction=push&removeID="+removeID,
			    success: function(msg){
				    	//Hide the spinner
				    	$("#modal-schedule .loading").hide();
				    	//Show success message
				    	$("#modal-schedule .saved").show();
				    	//Remove the row in the modal and the home page
					$(".schedule-"+msg+"").remove();						
			    }
		 });
	});
	 
        //Add a Game Ajax ----------------------------------------------------------------
	$("a.save-schedule").click(function(e) {
		e.preventDefault();
		
		//Clear stuff from previous submission
		$("#modal-schedule-add .saved").hide();	
		
		//Get the values
		var location = $("#location li a.checked").text();
		var hour = parseInt($("#hr").val()); //convert hour to number
		var hourtext = $("#hr").val(); //get version of hour for email
		var minutes = parseInt($("#min").val()); //convert minutes to number
		var minutetext = $("#min").val(); //get text version of minutes for email
		var timeofday = $("#time li a.checked").text();
		var date = $("#datepicker").val();
		
		//Check for and Display the Appropriate Errors
		if(isNaN(hour) || hour > 12){ $(".error-hr").show(); } else { $(".error-hr").hide(); }
		if(isNaN(minutes) || minutes.length < 2 || minutes > 59){ $(".error-min").show(); } else { $(".error-min").hide(); }
		
		//Check Again for Errors, and if it's all clear, proceed with the Ajax
		if(isNaN(hour) || isNaN(minutes) || minutes.length < 2 || minutes > 59 ){
			//Do nothing...
		} else {
			//Proceed...
			
			//Show the loading spinner
			$("#modal-schedule-add .loading").show();
			
			//Format the Date to Send to PHP
			if(timeofday == "PM"){ hour += 12; }
			var datetime = date+" "+hour+":"+minutes+":00";
			
			//Send the message to the PHP script
			$.ajax({
				type: "POST",
			    	url: "ui/scripts/schedule.php",
			    	data: "location="+location+"&datetime="+datetime+"&date="+date+"&hour="+hourtext+"&minutes="+minutetext+"&timeofday="+timeofday+"&direction=push",
				    success: function(msg){
					    	//Hide the spinner
					    	$("#modal-schedule-add .loading").hide();
					    	//Show success message
					    	$("#modal-schedule-add .saved").show();
					    	//Rebuild the Table on the Home Page ------
						    	$.ajax({
								type: "POST",
							    	url: "ui/scripts/schedule.php",
							    	data: "direction=pull",
								    success: function(msg){
									    	//Remove the Current Rows on the Home Page Table
									    	$("table#schedule tbody tr").remove();
									    	//Add the New Rows
									    	$("table#schedule tbody").append(msg);		    										    	
								    }
							 });
				    }
			 });		
		}
	});
	//Datepicker Initialization
	$("#datepicker").datepicker({ dateFormat: 'yy-mm-dd' });
});

