GM.namespace('GM.cms.shopmgt');

// Init

GM.cms.shopmgt.init = function ()
{
  var criteria = GM.dom.getElementsByClassName('criteria', 'div');
  
  for (var i = 0; i < criteria.length; i += 1) {
    GM.cms.util.checkboxgroup.addHandlers(criteria[i]);
  }

  var tables = GM.dom.getElementsByClassName('producttypes', 'table');
  
  for (var i = 0; i < tables.length; i += 1) {
    new GM.cms.table.TableController(tables[i]);
  }
  
  var tables = GM.dom.getElementsByClassName('ebayexport', 'table');
  
  for (var i = 0; i < tables.length; i += 1) {
    new GM.cms.table.TableController(tables[i]);
  }
  
  var tables = GM.dom.getElementsByClassName('products', 'table');
  
  for (var i = 0; i < tables.length; i += 1) {
    new GM.cms.shopmgt.ShopMgtTableController(tables[i]);
  }
  
  var passwords = GM.dom.getElementsByClassName('has-password', 'input');
  
  for (var i = 0; i < passwords.length; i += 1) {
    passwords[i].value = 'GMPASSWORD';
  }  
};

// TableController

GM.cms.shopmgt.ShopMgtTableController = function (table)
{
  GM.cms.table.TableController.call(this, table);
};
GM.cms.shopmgt.ShopMgtTableController.inherit(GM.cms.table.TableController);

GM.cms.shopmgt.ShopMgtTableController.prototype.addHandlers = function ()
{
  GM.cms.table.TableController.prototype.addHandlers.call(this);
  
  var batchLink = GM.dom.getElementsByClassName('insert-batch', 'a', this.table);
  
  if (batchLink[0]) {
    GM.event.unregister(batchLink[0], 'click', this.clickButton);
    GM.event.register(batchLink[0], 'click', this.clickBatch, this);
  }
};

GM.cms.shopmgt.ShopMgtTableController.prototype.removeHandlers = function ()
{
  var batchLink = GM.dom.getElementsByClassName('insert-batch', 'a', this.table);
  
  if (batchLink[0]) {
    GM.event.unregister(batchLink[0], 'click', this.clickBatch);
  }
  
  GM.cms.table.TableController.prototype.removeHandlers.call(this);
};

GM.cms.shopmgt.ShopMgtTableController.prototype.clickBatch = function (e, object)
{
  var target = GM.event.getTarget(e);
  
  GM.event.preventDefault(e);
  
  target.blur();
  
  if (target.className.indexOf('disabled') === -1) {
    var lock = new GM.dom.Lock({color: 'FFF', opacity: 0, cursor: 'wait', zIndex: 90});
    var href = target.href.replace('site/', 'async/') + '&ids=' + this.ids.join('|');
  
    target.blur();
  
    GM.request.send( {
      method:   'get',
      url:      href,
      callback: this.openPopupInsertBatch,
      context:  this,
      object:   {lock: lock}
    } );
  } 
};

GM.cms.shopmgt.ShopMgtTableController.prototype.openPopupInsertBatch = function (e, object)
{
  new GM.cms.shopmgt.ShopMgtBatchPopupController(object.request.responseText, object.lock);
};

GM.cms.shopmgt.ShopMgtTableController.prototype.openPopupInsert = function (e, object, lock)
{
  new GM.cms.shopmgt.ShopMgtPopupController(object.request.responseText, object.lock);
};

GM.cms.shopmgt.ShopMgtTableController.prototype.openPopupUpdate = function (e, object, lock)
{
  new GM.cms.shopmgt.ShopMgtPopupController(object.request.responseText, object.lock);
};

// BatchPopupController

GM.cms.shopmgt.ShopMgtBatchPopupController = function (string, lock)
{
  GM.cms.popup.PopupController.call(this, string, lock);
};
GM.cms.shopmgt.ShopMgtBatchPopupController.inherit(GM.cms.popup.PopupController);

GM.cms.shopmgt.ShopMgtBatchPopupController.prototype.updateApply = function (e, object)
{
  if (object.request.responseText) {
    this.close();
    
    new GM.cms.shopmgt.ShopMgtReviewBatchPopupController(object.request.responseText, object.lock); 
  }
};

//ReviewBatchPopupController

GM.cms.shopmgt.ShopMgtReviewBatchPopupController = function (string, lock)
{
  GM.cms.popup.PopupController.call(this, string, lock);
};
GM.cms.shopmgt.ShopMgtReviewBatchPopupController.inherit(GM.cms.popup.PopupController);

