window.onload = resize;

function resize()
{
	if($$('.content_right').length >= 1)
	{
		var r = $$('.content_right');
		var l = $$('.content_left');
		var rh = r[0].getCoordinates().height.toInt();
		var lh = l[0].getCoordinates().height.toInt();
		
		if(rh > lh)
			l[0].setStyle('height', rh-2 +'px');
		else
			r[0].setStyle('height', lh-32 +'px');
	}
}

var cart = new Class({
	
	setOverlay: function()
	{
		var bodyCoords = window.getSize();
		this.overlay = new Element('div').setProperty('id', 'overlay').setStyles({position: 'absolute', top: '0px', left: '0px', width: bodyCoords.scrollSize.x+'px', height: bodyCoords.scrollSize.y+'px'}).setOpacity(0).addClass('ie6fix').injectBefore(document.body.childNodes[1]);
	},
	
	setCart: function()
	{
		this.cart = new Element('div').setProperty('id', 'shopping_cart').setOpacity(0).injectInside($('cart'));
		var innerCart = new Element('div').setProperty('id', 'shopping_cart_inner').injectInside(this.cart);
		new Element('h1').setHTML('Shopping Cart').injectInside(innerCart);
		items = new Element('p').setHTML('Items: ').injectInside(innerCart);
		new Element('span').setProperty('id', 'cart_no').injectInside(items);
		var total = new Element('p').setHTML('Total: ').injectInside(innerCart);
		price = new Element('span').setHTML('&pound;').addClass('red').injectInside(total);
		new Element('span').setProperty('id', 'cart_price').injectInside(price);
		new Element('a').setHTML('Go To Checkout').setProperties({href: 'checkout.php', title: 'Checkout'}).injectInside(innerCart);
	},
	
	fade: function(el, deg, degd)
	{
		var fadeIn = new Fx.Styles(el, {duration: 500, transition: Fx.Transitions.sineIn});
		fadeIn.start({'opacity': [[deg], [degd]]});
	},
	
	getQuantity: function(id, price, type, name, weight)
	{
		this.setOverlay();
		this.fade(this.overlay, 0 , 0.9);
		var topY = window.getSize().scroll.y + (window.getSize().size.y / 2) - 200;
		var leftX = (window.getSize().scrollSize.x / 2) - 112;
		this.quantityHolder = new Element('div').setProperty('id', 'quantity_holder').setStyles({position: 'absolute', top: topY+'px', left: leftX+'px'}).injectBefore(document.body.childNodes[1]);
		var quantityHolderInner = new Element('div').setProperty('id', 'quantity_holder_inner').injectInside(this.quantityHolder);
		new Element('span').setHTML('<b>'+name+'</b><br />£'+price+' per '+weight+'<br /><br />').injectInside(quantityHolderInner);
		if( type == 1 ) {
			new Element('span').setHTML('Please Select A Weight: ').injectInside(quantityHolderInner);
			var weightSelect = new Element('select').setProperties({id: 'weight_select', name: 'weight'}).injectInside(quantityHolderInner);
			new Element('option').setProperty('value', 0.1135).setHTML('113.5 g').injectInside(weightSelect);
			new Element('option').setProperty('value', 0.227).setHTML('227 g').injectInside(weightSelect);
			new Element('option').setProperty('value', 0.3405).setHTML('340.5 g').injectInside(weightSelect);
			for( i = 0.5; i <= 10; i+=0.5 )
				new Element('option').setProperty('value', i).setHTML(i+' kg').injectInside(weightSelect);
			new Element('br').injectAfter(weightSelect);
		}
		new Element('span').setHTML('Please Select A Quantity: ').injectInside(quantityHolderInner);
		var quantitySelect = new Element('select').setProperties({id: 'quantity_select', name: 'quantity'}).injectInside(quantityHolderInner);
		for(i = 1; i <= 10; i++)
			new Element('option').setProperty('value', i).setHTML(i).injectInside(quantitySelect);
		
		if( type == 1 )
			new Element('span').setHTML('<br /><br /><i>Suggested serving portions<br />60 gram per person</i><br /><br />').injectInside(quantityHolderInner);
		
		var btt = new Element('div').addClass('ie6fix').setHTML('Add To Cart').injectInside(quantityHolderInner);
		//btt.addEvent( 'click', function (event, id, price) { this.addToCart(id, price) }.bindAsEventListener( this, [id,price] ) );
		btt.addEvent( 'click', this.addToCart.bind( this, [id, price, type] ) );
		if( type == 1 )
			new Element('span').setHTML('<br /><i class="small_desc">* 1 kg = 2,2 llbs</i>').injectInside(quantityHolderInner);
	},
	
	addToCart: function(id, price, type)
	{
		new Ajax('classes/class.cart.php', {
			method: 'post',
			postBody: {
				ajax: 'add',
				id: id,
				price: price,
				type: type,
				qty: $('quantity_select').getValue(),
				weight: ($('weight_select')?$('weight_select').getValue():0)
			},
			onComplete: this.updateCart.bind(this)
		}).request();
	},
	
	updateCart: function(res)
	{
		this.fade(this.overlay, 0.9, 0);
		this.fade(this.quantityHolder, 0.9, 0);
		this.overlay.remove();
		this.quantityHolder.remove();
		var obj = Json.evaluate(res);
		if(obj.type == 'success' && obj.old.products == 0)
		{
			this.setCart();
			$('cart_no').setHTML('0');
			$('cart_price').setHTML('00.00');
			(function(){this.fade(this.cart, 0 , 0.95)}).delay(500, this);
		}
		$('cart_no').setHTML(obj.updated.products)
		this.end = parseFloat(obj.updated.value);
		this.updating = parseFloat(obj.old.value);
		this._updateCart();
	},
	
	_updateCart: function() {
		if(this.updating <= this.end)
		{
			$('cart_price').setHTML(this.updating.toFixed(2))
			setTimeout(this._updateCart.bind(this), 1)
			this.updating = this.updating + .50;
		}
		else
			$('cart_price').setHTML(this.end);
	}

});
                           
var c = new cart();
