//Scripts for the RFL home page

r654 = {};

$(document).ready(function(){

	// enable tablesorter to sort numbers with commas in them.
	//From: http://lab.christianmontoya.com/jquery-tablesort-numbers-commas/
	jQuery.tablesorter.addParser({
		id: "commaDigit",
		is: function(s, table) {
			var c = table.config;
			return jQuery.tablesorter.isDigit(s.replace(/,/g, ""), c);
		},
		format: function(s) {
			return jQuery.tablesorter.formatFloat(s.replace(/,/g, ""));
		},
		type: "numeric"
	});
	
	
	//Make the table sortable
	//The columns are different if the user is "staff"
	//For staff, there will be more than 7 columns.
	
	//Only do it if the table exists
	//(It won't be there for non-logged in users, or users with 0 gardens.)
	if ($("#garden_table").length > 0){
		//count the number of columns:
		var ncols = $("#garden_table").find("tr")[0].cells.length;
		if (ncols > 7){
			//Staff view
			$("#garden_table").tablesorter({
				headers:{
					5: {sorter: "commaDigit"},
					6: {sorter: "commaDigit"},
					7: {sorter: "commaDigit"},
					8: {sorter: "commaDigit"},
					9: {sorter:false}
				}
			});
		} else {
			//Regular user view
			$("#garden_table").tablesorter({
				headers:{
					2: {sorter: "commaDigit"},
					3: {sorter: "commaDigit"},
					4: {sorter: "commaDigit"},
					5: {sorter: "commaDigit"},
					6: {sorter:false}
				}
			});
		}
	}
	//Launch Garden Button
	//$(".launch_garden").button({ icons: {primary:"ui-icon-pencil"} });
	$(".new_garden"   ).button({ icons: {primary:"ui-icon-plusthick"} });
	
	$(".launch_garden").click(launch_garden);
	$(".new_garden"   ).click(launch_garden);
	
	//Delete Garden Link
	//$(".delete_garden").button({ icons: {primary:"ui-icon-close"}, text: false});
	$(".delete_garden").click(function(){
		var garden_id = $(this).attr("title");
		$("#delete_id").val(garden_id);
		var garden_name = $(this).find(".garden_name").text();
		//launch a popup dialog to confirm the user really wants to delete.
		//actual delete code will be launched from the "Yes" button there
		$("#garden_name").html(garden_name);
		$("#delete_dialog").dialog("open");
	});
	
	//Duplicate Garden Link (all it does is launch the Duplicate Dialog box)
	$("#duplicate_link").click(function(){
		
		//make the first option in the dialog box selected.
		var first_val = $("#duplicate option:first").val();
		$("#duplicate").val(first_val);
		
		//get the name of the selected garden
		//var garden_name = $("#duplicate option:selected").text();
		
		//Finally, open the dialog box
		$("#duplicate_dialog").dialog("open");
		return false;
	});
	
	$("#register_button").button();
	$("#login_button").button();
	
	
	//Duplicate Garden Dialog
	$("#duplicate_dialog").dialog({
		autoOpen: false,
		modal: true,
		width: 400,
		height: 350,
		position: "center",
		buttons: {
			"Duplicate": duplicate_garden,
			"Cancel": function(){
				$(this).dialog("close");
			}
		},
	});

	//Delete confirmation dialog
	$("#delete_dialog").dialog({
		autoOpen:false,
		model: true,
		width: 300,
		heigh: 250,
		position: "center",
		buttons: {
			"Delete": delete_garden,
			"Cancel": function(){
				$(this).dialog("close");
			}
		}
	});
	
	// HELP Dialog Box 
	$("#helpdialog").dialog({
		autoOpen: false,
		width: 500,
		height:400,
		modal: true,
		position: "center",
		buttons: {"Close": function(){
			$(this).dialog("close"); 
			}
		}
	});
	
	//Create the MORE INFO buttons using rollover effects
	$(".moreinfo").mouseenter(function(){
		$(this).attr("src", "/site_media/images/info_mouseover.gif");
	});
	
	$(".moreinfo").mousedown(function(){
		$(this).attr("src", "/site_media/images/info_mousedown.gif");
	});
	
	$(".moreinfo").mouseup(function(){
		$(this).attr("src", "/site_media/images/info_mouseover.gif");
	});
	
	$(".moreinfo").mouseout(function(){
		$(this).attr("src", "/site_media/images/info.gif");
	});
	
	$(".moreinfo").each(function(i){
		//$(this).button({ icons: {primary:"ui-icon-info"}, text: false });   //OLD
		$(this).click(function(){
			//put the right contents in the dialog box.
			var helpID = $(this).attr("id");
			$("#helpinfo").html(r654.help[helpID]);
			$("#helpdialog").dialog("open");
			return false;
		});
	});
	
});

//Function that is called when the user clicks the delete button
//AND then clicks OK on the confirmation popup box.
function delete_garden(){
	var garden_id = $("#delete_id").val();
	$.ajax({
		type: "POST",
		url: "../deleter/",
		data: {"garden_id": garden_id},
		success: function(obj){
			if (obj.deleted == true){
				console.log("deleted");
				//OLD: remove the delete button and the garden button
				//$(".launch_garden[title='" + garden_id +"']").remove();
				//$(".delete_garden[title='" + garden_id +"']").remove();
				//NEW: remove the entire row from the table!
				$(".launch_garden[title='" + garden_id +"']").parent().parent().remove();
				//Need to update tablesorter's cache so the row doesn't re-appear when the user sorts (simple one-line command)
				$("#garden_table").trigger("update");
				
				$("#delete_dialog").dialog("close");
			} else {
				alert("Unable to delete your data.");
				$("#delete_dialog").dialog("close");
			}
		}
	});
}

function launch_garden(){
	var garden_id = $(this).attr("title");
	$("#garden_id").val(garden_id);
	$("#load_form").submit();
}


function duplicate_garden(){
	
	//ID of the garden that will be duplicated
	var garden_id = $("#duplicate option:selected").val();
	
	//Name for the new garden
	var garden_name = $("#duplicate_name").val();
	
	//series of steps that need to happen:
	//.1. Send this information to the server, where a script will do all the necessary duplicating, adding a new garden to the database.
	// 2. Reload this page (I think that's the easiest way, rather than monkeying around with adding rows to the table, etc... would require so many lines of code. 
	$.ajax({
		type: "POST",
		url: "../duplicater/",
		data: {"garden_id": garden_id, "garden_name": garden_name},
		success: function(obj){
			if(obj.duplicated != false){
				alert("Your landscape was cloned! Click OK to continue.");
				location.reload();
			} else {
				alert("Unable to duplicate landscape.");
				
			}
		}
	});
}