GM.cms.shopmgt.ShopMgtReviewBatchPopupController.prototype.addHandlers = function ()
{
  GM.cms.popup.PopupController.prototype.addHandlers.call(this);
  
  var select = this.popup.getElementsByTagName('select');
  
  for (var i = 0; i < select.length; i += 1) {
    GM.event.register(select[i], 'change', this.changeUpdate, this);
  }
  
  var img = this.popup.getElementsByTagName('img');
  
  for (var i = 0; i < img.length; i += 1) {
    GM.event.register(img[i], 'click', this.clickZoom, this);
  }
};

GM.cms.shopmgt.ShopMgtReviewBatchPopupController.prototype.removeHandlers = function ()
{
  var img = this.popup.getElementsByTagName('img');
  
  for (var i = 0; i < img.length; i += 1) {
    GM.event.unregister(img[i], 'click', this.clickZoom);
  }
  
  var select = this.popup.getElementsByTagName('select');
  
  for (var i = 0; i < select.length; i += 1) {
    GM.event.unregister(select[i], 'change', this.changeUpdate);
  }
  
  GM.cms.popup.PopupController.prototype.removeHandlers.call(this);
};

GM.cms.shopmgt.ShopMgtReviewBatchPopupController.prototype.changeUpdate = function (e)
{
  var target = GM.event.getTarget(e);
  var data   = 'module=shopmgt&submodule=product&act=updatebatchattachment&name=' + target.name + '&value=' + target.value;
  var lock   = new GM.dom.Lock({color: 'FFF', opacity: 0, cursor: 'wait', zIndex: 100});

  var contentDiv = GM.dom.getElementsByClassName('content batch', 'div', this.popup);
  var scrollTop  = (contentDiv[0]) ? contentDiv[0].scrollTop : 0;
  
  GM.request.send( {
    data:     data,
    method:   'post',
    url:      'module.do',
    callback: this.handleUpdate,
    context:  this,
    object:   {lock: lock, scrollTop: scrollTop}
  } ); 
};

GM.cms.shopmgt.ShopMgtReviewBatchPopupController.prototype.handleUpdate = function (e, object)
{
  if (object.request.responseText) {
    var url = object.request.responseText.replace(/&amp;/g, '&');
  
    GM.request.send( {
      method:   'get',
      url:      url,
      callback: this.updatePopup,
      context:  this,
      object:   object
    } );
  }  
};

GM.cms.shopmgt.ShopMgtReviewBatchPopupController.prototype.updatePopup = function (e, object)
{
  var middle = GM.dom.getElementsByClassName('middle', 'div', this.popup);
  
  if (middle[0]) {
    this.removeHandlers();
    
    middle[0].innerHTML = object.request.responseText;
    
    var contentDiv = GM.dom.getElementsByClassName('content batch', 'div', this.popup);

    if (contentDiv[0]) {
      contentDiv[0].scrollTop = object.scrollTop;
    }
    
    this.addHandlers();
  };
  
  object.lock.unlock();
};

GM.cms.shopmgt.ShopMgtReviewBatchPopupController.prototype.clickZoom = function (e)
{
  var target = GM.event.getTarget(e);
  var lock   = new GM.dom.Lock({color: '000', opacity: 0.3, cursor: 'wait', zIndex: 110});
  var src    = target.src.replace(/w=100/, 'w=650') + '&e=1';
  var alt    = target.alt;
  
  new GM.cms.popup.ImageController(src, alt, lock);
};

// PopupController

GM.cms.shopmgt.ShopMgtPopupController = function (string, lock)
{
  GM.cms.popup.PopupController.call(this, string, lock);
  
  this.addDeleteAttachmentHandlers();  
  this.addInsertAttachmentHandlers();  
  GM.cms.shopmgt.types.addHandlers(this.popup);
};
GM.cms.shopmgt.ShopMgtPopupController.inherit(GM.cms.popup.PopupController);

GM.cms.shopmgt.ShopMgtPopupController.prototype.close = function ()
{
  GM.cms.shopmgt.types.removeHandlers(this.popup);
  this.removeInsertAttachmentHandlers();
  this.removeDeleteAttachmentHandlers();  

  GM.cms.popup.PopupController.prototype.close.call(this);
};

GM.cms.shopmgt.ShopMgtPopupController.prototype.addDeleteAttachmentHandlers = function ()
{
  var links = GM.dom.getElementsByClassName('delete', 'a', this.popup);
  
  for (var i = 0; i < links.length; i += 1) {
    GM.event.register(links[i], 'click', this.clickDelete, this);
  }
};

