/**
 * Checkout.js - Checkout functionality
 * 
 * @param {Mixed} form Selector or reference of the checkout form
 * @param {Object} options Optional parameters
 */
function CheckoutForm(form, options) {
	this.form = $(form);
	this.options = options;
	this.init();
};

CheckoutForm.prototype = {
	/**
	 * Initialize
	 */
	init: function() {
		if(this.form.length) {
			this.options = $.extend({
				
			}, this.options || {});
			
			switch(this.form.attr('name')) {
				case 'checkoutCart':
					this.initCart();
					break;
				case 'checkoutPersonalDetails':
					this.initPersonalDetails();
					break;
				case 'checkoutShippingPayment':
					this.initShippingPayment();
					break;
			}
		}
	},
	
	/**
	 * Initialize cart
	 */
	initCart: function() {
		var self = this;
		
		this.form.find('.spinner-input').each(function() {
			new Spinner(this, {
				min: 1,
				onUpdate: function() {
					
				}
			});
		});
	},
	
	/**
	 * 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);
		});
	},
	
	/**
	 * 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);
		});
	}
};
