var VideoDialog = Class.create({
  initialize: function(element) {
    this.element = $(element);
    this.content = this.element.down('.dialog_border.content');
    this.buildDialog();
  },
  show: function() {
    this.element.open();
  },
  buildDialog: function() {
    this.content.insert(new Element("h2").update("Insert Video"));
    this.content.insert(new Element('div', {style: 'font-weight: bold; margin-top: 8px'}).update('Paste Embed Code Below'));
    this.textarea = new Element('textarea', {style: 'width: 100%; height: 100px'});
    this.content.insert(this.textarea);
    this.createButtons();
  },
  createButtons: function() {
    var container = new Element("div", {className: 'dialog_buttons'});
    this.cancelButton = new Element('img', {src: '/images/cancel_button.png'});
    this.cancelButton.observe('click', this.cancel.bind(this));
    container.insert(this.cancelButton);
    this.okButton = new Element('img', {src: '/images/ok_button.png'});
    this.okButton.observe('click', this.ok.bind(this));
    container.insert(this.okButton);
    this.content.insert(container);
  },
  cancel: function() {
    if (this.placeholder) {
      if (this.placeholder.parentNode)
        this.placeholder.remove();
      this.placeholder = null;
    }
    this.element.close();
  },
  ok: function() {
    if (window.PageEditor.range && Prototype.Browser.IE)
      window.PageEditor.range.select()
    var node = Selection().getRange().getNode();
    if (node && node.nodeName != 'P')
      node = node.up('p');
    if (node && node.nodeName != 'P')
      node = node.next('p');
    if (!node)
      node = $$('.editable p')[0];
    if (!node)
      return;
    
    node.insert(this.textarea.value.gsub(/width="\d+"/, 'width="100%"').gsub(/height="\d+"/, 'height="340px"'));
    this.element.close();
  }
})
