/* Initial js package setup */

try {
  if (typeof(Huxley.Base) == 'undefined') {
        throw "";
    }
} catch (e) {
    throw "Huxley.Dialog depends on Huxley.Base!";
};

if (typeof(Huxley.Dialog) == 'undefined') {
  Huxley.Dialog = {};
};

Huxley.Dialog.NAME = "Huxley.Dialog";

/* Defining the Dialog class */

Huxley.Dialog.Dialog = function () {
  /* set up basic attributes for the Dialog object */
  this.height = 400;
  this.width = 600;
  this.top = 0;
  this.left = 0;
  this.reloadOnClose = false;
};

Huxley.Dialog.Dialog.prototype = {
  init: function (options) {
    this.height = options.height || this.height;
    this.width = options.width || this.width;
    this.top = options.top || this.top;
    this.left = options.left || this.left;
    this.centerHoriz = options.centerHoriz || false;
    this.centerVert = options.centerVert || false;
    this.reloadOnClose = options.reloadOnClose || this.reloadOnClose;
    this.url = options.url || null;
    this.callback = options.callback || null;

    this.overlay = MochiKit.DOM.createDOM('DIV', {'id':'GB_overlay'});
    MochiKit.DOM.insertSiblingNodesBefore(Huxley.Base.getBody().childNodes[0], this.overlay);

    this.window = MochiKit.DOM.createDOM('DIV', {'id':'GB_window'});
    MochiKit.DOM.insertSiblingNodesAfter(this.overlay, this.window);

    this.header = MochiKit.DOM.createDOM('DIV', {'id':'GB_header'});
    this.caption = MochiKit.DOM.createDOM('DIV', {'id':'GB_caption'});
    this.close = MochiKit.DOM.createDOM('DIV', {'id':'GB_close'}, MochiKit.DOM.createDOM('IMG', {'src':'/images/close.gif', 'alt':'Close window'}));
    this.close.onclick = MochiKit.Base.partial(this.hide, this);
    hideDialog = this.close.onclick;
    MochiKit.DOM.appendChildNodes(this.header, this.caption, this.close);
    MochiKit.DOM.insertSiblingNodesAfter(this.overlay, this.window);
    MochiKit.DOM.insertSiblingNodesAfter(this.overlay, this.header);

    this.iframe = MochiKit.DOM.createDOM('IFRAME', {'id':'GB_frame', 'name': 'GB_frame'});
    MochiKit.DOM.appendChildNodes(this.window, this.iframe);
  },

  show: function () {
    this.iframe.src = this.url;
    this.overlay.style.display = 'block';
    this.header.style.display = 'block';
    this.window.style.display = 'block';
    update = MochiKit.Base.partial(this.update, this);
    update();
    MochiKit.Signal.connect(window, 'onresize', update);
    MochiKit.Signal.connect(window, 'onscroll', update);
  },

  update: function (self) {
    var viewportSize = Huxley.Base.getViewportSize();
    var xPos = 0;
    var yPos = 0;

    //log('width: '+self.width+', height:'+self.height);
    /* set the width */
    if (self.width > viewportSize[0]) {
      self.window.style.width = viewportSize[0] + "px";
      self.iframe.style.width = viewportSize[0] + "px";
      self.header.style.width = viewportSize[0] + "px";
      xPos = 0;
    } else {
      self.window.style.width = self.width + "px";
      self.iframe.style.width = self.width + "px";
      self.header.style.width = self.width + "px"; 
      xPos = ((self.centerHoriz == true) ? ((viewportSize[0] - self.window.offsetWidth) /2) : self.left);
    }
    
    if (self.height > viewportSize[1]) {
      self.window.style.height = viewportSize[1] + "px";
      self.iframe.style.height = viewportSize[1] - 5 + "px";
      yPos = 0;
    } else {
      self.window.style.height = self.height + "px";
      self.iframe.style.height = self.height - 5 + "px";
      yPos = ((self.centerVert == true) ? (Huxley.Base.getScrollTop() + ((viewportSize[1] - self.height) /2)) : self.top);
    }
    
    self.window.style.left = ((viewportSize[0] - self.window.offsetWidth) /2) + "px";
    self.header.style.left = ((viewportSize[0] - self.header.offsetWidth) /2) + "px";

    self.setOverlaySize(self);

    self.setPosition(self, xPos, yPos);
  },

  setPosition: function (self, xPos, yPos) {
   // log('xPos: '+xPos+', yPos:'+yPos);

    self.header.style.top =  yPos + "px";
    self.header.style.left =  xPos + "px";
    self.window.style.top = yPos + 27 + "px";
    self.window.style.left = xPos + "px";
  },

  setOverlaySize: function(self) {
    var viewportSize = Huxley.Base.getViewportSize();

    self.overlay.style.width = viewportSize[0] + "px";
    var max_height = Math.max(Huxley.Base.getScrollTop()+viewportSize[1], Huxley.Base.getScrollTop()+self.height+30);
    self.overlay.style.height = max_height + "px";
  },

  hide: function (self) {
    self.iframe.src = "";
    self.window.style.display = 'none';
    self.header.style.display = 'none';
    self.overlay.style.display = 'none';
    if (self.reloadOnClose == true) {
      parent.location.reload();
    }
  }
};

Huxley.Base.update(Huxley.Dialog, {
  show: function (url, height, width, /* optional */ reloadOnClose, callback) {
    var dialog = new Huxley.Dialog.Dialog;
    dialog.init({url:           url,
                 height:        height,
                 width:         width,
                 centerHoriz:   true,
                 reloadOnClose: reloadOnClose,
                 callback:      callback});
    dialog.show();
    return false;
  },

  showCenter: function (url, height, width, /* optional */ reloadOnClose, callback) {
    var dialog = new Huxley.Dialog.Dialog;
    dialog.init({url:           url,
                 height:        height,
                 width:         width,
                 centerHoriz:   true,
                 centerVert:    true,
                 reloadOnClose: reloadOnClose,
                 callback:      callback});
    dialog.show();
    return false;
  },

  showXY: function (url, height, width, xPos, yPos, /* optional */ reloadOnClose, callback) {
    var dialog = new Huxley.Dialog.Dialog;
    dialog.init({url:           url,
                 height:        height,
                 width:         width,
                 top:           yPos,
                 left:          xPos,
                 reloadOnClose: reloadOnClose,
                 callback:      callback});
    dialog.show();  
    return false;
  },

  hide: function () {
    parent.hideDialog();
  }
});

