var Cart = new Class({
  Implements: Options,
  debug: false,

  initialize: function ()
  { 
    if ( this.bootstrap() ) {
      if(this.debug) console.log( 'Shop cart initialized..' );
    }
  },

  changePayment: function ( element )
  {
    if(this.debug) console.log( element.value );
    if(this.debug) console.log( document.payment.action );
    if( element.value == 'iDeal' ) {
      var action = '/shop/order/idealpayment.html';
    } else if( element.value == 'mrcash' ) {
      var action = '/shop/order/mrcashpayment.html';
    } else {
      var action = '/shop/order/done.html';
    }
    $('payment_form').set('action', action );
  },

  updateField: function ( element )
  {
    if( $('shipping').checked ) $( 'customer_shipping_' + element.id.substr(9) ).value = element.value;
  },

  get: function ()
  {
    new Request.JSON({url: '/index.php?module=shop&section=order&action=getCart&format=json', method: 'get', onSuccess: function( response ){
      if(this.debug) console.log( response );
      this.updateCart( response );
    }.bind(this)}).get();
  },

  updateCart: function ( obj )
  {
    if( obj && obj.items ) {
      total = obj.total.toFloat().round(2);
      total += '';
      total = ( total.indexOf('.') > 0 ) ? total: total + '.00';
      $('cart_status').set('html', 'Producten: ' + obj.items + ' totaal: &euro; ' + total.replace('.', ',') );
    }
  },

  add: function ( form )
  {
    new Request.JSON({url: form.action, method: 'post', onSuccess: function( response ){
      if(this.debug) console.log( response );
      this.updateCart( response );
      window.location = '/shop/order/cart.html';
    }.bind(this)}).send(form);
  },

  bootstrap: function ()
  {
    this.get();
    return true;
  }
});

