// index.js

function createTestimonialsDialog()
{
	var dialog = jQuery("<div/>").attr("id", "testimonialsDialog");
	var table= jQuery("<table/>").attr("id", "testimonialsTable");
	dialog.append(table);
	
	var createRow = function(table, row)
	{
		  var id = jQuery("<input/>").attr("type", "hidden");
		  var testimonial = jQuery("<textarea/>").addClass("testimonialEditor");
		  if(row != null)
		  {
			id.val(row['id']); 
			testimonial.val(row["testimonial"]);
		  }
		  
		  var testimonialCell = jQuery("<td/>").append(testimonial, id);
		  
		  var saveButton = jQuery("<button/>").text(" Save ").button();
		  var saveCell = jQuery("<td/>").append(saveButton);
		  
		  var deleteButton = jQuery("<button/>").text("Delete").button();
		  var deleteCell = jQuery("<td/>").append(deleteButton);
		  
		  var tableRow = jQuery("<tr/>").append(testimonialCell).append(saveCell).append(deleteCell);
		  table.append(tableRow);
		  
		  //set the save event now that we are in the dom
		  saveButton.click(function()
			{
				jQuery.post("./inc/ajax.php", {"type":"saveTestimonial", 
							"id": id.val(), "testimonial": testimonial.val()}, 
							function(data)
						   {
								data = jQuery.parseJSON(data);
								id.val(data['id']);
								saveButton.find("span").addClass("saved");
								setTimeout(function(){saveButton.find("span").removeClass("saved")}, 1000);
																
						   });
			});
		  deleteButton.click(function()
		  {
			  jQuery.post("./inc/ajax.php", {"type":"deleteTestimonial", 
							"id": id.val()}, 
							function(data)
						   {
								data = jQuery.parseJSON(data);
								if(data['success'] == true)
								{
									tableRow.remove();
								}
								else
								{
									alert("Database error!");
								}
																
						   });
			  
		  });
	
		
	}
	
	dialog.dialog({"autoOpen":false, 
				  "title":"Edit Testimonials", 
				  "minWidth":1000,
				  "position":"top",
				  "open":function(event, ui)
					{
						table.find("tr").remove();
						jQuery.post("./inc/ajax.php", {"type":"getTestimonials"}, function(data)
					   {
						   data = jQuery.parseJSON(data);
						   jQuery.each(data, function(index, row)
						  {
							 createRow(table, row) 
						  });
					   });
																	   
					},
					"buttons":
							{"new":function()
								{
									createRow(table, null);
								}
							}
					
					});
	
	return dialog;
		
}

function createNewsDialog()
{
	var dialog = jQuery("<div/>").attr("id", "newsDialog");
	var newsEditor = jQuery("<textarea/>").attr("id", "newsEditor");
	dialog.append(newsEditor);
	dialog.dialog({"autoOpen":false, 
				  "title":"Edit News", 
				  "width":190,
				  "open":function(event, ui)
					{
						jQuery.post('./inc/ajax.php', {"type":"getNews"}, function(data)
						{
							data = jQuery.parseJSON(data);
							newsEditor.val(data['news']);
						});
					}, 
					"buttons":{"save":function()
					{
						jQuery.post('./inc/ajax.php', {"type":"saveNews", "text":newsEditor.val()}, function(data)
						{
							data = jQuery.parseJSON(data);
							jQuery("#newsText").empty();
							jQuery("#newsText").text(newsEditor.val());
						});
						dialog.dialog("close");
					}}
	
	});
	return dialog;
		
}
