'use strict';

var doc = global.document;
var ui = require('./ui');
var util = require('./util');
var configure = require('./configure');
var Editor = require('./Editor');
var nextId = 0;

function convertTabs () {
  util.addEventDelegate(doc, 'pmk-input', 'keydown', ui.convertTabs);
}

function ponymark (containers) {
  var postfix = nextId++;
  var editor;

  if (Object.prototype.toString.call(containers) !== '[object Object]') {
    containers = {
      buttons: containers,
      input: containers,
      preview: containers
    };
  }

  markup(containers, postfix);

  editor = new Editor(postfix);
  editor.run();

  return editor.api;
}

function markup (containers, postfix) {
  var buttonBar = doc.createElement('div');
  var preview = doc.createElement('div');
  var input = doc.createElement('textarea');

  buttonBar.id = 'pmk-buttons-' + postfix;
  buttonBar.className = 'pmk-buttons';
  preview.id = 'pmk-preview-' + postfix;
  preview.className = 'pmk-preview';
  input.id = 'pmk-input-' + postfix;
  input.className = 'pmk-input';
  input.placeholder = containers.placeholder || containers.input.getAttribute('placeholder') || '';

  containers.buttons.appendChild(buttonBar);
  containers.input.appendChild(input);
  containers.preview.appendChild(preview);
}

module.exports = ponymark;

ponymark.Editor = Editor;
ponymark.convertTabs = convertTabs;
ponymark.configure = configure;

@@ -300,26 +300,17 @@
 nymark (
-containers
+o
 ) %7B%0A  va
@@ -383,26 +383,17 @@
 ng.call(
-containers
+o
 ) !== '%5B
@@ -415,26 +415,17 @@
 ) %7B%0A    
-containers
+o
  = %7B%0A   
@@ -436,26 +436,17 @@
 uttons: 
-containers
+o
 ,%0A      
@@ -452,26 +452,17 @@
  input: 
-containers
+o
 ,%0A      
@@ -470,26 +470,17 @@
 review: 
-containers
+o
 %0A    %7D;%0A
@@ -493,26 +493,17 @@
  markup(
-containers
+o
 , postfi
@@ -597,26 +597,17 @@
 markup (
-containers
+o
 , postfi
@@ -707,391 +707,498 @@
 var 
-input = doc.createElement('textarea');%0A%0A  buttonBar.id = 'pmk-buttons-' + postfix;%0A  buttonBar.className = 'pmk-buttons';%0A  preview.id = 'pmk-preview-' + postfix;%0A  preview.className = 'pmk-preview';%0A  input.id = 'pmk-input-' + postfix;%0A  input.className = 'pmk-input';%0A  input.placeholder = containers.placeholder %7C%7C containers.input.getAttribute('placeholder') %7C%7C '';%0A%0A  containers
+existing = o.input && /textarea/i.test(o.input.tagName);%0A  var input;%0A%0A  if (!existing) %7B%0A    input = doc.createElement('textarea');%0A    input.className = 'pmk-input';%0A    input.placeholder = o.placeholder %7C%7C o.input.getAttribute('placeholder') %7C%7C '';%0A  %7D else %7B%0A    input = o.input;%0A  %7D%0A  input.id = 'pmk-input-' + postfix;%0A%0A  buttonBar.id = 'pmk-buttons-' + postfix;%0A  buttonBar.className = 'pmk-buttons';%0A  preview.id = 'pmk-preview-' + postfix;%0A  preview.className = 'pmk-preview';%0A%0A  o
 .but
@@ -1226,28 +1226,41 @@
 onBar);%0A
+%0A
   
-containers
+if (!existing) %7B%0A    o
 .input.a
@@ -1284,18 +1284,14 @@
 ;%0A  
-containers
+%7D%0A%0A  o
 .pre

rearrange options