﻿
function addToCart(productVariationId) {
	var item = productVariationId;
	var cart = $.cookie('cart');
//	alert($.cookie('cart'));
	
	if (cart != null) {
		$.cookie('cart', cart +  item + '_', { path: '/', expires: 90 });
	} else {
		$.cookie('cart', item + '_', { path: '/', expires: 90 });
	}

//	alert($.cookie('cart'));
	
	var items = $.cookie('cart').split('_');

	showCartItems(true);
}


var _addCartFlashCount = 0;

function animate(toRed) {
	_addCartFlashCount++;
	$('#cartLink').animate({
		color: toRed ? '#FF0000' : '#FFFFFF'
	}, 700,
		function() {
			if (_addCartFlashCount < 5) { animate(!toRed) }
		}
	);
}

function removeAllFromCart() {
	$.cookie('cart', null);
}

function removeFromCart(idx) {
	var cart = $.cookie('cart')
	var cartItems = cart.toString().split('_');

	cart = '';

	// rebuild the cart
	for (i = 0; i < cartItems.length; i++) {
		if (i != idx && cartItems[i].length > 0) {
			cart += cartItems[i] + '_';
		}
	}
	$.cookie('cart', cart, { path: '/', expires: 90 });
	location.reload();

}

function showCartItems(flash) {

	// Can't use Jquery on the front page coz of Facebook
	var cookie = document.cookie;
	var pos = cookie.indexOf('cart=');
//	alert(cookie);
	
	if (pos > -1){
		var posB = cookie.indexOf(';', pos);
		if (posB==-1)
			posB = cookie.length;
			
		var cart = cookie.substring(pos, posB);
		
		var items = cart.split('_');
		document.getElementById('cartLink').innerHTML = 'SHOPPING CART (' + (items.length-1) + ')';
//	}else{
//		$.cookie('cart', '', { path: '/', expires: 90 });
//		document.getElementById('cartLink').innerHTML = 'SHOPPING CART';
	}
	
	if (flash) {
		_addCartFlashCount = 0;
		animate(true);
	}
}


