/*

Este arquivo contem funcoes para gerenciamento de um carrinho de compras

Pre-requisito: cookie

*/

function carrinhoDeCompras() {

	/* Quantidade de dias que o cookie deve permanecer no browser */
	this.prazoExpiracao = 1;

	/* Quantidade de bytes que cada cookie pode receber */
	this.bytesPorCookie = 4000;

	this.adicionar = function (produto, descricao) {

		var objCookie = new cookie();
		var ultimoIndice = this.organizar(produto);

		if (arguments.length > 2) {

			for (var i = 2; i < arguments.length; i++) {

				descricao += "*" + arguments[i];

			}

		}

		var conteudo = objCookie.ler(produto.concat(ultimoIndice));

		if (conteudo === false) {

			var conteudo = "";

		}

		if (conteudo.concat("#",descricao).length > this.bytesPorCookie) {

			ultimoIndice++;
			var conteudo = "";

		}

		conteudo += ((conteudo != "") ? "#" : "") + descricao;

		objCookie.criar(produto.concat(ultimoIndice),conteudo,this.prazoExpiracao);

	}

	this.remover = function (produto, descricao) {

		var objCookie = new cookie();
		var indice = 0;
		var retorno = false;
		var valores = new Array();
		var novosValores = new Array();

		if (arguments.length > 2) {

			for (var i = 2; i < arguments.length; i++) {

				descricao += "*" + arguments[i];

			}

		}

		while (objCookie.ler(produto.concat(indice)) !== false) {

			var valores = valores.concat(objCookie.ler(produto.concat(indice)).split("#"));

			indice++;

		}

		for (var i = 0; i < valores.length; i++) {

			if (valores[i] != descricao) {

				novosValores[novosValores.length] = valores[i];

			}
			else {

				retorno = true;

			}

		}

		this.organizar(produto,novosValores);

		return retorno;

	}

	this.organizar = function (produto) {

		var objCookie = new cookie();
		var indice = 0;
		var valores = new Array();

		if (arguments.length > 1) {

			var novosValores = arguments[1];

		}
		else {

			var novosValores = false;

		}

		while (objCookie.ler(produto.concat(indice)) !== false) {

			var valores = valores.concat(objCookie.ler(produto.concat(indice)).split("#"));

			objCookie.apagar(produto.concat(indice));

			indice++;

		}

		if (novosValores) {

			var valores = novosValores;

		}

		var indice = 0;

		for (var i = 0; i < valores.length; i++) {

			var conteudo = objCookie.ler(produto.concat(indice));

			if (conteudo === false) {

				var conteudo = "";

			}

			if (conteudo.concat("#",valores[i]).length > this.bytesPorCookie) {

				indice++;
				var conteudo = "";

			}

			conteudo += ((conteudo != "") ? "#" : "") + valores[i];

			objCookie.criar(produto.concat(indice),conteudo,this.prazoExpiracao);

		}

		return indice;

	}

	this.listar = function (produto) {

		var objCookie = new cookie();
		var indice = 0;
		var valores = new Array();

		while (objCookie.ler(produto.concat(indice)) !== false) {

			var valores = valores.concat(objCookie.ler(produto.concat(indice)).split("#"));

			indice++;

		}

		for (var i = 0; i < valores.length; i++) {

			valores[i] = valores[i].split("*");

		}

		return valores;

	}

	this.limpar = function (produto) {

		var objCookie = new cookie();
		var indice = 0;
		var valores = new Array();

		while (objCookie.ler(produto.concat(indice)) !== false) {

			objCookie.apagar(produto.concat(indice));

			indice++;

		}

	}

	this.listarTodos = function () {

		var objCookie = new cookie();
		var valores = new Array();
		var produtos = objCookie.listar();

		for (var i = 0; i < produtos.length; i++) {

			var produtoAtual = produtos[i].match(/([a-z]+)/gi)[0];
			var produtoRepetido = false;

			for (var j = 0; j < i; j++) {

				if (produtoAtual == produtos[j].match(/([a-z]+)/gi)[0]) {

					produtoRepetido = true;
					break;

				}

			}

			if (!produtoRepetido) {

				valores[valores.length] = new Array(produtoAtual,this.listar(produtoAtual));

			}

		}

		return valores;

	}

	this.limparTodos = function () {

		var objCookie = new cookie();
		var produtos = objCookie.listar();

		for (var i = 0; i < produtos.length; i++) {

			var produtoAtual = produtos[i].match(/([a-z]+)/gi)[0];
			var produtoRepetido = false;

			for (var j = 0; j < i; j++) {

				if (produtoAtual == produtos[j].match(/([a-z]+)/gi)[0]) {

					produtoRepetido = true;
					break;

				}

			}

			if (!produtoRepetido) {

				this.limpar(produtoAtual);

			}

		}

	}

}