define(function(require, exports, module) {
  'use strict';
  var TSCORE = require("tscore");
  console.log("Loading: ext.api.js");
  
  window.addEventListener("message", handleMessage , false);

  function handleMessage(msg) {
    var data = JSON.parse(msg.data);
    var command = data.command;

    switch (command) {
      case "hideAllMenus":
        TSCORE.hideAllDropDownMenus();
        $('#searchOptions').hide();
        $('#fileExtensionMenu').parent().removeClass('open');
        $('#perspectiveSwitcher').parent().removeClass('open');
        $('#locationsList').parent().removeClass('open');
        $('#tagSuggestionsMenu').parent().removeClass('open');
        break;
      case "playbackEnded":
        openNextFile();
        break;
      case "openLinkExternally":
        if (data.link) {
          openLinkExternally(data.link);
        }
        break;
      case "contentChangedInEditor":
        TSCORE.FileOpener.setFileChanged(true);
        break;
      default:
        console.log("Not recognized messaging command: " + msg);
        break;
    }
  }

  function openNextFile() {
    //if(TSCORE.selectedFiles.length > 0) {
      // TODO open next selected file
    //} else {
    TSCORE.FileOpener.openFile(TSCORE.PerspectiveManager.getNextFile(TSCORE.FileOpener.getOpenedFilePath()));
    //}
  }

  function openLinkExternally(uri) {
    uri = decodeURIComponent(uri);
    if (
        uri.indexOf("http://") === 0 ||
        uri.indexOf("https://") === 0 ||
        uri.indexOf("file://") === 0
    ) {
      TSCORE.IO.openFile(uri);
    } else {
      console.log("Not supported URL format: " + uri);
    }
  }

});
@@ -125,18 +125,16 @@
 i.js%22);%0A
-  
 %0A  windo
@@ -176,17 +176,16 @@
 eMessage
- 
 , false)
@@ -308,24 +308,105 @@
 (command) %7B%0A
+      case %22saveDocument%22: %0A        TSCORE.FileOpener.saveFile();%0A        break;%0A
       case %22
@@ -1230,18 +1230,16 @@
 ) %7B%0A    
-  
 // TODO 
@@ -1481,32 +1481,36 @@
    if (%0A        
+    
 uri.indexOf(%22htt
@@ -1525,32 +1525,36 @@
 == 0 %7C%7C%0A        
+  
+  
 uri.indexOf(%22htt
@@ -1562,32 +1562,36 @@
 s://%22) === 0 %7C%7C%0A
+    
         uri.inde

extend extension api - save document from editorText