$(document).ready(function(){
	// Apply rounded borders
	applyRoundedBorders();
	
	//focus on new task field
	document.newTaskForm.newTaskDetails.focus();
	
	//hide stuff
	hideAll();
	
	//mouse pointers
	applyMousePointers();
	
	//add markers
	recalibrateLabels();
	
	//load currentTask
	//update currentTask
	if($(".current").find("#taskLabel").text()!=""){
		$(".currentTask").find("h2").replaceWith("<h2>"+ $(".current").find("#taskLabel").text() +"</h2>");
		//alert($(".currentTask").find("input:first").attr("value"));
		$(".currentTask").find("input:first").attr("value", $(".current").find("input:first").attr("value"));
	}
	
	//datepicker
	$("#dueDateDetails").datepicker({altField: '#mySQLDate',altFormat: 'yy-mm-dd'});
	
	//Show or Hide TaskList
	$("#showTaskListLabel").click(function(){
		$("#taskListTable").slideToggle("slow");
	});
	
	// Advanced Options Toggling
	$("#aOptionsLabel").click(function(){
		$("#aOptions").slideToggle();
		$("#addTaskButton").slideToggle();
	});

	//Apply Listeners
	applyTaskDetailListener(); // Task Detail Toggling Listener
	applyTaskListCheckboxListener(); 	// Tasklist Checkbox Listener
	newTaskListener(); 	//New Task Listener
	currentTaskDoneListener(); // CurrentTask Done Listener
	skipButtonListener(); 	// SkipTask Button Listener
	applyDeleteIconListener(); 	//DeleteIcon Listener
	
});

function hideAll(){
	$("#aOptions").hide();
	$("#tasktable").find("table").find("tr").find("#taskDetail").hide();
	$("#taskListTable").hide();
}

function applyRoundedBorders(){
	$('.currentTask').corners("30px 30px");
	$('#tasktable').find("table").corners("10px 10px");
}

function applyMousePointers(){
	
	$("#showTaskListLabel").mouseenter(function(){
		$("#showTaskListLabel").css("cursor", "pointer");
	});
	
	$("#aOptionsLabel").mouseenter(function(){
		$("#aOptionsLabel").css("cursor", "pointer");
	});
}

function applyTaskListCheckboxListener(){
	$("#taskEntryCheckBox > input").unbind();
	
	$("#taskEntryCheckBox > input").click(function(e){
		$.post("dbhandler.php", 
				{SETDONEID: $(this).parents("tr").attr("id"),
				USERNAME: $("#username").val()}, 
				function(data, textStatus){
					$("#taskDetail > input[value="+ data + "]").parents("tr").slideUp("slow", function(){
						$("#taskDetail > input[value="+ data + "]").parents("tr").remove();
						
						recalibrateLabels();
					});	
				}, "text"
		);
	});
}

function newTaskListener(){
	$("#tasktable").find("form").submit(function(e){
		e.preventDefault();
		if($("#newTaskDetails").val()==null || trim($("#newTaskDetails").val())==""){
			window.alert("Empty la dey!");
		}
		else{
			$.post("dbhandler.php", 
					{NEWTASK: $("#newTaskDetails").val(), 
					USERNAME: $("#username").val(), 
					NOTES: $("#notesDetails").val(),
					CATEGORY: $("#categoryDetails").val(),
					DUEDATE: $("#mySQLDate").val()},
					function(data, textStatus){
						if(data!=0){ // a failed insert will return a bunch of HTML, check for that
							//append to tasklist
							$("#taskListTable").find("table").append("<tr id='taskEntry["
							+data+"]'><td><div id='taskLabel'>"+$("#newTaskDetails").val()+"</div><div id='taskDetail'>"
							+"<input type='hidden' id='taskID' value='"+ data +"'>"
							+"<strong>Due Date</strong><br/>"+$("#dueDateDetails").val()+"<br/><br/>"
							+"<strong>Category</strong><br/>"+$("#categoryDetails").val()+"<br/><br/>"
							+"<strong>Notes</strong><br/>"+$("#notesDetails").val()
							+"</div></td><td valign='top'><span id='taskEntryCheckBox'><input type='checkbox'></span><span id='deleteIcon'><img src='images/delete_icon.gif'></span></td></tr>");
							
							recalibrateLabels();
							
							if($(".last").hasClass("first")){
								$(".last").addClass("current");
																
								//update currentTask
								$(".currentTask").find("h2").replaceWith("<h2>"+ $(".current").find("#taskLabel").text() +"</h2>");
								
								$(".currentTask").find("input:first").attr("value", data);
								
								//show buttons if hidden
								$("#doneButton").show();
								
								
								//alert("posting "+ $("#currTaskID").val() + " " + $("#username").val());
								$.post("dbhandler.php", 
									{SETCURRENTID: $("#currTaskID").val(),
									USERNAME: $("#username").val()}); 
							}
							else{
								$("#skipButton").show();
							}
							
							//hide details
							$("#tasktable").find("table").find("tr:last").find("#taskDetail").hide();
							
							//add handler
							applyTaskDetailListener();
							
							// Tasklist Checkbox Listener
							applyTaskListCheckboxListener();
							
							applyDeleteIconListener();
																					
							//reset form
							document.newTaskForm.reset();
							
							//focus on newTaskField
							document.newTaskForm.newTaskDetails.focus();		
						}
						else{
							alert("There was an error trying to add your task. Please try again later.");
						}
						/*location.href="index.php";*/
					}, "text"
			);
		}
	});
}