GM.cms.shopmgt.ShopMgtPopupController.prototype.removeDeleteAttachmentHandlers = function ()
{
  var links = GM.dom.getElementsByClassName('delete', 'a', this.popup);
  
  for (var i = 0; i < links.length; i += 1) {
    GM.event.unregister(links[i], 'click', this.clickDelete);
  }
};

GM.cms.shopmgt.ShopMgtPopupController.prototype.addInsertAttachmentHandlers = function ()
{
  var input = this.popup.getElementsByTagName('input');
  
  for (var i = 0; i < input.length; i += 1) {
    if (input[i].name === 'file') {
      GM.event.register(input[i], 'change', this.changeInsert, this);
    }
  }
};

GM.cms.shopmgt.ShopMgtPopupController.prototype.removeInsertAttachmentHandlers = function ()
{
  var input = this.popup.getElementsByTagName('input');
  
  for (var i = 0; i < input.length; i += 1) {
    if (input[i].name === 'file') {
      GM.event.unregister(input[i], 'change', this.changeInsert);
    }
  }
};

GM.cms.shopmgt.ShopMgtPopupController.prototype.clickDelete = function (e)
{
  GM.event.preventDefault(e);
  GM.event.cancelBubble(e);
 
  var target = GM.event.getTarget(e);
  var lock   = new GM.dom.Lock({color: 'FFF', opacity: 0, cursor: 'wait', zIndex: 100});
  
  GM.request.send( {
    method:   'get',
    url:      target.href,
    callback: this.handleRequest,
    context:  this,
    object:   {lock: lock}
  } );
};

GM.cms.shopmgt.ShopMgtPopupController.prototype.changeInsert = function (e)
{
  GM.event.preventDefault(e);
  
  var target = GM.event.getTarget(e);
  var lock   = new GM.dom.Lock({color: 'FFF', opacity: 0, cursor: 'wait', zIndex: 100});
  
  GM.request.send( {
    data:     'part=attachment',
    form:     target.form,
    callback: this.handleRequest,
    context:  this,
    object:   {lock: lock}
  } );
  
  target.value = null;
  target.blur();
};

GM.cms.shopmgt.ShopMgtPopupController.prototype.handleRequest = function (e, object)
{
  if (object.request.responseText) {
    var url = object.request.responseText.replace(/&amp;/g, '&');
  
    GM.request.send( {
      method:   'get',
      url:      url,
      callback: this.updateAttachments,
      context:  this,
      object:   {lock: object.lock}
    } );
  }
};
  
GM.cms.shopmgt.ShopMgtPopupController.prototype.updateAttachments = function (e, object)
{
  var tmp = document.createElement('div');
  tmp.innerHTML = object.request.responseText;
  
  var attachments = GM.dom.getElementsByClassName('attachments_list', 'div', this.popup);
  
  if (attachments[0] && tmp.firstChild) {
    this.removeDeleteAttachmentHandlers();
  
    attachments[0].innerHTML = tmp.firstChild.innerHTML;
    
    this.addDeleteAttachmentHandlers();
  }
  
  object.lock.unlock();
};

GM.cms.shopmgt.types =
{
  typeNames: ['sys_quantity', 'sys_price', 'sys_title', 'sys_description'],

  addHandlers: function (element)
  {
    var form = element.getElementsByTagName('form');
    
    if (form[0]) {
      for (var i = 0; i < form[0].elements.length; i += 1) {
        var name = form[0].elements[i].name;
        
        if (GM.cms.shopmgt.types.typeNames.indexOf(name) !== -1) {
          GM.event.register(form[0].elements[i], 'blur', GM.cms.shopmgt.types.blurFill, form[0].elements[i]);
        }
      }
    }
  },
  
  removeHandlers: function (element)
  {
    var names = ['sys_quantity', 'sys_price', 'sys_title', 'sys_description'];
    var form  = element.getElementsByTagName('form');
    
    if (form[0]) {
      for (var i = 0; i < form[0].elements.length; i += 1) {
        var name = form[0].elements[i].name;
        
        if (GM.cms.shopmgt.types.typeNames.indexOf(name) !== -1) {
          GM.event.unregister(form[0].elements[i], 'blur', GM.cms.shopmgt.types.blurFill);
        }
      }
    }
  },
  
  blurFill: function (e)
  {
    this.value = this.value || this.className;
  }
};

GM.event.register(window, 'load', GM.cms.shopmgt.init);

