// Copyright 1999 Macromedia, Inc. All rights reserved.

//******************* GLOBALS **********************

var CMD_TO_LAUNCH = "Design Notes.htm";

//***************** API  ******************

function canAcceptCommand() {
  var retVal = true;             //always true for document window
    
  if (site.windowIsFrontmost()) { //if called for site window
    retVal = getSiteSelFile();    //true only if single-selection
  } else if (dw.getDocumentDOM() == null) retVal = false;

  return retVal;
}

//***************** LOCAL FUNCTIONS  ******************

function initializeUI() {
  var filePath = "", src, metafile;

  MMNotes.UpdateSite = false;

  //ensure file is saved

  if (site.windowIsFrontmost()) {                    //if called for site window
    if (site.getFocus().toLowerCase() != "remote") { //local or map file selected
      src = getSiteSelFile();
      if (src) {                                   //single selection
        var fObj = new File(src);
        filePath = fObj.getAbsolutePath(); //resolve the local path of the selection
        if (fObj.canWrite()) {
		  MMNotes.FileInfo_isWriteable = true;
		  MMNotes.UpdateSite = true;
		}
		else {
          alert(MSG_ReadOnlyFile);
          filePath = fObj.getAbsolutePath(); //resolve the local path of the selection
		  MMNotes.FileInfo_isWriteable = false;
        }
      }
    } else {
      alert(MSG_RemoteFileSel);
    }

  } else {                        //called for doc window
    filePath = dreamweaver.getDocumentPath("document");
    if (!filePath) {
      if (confirm(MSG_WantSave) && dw.canSaveDocument(dreamweaver.getDocumentDOM('document'))) {
        dw.saveDocument(dreamweaver.getDocumentDOM('document'));
        filePath = dreamweaver.getDocumentPath("document");
      }
    } else { //file exists, ensure it's writeable
      var fObj = new File(filePath);
      if (fObj.canWrite()) {
		MMNotes.FileInfo_isWriteable = true;
	  }
	  else {
        //filePath = "";           //clear, because we can't edit
        MMNotes.FileInfo_isWriteable = false;
		alert(MSG_ReadOnlyFile);
      }

    }
  }

  if (filePath) {
    metafile = (MMNotes.open(filePath));  //open, or create metafile
    if (metafile) {                            //if okay to open
      MMNotes.close(metafile);                  //close it
      MMNotes.FileInfo_filePath = filePath;     //store the filePath as a global
      dreamweaver.popupCommand(CMD_TO_LAUNCH); //launch the command
    } else {
      alert(MSG_MetaDisabled);
    }
  }
}


function getSiteSelFile() {
  var fileList = site.getSelection();
  return ((fileList.length == 1)? fileList[0] : false);
}
