JSAN.require('Widget.Dialog');

if (typeof Photozou.Dialog == 'undefined') Photozou.Dialog = {}

Photozou.Dialog.VERSION = '0.01';

Photozou.Dialog = function() {
  var self = this
  this.options = {
    'modal': true,
    'overlay': true,
    'opacity': 0.3,
    'height': 100,
    'width': 300,
    'escapeText': true,
    'okText': 'OK',
    'cancelText': 'Cancel',
    'title': '',
    'onOk': function() { self.close() },
    'onCancel': function() { self.close() }
  }
}

Photozou.Dialog.prototype = new Widget.Dialog

Photozou.Dialog.wait = function(msg, options) {
  var dialog = new Photozou.Dialog
  dialog.wait(msg, options)
  return this
}

Photozou.Dialog.confirm = function(msg, options) {
  var dialog = new Photozou.Dialog
  dialog.confirm(msg, options)
  return this
}

Photozou.Dialog.prototype.wait = function(msg, options) {
  var self = this
  var options = (function(d,s){for(var p in s)d[p]=s[p];return d})(this.options, options || {})
  var dwindow = this.getWindow(options.height, options.width);

  // dheader
  var dheader = document.createElement('div');
  dheader.className = 'dheader';
  dheader.appendChild(document.createTextNode(options.title));
  dwindow.appendChild(dheader);

  // dbody
  var dbody = document.createElement('div');
  dbody.className = 'dbody';
  dwindow.appendChild(dbody);

  // msg
  var dmsg = document.createElement('div');
  dmsg.id = 'dmsg';
  dmsg.style.padding = '6px';
  dmsg.innerHTML = msg
  dbody.appendChild(dmsg);

  // dfooter
  var dfooter = document.createElement('div');
  dfooter.className = 'dfooter';
  dfooter.appendChild(document.createTextNode(options.title));
  dwindow.appendChild(dfooter);

  if (this.options.overlay) {
    var o = this.getOverlay()
    o.style.display = 'none'
    document.body.appendChild(o);
    new Effect.Appear(o, {'from':0, 'to':0.3, 'delay':0, 'duration':0.4})
  }

  dwindow.style.display = 'none'
  document.body.appendChild(dwindow);
  new Effect.Appear(dwindow, {'delay':0, 'duration':0.4})
}


Photozou.Dialog.prototype.confirm = function(msg, options) {
  var self = this
  var options = (function(d,s){for(var p in s)d[p]=s[p];return d})(this.options, options || {})
  var dwindow = this.getWindow(options.height, options.width);

  // dheader
  var dheader = document.createElement('div');
  dheader.className = 'dheader';
  dheader.appendChild(document.createTextNode(options.title));
  dwindow.appendChild(dheader);

  // dbody
  var dbody = document.createElement('div');
  dbody.className = 'dbody';
  dwindow.appendChild(dbody);

  // msg
  var dmsg = document.createElement('div');
  dmsg.id = 'dmsg';
  dmsg.style.padding = '6px';
  if (options.escapeText) {
    dmsg.appendChild(document.createTextNode(msg));
  } else {
    dmsg.innerHTML = msg;
  }
  dbody.appendChild(dmsg);

  // buttons
  var dbuttons = document.createElement('div');
  dbuttons.id = 'dbuttons';
  dbuttons.style.padding = '6px';

  // ok
  var dbuttonOk = document.createElement('button');
  dbuttonOk.id = 'dbutton_ok';
  dbuttonOk.className = 'dbutton';
  dbuttonOk.appendChild(document.createTextNode(options.okText));
  dbuttonOk.onclick = function() {
    options.onOk();
  };

  dbuttons.appendChild(dbuttonOk);

  // cancel
  var dbuttonCancel = document.createElement('button');
  dbuttonCancel.id = 'dbutton_cancel';
  dbuttonCancel.className = 'dbutton';
  dbuttonCancel.appendChild(document.createTextNode(options.cancelText));
  dbuttonCancel.onclick = function() {
    options.onCancel();
  }
  dbuttons.appendChild(dbuttonCancel);

  dbody.appendChild(dbuttons);

  // dfooter
  var dfooter = document.createElement('div');
  dfooter.className = 'dfooter';
  dfooter.appendChild(document.createTextNode(options.title));
  dwindow.appendChild(dfooter);

  if (this.options.overlay) {
    var o = this.getOverlay()
    o.style.display = 'none'
    o.style.zIndex = 2;
    document.body.appendChild(o);
    new Effect.Appear(o, {'from':0, 'to':0.3, 'delay':0, 'duration':0.4})
  }
  dwindow.style.display = 'none'
  dwindow.style.zIndex = 3;
  document.body.appendChild(dwindow);
  new Effect.Appear(dwindow, {'delay':0, 'duration':0.4, 'afterFinish':function(){
    if (!dbuttonOk.disabled) {
      dbuttonOk.focus()
    }
  }})
}

Photozou.Dialog.prototype.close = function() {
  new Effect.Fade('doverlay', {'delay':0, 'duration':0.3, 'afterFinish': function() {
    document.body.removeChild($('doverlay'));
  }})
  new Effect.Fade('dwindow', {'delay':0, 'duration':0.3, 'afterFinish': function() {
    document.body.removeChild($('dwindow'));
  }})
  return this;
}

