GM.namespace('GM.cms.popup');

GM.cms.popup.PopupController = function (string, lock)
{
  var div = document.createElement('div');
    
  var top = document.createElement('div');
  top.className = 'top';
  div.appendChild(top);
  
  var middle = document.createElement('div');
  middle.className = 'middle';
  middle.innerHTML = string;
  div.appendChild(middle);
  
  var bottom = document.createElement('div');
  bottom.className = 'bottom';
  div.appendChild(bottom);
  
  document.body.appendChild(div);
    
  div.className = (middle.firstChild.className) ? 'popup ' + middle.firstChild.className : 'popup';
    
  GM.dom.center(div);
  
  div.style.position = 'fixed';
  
  this.popup = div;
  this.lock  = lock;
  
  this.addHandlers();
  
  GM.event.register(window.document, 'keypress', this.escapeClose, this);
  
  this.lock.getElement().style.cursor = 'default';  
}

GM.cms.popup.PopupController.prototype.addHandlers = function ()
{
  if (this.popup) {
    var title = GM.dom.getElementsByClassName('title', 'div', this.popup);
    
    if (title[0]) {
      this.drag = new GM.dom.Drag(this.popup, title[0].firstChild, null, 100);
      
      var close = document.createElement('span');
      close.className = 'close';
      close.innerHTML = 'x';
      title[0].appendChild(close);
      
      GM.event.register(close, 'click', this.closePopup, this);
    }
    
    var apply = GM.dom.getElementsByClassName('apply', 'input', this.popup);
    
    for (var i = 0; i < apply.length; i += 1) {  
      GM.event.register(apply[i], 'click', this.clickApply, this);
    }
    
    var close = GM.dom.getElementsByClassName('close', 'input', this.popup);
    
    for (var i = 0; i < close.length; i += 1) {  
      GM.event.register(close[i], 'click', this.closePopup, this);
    }
    
    GM.cms.util.fold.addHandlers(this.popup);
    GM.cms.help.addHandlers(this.popup);
  }
}

GM.cms.popup.PopupController.prototype.removeHandlers = function ()
{
  if (this.popup) {
    var close = GM.dom.getElementsByClassName('close', 'span', this.popup);
      
    if (close[0]) {
      GM.event.unregister(close[0], 'click', this.closePopup);
    }
    
    var apply = GM.dom.getElementsByClassName('apply', 'input', this.popup);
      
    for (var i = 0; i < apply.length; i += 1) {  
      GM.event.unregister(apply[i], 'click', this.clickApply);
    }
    
    var close = GM.dom.getElementsByClassName('close', 'input', this.popup);
    
    for (var i = 0; i < close.length; i += 1) {  
      GM.event.unregister(close[i], 'click', this.closePopup);
    }
    
    GM.cms.util.fold.removeHandlers(this.popup);
    GM.cms.help.removeHandlers(this.popup);
  }
  
  if (this.drag) {
    this.drag.removeHandlers();
    this.drag = null;
  }  
}

GM.cms.popup.PopupController.prototype.escapeClose = function (e)
{
  if (GM.event.keyPressed(e, 27) ) {
    this.closePopup(e);
  }
}

GM.cms.popup.PopupController.prototype.closePopup = function (e)
{
  this.removeHandlers();

  if (this.popup) {
    this.popup.parentNode.removeChild(this.popup);
    this.popup = null;
  }
  
  if (this.lock) {
    this.lock.unlock();
    this.lock = null;
  }  
  
  GM.event.unregister(window.document, 'keypress', this.escapeClose);
}

GM.cms.popup.PopupController.prototype.clickApply = function (e)
{
  GM.event.preventDefault(e);
  
  var target = GM.event.getTarget(e);
  
  if (target.form) {
    var lock = new GM.dom.Lock({color: 'FFF', opacity: 0, cursor: 'wait', zIndex: 100});
    
    GM.request.send( {
      data:     'async=true',
      form:     target.form,
      callback: this.handleApply,
      context:  this,
      object:   {lock: lock}
    } );    
  }
}

GM.cms.popup.PopupController.prototype.handleApply = function (e, object)
{
  if (object.request.responseText) {
    var url = object.request.responseText.replace(/&amp;/g, '&');
  
    GM.request.send( {
      method:   'get',
      url:      url,
      callback: this.updateApply,
      context:  this,
      object:   {lock: object.lock}
    } );
  }
}

GM.cms.popup.PopupController.prototype.updateApply = function (e, object)
{
  var middle = GM.dom.getElementsByClassName('middle', 'div', this.popup);
  
  if (middle[0] && object.request.responseText) {
    this.removeHandlers();
    
    middle[0].innerHTML = object.request.responseText;
    
    this.addHandlers();
  }
  
  object.lock.unlock();
}
