var Prescription2 = {};Prescription2.Cart = AbstractCart.extend({
  options: {},

  initialize: function(cart, url, options) {
    this.url = url;
    this.setOptions(options);
    this.positionHandler = this.handlePosition.bind(this);
    this.myAjax = new Ajax(this.url, {
      method: 'get',
      data: { domId: cart.id },
      update: cart,
      onComplete: this.handleAjaxCompleted.bind(this)
    }).request();
  },
  
  handleAjaxCompleted: function() {
    var items = $$('.prodotti_elementlista').map(function(el) { return el.getElement('img'); });
    if ($('immagine_inPrimoPiano')) items.push($('immagine_inPrimoPiano'));
    this.parent(this.myAjax.options.update, items);
  },
  
  init: function() {
    this.parent();
    window.addEvent('scroll', this.positionHandler);
    this.viewHeader = this.cart.getElement('.view-header');
    this.minimize = this.viewHeader.getElement('a.minimize').addEvent('click', this.handleMinimize.bindWithEvent(this));
    this.maximize = this.viewHeader.getElement('a.maximize').addEvent('click', this.handleMaximize.bindWithEvent(this));
    this.viewContent = this.cart.getElement('.view-content');
    this.emptyCartMessage = this.viewContent.getElement('.empty-cart-message');
    this.accordion = new Accordion('.toggler', '.element', {
      opacity: false
    }, this.cart.getElement('.accordion'));

    this.sections = this.cart.getElements('.section').map(function(el) {
      return new Prescription2.CartSection(this, el);
    }, this);
    this.sectionDrops = [];
    this.sectionDropFxs = [];
    this.sections.each(function(section, i) {
      var drop = section.el.getElement('.toggler');
      this.sectionDrops.push(drop);
      this.sectionDropFxs.push(drop.effect('background-color', {wait: false}));
      section.addEvent('reload', this.handleSectionReload.bind(this, i));
      section.addEvent('inited', this.handleSectionInited.bind(this, i));
      section.addEvent('uniniting', this.handleSectionUniniting.bind(this, i));
      this.handleSectionInited(i);
    }, this);

    this.addSectionForm = this.cart.getElement('.addSection').addEvent('submit', this.handleAddSection.bindWithEvent(this));
    
    this.viewHeader.getElement('.title').setText(this.options.cartTitle);
    this.emptyCartMessage.setText(this.options.emptyCartMessage);
    this.addSectionForm.getElement('input[name=sectionName]').getPrevious().setText(this.options.sectionNameLabel);
    this.addSectionForm.getElement('input[name=sectionCategory]').getPrevious().setText(this.options.sectionCategoryLabel);
    this.addSectionForm.getElement('input[type=submit]').value = this.options.addSectionButton;

    this.addEvent('emptydrop', this.handleEmptydrop.bind(this));
  },
  
  uninit: function() {
    this.parent();
    window.removeEvent('scroll', this.positionHandler);
    this.sections.each(function(section) {
      section.removeEvent('reload');
      section.uninit();
    });
    
    this.addSectionForm.removeEvent('submit');
    this.removeEvent('emptydrop');
  },
  
  reload: function() {
    this.uninit();
    new Ajax(this.url, {
      method: 'get',
      data: { _eventName: 'reload', id: this.id, domId: this.cart.id },
      update: this.cart,
      onComplete: function() {
        this.init();
        this.fireEvent('reload');
      }.bind(this)
    }).request();
  },
  
  getDroppables: function() {
    return this.cart.getElements('.section');
  },
  
  dropItem: function(item, i) {
    this.drops[i]._control.addItem(item.className.replace(/product\-(\d+)/, '$1'));
  },
  
  handleAddSection: function(e) {
    e.stop();
    var data = { _eventName: 'addSection' };
    var err = false;
    this.addSectionForm.getElements('input[type=text]').each(function(el) {
      data[el.name] = el.value;
      if (!err && !el.value) {
        err = true;
        new Fx.Style(el, 'background-color', { duration: 500 }).start('#f00', '#fff');
        el.focus();
      }
    });
    if (err) return;
    new Ajax(this.url, {
      method: 'post',
      data: data,
      onComplete: function() {
        this.reload();
      }.bind(this)
    }).request();
  },
  
  handleSectionReload: function(i) {
    this.dropFxs[i].start('7389AE').chain(this.dropFxs[i].start.pass('ffffff', this.dropFxs[i]));
    this.accordion.previous = -1;
    this.accordion.display(this.drops[i].getElement('.element'));
  },

  deleteSection: function(section) {
    if (confirm(this.options.removeSectionMessage)) {
      new Ajax(this.url, {
        method: 'post',
        data: { _eventName: 'deleteSection', sectionId: section.id },
        onComplete: function() {
          this.reload();
        }.bind(this)
      }).request();
    }
  },

  moveSection: function(section, delta) {
    new Ajax(this.url, {
      method: 'post',
      data: { _eventName: 'moveSection', sectionId: section.id, delta: delta },
      onComplete: function() {
        this.reload();
      }.bind(this)
    }).request();
  },
  
  handleMinimize: function(e) {
    e.stop();
    this.cart.getElement('.view-content').setStyle('display', 'none');
  },
  
  handleMaximize: function(e) {
    e.stop();
    this.cart.getElement('.view-content').setStyle('display', '');
  },
  
  handleSectionInited: function(i) {
    this.sections[i].el.getElements('.item').each(function(el) {
      var img = el.getElement('img');
      img.addEvent('mousedown', this.handleSectionItemMousedown.bindWithEvent(this, el._control));
    }, this);
  },
  
  handleSectionUniniting: function(i) {
    this.sections[i].el.getElements('.item').each(function(el) {
      el.getElement('img').removeEvent('mousedown');
    }, this);
  },
  
  handleSectionItemMousedown: function(e, item) {
    var img = item.el.getElement('img');
    var clone = img.clone()
      .setStyles(img.getCoordinates())
      .setStyles({'opacity': 0.7, 'position': 'absolute'})
      .addEvent('emptydrop', function() {
        clone.remove();
      }.bind(this)).inject(document.body);
    
    this.sectionDrops.each(function(drop, i) {
      drop.addEvents({
        'drop': function() {
          this.sectionDrops.each(function(drop) {
            drop.removeEvents();
          });
          clone.remove();
          this.reassignItem(item, this.sections[i]);
        }.bind(this),
        'over': function() {
          this.sectionDropFxs[i].start('98B5C1');
        }.bind(this),
        'leave': function() {
          this.sectionDropFxs[i].start('#ECE9D8');
        }.bind(this)
      });
    }, this);
    
    var drag = clone.makeDraggable({
      droppables: this.sectionDrops
    });
    drag.start(e);
  },
  
  reassignItem: function(item, section) {
    new Ajax(this.url, {
      method: 'post',
      data: { _eventName: 'reassignItem', itemId: item.id, sectionId: section.id },
      onComplete: function() {
        this.reload();
      }.bind(this)
    }).request();
  },

  handlePosition: function() {
    this.cart.setStyle('top', Window.getScrollTop() + 'px');
  },

  handleEmptydrop: function(item) {
    var p = item.getParent();
    if (p.hasClass('prodotti_elementlista')) location.href = p.href;
  }

});
Prescription2.Cart.implement(new Options);
Prescription2.CartSection = new Class({
  initialize: function(cart, el) {
    this.cart = cart;
    this.el = $(el);
    this.el._control = this;
    this.id = this.el.id.match(/\d+$/);
    this.init();
  },
  
  init: function() {
    this.fireEvent('initing');
    this.moveDown = this.el.getElement('a.move-down').addEvent('click', this.handleMove.bindWithEvent(this, 1));
    this.moveUp = this.el.getElement('a.move-up').addEvent('click', this.handleMove.bindWithEvent(this, -1));
    this.del = this.el.getElement('a.delete').addEvent('click', this.handleDelete.bindWithEvent(this));
    this.items = this.el.getElements('.item').map(function(el) {
      return new Prescription2.CartItem(this, el);
    }, this);
    this.fireEvent('inited');
  },
  
  uninit: function() {
    this.fireEvent('uniniting');
    this.del.removeEvent('click');
    this.moveDown.removeEvent('click');
    this.moveUp.removeEvent('click');
    this.items.each(function(item) {
      item.uninit();
    });
    this.fireEvent('uninited');
  },
  
  reload: function() {
    this.uninit();
    new Ajax(this.cart.url, {
      method: 'get',
      data: { _eventName: 'reloadSection', sectionId: this.id },
      update: this.el.getElement('.store'),
      onComplete: function() {
        this.init();
        this.fireEvent('reload');
      }.bind(this)
    }).request();
  },

  handleDelete: function(e) {
    e.stop();
    this.cart.deleteSection(this);
  },
  
  handleMove: function(e, delta) {
    e.stop();
    this.cart.moveSection(this, delta);
  },
  
  addItem: function(productId) {
    new Ajax(this.cart.url, {
      method: 'post',
      data: { _eventName: 'addItem', sectionId: this.id, productId: productId },
      onComplete: function() {
        this.reload();
      }.bind(this)
    }).request();
  },

  deleteItem: function(item) {
    if (confirm(this.cart.options.removeItemMessage)) {
      new Ajax(this.cart.url, {
        method: 'post',
        data: { _eventName: 'deleteItem', itemId: item.id },
        onComplete: function() {
          this.reload();
        }.bind(this)
      }).request();
    }
  },

  moveItem: function(item, delta) {
    new Ajax(this.cart.url, {
      method: 'post',
      data: { _eventName: 'moveItem', itemId: item.id, delta: delta },
      onComplete: function() {
        this.reload();
      }.bind(this)
    }).request();
  }

});
Prescription2.CartSection.implement(new Events);
Prescription2.CartItem = new Class({
  initialize: function(section, el) {
    this.section = section;
    this.el = $(el);
    this.el._control = this;
    this.id = this.el.id.match(/\d+$/);
    this.init();
  },
  
  init: function() {
    this.moveDown = this.el.getElement('a.move-down').addEvent('click', this.handleMove.bindWithEvent(this, 1));
    this.moveUp = this.el.getElement('a.move-up').addEvent('click', this.handleMove.bindWithEvent(this, -1));
    this.del = this.el.getElement('a.delete').addEvent('click', this.handleDelete.bindWithEvent(this));
  },
  
  uninit: function() {
    this.del.removeEvent('click');
    this.moveDown.removeEvent('click');
    this.moveUp.removeEvent('click');
  },
  
  handleDelete: function(e) {
    e.stop();
    this.section.deleteItem(this);
  },
  
  handleMove: function(e, delta) {
    e.stop();
    this.section.moveItem(this, delta);
  }
});
