var COOKIE_NAME = 'basketProducts';
var options = { path: '/', expires: 10 };

function cookieUpdateProduct(prodId, value) {
	// Get cookie value
	var cookieValue = $.cookie(COOKIE_NAME);
	if (cookieValue != undefined) {
		var obj = $.evalJSON(cookieValue);
		var newValues = new Array();
		var found = false;
		for (x in obj) {
			if (obj[x][0] == prodId) {
				obj[x][1] = value;
				found = true;
			}
			if (obj[x][0] != undefined) {
				// Add current product to list
				newValues.push(obj[x]);
			}
		}
		if (!found) {
			newValues.push(new Array(prodId, value));
		}
		$.cookie(COOKIE_NAME, $.toJSON(newValues), options);
		updateShoppingListCounter();		
		return newValues.length;
	} else {
		var newValues = new Array();
		$.cookie(COOKIE_NAME, $.toJSON(newValues), options);
		showShoppingListNotification();
	}
}
	
function cookieRemoveProduct(prodId) {
	// Get cookie value
	var cookieValue = $.cookie(COOKIE_NAME);
	if (cookieValue != undefined) {
		var obj = $.evalJSON(cookieValue);
		var newValues = new Array();
		for (x in obj) {
			if (obj[x][0] != prodId) {
				newValues.push(obj[x]);
			}
		}
		$.cookie(COOKIE_NAME, $.toJSON(newValues), options);
		updateShoppingListCounter();
		return newValues.length;
	} else {
		var newValues = new Array();
		$.cookie(COOKIE_NAME, $.toJSON(newValues), options);
		updateShoppingListCounter();
	}
}

function cookieAddProduct(prodId) {
	// Get cookie value
	var naam = '';
	var anchor = $('a[rel='+prodId+']');
	if(anchor) {
		var ul = anchor.parent().parent();
		var main = $('h2 a', ul).html();
		var sub = $('h3 a', ul).html();
		if(main && sub) {
			naam = main+sub;
		} else {
			var naam_li = $('#breadcrumb ul li');
			naam = $(naam_li[naam_li.length-1]).html();
		}
	}
	_gaq.push(
		['_trackEvent', 'shopping list', 'add', naam]
	);

	var cookieValue = $.cookie(COOKIE_NAME);
	if (cookieValue != undefined) {
		var obj = $.evalJSON(cookieValue);
		var newValues = new Array();
		var found = false;
		for (x in obj) {
			if (obj[x][0] == prodId) {
				obj[x][1]++;
				found = true;
			}
			if (obj[x][0] != undefined) {
				// Add current product to list
				newValues.push(obj[x]);
			}
		}
		if (!found) {
			newValues.push(new Array(prodId, 1));
		}
		$.cookie(COOKIE_NAME, $.toJSON(newValues), options);
	} else {
		var newValues = new Array();
		newValues.push(new Array(prodId, 1));
		$.cookie(COOKIE_NAME, $.toJSON(newValues), options);
	}
	updateShoppingListCounter();
	showShoppingListNotification();
}

function cookieCountProducts(){
    var cookieValue = $.cookie(COOKIE_NAME);
    if (cookieValue != undefined) {
        var obj = $.evalJSON(cookieValue);
        var products = 0
        for(x in obj){
		if (obj[x][0] != undefined) {
			products++;
		}
        }
        return products
    }
	return 0;
}

function updateShoppingListCounter(){
	if($('#shopping-list-count').length){
	     $('#shopping-list-count').html(cookieCountProducts());
	}    
}

function showShoppingListNotification() {
	var status = $('#shopping-list-status');
	status.slideDown('slow', function() {
		var t = setTimeout(function() {
        		status.slideUp('slow');
    		}, 3000);
	});	
}
