var $shopcart = {
	add: function (value) {
		var flag = false;
		var itens = $shopcart.view();
		// verifica se o item já está incluso
		for (var i=0; i < itens.length; i++) {
			if (itens[i] == value) {
				flag = true;
				break;
			}
		}
		
		// caso o item não tenha sido selecionado seleciona-o e grava no cookie
		if (flag == false) {
			var cookie_cart = $cookie.get('cart');
			new_item = "|" + value;
			$cookie.set('cart', cookie_cart + new_item);
			
			alert("Imóvel adicionado a sua lista de 'Meus Imóveis'");
		}
		else
			alert('Este imóvel já foi selecionado a sua listagem!!!');
	},
	
	rmv: function (item) {
		var cookie_cart = $cookie.get('cart');
		var new_cart = cookie_cart.replace("|" + item, "");
		$cookie.set('cart', new_cart);
	},
	
	view: function () {
		var cookie_cart = $cookie.get('cart');
		
		if (cookie_cart != '')
			return cookie_cart.split('|');
		else
			return '';
	},
	
	show: function () {
		var arr_fields = new Array("index", "label");
		var cont ='';
		var arr_cart = $shopcart.view();
		if (arr_cart != '') {
			for (i=1; i < arr_cart.length; i++) {
				var arr_item = arr_cart[i].split('&');
				var item  = new Array(arr_item.length);
				var itemx = new Array();
				
				for (j=0; j < arr_item.length; j++) {
					var key = arr_fields[j];
					itemx[key] = arr_item[j];
				}
				
				item[i] = itemx;
				
				cont += "<dl>";
				cont += '<dt><a href="index.php?p=show_imovel&amp;id=' + item[i]["index"] + '" title="ver mais informações">' + item[i]["label"] + '</a></dt>';
				cont +="<dd align=\"center\"><a href=\"index.php?p=meus_imoveis\" onclick=\"$shopcart.rmv('" + arr_item[0] + "')\">Retirar</a></dd>";
				cont +="</dl>";
				
				// adiciona os itens no input de listagem
				$g.id('listagem').value += item[i]["label"] + ' | ';
			}
			$g.id('box_shopcart').innerHTML = cont;
		}
		else {
			document.write('<center><strong>Nenhum imóvel selecionado!!!</strong></center>');
		}
	}
}
