/**
 * Checkout.js - Checkout functionality
 *
 * @param {Mixed} form Selector or reference of the checkout form
 * @param {Object} options Optional parameters
 */
function CheckoutForm(form) {
	this.form = $(form);
	this.updateTimeout = null;
	this.init();
};

CheckoutForm.prototype = {
	/**
	 * Initialize
	 */
	init: function() {
		if(this.form.length) {
			switch(this.form.attr('name')) {
				case 'checkoutCart':
					this.initCart();
					break;
				case 'checkoutPersonalDetails':
					this.initPersonalDetails();
					break;
				case 'checkoutOverview':
					this.initOverview();
					break;
			}
		}
	},

	/**
	 * Initialize cart
	 */
	initCart: function() {
		var self = this;

		$('.validation-icon, #recalculate').hide();

		this.form.find('.spinner-input').each(function() {
			new Spinner(this, {
				min: 1,
				max: $(this).attr('data-max'),
				updateDelay: 250,
				onUpdate: function() {
					self.sendUpdateRequest();
				}
			});
		});

		this.form.find('#checkout-gift-wrap').click(function() {
			self.sendUpdateRequest();
		});

		this.form.find('#checkout-coupon-code').keyup(function() {
			self.clearTimeout(self.updateTimeout);

			if(this.value.length > 3) {
				self.updateTimeout = setTimeout(function() {
					self.sendUpdateRequest();
				}, 1000);
			}
		});
	},

	/**
	 * Initialize personal details
	 */
	initPersonalDetails: function() {
		var self = this;

		if(!this.form.find('input[name="altShippingAddress"]').is(':checked')) {
			this.collapse($('#alt-shipping-address'));
		}

		this.form.find('input[name="altShippingAddress"]').click(function() {
			self.altShippingAddressClickHandler(this);
		});
	},

	/**
	 * Initialize shipment
	 */
	initShippingPayment: function() {
		var self = this;

		this.form.find(':checked').parents('.option').each(function() {
			self.optionClickHandler(this);
		});

		this.form.find('.option').click(function() {
			self.optionClickHandler(this);
		});
	},

	/**
	 * Initialize overview
	 */
	initOverview: function() {
		this.form.submit(function() {
			$('#checkout').html('<div id="checkout-loader"><img src="/img/loader.gif" alt="De gegevens worden opgehaald, ogenblik geduld a.u.b." width="48" height="48"></div>');
		});
	},

	/**
	 * When the alt shipping address checkbox fires onclick
	 *
	 * @param {Object} el The HTMLInputElement
	 */
	altShippingAddressClickHandler: function(el) {
		$(el).is(':checked') ? this.expand('#alt-shipping-address') : this.collapse('#alt-shipping-address');
	},

	/**
	 * When a shipping- or payment method option fires onclick
	 *
	 * @param {Object} el The option HTMLElement
	 */
	optionClickHandler: function(el) {
		var el = $(el);
		el.siblings('.option').removeClass('selected');
		el.addClass('selected').find('input[type="radio"]').attr('checked', true);
		el.parents('.options').removeClass('error');
	},

	/**
	 * Collapse element
	 *
	 * @param {Object} el The element to collapse
	 */
	collapse: function(el) {
		$(el).removeClass('expanded').addClass('collapsed');
		this.toggleFormElementsByParent(el, true);
	},

	/**
	 * Expand element
	 *
	 * @param {Object} el The element to expand
	 */
	expand: function(el) {
		$(el).removeClass('collapsed').addClass('expanded');
		this.toggleFormElementsByParent(el, false);
	},

	/**
	 * Toggle a form element
	 *
	 * @param {Object} el The HTMLElement
	 * @param {Boolean} toggle True to enable the form element, false to disable
	 */
	toggleFormElement: function(el, toggle) {
		if(typeof toggle === "boolean") {
			el.disabled = toggle;
		}
		else {
			el.disabled = el.disabled ? false : true;
		}
	},

	/**
	 * Toggle form elements inside a parent element
	 *
	 * @param {Object} parent The HTMLElement
	 * @param {Boolean} toggle True to enable the form element, false to disable
	 */
	toggleFormElementsByParent: function(parent, toggle) {
		var self = this;

		$(parent).find(':input').each(function() {
			self.toggleFormElement(this, toggle);
		});
	},

	/**
	 * Request updated data
	 */
	sendUpdateRequest: function() {
		var self = this;

		$.ajax({
			url: $(self.form).attr('action'),
			cache: false,
			dataType: 'json',
			type: 'POST',
			data: self.form.serialize(),
			success: function(data) {
				self.updateCart(data);
			}
		});
	},

	/**
	 * Update the cart
	 *
	 * @param {Object} data The updated data in JSON format
	 */
	updateCart: function(data) {
		/*
		 * JSON:
		 *
			{
				"itemSubtotals": [
					{
						"itemId": 0,
						"subtotal": "&euro; 118,00"
					},
					{
						"itemId": 1,
						"subtotal": "&euro; 25,90"
					},
					{
						"itemId": 2,
						"subtotal": "&euro; 73,50"
					}
				],
				"cartOptions": [
					{
						"optionId": 0,
						"subtotal": "&euro; 2,50"
					},
					{
						"optionId": 1,
						"valid": true,
						"subtotal": "&euro; -40,00"
					}
				],
				"subtotal": "&euro; 217,40",
				"vat": "&euro; 51,00",
				"grandTotal" : "&euro; 268,40"
			}
		 */

		/*for(var i = 0; i < data.itemSubtotals.length; i++) {
			$('#cart-item-' + data.itemSubtotals[i].itemId + ' .total').html(data.itemSubtotals[i].subtotal);
		}

		$('#checkout-grand-total .gross span').html(data.grosstotal);
		$('#checkout-grand-total .discount span').html(data.discounttotal);
		$('#checkout-grand-total .net span').html(data.nettotal);
		$('#checkout-grand-total .quantity span').html(data.quantity);
		$('#checkout-grand-total .total span').html(data.total);*/

        // Werk de prijzen bij
        for (var i = 0; i < data.items.length; i++) {
            $('#checkout-cart-item-' + data.items[i].id + ' .total').html(data.items[i].itemTotal);
            $('#offer-cart-item-' + data.items[i].id + ' .total').html(data.items[i].itemTotalOffer);
            // Kijk of de aanvraag voor de interne of de externe webshop is
            if($('#checkout-grand-total').length) {
                $('#offer-cart-item-' + data.items[i].id + ' .quantity').html(data.items[i].quantity);
            }
        }

        // $('#offer-grand-total span.price-total').html('Totaal: ' + data.totalOffer);

        $('.checkout-grand-total.first span.price-total').html('Totaal excl. BTW: ' + data.totalOffer);
        $('.checkout-grand-total.btw span.price-total').html('BTW: ' + data.totalBtwOffer);
        $('.checkout-grand-total.last span.price-total').html('Totaal incl. BTW: ' + data.totalPriceBtwOffer);
        
        $('#checkout-grand-total span.price-total').html('Totaal: ' + data.total);
        
        // Werk de speciale kosten bij
        for (var i = 0; i < data.itemOtherCosts.length; i++) {
            $('#other-cart-item-' + data.itemOtherCosts[i].id + ' .quantity').html(data.itemOtherCosts[i].quantity);
            $('#other-cart-item-' + data.itemOtherCosts[i].id + ' .total').html(data.itemOtherCosts[i].itemTotalOffer);
        }
	},

	/**
	 * Clear a timeout
	 *
	 * @param {Number} timeout The ID of the timeout to clear
	 */
	clearTimeout: function(timeout) {
		if(timeout != null) {
			clearTimeout(timeout);
			timeout = null;
		}
	}
};

