var shop = {
	viewing : '',
	href : '',
	cart : {
		items : [],
		add : function (s_title, s_size, n_lightboximage) {
			flag = null;
			this.items.each (function(o_item,n_index){
				if (o_item.title == s_title && o_item.size == s_size) {
					o_item.quantity ++; flag = n_index; return;
				};
			});
			if (flag === null) {
				//todo: need to search for group and insert at that point, for example: adding a new print
				//of an existing group, when there are multiple groups already.
				this.items.unshift ({
					title    : s_title,
					size     : s_size,
					quantity : 1,
					lightbox : n_lightboximage
				});
				return 0;
			} else {
				return flag;
			}
		},
		remove : function (n_index) {
			if (-- this.items[n_index].quantity == 0) {
				this.items[n_index] = null;
				this.items = this.items.compact ();
				return null;
			} else {
				return n_index;
			};
		}
	},
	
	//user clicks an add-to-cart button
	addToCart : function (e_element, s_size) {
		//has the user clicked a print first?
		if (!this.viewing) {
			$('added').update ("Please select your print first").show ().fade ({from: 0.0, to: 0.65, duration: 0.3, queue: 'cart'}).
			fade ({from: 0.65, to: 0.0, duration: 0.3, delay: 2.0, queue: 'cart'});
			
		} else {
			//find the corresponding lightbox image
			var lightbox_image = '';
			window.lightbox.imageArray.each (function(a_item){
				if (a_item[1] == this.viewing) {lightbox_image = a_item[0]; return;}
			}, this);
			
			this.display (this.cart.add (this.viewing, s_size, lightbox_image));
			
			$('added').update ("Added to Cart").fade ({from: 0.0, to: 0.65, duration: 0.3, queue: 'cart', beforeStart: function(){
				$('added').setOpacity (0.0).show ();
			}}).
			fade ({from: 0.65, to: 0.0, duration: 0.3, delay: 2.0, queue: 'cart'});
		}
	},
	
	removeItem : function (n_index) {
		this.display (this.cart.remove (n_index));
	},
	
	//generate the cart html
	display : function (n_item) {
		var total    = 0,
		    delivery = 0,
		    html     = '',
		    title    = '',
		    c        = 1
		;
		if (!this.cart.items.length) {$('cart').update ('<p>Shopping Cart is Empty</p>'); return;};
		
		html += '<form id="paypal" action="https://www.paypal.co.uk/cgi-bin/webscr" method="post">'+
		        '<input type="hidden" name="cmd" value="_cart">'+
			'<input type="hidden" name="upload" value="1">'+
			'<input type="hidden" name="business" value="kiran_182@hotmail.com">'+
			'<input type="hidden" name="currency_code" value="GBP">'+
			'<table>'
		;
		this.cart.items.each (function(o_item,n_index){
			var price = parseInt ($('size-'+o_item.size).innerHTML.substr (1), 10);
			if (title != o_item.title) {
				title = o_item.title;
				html += '<tr><td colspan="4">';
				html += '<a href="'+o_item.lightbox+'" rel="lightbox" title="'+o_item.title+'">'+o_item.title+'</a>'+
					'</td></tr>\n'
				;
			};
			html += ('<tr#{id}><td><input type="hidden" name="item_name_#{c}" value="#{name}">#{size}</td>'+
				'<td align="right"><input type="hidden" name="amount_#{c}" value="#{price}">&pound;#{price}</td>'+
				'<td align="right"><input type="hidden" name="quantity_#{c}" value="#{quantity}">'+
				'<a href="javascript:shop.removeItem(#{index});" title="click to remove item">'+
				'&times;</a> #{quantity}</td><td align="right">&pound;#{total}</td></tr>\n').interpolate (
			{
				'id'       : n_index === null ? '' :  (n_index == n_item ? ' id="cart-anim"' : ''),
				'c'        : c ++,
				'name'     : o_item.title+' ('+o_item.size+')'.escapeHTML (),
				'size'     : o_item.size,
				'price'    : price,
				'index'    : n_index,
				'quantity' : o_item.quantity,
				'shipping' : (o_item.size == '20x30' || o_item.size == '30x40') ? 18 : 11,
				'total'    : price * o_item.quantity
			});
			total += (price * o_item.quantity);
			delivery += ((o_item.size == '20x30' || o_item.size == '30x40') ? 18 : 11) * o_item.quantity;
		}, this);
		//total
		html += '<tr><th colspan="3">total</th><td align="right"><b>&pound;'+total+'</b></td></tr>' +
			'<tr><th colspan="3">postage</th><td align="right"><b>Free!</b></td></tr>' +
		        '</table><p><a id="checkout" href="javascript:$(\'paypal\').submit ();">checkout!</a></p>' + 
		        '</form>'
		;
		$('cart').update (html);
		if (n_item !== null) $('cart-anim').highlight ();
	}
};