function currentTaskDoneListener(){
	$("#doneButton").click(function(){
		$.post("dbhandler.php", 
				{SETDONEID: $("#currTaskID").val(),USERNAME: $("#username").val()}, 
				function(data, textStatus){
					$(".currentTask").fadeOut("slow", function(){
						//addClass to next
						if($(".current").hasClass("last")){
							//addClass to next
							$("#tasktable").find("table").find("tr:first").addClass("current");
							
							//removeClass from present
							$("#tasktable").find("table").find(".current:last").removeClass("current");
						}
						else{
							//addClass to next
							$(".current ~ tr:first").addClass("current");
						
							//removeClass from present
							$("#tasktable").find("table").find(".current:first").removeClass("current");
						}
						
						//update currentTask
						$(".currentTask").find("h2").replaceWith("<h2>"+ $(".current").find("#taskLabel").text() +"</h2>");
						//alert($(".currentTask").find("input:first").attr("value"));
						$(".currentTask").find("input:first").attr("value", $(".current").find("input:first").attr("value"));
						
						//update database
						$.post("dbhandler.php", 
								{SETCURRENTID: $("#currTaskID").val(),
								USERNAME: $("#username").val()}, 
								function(data, textStatus){
									location.href="index.php";
								}, "text");
					});
				}, "text"
		);
	});
}

function skipButtonListener(){
	$("#skipButton").click(function(){
		//addClass to next
		if($(".current").hasClass("last")){
			//addClass to next
			$("#tasktable").find("table").find("tr:first").addClass("current");
			
			//removeClass from present
			$("#tasktable").find("table").find(".current:last").removeClass("current");
		}
		else{
			//addClass to next
			$(".current ~ tr:first").addClass("current");
		
			//removeClass from present
			$("#tasktable").find("table").find(".current:first").removeClass("current");
		}
		
		//update currentTask
		$(".currentTask").find("h2").replaceWith("<h2>"+ $(".current").find("#taskLabel").text() +"</h2>");
		//alert($(".currentTask").find("input:first").attr("value"));
		$(".currentTask").find("input:first").attr("value", $(".current").find("input:first").attr("value"));
		
		//update database
		$.post("dbhandler.php", 
				{SETCURRENTID: $("#currTaskID").val(),
				USERNAME: $("#username").val()}); 
	});
}

function applyDeleteIconListener(){
	$("#deleteIcon > img").unbind();
	
	$("#deleteIcon > img").click(function(){
		$.post("dbhandler.php", 
			{DELETEID: $(this).parents("tr").find("#taskDetail").find("#taskID").attr("value"),
			USERNAME: $("#username").val()},
			function(data, textStatus){
				removeTaskRow(data);
			}, "text");
	});
}

function applyTaskDetailListener(){
	$("#tasktable").find("table").find("tr").find("td:first").unbind();
	
	$("#tasktable").find("table").find("tr").find("td:first").click(function(){
		$(this).find("#taskDetail").slideToggle();
	});
}

function removeTaskRow($taskID){
	$("#taskDetail > input[value="+ $taskID + "]").parents("tr").slideUp("slow", function(){
		$("#taskDetail > input[value="+ $taskID + "]").parents("tr").remove();
		
		recalibrateLabels();						
	});
}

function recalibrateLabels(){
		//remove existing first/last marker
		//alert("removing labels");
		$("#tasktable").find("table").find(".last").removeClass("last");
		$("#tasktable").find("table").find(".first").removeClass("first");
	
		//set the last marker
		//alert("add last labels");				
		$("#tasktable").find("table").find("tr:last").addClass("last");
						
		//set first marker
		//alert("setting first marker");
		$("#tasktable").find("table").find("tr:first").addClass("first");
	
		//alert("handling skip button");
		if($(".last").hasClass("first")){
			$("#skipButton").hide();
			$("#showTaskListLabel").hide();
			$("#taskListTable").find("table").hide();																
		}
		else{
			$("#skipButton").show();
			$("#showTaskListLabel").show();
			$("#taskListTable").find("table").show();
		}
}
