var ShoppingCart = function() {
return { // singleton

refresh: function() {
	if($('#cart').length)
		this.refreshCart();
	else if($('#checkout').length) {
		var fk_delivery = $('input[name="delivery"]:checked').val();
		var fk_payment = $('input[name="payment"]:checked').val();

		if(typeof(fk_delivery) != 'undefined') {
			if(typeof(fk_payment) != 'undefined')
				this.refreshCheckout(fk_delivery, fk_payment);
			else
				this.refreshCheckout(fk_delivery);
		} else
			this.refreshCheckout();
	}
},

refreshCart: function() {
	var serviceUrl = '/wf-service/cart/browse';
	$('#cart').html(
		'<div style="text-align: center; padding: 10px;">'
		+ '<img src="/public/images/ajax-loader.gif">'
		+ '</div>'
	);
	jQuery.getJSON(
		serviceUrl,
		function(response) {
			var html = '';

			if(typeof response != 'undefined' && typeof response.products != 'undefined') {
				var totalPrice = response.totalPrice;
				var totalPriceWithTax = response.totalPriceWithTax;
				var products = response.products;

				html += '<table width="100%">';
				for(var i=0, n=products.length; i<n; i++) {
					var product = products[i];

					html += '<tr>';
						html += '<td width="25">';
							html += '<img class="productImage" src="' + product.image + '"';
							html += ' width="25" height="25">';
						html += '</td>';
						html += '<td>';
							html += '<a class="productLink" href="/' + globals.language + '/detail/' + product.seoUrl + '.html">';

							var productTitle = '';
							if(typeof(product.title) == 'undefined')
								productTitle = product.catalogNumber;
							else
								productTitle = product.title;

							if(productTitle.length > 10)
								html += productTitle.substring(0, 7) + '...';
							else
								html += productTitle;
							html += "</a>";
						html += '</td>';
						html += '<td>';
//							html += '<input type="text"';
//							html += 'style="width: 25px; text-align: right;"';
//							html += ' value="' + product.count + '"';
							html += product.count + 'x';
//							html += '>';
//							html += 'x';
						html += '</td>';
						html += '<td style="text-align: right;">';
							if(globals.settings.displayPriceWithTax)
								html += product.priceWithTax;
							else
								html += product.price;
						html += '</td>';
						html += '<td>';
							html += ' ' + globals.currency.code;
						html += '</td>';
						html += '<td>';
							html += '<img src="/public/images/delete.png"';
							html += ' onclick="ShoppingCart().removeProduct(' + product.id + ')"';
							html += ' width="15" height="15">';
						html += '</td>';
					html += '</tr>';
				}
					html += '<tr>';
						html += '<td colspan="6">';
							html += '<hr>';
						html += '</td>';
					html += '</tr>';

					html += '<tr>';
						html += '<td colspan="2">';
							html += _t('totalPrice', 'c');
						html += '</td>';
						html += '<td colspan="4" style="text-align: right;">';
							html += totalPrice + ' ' + globals.currency.code;
						html += '</td>';
					html += '</tr>';
					html += '<tr>';
						html += '<td colspan="2">';
							html += _t('totalPriceWithTax', 'c');
						html += '</td>';
						html += '<td colspan="4" style="text-align: right;">';
							html += totalPriceWithTax;
							html += ' ' + globals.currency.code;
						html += '</td>';
					html += '</tr>';

				html += '<tr>';
					html += '<td colspan="3">';
						html += '<a onclick="ShoppingCart().clear()" href="#">' + _t('clear'), 'u' + '</a>';
					html += '</td>';
					html += '<td colspan="3" style="text-align: right">';
						html += '<a href="' + globals.baseUrl + '/checkout">' + _t('f_checkout_title', 'u') + '</a>';
					html += '</td>';
				html += '</tr>';

				html += '</table>';

				// $('#amountOfBalance').html(response.amountOfBalance.credit);
			} else {
				// cart is empty
				html  = '<div style="text-align: center; padding: 5px;">';
				html += _t('cartIsEmpty', 'c');
				html += '</div>'
			}

			$('#cart').html(html);
		}
	);
},

clear: function() {
	var serviceUrl = '/wf-service/cart/clear';
	jQuery.get(
		serviceUrl,
		function(response) {
			ShoppingCart().refresh();
		}
	);
},

refreshCheckout: function(fk_delivery, fk_payment) {
	var serviceUrl = '/wf-service/cart/checkout';
	$('#checkout').html(
		'<div style="text-align: center; padding: 10px;">'
		+ '<img src="/public/images/ajax-loader.gif">'
		+ '</div>'
	);

	if(fk_delivery !== null && typeof(fk_delivery) != 'undefined')
		serviceUrl += '/delivery/' + fk_delivery;
	if(fk_payment !== null && typeof(fk_payment) != 'undefined')
		serviceUrl += '/payment/' + fk_payment;

//	var payment = $('[name="payment"]').val();
//	serviceUrl += '/payment/' + payment;

	jQuery.getJSON(
		serviceUrl,
		function(response) {
			var html = '';

			if(response.toString().length > 0 && response.products !== null) {
				var totalPrice = response.totalPrice;
				var totalPriceWithTax = response.totalPriceWithTax;
				var totalWeight = response.totalWeight;
				var totalVolume = response.totalVolume;
				var products = response.products;
				var delivery = response.delivery;
				var discounts = response.discounts.items;

				html += '<table width="100%">';
				for(var i=0, n=products.length; i<n; i++) {
					var product = products[i];
					var productTitle = '';
					if(typeof(product.title) == 'undefined')
						productTitle = product.catalogNumber;
					else
						productTitle = product.title;


					html += '<tr>';
						html += '<td width="25">';
							html += '<img src="' + product.image + '"';
							html += ' width="25" height="25">';
						html += '</td>';

						html += '<td>';
							html += '<a href="/' + globals.language + '/detail/' + product.seoUrl + '.html">';
								if(productTitle.length > 20)
									html += productTitle.substring(0, 20) + '...';
								else
									html += productTitle;
							html += "</a>";
						html += '</td>';

						html += '<td>';
							if(parseInt(product.count) > parseInt(product.quantityOnStock))
								html += '<b style="color: red">' + _t('onway') + '</b>';
							else
								html += _t('onstock');
						html += '</td>';

						html += '</td>';
						html += '<td style="width: 100px;">';
							html += '<input type="text"';
							html += 'id="productCount' + product.id + '"';
							html += ' style="width: 25px; text-align: right;"';
							html += ' onkeydown="ShoppingCart().processKey(event, ' + product.id + ')"';
							html += ' value="' + product.count + '"';
							html += product.count;
							html += '>';
							html += '&nbsp;';
							html += '<a href="#" onclick="ShoppingCart().addProduct(' + product.id + ')">' + _t('update', 'u') + '</a>';
						html += '</td>';

						html += '<td style="width: 50px; text-align: right;">';
							if(globals.settings.displayPriceWithTax)
								html += product.priceWithTax;
							else
								html += product.price;
						html += '</td>';

						html += '<td style="width: 10px;">';
                            html += ' ' + globals.currency.code;
						html += '</td>';

						html += '<td style="width: 20px;">';
							html += '<img src="/public/images/delete.png"';
							html += ' onclick="ShoppingCart().removeProduct(' + product.id + ')"';
							html += ' width="15" height="15">';
						html += '</td>';
					html += '</tr>';
				}

				if(delivery) {
					html += '<tr>';
						html += '<td colspan="4">' + delivery.title + '</td>';
						html += '<td style="text-align: right;">' + delivery.price + '</td>';
						html += '<td colspan="3">' + globals.currency.code + '</td>';
					html += '</tr>';
				}

				if(discounts) {
					for(var i=0, n=discounts.length; i<n; i++) {
						var discount = discounts[i];
						html += '<tr>';
							html += '<td colspan="4">' + discount.title + '</td>';
							html += '<td style="text-align: right;">'
							//html += discount.price > 0 ? '+' : '-';
							html += discount.price;
							html += '</td>';
							html += '<td colspan="3">' + globals.currency.code + '</td>';
						html += '</tr>';
					}
				}

					html += '<tr>';
						html += '<td colspan="7">';
							html += '<hr>';
						html += '</td>';
					html += '</tr>';

					html += '<tr>';
						html += '<td colspan="4">' + _t('totalPrice', 'c') + '</td>';
						html += '<td style="text-align: right;"><b>' + totalPrice + '</b></td>';
						html += '<td colspan="3">' + globals.currency.code + ' </td>';
					html += '</tr>';

					html += '<tr>';
						html += '<td colspan="4">' + _t('totalPriceWithTax', 'c') + '</td>';
						html += '<td style="text-align: right;"><b>' + totalPriceWithTax + '</b></td>';
						html += '<td colspan="3">' + globals.currency.code + '</td>';
					html += '</tr>';

					html += '<tr>';
						html += '<td colspan="8">&nbsp;</td>';
					html += '</tr>';

					html += '<tr>';
						html += '<td colspan="4">' + _t('totalWeight', 'c') + '</td>';
						html += '<td style="text-align: right;">' + totalWeight + '</td>';
						html += '<td colspan="3">kg</td>';
					html += '</tr>';

					html += '<tr>';
						html += '<td colspan="4">' + _t('totalVolume', 'c') + '</td>';
						html += '<td style="text-align: right;">' + totalVolume + '</td>';
						html += '<td colspan="3">m &sup3;</td>';
					html += '</tr>';
				html += '</table>';

				// $('#amountOfBalance').html(response.amountOfBalance.credit);
			} else {
				// cart is empty
				html  = '<div style="text-align: center; padding: 5px;">';
				html += _t('cartIsEmpty', 'c');
				html += '</div>'
			}

			$('#checkout').html(html);
		}
	);
},

addProduct: function(fk_product, productCountElement) {
	var countElement = '#productCount';
	if(typeof(productCountElement) !== 'undefined')
		countElement = '#' + productCountElement;

	var productCount = $(countElement + fk_product).val();
	var json = '{"product":' + fk_product + ', "productCount":' + productCount + '}';

	var serviceUrl = '/wf-service/cart/add-product';
	jQuery.post(
		serviceUrl,
		json,
		function(response, status, onRefresh) {
			ShoppingCart().refresh();
		}
	);
},

removeProduct: function(fk_product) {
	var json = '{"product":' + fk_product + '}';

	var serviceUrl = '/wf-service/cart/remove-product';
	jQuery.post(
		serviceUrl,
		json,
		function(response, status) {
			ShoppingCart().refresh();
		}
	);
},

processKey: function(event, fk_product, productCountElement) {
	if(event.keyCode == 13) { // enter
		this.addProduct(fk_product, productCountElement);
	} else if(event.keyCode == 38) { // up arrow
		var countElement = '#productCount';
		if(typeof(productCountElement) !== 'undefined')
			countElement = '#' + productCountElement;
		countElement += fk_product;
		var productCount = parseInt($(countElement).val());

		if(productCount !== NaN) {
			productCount++;
			$(countElement).val(productCount);
		}
	} else if(event.keyCode == 40) { // down arrow
		var countElement = '#productCount';
		if(typeof(productCountElement) !== 'undefined')
			countElement = '#' + productCountElement;
		countElement += fk_product;
		var productCount = parseInt($(countElement).val());

		if(productCount !== NaN && productCount > 1) {
			productCount--;
			$(countElement).val(productCount);
		}
	}
}

}}

