
	$( function() {
		$('.addToCart').live('click', function() {
			var qty = $(this).parent().find('#qty').val() || $(this).attr('qty');
			
			if( qty == 0 || qty == 'undefined' ) {
				
				var box = $(this).prev().queue('fx');
				
				$(this).prev().fadeOut('fast');
				$(this).prev().css({ borderColor: '#ff0000' });
				$(this).prev().fadeIn('fast');
				
			} else {
				
				addToCart( $(this).attr('prod_id'), qty, $(this).attr('warehouse') );
				
				// check for a callback and pass self to it
				var element = this;
				if( $(this).attr('callback') != "" ) {
					var callback = window[$(this).attr('callback')];
					if( typeof callback == 'function' ) {
						callback( element );
					}
				}
			}
			return false;
		});
		
		$('.editCartItem').live('click', function() {
			var qty = $(this).parent().find('#qty').val() || $(this).attr('qty');
			
			editCartItem( $(this).attr('prod_id'), qty, $(this).attr('warehouse') );
			
			// check for a callback and pass self to it
			var element = this;
			if( $(this).attr('callback') != "" ) {
				callback = window[$(this).attr('callback')];
				if( typeof callback == 'function' ) {
					callback( element );
				}
			}
		});
		
		$('.alternativeShipping input').click( function() {
			if( this.checked ) {
				window.location = '/cart/?action=overrideShipping&shipping=prepaid';
			} else {
				window.location = '/cart/?action=overrideShipping&shipping=paynow';
			}
		});
		
		$('.continueShoppingContent a.continue').click( function() {
			$('#cboxClose').click();
		});
		$('.continueShoppingContent a.gotocart').click( function() {
			window.location = '/cart';
		});
		
	});

	function editCartItem( id, qty, warehouse ) {
		$.post('/cart/ajax/', {
			action: 'edit',
			id: id,
			qty: qty,
			warehouse: warehouse
		}, function( data ) {
			
			
		});
	}
	
	function addToCart(id, qty, warehouse) {
	
		$.post('/cart/ajax/', {
			action: 'add',
			id: id,
			qty: qty,
			warehouse: warehouse
		}, function(data) {
			
				$('.mamabar .cart .counter').fadeOut('fast').html( data.itemCount ).fadeIn('fast');
				
				if( typeof data.remaining == 'number' && data.remaining > 0 ) {
					
					var container = $('.alternateWarehouse .alternateWarehouseContent');
					var warehouseList = container.find('.warehouseList').empty();
					
					container.find('.allavailable, .someavailble, .requestedItems div').hide();
					
					// Show the text block for remaining product in one or more warehouses
					if( data.otherWarehouseQty.length == 1 ) {
						container.find('.one').show();
					} else if( data.otherWarehouseQty.length > 1 ) {
						container.find('.more').show();
					} else if ( data.otherWarehouseQty.length == 0 ) {
						container.find('.none').show();
					}
					
					if( data.qtyInStock >= data.remaining ) {
						container.find('.allavailable').show();
					} else {
						container.find('.someavailble').show();
						container.find('.available').text( data.qtyInStock );
					}
					
					container.find('span.requested').text( data.added + data.remaining );
					container.find('span.added').text( data.added );
					container.find('span.remaining').text( data.remaining );
					
					// Create list of warehouses which have stock
					
					for( var i in data.otherWarehouseQty ) {
						var w = data.otherWarehouseQty[i];

						var warehouse = container.find('.warehousetemplate').clone();
						$(warehouse).find('.location').text( w.city + ', ' + w.province );
						$(warehouse).find('.addToCart').attr({
							qty: w.qty,
							warehouse: w.warehouseId,
							prod_id: data.itemId,
							callback: 'updateAlternateWarehouse'
						});
						$(warehouse).appendTo(warehouseList).removeClass('warehousetemplate');
					}
					
					var height = 200 + 50 * data.otherWarehouseQty.length;
					$.colorbox({
						opacity: 0.5,
						overlayClose: false,
						width: 700,
						height: height,
						inline: true,
						href: '.alternateWarehouse .alternateWarehouseContent'
					});
					return;
				} else {
					$.colorbox({
						opacity: 0.5,
						overlayClose: false,
						width: 330,
						height: 130,
						inline: true,
						href: '.continueShopping .continueShoppingContent'
					});
				}
				
				
				
		}, 'json');
		
	};
	
	function updateAlternateWarehouse( element ) {
		var remaining = $(element).closest('.alternateWarehouseContent').find('.requestedItems .remaining:visible');
		remaining.text( remaining.text() - $(element).attr('qty') );
		$(element).parent().slideUp('fast', function() {
			$(this).remove();
		});
	}

	
