var ShippingCalculator = Class.create({
	initialize: function() {
		this.form = $('ShippingCalculator');
		this.overlay = new Element('div').setStyle({
			'position': 'absolute',
			'top': 0,
			'left': 0,
			'z-index': 99,
			'width': '100%',
			'height': Element.getHeight(document.body) + 'px',
			'backgroundColor': '#000',
			'display': 'none'
		});
		$(document).body.insert(this.overlay);
		Event.observe($(document), 'shippingcalculator:display', this.display.bind(this));
		Event.observe($(document), 'shippingcalculator:update', this.update.bind(this));
		Event.observe($(document), 'shippingcalculator:close', this.close.bind(this));
		$('country').observe('change', function() {
			this.handle_buttons();
		}.bind(this));
		$('state').observe('change', function() {
			this.handle_buttons();
		}.bind(this));
		$('zip').observe('keyup', function() {
			this.handle_buttons();
		}.bind(this));
		this.form.select('input[type="radio"]').invoke('observe', 'click', function() {
			this.handle_buttons();
		}.bind(this));
		this.fix_radios();
		this.handle_buttons();
	},
	display: function() {
		this.center();
		this.overlay.appear({
			duration: 0.2,
			from: 0.0,
			to: 0.6
		});
		this.form.appear({
			duration: 0.2,
			from: 0.0,
			to: 1
		});
	},
	close: function() {
		this.overlay.fade({
			duration: 0.2
		});
		this.form.fade({
			duration: 0.2
		});
	},
	handle_buttons: function() {
		if (($F('country').length > 0) && ($F('state').length > 0) && ($F('zip').length > 0)) {
			$('update').show();
		} else {
			$('update').hide();
		}
		var selected_shipping = this.form.select('input[type="radio"]').find(function(cb) {
			return cb.checked;
		});
		if (!selected_shipping) {
			if ($('submit')) {
				$('submit').hide();
			}
		} else {
			if ($('submit')) {
				$('submit').show();
			}
		}
	},
	fix_radios: function() {
		this.form.select('input[type="radio"]').each(function(btn) {
			btn.writeAttribute('onfocus', '');
		});
	},
	update: function() {
		var selected_shipping = this.form.select('input[type="radio"]').find(function(cb) {
			return cb.checked;
		});
		new Ajax.Updater('methods', 'partial_shipping_methods.php', {
			method: 'get',
			parameters: {
				country: $F('country'),
				state: $F('state'),
				zip: $F('zip'),
				method: selected_shipping ? selected_shipping.getValue() : ''
			},
			onLoading: function() {
				$('methods').update('Updating...')
			},
			onComplete: function() {
				this.fix_radios();
				this.handle_buttons();
				this.center();
				this.form.select('input[type="radio"]').invoke('observe', 'click', function() {
					this.handle_buttons();
				}.bind(this));
			}.bind(this),
			onFailure: function() {
				$('methods').update('An error occurred while retrieving shipping methods');
			}
		});
	},
	center: function() {
		var view = $(document).viewport.getDimensions();
		var offset = $(document).viewport.getScrollOffsets();
		this.form.setStyle({
			'top': ((view.height - this.form.getHeight()) / 2) + offset.top + 'px',
			'left': ((view.width - this.form.getWidth()) / 2) + 'px'
		});
	}
});
