/******************************************
 * The Twopage Checkout Class
 ******************************************/
if (typeof crumbs.Checkout == 'undefined' || !crumbs.Checkout) {
    crumbs.Checkout = {};
}

crumbs.Checkout.Twopage = Class.create({
	initialize: function (step, forms) {
		this.step = step;
		this.forms = forms;

		// submitters
		this.checkoutForm = $('checkout-form');
		this.submitButton = $('twopage-submit');
		this.submitButton.observe('click', this.submitCheckout.bindAsEventListener(this));
	},

	/**
	 * Event fired when the 'Next' button is clicked to submit the checkout form
	 */
	submitCheckout: function (event) {
		var success = true;
		switch (this.step) {
			case 'address':
				billForm = this.forms[0];
				shipForm = this.forms[1];
				success = billForm.validator.validate();
				if (!shipForm.form.hasClassName('hidden')) {
					success &= shipForm.validator.validate();
				}
				break;

			case 'shipping_method':
			case 'payment':
				success = this._validateForms();
				break;
		}		
		if (success) {
			this.checkoutForm.submit();
		}
	},

	/**
	 * Validates the forms and returns the result.
	 */
	_validateForms: function () {
		for (i = 0; i < this.forms.length; i++) {
			var validate = this.forms[i].validator.validate();
			if (!validate) {
				return false;
			}
		}
		return true;
	}
});

/**
 * The Twopage Calendar class
 */
crumbs.Checkout.Calendar = Class.create({
	initialize: function (config) {
		this.config = config;
		this.table = $('delivery-date-calendar');
		this.hidden = $('delivery-date');
		var methodContainer = $('available-methods');
		if (methodContainer) {
			this.shippingMethods = methodContainer.select('ul li input');
		}
		
		this.table.select('a').each(function (el) {
			if (this.hidden.value == el.getAttribute('date')) {
				this._selectDate(el);  // set to previously chosen date
			}
			el.observe('click', this.eventClickCalendarDay.bindAsEventListener(this));
		}.bind(this));

		if (!this.table.down('.selected')) {
			this._selectDate(this.table.down('a'));  // default the delivery to the first available.
		}
		methodContainer.show();
	},

	/**
	 * Calendar day click event.
	 */
	eventClickCalendarDay: function (event) {
		this._selectDate(event.element());
	},

	/**
	 * Sets dayLink as the selected delivery date in the calendar.
	 */
	_selectDate: function (dayLink) {
		this.table.select('td.selected').each(function (el) {
			el.removeClassName('selected');
		});
		dayLink.up('td').addClassName('selected');
		var date = dayLink.getAttribute('date');
		this.hidden.value = date;
		this._toggleMethods(date);		
	},

	/**
	 * Iterates the shipping and show/hides them according to the config for the provided date.
	 */
	_toggleMethods: function (date) {
		for (i = 0; i < this.shippingMethods.length; i++) {  // iterage shipping methods
			var method = this.shippingMethods[i];
			var found = false;
			for (var j = 0; j < this.config[date].length; j++) {  // iterate valid codes for the selected date
				var code = this.config[date][j];
				if (method.id == 's_method_' + code) {
					found = true;
					break;
				}
			}
			if (found) {
				method.enable();
				method.up('li').show();
			}
			else {
				method.disable().checked = false
				method.up('li').hide();
			}
		}
	}
});

crumbs.Checkout.Cart = Class.create({
	initialize: function () {
		this.url = "";

		this.productLinks = $$("a.product-modal-link");
		for (var i = 0; i < this.productLinks.length; i++) {
			Event.observe(this.productLinks[i], "click", this.openProductModal);
		}
	},

	openProductModal: function (event) {
		var modal = screenManager.getProductModal();
		modal.setId(this.getAttribute("productid"));
		modal.setUrl(this.getAttribute("url"));
		modal.load();
	}
});

