GM.namespace('GM.cms.popup');

/* PopupManager */

GM.cms.popup.PopupManager = function ()
{
  var popups = [];
  
  function escapeClose (e)
  {
    if (GM.event.keyPressed(e, 27) ) {
      GM.event.preventDefault(e);
      
      popups[popups.length - 1].close();
    }
  }
  
  return {
    
    register: function(popup)
    {
      popups.push(popup);
      
      if (popups.length === 1) {
        GM.event.register(window.document, 'keydown', escapeClose);
      }
    },
    
    unregister: function(popup)
    {
      for (var i = 0; i < popups.length; i += 1) {
        if (popups[i] === popup) {
          popups.splice(i, 1);
        }
      }
      
      if (popups.length === 0) {
        GM.event.unregister(window.document, 'keydown', escapeClose);
      }
    }
  };
}();

/* PopupController */

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();
  
  this.lock.getElement().style.cursor = 'default';
  
  GM.cms.popup.PopupManager.register(this);
};

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.close, 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.close, 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.close);
    }
    
    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.close);
    }
    
    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.close = function ()
{
  GM.cms.popup.PopupManager.unregister(this);
  
  this.removeHandlers();

  if (this.popup) {
    this.popup.parentNode.removeChild(this.popup);
    this.popup = null;
  }
  
  if (this.lock) {
    this.lock.unlock();
    this.lock = null;
  }
};

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();
};

/* ImageController */

GM.cms.popup.ImageController = function (src, alt, lock)
{
  var img = document.createElement('img');
  img.src = src;
  img.alt = alt;
  img.className = 'lightbox';
  
  this.img  = img;
  this.lock = lock;
  
  if (img.complete) {
    document.body.appendChild(img);
    
    this.center();
  } else {
    GM.event.register(img, 'load', function (e) {
      this.center();
    }, this);
    
    document.body.appendChild(img);  
  }  
  
  GM.event.register(lock.getElement(), 'click', this.close, this);
    
  GM.cms.popup.PopupManager.register(this);
};

GM.cms.popup.ImageController.prototype.center = function ()
{
  GM.dom.center(this.img);
  
  this.img.style.visibility           = 'visible';
  this.img.style.position             = 'fixed';
  this.lock.getElement().style.cursor = 'default';
};

GM.cms.popup.ImageController.prototype.close = function ()
{
  GM.cms.popup.PopupManager.unregister(this);
  
  GM.event.unregister(this.lock.getElement(), 'click', this.close);
  
  this.lock.unlock();
  this.img.parentNode.removeChild(this.img);
};

