//Should be defined in library.php
//var baseurl = 'http://www.jaqkcellars.com/';
// Update Totals And Quantity Of Cart
function updateTotals(cart) {
	// Test For Null Or Empty Cart
	if (cart != null && cart != undefined) {

		var quantity = 0;
		// Get Actual Quantity Of Cart
		$.each(cart.CartItems._CartItem, function() {
			quantity = quantity + this._Quantity;
		});

		// Update Total Price And Quantity
		if (quantity != 0) {
			$('.actions').show();
			$('.quickCartSubTotal .TotalPrice').text(cart.SubTotal._FormattedPrice);
			$('#emptyCart').hide();
			$('.itemCount').text(quantity);
		} else {
			$('.itemCount').text(quantity);
			$('.actions').hide();
			$('#emptyCart').show();
		}
	}
}

var delegateAddToCart = null;
//  Add Product To Cart
function addToCart(id, quantity, wine) {

	// Check If Wine Product
	if(wine){
		// Check If Cookie Is Valid
		var state = $.cookie('state');
		var birthdate = $.cookie('birthdate');
		if (state == undefined || birthdate == undefined) {
			delegateAddToCart = function() { addToCart(id, quantity, false);  };
			// Open Modal
			ShowSelectStateModal();
		} else {
			add(id, quantity);
		}
	} else {
		add(id, quantity);
	}
}

function add(id, quantity){
		var url = baseurl + 'WebService/AddToCart/'+ id + '/' + quantity;
		$.ajax({
			type: 'GET',
			url: url,
			dataType: 'json',
			success: function(cart) {
			updateCart(cart);
			isOpen();
			window.scroll(0, 0);
			},
			error: function(cart) {
				alert('Cart update error');
			}
		});
}

// Test If Quick Cart Is Open
function isOpen(){
	var top = parseInt($('#quickCart-outer').css('marginTop'));
	if(top < -1)
	{
		$("#quickCartButton").click();
	}
}

// Modify Quick Cart
function modifyCart() {

	$('#quickCart table tr.cartRow td').each(function() {

		var id = $('input[type=hidden]', this).val();
		var quantity = $('input[type=text]', this).val();

		var url = baseurl+'WebService/UpdateCart/'+id+'/'+quantity;

		$.ajax({
			type: 'GET',
			url: url,
			success: function() {

			},
			error: function(data) {
				alert('Cart update error');
				return false;
			}
		});
	});

	setTimeout('getCart();', 1500);
}

function getCart() {

	var url = baseurl + 'WebService/GetCart'

	$.ajax({
		type: 'GET',
		url: url,
		dataType: 'json',
		success: function(cart) {
			updateCart(cart);
		},
		error: function(data) {
			alert('Cart update error');
		}
	});
}

// Update Quick Cart Display
function updateCart(cart) {
	// Test For Empty Cart
	if (cart != null && cart != undefined) {
		var html;

		// Get HTML template
		$.get( baseurl + 'Common/QuickCartTemplate.html', function(data) {

			// Clear Old Cart
			$('.quickCartTable tr').remove();

			// Assign Template To New Variable
			html = data;

			// Append Data To Template
			$.each(cart.CartItems._CartItem, function() {
				var item = html;
				
				if (this._Size != null && this._Size.length > 0){
					item = item.replace(/\{Size\}/g, this._Size + ' / ');
				} else {
					item = item.replace(/\{Size\}/g, '');
				}
				
				$('.quickCartTable').append(item.replace(/\{Title\}/g, this._ProductTitle).replace(/\{SkuId\}/g, this._SkuId).replace(/\{Price\}/g, this._Price._FormattedPrice).replace(/\{Quantity\}/g, this._Quantity).replace(/\{ProductType\}/g, this._ProductType));
			
				
			});
		});
	}

	updateTotals(cart);
}

// Delete From Quick Cart
function deleteFromCart(id, caller) {
	var url = baseurl + 'WebService/DeleteFromCart/' + id;
	$.ajax({
		type: 'GET',
		url: url,
		dataType: 'json',
		success: function(cart) {
			$(caller).parents('td.Repeated:first').animate({ height: 0 }, 500).empty();
			updateTotals(cart);
		},
		error: function(xhr, status, error) {
			alert('Cart update error '+xhr.status);
		}
	});
}

function showDelete(caller) {
	$(caller).parents('td:first').find('> .Confirm').css('display', 'block').end().find('> .Item').hide();
}

function hideDelete(caller) {
	$(caller).parents('td:first').find('> .Confirm').hide();
	$(caller).parents('td:first').find('> .Item').show();
}
