/* Demonstration of embedding CodeMirror in a bigger application. The
 * interface defined here is a mess of prompts and confirms, and
 * should probably not be used in a real project.
 */

function MirrorFrame(place, options, owner) {
  this.home = document.createElement("DIV");
  if (place.appendChild)
    place.appendChild(this.home);
  else
    place(this.home);

  var self = this;

  function makeButton(name, action) {
    var button = document.createElement("INPUT");
    button.type = "button";
    button.value = name;
    self.home.appendChild(button);
    button.onclick = function(){self[action].call(self);};
  }
//  makeButton("Search", "search");
  makeButton(codeEditorSearchLabel, "search");

  if(owner) {
	  makeButton(codeEditorReplaceLabel, "replace");
	  makeButton(codeEditorJumpToLineLabel, "jump");
	//  makeButton("Indent all", "reindent");
	  makeButton(codeEditorSaveLabel, "save");
	}

  this.mirror = new CodeMirror(this.home, options);

}

MirrorFrame.prototype = {

  setCode: function(text) {
	this.mirror.setCode(text);
  },


  getCode: function() {
  	return this.mirror.getCode();
  },


save: function() {
	if(currentFile.length > 0) {
		saveFile(currentFile, isCurrentSourceFileTest, null, null, 0 );
	}
},

  search: function() {
    var text = prompt("Enter search term:", "");
    if (!text) return;

    var first = true;
    do {
      var cursor = this.mirror.getSearchCursor(text, first);
      first = false;
      while (cursor.findNext()) {
        cursor.select();
        if (!confirm("Search again?"))
          return;
      }
    } while (confirm("End of document reached. Start over?"));
  },

  replace: function() {
    // This is a replace-all, but it is possible to implement a
    // prompting replace.
    var from = prompt("Enter search string:", ""), to;
    if (from) to = prompt("What should it be replaced with?", "");
    if (!to) return;

    var cursor = this.mirror.getSearchCursor(from, false);
    while (cursor.findNext())
      cursor.replace(to);
  },


jumpToLine: function(linePos) {
	this.mirror.jumpToLine(linePos);
},

  jump: function() {
    var line = prompt("Jump to line:", "");
    if (line && !isNaN(Number(line)))
      this.mirror.jumpToLine(Number(line));
  },

  line: function() {
    alert("The cursor is currently at line " + this.mirror.currentLine());
    this.mirror.focus();
  },

  macro: function() {
    var name = prompt("Name your constructor:", "");
    if (name) {
      if (!this.mirror.replaceSelection("function " + name + "() {\n  \n}\n\n" + name + ".prototype = {\n  \n};\n", true))
        alert("Place the cursor in the document first.");
    }
  },

  reindent: function() {
  //  this.mirror.reindent();
  }

};

