// set up the defaults for all AJAX calls made with jQuery
$.ajaxSetup(
   {
   type: 'POST',
   dataType: 'html'
   });

// get the auth key out of the query string (if any)
var auth_key = $.jqURL.get('auth');
if (!auth_key)
   {
   auth_key='';
   }

function submitComment()
   {
   var body = document.getElementById('comment_body').value;
   var snapshot_id = document.getElementById('snapshot_id').value;

   if (body)
      {
      $.ajax(
         {
         data:
            {
            proc: 'snapshot_comment',
            id: snapshot_id,
            body: body
            },
         success: function()
            {
            window.location.reload();
            }
         });
      }
   else
      {
      alert("You didn't enter a comment!");
      }

   return false;
   }

function displayCommentDialog(snapshotId)
   {
   if (!isLoggedIn())
      {
      login("displayCommentDialog("+snapshotId+")", null);
      return;
      }

   var html = '';
   html += '<TEXTAREA ID="comment_body"></TEXTAREA><BR>';
   html += '<INPUT TYPE="hidden" id="snapshot_id" name="snapshot_id" VALUE="'+snapshotId+'">';
   html += '<INPUT TYPE="submit" VALUE="Save" onClick="this.disabled=1; submitComment();">';

   dialog_ask("Add a Comment", html, null, '', 'comment_body');
   }

function snapshotCommentEditSubmit(commentId)
   {
	var body = jQuery.trim(document.getElementById('comment_body').value);

   if (body)
      {
      $.ajax(
         {
         data:
            {
            proc: 'update_snapshot_comment',
            comment_id: commentId,
            body: body
            },
         success: function()
            {
            window.location.reload();
            }
         });
      }
   else
      {
      alert("You didn't enter a comment!");
      }

   return false;
   }

function snapshotEditSubmit(snapshotId)
   {
	var name = jQuery.trim(document.getElementById('snapshot_name').value);
	var description = jQuery.trim(document.getElementById('snapshot_description').value);

   if (name && description)
      {
      $.ajax(
         {
         data:
            {
            proc: 'update_snapshot',
            snapshot_id: snapshotId,
            name: name,
            description: description
            },
         success: function()
            {
            window.location.reload();
            }
         });
      }
   else
      {
      alert("You didn't enter a description!");
      }

   return false;
   }

function confirmSnapshotCommentDelete(delete_id)
   {
	var delete_html = '<CENTER><FORM ACTION="" METHOD="POST" onsubmit="return false;"><INPUT TYPE="button" onclick="commentDelete('+delete_id+');" VALUE="Yes, delete this comment permanently."></FORM></CENTER>';

	return dialog_ask("Confirm delete", delete_html);
   }

function confirmSnapshotDelete(delete_id, numComments, destinationURL)
   {
   var warningText = "Do you really want to delete this snapshot?";
   if (numComments > 0)
      {
      if (numComments == 1)
         {
         warningText += " Deleting it will also delete its conversation comment.";
         }
      else
         {
         warningText += " Deleting it will also delete its " + numComments + " conversation comments.";
         }
      }
   var delete_html = warningText + '<CENTER><FORM ACTION="" METHOD="POST" onsubmit="return false;"><INPUT TYPE="button" onclick="snapshotDelete('+delete_id+',\''+destinationURL+'\');" VALUE="Yes, delete this snapshot permanently"></FORM></CENTER>';

	return dialog_ask("Confirm delete", delete_html);
   }

function commentDelete(commentId)
   {
   if (commentId)
      {
      $.ajax(
         {
         data:
            {
            proc: 'delete_snapshot_comment',
            comment_id: commentId
            },
         success: function()
            {
            window.location.reload();
            }
         });
      }
   else
      {
      alert("You didn't specify a comment to delete!");
      }
   }

function snapshotDelete(snapshotId, destinationURL)
   {
   if (snapshotId)
      {
      $.ajax(
         {
         data:
            {
            proc: 'delete_snapshot',
            snapshot_id: snapshotId
            },
         success: function()
            {
            window.location.href = destinationURL;
            }
         });
      }
   else
      {
      alert("You didn't specify a snapshot to delete!");
      }
   }

function snapshotCommentEdit(commentId)
   {
   if(isLoggedIn())
      {
        str = '/snapshots/comments/' + commentId + '.json';
	$.getJSON(str + "?callback=?",
	  function(data) {
            comment = data;
            if (comment)
               {
               dialog_ask("Edit Comment", '<TEXTAREA ID="comment_body">'+comment.message+'</TEXTAREA><BR><INPUT TYPE="submit" VALUE="Save" onClick="this.disabled=true; snapshotCommentEditSubmit('+commentId+')">', null,'','comment_body');

               // undo the hack from rpc.php which inserts <br> tags for line breaks
               var commentBody = document.getElementById('comment_body');
               commentBody.value = commentBody.value.replace(/<br>/g,"\r");
               }
          }
        );

      /*
      $.ajax(
         {
         data:
            {
            proc: 'getSnapshotCommentAsJSON',
            comment_id: commentId,
            auth: auth_key
            },
         success: function(retVal)
            {
            eval("var comment = " + retVal);
            if (comment)
               {
               dialog_ask("Edit Comment", '<TEXTAREA ID="comment_body">'+comment.message+'</TEXTAREA><BR><INPUT TYPE="submit" VALUE="Save" onClick="this.disabled=true; snapshotCommentEditSubmit('+commentId+')">', null,'','comment_body');

               // undo the hack from rpc.php which inserts <br> tags for line breaks
               var commentBody = document.getElementById('comment_body');
               commentBody.value = commentBody.value.replace(/<br>/g,"\r");
               }
            }
         });
        */
      }

   return false;
   }

function snapshotEdit(snapshotId)
   {
   if(isLoggedIn())
      {
      $.ajax(
         {
         data:
            {
            proc: 'getSnapshotAsJSON',
            snapshot_id: snapshotId,
            auth: auth_key
            },
         success: function(retVal)
            {
            eval("var snapshot = " + retVal);
            if (snapshot)
               {
               dialog_ask("Edit Snapshot",
                          'Snapshot Name: <input type="text" id="snapshot_name" value="'+snapshot.name+'"><br>Description: <TEXTAREA ID="snapshot_description">'+snapshot.description+'</TEXTAREA><BR><INPUT TYPE="submit" VALUE="Save" onClick="this.disabled=true; snapshotEditSubmit('+snapshotId+')">',
                          null,
                          '',
                          'snapshot_name');

               // undo the hack from rpc.php which inserts <br> tags for line breaks
               var snapshotBody = document.getElementById('snapshot_description');
               snapshotBody.value = snapshotBody.value.replace(/<br>/g,"\r");
               }
            }
         });
      }

   return false;
   }
