(function () {

var methodsData = {
	AGENCY: {
		steps: ['method', 'date', 'address', 'delivery', 'confirm'],
		isDelivery: true,
		textId: 'agencyText'
	},
	DELIVERY: {
		steps: ['method', 'date', 'address', null, 'confirm'],
		isDelivery: true,
		textId: 'deliveryText'
	},
	PICKUP: {
		steps: ['method', 'date', 'address', 'delivery', 'confirm'],
		isDelivery: false,
		textId: 'pickupText'
	}
};

Hedera.Checkout = new Class
({
	Extends: Hedera.Form

	,initialize: function (props)
	{
		this.today = new Date ();
		this.today.setHours (0, 0, 0, 0);
		this.parent (props);
	}

	,activate: function ()
	{
		this.autoStepLocked = true;
	}

	,onValuesReady: function ()
	{
		var orderForm = this.$.orderForm;
		var defaultsForm = this.$.defaults;

		if (!(orderForm.ready && defaultsForm.ready))
			return;

		var date;

		if (orderForm.numRows > 0)
		{
			var i = orderForm;
			date = i.$.date_send;
		}
		else
			var i = defaultsForm;
		
		if (!date || date.getTime () < (new Date ()).getTime ())
		{
			date = new Date ();

			if (i.$.delivery_method != 'PICKUP')
				date.setDate (date.getDate () + 1);

			date.setHours (0, 0, 0, 0);
		}
		
		this.$.lot.assign ({
			date: date,
			method: i.$.delivery_method,
			agency: i.$.agency_id,
			address: i.$.address_id
		});
		this.autoStepLocked = false;
	}
	
	,disableButtons: function (disable)
	{
		this.$.confirmButton.disabled = disable;
	}

	,onConfirmClick: function ()
	{
		this.disableButtons (true);
	
		var query = 'CALL basketConfigure (#date, #method, #agency, #address)';	
		this.conn.execQuery (query,
			this.onBasketConfigured.bind (this), this.$.lot.params);
	}

	,onBasketConfigured: function (resultSet)
	{
		this.disableButtons (false);

		if (!resultSet.fetchResult ())
			return;
	
		var isUpdated = this.$.orderForm.numRows > 0;
		Htk.Toast.showMessage (_(isUpdated ? 'OrderUpdated' : 'OrderStarted'));

		this.hash.setAll ({form: 'ecomerce/catalog'});
	}

	,onCancelClick: function ()
	{
		if (this.$.orderForm.numRows > 0)
			window.history.back();
		else
			this.hash.setAll ({form: 'ecomerce/orders'});
	}
	
	,stepFunc: function (stepIndex)
	{
		var method = this.$.rgMethod.value;
		var methodData = methodsData[method];
		var stepName;

		if (methodData !== undefined)
			stepName = methodData.steps[stepIndex];
		else
			stepName = 'method';

		if (!stepName)
			return null;

		switch (stepName)
		{
			case 'date':
				var dateQuestion = methodData.isDelivery ?
					'OrderDateDeliveryQuestion' : 'OrderDatePickupQuestion';
				Vn.Node.setText (this.$.dateQuestion, _(dateQuestion));
				this.$.calendar.goToSelectedMonth ();
				break;
			case 'delivery':
				var whQuestion = methodData.isDelivery ?
					'AgencyQuestion' : 'PickupWarehouseQuestion';
				Vn.Node.setText (this.$.whQuestion, _(whQuestion));
				this.$.agencies.refresh ();
				break;
			case 'confirm':
				for (var myMethod in methodsData)
				{
					var display = myMethod == method ? 'block': 'none';
					this.$[methodsData[myMethod].textId].style.display = display;
				}
				break;
		}
		
		return stepName;
	}
	
	,onFieldChange: function (_, value)
	{
		if (!this.autoStepLocked && value != null)
			setTimeout (this.goNextStep.bind (this), 75);
	}
	
	,goNextStep: function ()
	{
		this.$.assistant.moveNext ();
	}
	
	,addressRenderer: function (builder, form)
	{
		builder.$.address.addEventListener ('click',
			this.onAddressClick.bind (this, form.$.id));
	}
	
	,onAddressClick: function (addressId)
	{
		this.$.lot.set ('address', addressId);
		this.goNextStep ();
	}
	
	,onAddressChange: function ()
	{
		if (this.selectedNode)
			Vn.Node.removeClass (this.selectedNode, 'selected');

		var row = this.$.addresses.search ('id', this.$.lot.$.address);

		if (row != -1)
		{
			var builder = this.$.repeater.getScope (row);
		
			this.selectedNode = builder.$.address;
			Vn.Node.addClass (this.selectedNode, 'selected');
		}

		this.$.addressForm.row = row;
	}
	
	,onAgenciesReady: function (model)
	{
		if (model.ready && model.numRows == 0)
			Htk.Toast.showError (_('NoAgeciesAvailableForDate'));
	}
	
	,onWarehousesReady: function (model)
	{
		if (model.ready && model.numRows == 0)
			Htk.Toast.showError (_('NoWarehousesAvailableForDate'));
	}
	
	,calendarRestrict: function (date)
	{
		return date.getTime () >= this.today.getTime ();
	}
});

})();
