document.observe("dom:loaded", function() {
	centerTheDrawer();
	Event.observe(window, 'resize', centerTheDrawer);
	
	$$('#shopping_cart_drawer .handle').invoke('observe', 'click', function(event) {
		if(this.up('#shopping_cart_drawer').hasClassName('open')) {
			new Effect.Morph(this.up('#shopping_cart_drawer').identify(), {
				style: {
					bottom: '-152px'
				},
				duration: 0.3
			});
			this.up('#shopping_cart_drawer').removeClassName('open');
		}
		else {
			new Effect.Morph(this.up('#shopping_cart_drawer').identify(), {
				style: {
					bottom: '0px'
				},
				duration: 0.3
			});
			this.up('#shopping_cart_drawer').addClassName('open');
		}
		
		event.stop();
	});
});

function centerTheDrawer() {
	var drawer = $('shopping_cart_drawer');
	if (drawer)
	{
		var goal = document.viewport.getWidth()/2 - drawer.getWidth()/2;
		
		drawer.setStyle({
			left: Math.max(goal, -38) + 'px'
		});
	}
}