var ifc;

function initDataProperties()
{
  var soid = $('#sourceObjectId').val();

  //inline link file chooser
  ifc = new Filechooser("inlineLinkPopupContainer", constructHyperLink, function()
  {
    this.filechooser.panel.hide();
  }, {editorName:'commentsEditor'});


  //add the button to save the comments
  if ($('#saveCommentsButton').length)
  {
    $('#saveCommentsButton').click(saveComments);
  }

  //add the delete button
  if ($('#deleteButton').length)
  {
    $('#deleteButton').click(deleteFile);
  }


}

function saveComments()
{
  var ed = tinyMCE.get('commentsEditor');
  ed.setProgressState(1); // Show progress

  var callData = new Object();

  //save all of the comments on the document versions
  $('.commentChanged').each(function()
  {
    var theseComments = $(this).val();
    var versionId = $(this).attr('id');
    callData[versionId] = theseComments;
  });

  //add the description of the document record
  var callString2 = YAHOO.lang.JSON.stringify({id: $('#sourceObjectId').val(), desc: ed.getContent(), type:'DocumentRecord', comments: callData});

  var saveCommentsCallback = {
    success : function (o)
    {
      var result;
      try
      {
        var ed = tinyMCE.get('commentsEditor');
        ed.setProgressState(0); // Show progress

        result = YAHOO.lang.JSON.parse(o.responseText);
        if (result.error)
        {
          alert("Error: " + result.message);
        }
        else
        {
          var color = '#2D962D';
          var text = 'Details Saved';
          var element = document.getElementById('saveStatus');
          notifyStatus(color, text, element);
        }
      }
      catch (x)
      {
        alert("Saving Comments failed");
      }
    },

    failure: function()
    {
      var ed = tinyMCE.get('commentsEditor');
      ed.setProgressState(0);
      alert("Saving properties failed");
    },
    timeout:7000
  };

  var url = "../servlets/fileapi?method=saveComments";
  YAHOO.util.Connect.asyncRequest('POST', url, saveCommentsCallback, callString2);

}


function deleteFile()
{
  // Get different delete text if it is present  
  var deleteDialogText = "Delete " + $('#sourceObjectName').val() + "? \nIf you are deleting a directory the contents will be deleted too.";
  if ($('#deleteDialogText').length)
  {
    deleteDialogText = $('#deleteDialogText').val();
  }


  var handleYes = function()
  {
    //construct the data - we need the id of the object to be deleted.  This is set in the labelClick function
    var callData = {id: $('#sourceObjectId').val()};
    var callString = YAHOO.lang.JSON.stringify(callData);

    //post the data back and update the cell if it completed successfully
    YAHOO.util.Connect.asyncRequest("POST", "../servlets/fileapi?method=delete", {
      success: function (o)
      {
        //get the results into a JS assoicative array
        var oResults = eval("(" + o.responseText + ")");
        if (oResults.Result == "success")
        {
          history.go(-1);
        }
        else
        {
          alert(oResults.Error);
        }
      },
      failure: function (o)
      {
        alert(o.statusText);
      }
    }, callString);

    this.hide();
  };

  var handleNo = function()
  {
    this.hide();
  };

  // Instantiate the Dialog
  var simpledialog1 = new YAHOO.widget.SimpleDialog("si",
  { width: "300px",
    fixedcenter: true,
    visible: false,
    draggable: false,
    close: true,
    text: deleteDialogText,
    icon: YAHOO.widget.SimpleDialog.ICON_HELP,
    constraintoviewport: true,
    buttons: [
      {
        text:"Yes",
        handler:handleYes
      },
      {
        text:"No",
        handler:handleNo,
        isDefault:true
      }
    ]
  });
  simpledialog1.setHeader("Are you sure?");

  // Render the Dialog
  simpledialog1.render("dialog");

  //display the dialog
  simpledialog1.show();
}

//initialise TinyMCE
tinyMCE.init({
  mode : "textareas",
  theme : "simple"

});



