/*
Cart JS pour Piecesavenue
Copyright(c) 2009, Skalpel.

Author : Killian

*/



window.addEvent('domready', function() {
	new SKjs.AccessoriesList();
});

SKjs.AccessoriesList = new Class({
    Implements: [Events, Options],
	options: {
        elmAddToCart:      'a.link_add_to_cart',
        elmQuantityText:        'qte',
        elmQuantityAdd:         'plus',
        elmQuantityRemove:      'minus'
	},

	initialize: function(options) {
		this.setOptions(options);

		this.dom            = {};
		this.fx             = {};
        this.cart           = new SKjs.Panier();

        this.truck          = new SKjs.Camion();

		this.initDom();
		this.initEvents();
	},

	/*
	Property :
		On initialise l'ensemble des evenements
	*/
	initDom: function() {
        this.dom.addtocart          = $$(this.options.elmAddToCart);
        this.dom.qty                = $(this.options.elmQuantityText);
        this.dom.qtyAdd             = $(this.options.elmQuantityAdd);
        this.dom.qtyRemove          = $(this.options.elmQuantityRemove);
	},

	/*
	Property :
		On initialise l'ensemble des evenements
	*/
	initEvents: function() {
        this.dom.addtocart.each(function(button) {
            button.addEvent('click', this.onAdd.bind(this));
        }, this);

        if ($chk(this.dom.qty)) {
            this.dom.qty.addEvent('keyup', this.checkQty.bind(this));
        }

        // Product number buttons
        if ($chk(this.dom.qtyAdd)) {
            this.dom.qtyAdd.addEvent('click', this.onAddQuantity.bind(this));
        }
        if ($chk(this.dom.qtyRemove)) {
            this.dom.qtyRemove.addEvent('click', this.onRemoveQuantity.bind(this));
        }
	},

    checkQty: function(e) {
        this.dom.qty.value = this.dom.qty.value.replace(/[^0-9]/g, '');
    },

    onAdd: function(e) {
        var event = new Event(e);
        event.stop();
        var nbr = 1;

        if ($chk(this.dom.qty) && this.dom.qty.value !== '') {
            nbr = parseInt(this.dom.qty.value);
        }

        var id = e.target.id.split('_');
        this.cart.add(id[1], 'DbAccessoryproduct', nbr, e.target.href);
        this.truck.confirm('Votre produit a été ajouté au panier', {
            onConfirm: function(e) {
                var event = new Event(e);
                event.stop();
            },
            onCancel: function(e) {
                (function() {
                    window.location = $('cart_link').href;
                }).bind(this).delay(500);
            }
        });
    },

    /*
    Increase quantity
     */
    onAddQuantity: function(e) {
        var event = new Event(e);
        event.stop();

        this.dom.qty.value = this.dom.qty.value.toInt();
        this.dom.qty.value++;
    },

    /*
    Decrease quantity
     */
    onRemoveQuantity: function(e) {
        var event = new Event(e);
        event.stop();

        this.dom.qty.value = this.dom.qty.value.toInt();
        this.dom.qty.value--;

        if (this.dom.qty.value < 1) {
            this.dom.qty.value  = 1;
        }
    }
});