/*
 * @author Bramus! <bramus@netlash.com>
 */

	/**
	 * JS_NETLASH Object
	 * -------------------------------------------------------------
	 */
	if (!JS_NETLASH) { var JS_NETLASH = new Object(); }

	/**
	 * JS_NETLASH - language Object
	 * -------------------------------------------------------------
	 */
 	JS_NETLASH.languages =
 	{
		nl				: 1,
		fr				: 2,
		en				: 3
	};

	/**
	 * JS_NETLASH - main Object
	 * -------------------------------------------------------------
	 */
 	JS_NETLASH.main =
 	{
		parseint	: function(val)
		{
			var toReturn =  parseInt((val + '').replace(/^0+/g, ''));
			return isNaN(toReturn)?0:toReturn;
		}
	};

	/**
	 * JS_NETLASH - shopCore Object
	 * -------------------------------------------------------------
	 */
	JS_NETLASH.shopCore =
	{
		/**
		 * Datamembers
		 * ---------------------------------------------------------
		 */
		loadingImage	: '<img src="/modules/core/layout/images/loading.gif" width="16" height="16" alt="loading" title="loading" />',
		languageId		: 1,
		origQtys		: Array(),

		/**
		 * hookEvents
		 *
		 * @return void
		 * @param int step
		 * ---------------------------------------------------------
		 */
	 	hookEvents		: function(step, extra)
	 	{
			$(document.body).bind('mousemove', function(evt) { $('.fade').fadeOut(2500);});

			switch (parseInt(step))
			{
				/**
				 * STEP 0
				 * ---------------------------------------------
				 */
			 	case 0:
					$('#cartContent a').bind('mouseover', function(e) { JS_NETLASH.shopCore.cartOverlay(true, e); });
					$('#cartContent a').bind('mouseout', function(e) { JS_NETLASH.shopCore.cartOverlay(false, e); });
				break;

				/**
				 * STEP 1
				 * ---------------------------------------------
				 */
				case 1:
					// remove links
					$('.datagridRemoveRow').bind('click', function(e)
					{
						// get product id
						var id = $(this).attr('id').substring(8);
						var property = undefined;

						// product has property
						if (id.indexOf('-') > 0)
						{
							property = id.substr(id.indexOf('-') + 1);
							id = id.substr(0, id.indexOf('-'));
						}

						// delete it
						JS_NETLASH.shopCore.deleteProduct($(this).attr('href'), id, property, true);
						return false;
					});

					// qty inputs : store orig value on focus
					$('.amount input').bind('focus', function(e)
					{
						JS_NETLASH.shopCore.origQtys[$(this).attr("id").substring(7)] = JS_NETLASH.main.parseint($(this).val());
					});

					// qty inputs : update qty on blur (if differes from orig qty)
					$('.amount input').bind('blur', function(e)
					{
						// new value contains a string of some sort : update value to parseInt version.
						if ((JS_NETLASH.main.parseint($(this).val()) + '') != $(this).val())
						{
							$(this).val(isNaN(JS_NETLASH.main.parseint($(this).val())) ? 0 : JS_NETLASH.main.parseint($(this).val()));
						}

						// valid positive value (bigger than or equal to zero)
						if (JS_NETLASH.main.parseint($(this).val()) >= 0)
						{
							// get product id
							var id = $(this).attr('id').substring(7);
							var property = undefined;

							// product has property
							if (id.indexOf('-') > 0)
							{
								property = id.substr(id.indexOf('-') + 1);
								id = id.substr(0, id.indexOf('-'));
							}

							// value different from orig value : who you gonna call?
							if (JS_NETLASH.main.parseint($(this).val()) != JS_NETLASH.shopCore.origQtys[$(this).attr("id").substring(7)])
							{
								JS_NETLASH.shopCore.updateProduct('/ajax.php?module=shop&action=update_product&type=' + $(this).attr('rel'), id, property, $(this).val(), true);
							}
						}
						// not a positive value (smaller than zero)
						else
						{
							$(this).val(JS_NETLASH.shopCore.origQtys[$(this).attr("id").substring(7)]);
						}

						JS_NETLASH.shopCore.updateShipping('/ajax.php?module=shop&action=update_shipping');
						return false;
					});

					// shipping ddm
					if (extra !== true)
					{
						$('#cartShippingDdm').bind('change', function(e)
						{
							JS_NETLASH.shopCore.updateShipping('/ajax.php?module=shop&action=update_shipping', $(this).val());
						});
					}

					// hook couponcode thing onblur event : store and reget cart
					$('#couponcode').bind('blur', function (e)
					{
						JS_NETLASH.shopCore.updateCouponcode('/ajax.php?module=shop&action=update_couponcode', $(this).val());
						JS_NETLASH.shopCore.updateShipping('/ajax.php?module=shop&action=update_shipping');
					});

					// hook clientcode thing onblur event : store and reget cart
					$('#clientcode').bind('blur', function (e)
					{
						JS_NETLASH.shopCore.updateClientcode('/ajax.php?module=shop&action=update_clientcode', $(this).val());
						JS_NETLASH.shopCore.updateShipping('/ajax.php?module=shop&action=update_shipping');
					});
				break;

				/**
				 * STEP 3
				 * ---------------------------------------------
				 */
				case 3:
					// delivery address equal?
					$('#frm3_delivery_address').bind("click", function(e)
					{
						$('#delivery-data').css('display', ($(this).attr('checked')?'block':'none'));
						if ($.browser.msie && (parseInt($.browser.version) <= 6))	$('#logoStrip').css('position','relative').css('position','absolute');
					});

					// invoice checked?
					$('#frm3_invoice').bind("click", function(e)
					{
						$('#invoice-data').css('display', ($(this).attr('checked')?'block':'none'));
						if ($.browser.msie && (parseInt($.browser.version) <= 6))	$('#logoStrip').css('position','relative').css('position','absolute');
					});

					// delivery address equal?
					$('#frm3_personal_message').bind("click", function(e)
					{
						$('#message-data').css('display', ($(this).attr('checked')?'block':'none'));
						if ($.browser.msie && (parseInt($.browser.version) <= 6))	$('#logoStrip').css('position','relative').css('position','absolute');
					});
				break;
			}

		},

		/**
		 * cartOverlay - updates the shippingcost
		 * @return void
		 *
		 * @param bool show
		 * @param Object evt
		 * --------------------------------------------------------
		 */
		cartOverlay	: function(show, evt)
		{
			if (show === false)
			{
				$('#cartOverlay').css('display','none');
			}
			else
			{
				offset = $(evt.target).offset();
				$('#cartOverlay').css({
					top : offset.top - ($('#cartOverlay').height() / 2) + 'px',
					left : offset.left - $('#cartOverlay').width() - 40 + 'px',
					display : 'block'
				});
			}
		},

		/**
		 * updateShipping - updates the shippingcost
		 * @return void
		 *
		 * @param string url
		 * @param int shippingId
		 * --------------------------------------------------------
		 */
	 	updateShipping	: function (url, shippingId)
	 	{
			// hide dropdown, hide image & show spinner
			$('#cartShippingDdm').css("display", "none").next().css("display", "none").after(JS_NETLASH.shopCore.loadingImage);

			// make the call
			$.ajax(
			{
				// URL to post to
				url: url + '&language_id=' + JS_NETLASH.shopCore.languageId + '&method=ajax',

				// Method: get/post
				type: 'post',

				// What data do we expect to be returned?
				dataType: 'json',

				// Cache the url (best set to false!)
				cache: false,

				// the data to post
				data: "shipping_id=" + shippingId,

				// Success : call has been made and we have some data back!
				success:	function(json)
				{
					if (JS_NETLASH.main.parseint(json.status.code) >= 400)
					{
						alert("There was an error! : " + json.status.text);
//						window.location.reload();
					}
					else
					{
						JS_NETLASH.shopCore.rebuildShipping(json.content, true);
					}
				},

				// Error : could not make the call
				error:		function(xhr,err,e)
				{
					// Give notice
					alert( "XHR Error: " + err );

					// refresh the page
//					window.location.reload();
				}
			});
		},

		/**
		 * rebuildShipping - rebuild the shippingcost dropdown
		 * @return void
		 *
		 * @param Object json
		 * @param bool updateTotals
		 * --------------------------------------------------------
		 */
		rebuildShipping		: function(content, updateTotals)
		{
			// clear dropdown
			$('#cartShippingDdm').html('');

			// fix ze selected index
			counter 		= 0;
			selectedidx 	= 0;
			shippingPrice	= 0;

			// loop all values (shipping options) and add them to the dropdown
			for (ship in content.opts)
			{
				// append option
				$('#cartShippingDdm').append('<option value="' + content.opts[ship].id + '"' + ((content.opts[ship].id == content.id) ? ' selected="selected"' : '') + '>' + content.opts[ship].title + '</option>');

				// selectedindex?
				selectedidx = (content.opts[ship].id == content.id)?counter:selectedidx;
				shippingPrice = (content.opts[ship].id == content.id)?content.opts[ship].price:shippingPrice;
				counter++;
			}

			// set selectedIndex on dropdown
			$('#cartShippingDdm').get(0).selectedIndex = selectedidx;

			// update image
			$('#cartShippingImage').attr('src', '/modules/core/layout/images/flags/' + content.id + '.gif');

			// update shippingPrice
			$('#shippingCost span').html(shippingPrice);

			// only to be done after a single shippingcost update
			if (updateTotals == true)
			{
				// recalc total costs
				JS_NETLASH.shopCore.updateTotals();

				// reactivate ddm, reactivate image and hide spinner
				$('#cartShippingDdm').css('display', '').next().css('display', '').next().remove();
			}
		},

		/**
		 * updateCouponcode - updates the couponcode
		 * @return void
		 *
		 * @param string url
		 * @param string couponCode
		 * --------------------------------------------------------
		 */
	 	updateCouponcode	: function (url, couponCode)
	 	{
			// hide dropdown, hide image & show spinner
			$('#couponcode').attr('disabled', 'disabled');
			$('#couponCodeReduction').html(JS_NETLASH.shopCore.loadingImage);

			// make the call
			$.ajax(
			{
				// URL to post to
				url: url + '&language_id=' + JS_NETLASH.shopCore.languageId + '&method=ajax',

				// Method: get/post
				type: 'post',

				// What data do we expect to be returned?
				dataType: 'json',

				// Cache the url (best set to false!)
				cache: false,

				// the data to post
				data: 'couponcode=' + couponCode,

				// Success : call has been made and we have some data back!
				success:	function(json)
				{
					if (JS_NETLASH.main.parseint(json.status.code) >= 400)
					{
						alert('There was an error! : ' + json.status.text);
//						window.location.reload();
					}
					else
					{
						JS_NETLASH.shopCore.rebuildCouponcode(json.content, true);
					}
				},

				// Error : could not make the call
				error:		function(xhr,err,e)
				{
					// Give notice
					alert("XHR Error: " + err);

					// reactivate field
					$('#couponcode').attr('disabled', '');

					// refresh the page
//					window.location.reload();
				}
			});
		},

		/**
		 * updateClientcode - updates the clientcode
		 * @return void
		 *
		 * @param string url
		 * @param string clientCode
		 * --------------------------------------------------------
		 */
	 	updateClientcode	: function (url, clientCode)
	 	{
			// hide dropdown, hide image & show spinner
			$('#clientcode').attr('disabled', 'disabled');
			$('#clientCodeReduction').html(JS_NETLASH.shopCore.loadingImage);

			// make the call
			$.ajax(
			{
				// URL to post to
				url: url + '&language_id=' + JS_NETLASH.shopCore.languageId + '&method=ajax',

				// Method: get/post
				type: 'post',

				// What data do we expect to be returned?
				dataType: 'json',

				// Cache the url (best set to false!)
				cache: false,

				// the data to post
				data: 'clientcode=' + clientCode,

				// Success : call has been made and we have some data back!
				success:	function(json)
				{
					if (JS_NETLASH.main.parseint(json.status.code) >= 400)
					{
						alert('There was an error! : ' + json.status.text);
//						window.location.reload();
					}
					else
					{
						JS_NETLASH.shopCore.rebuildClientcode(json.content, true);
					}
				},

				// Error : could not make the call
				error:		function(xhr,err,e)
				{
					// Give notice
					alert("XHR Error: " + err);

					// reactivate field
					$('#clientcode').attr('disabled', '');

					// refresh the page
//					window.location.reload();
				}
			});
		},

		/**
		 * rebuildCouponcode - rebuild the shippingcost dropdown
		 * @return void
		 *
		 * @param Object json
		 * --------------------------------------------------------
		 */
		rebuildCouponcode		: function(json)
		{
			if ($('#couponcode').size() > 0)
			{
				// set content
				switch(json.reduction_type)
				{
					case 'price':
						// just set it
						$('#couponCodeReduction').html('&euro; <span>' + json.reduction + '</span>');
					break;

					case 'percentage':
						// calc the total
						var total = 0;

						$('.subtotal span').each(function(i)
						{
							total += parseFloat($(this).html());
						});

						reductionInEuro = (total / 100 * parseFloat(json.reduction))

						// just set it
						$('#couponCodeReduction').html('&euro; <span>' + JS_NETLASH.shopCore.reformatPrice(reductionInEuro) + '</span> (' + json.reduction + '%)');
					break;
				}

				// reactivate field
				$('#couponcode').attr('disabled', '');
			}

			// Update final total
			JS_NETLASH.shopCore.updateTotals('', false);
		},

		/**
		 * rebuildClientcode - rebuild the shippingcost dropdown
		 * @return void
		 *
		 * @param Object json
		 * --------------------------------------------------------
		 */
		rebuildClientcode		: function(json)
		{
			if ($('#clientcode').size() > 0)
			{
				// set content
				switch(json.reduction_type)
				{
					case 'validation':
						// just set it
						$('#clientCodeReduction').html('<img src="/private/modules/core/layout/images/icons/' + (json.reduction ? 'Y' : 'N') + '.gif" />');
					break;

					case 'price':
						// just set it
						$('#clientCodeReduction').html('&euro; <span>' + json.reduction + '</span>');
					break;

					case 'percentage':
						// calc the total
						var total = 0;

						$('.subtotal span').each(function(i)
						{
							total += parseFloat($(this).html());
						});

						reductionInEuro = (total / 100 * parseFloat(json.reduction))

						// just set it
						$('#clientCodeReduction').html('&euro; <span>' + JS_NETLASH.shopCore.reformatPrice(reductionInEuro) + '</span> (' + json.reduction + '%)');
					break;
				}

				// reactivate field
				$('#clientcode').attr('disabled', '');
			}

			// Update final total
			JS_NETLASH.shopCore.updateTotals('', false);
		},

		/**
		 * addProduct
		 *
		 * @return void
		 * @param string url
		 * @param int id
		 * @param int qty
		 * ---------------------------------------------------------
		 */
		addProduct		: function(url, id, qty, productOptions, isTable, type)
		{
			// show spinner
			$('#cartContent').html(JS_NETLASH.shopCore.loadingImage);

			// make the call
			$.ajax(
			{
				// URL to post to
				url: url + '&language_id=' + JS_NETLASH.shopCore.languageId + '&method=ajax',

				// Method: get/post
				type: 'post',

				// What data do we expect to be returned?
				dataType: 'json',

				// Cache the url (best set to false!)
				cache: false,

				// the data to post
				data: 'product_id=' + id + '&amount=' + qty + '&params=' + productOptions+ '&type=' + type,

				// Success : call has been made and we have some data back!
				success:	function(json)
				{
					if (JS_NETLASH.main.parseint(json.status.code) >= 400)
					{
						alert('There was an error! : ' + json.status.text);
						JS_NETLASH.shopCore.refreshCart(isTable);
					}
					else
					{
						$('.report').html(json.content.text.message).show();
						JS_NETLASH.shopCore.rebuildCart(json.content, isTable);
					}
				},

				// Error : could not make the call
				error:		function(xhr,err,e)
				{
					// Give notice
					alert('XHR Error: ' + err );

					// try a refresh of the cart
					JS_NETLASH.shopCore.refreshCart(isTable);
				}
			});
		},

		/**
		 * deleteProduct - deletes a product
		 *
		 * @return void
		 * @param string url
		 * @param int id
		 * ---------------------------------------------------------
		 */
		deleteProduct		: function(url, id, property, isTable)
		{
			// call updateProduct, with 0 as quantitity
			JS_NETLASH.shopCore.updateProduct(url, id, property, 0, isTable);

			// update shipping info
//			JS_NETLASH.shopCore.updateShipping('/ajax.php?module=shop&action=update_shipping');
		},

		/**
		 * updateProduct - update product
		 *
		 * @return void
		 * @param string url
		 * @param int id
		 * @param int qty
		 * ---------------------------------------------------------
		 */
		updateProduct		: function(url, id, property, qty, isTable)
		{
			// show spinner
			if (isTable == true)
			{
				$('#row-' + id + (property != undefined ? '-' + property : '') + ' input').val(JS_NETLASH.main.parseint(qty)).attr('disabled', 'disabled');
				$('#row-' + id + (property != undefined ? '-' + property : '') + ' .datagridRemoveRow').css('display', 'none').after(JS_NETLASH.shopCore.loadingImage);
			}
			else
			{
				$('#cartContent').html('');
			}

			// make the ajax call
			$.ajax(
			{
				// URL to post to
				url: url + '&language_id=' + JS_NETLASH.shopCore.languageId + '&method=ajax',

				// Method: get/post
				type: 'post',

				// What data do we expect to be returned?
				dataType: 'json',

				// Cache the url (best set to false!)
				cache: false,

				// the data to post
				data: 'product_id=' + id + (property != undefined ? '&property_id=' + property : '') + '&amount=' + qty,

				// Success : call has been made and we have some data back!
				success:	function(json)
				{
					if (JS_NETLASH.main.parseint(json.status.code) >= 400)
					{
						alert('There was an error! : ' + json.status.text);
						JS_NETLASH.shopCore.refreshCart(isTable);
					}
					else
					{
						// update the cart
						if (isTable == true) JS_NETLASH.shopCore.updateCartTableRow(id, property, qty);
						else JS_NETLASH.shopCore.rebuildCart(json.content);
					}
				},

				// Error : could not make the call
				error:		function(xhr,err,e)
				{
					alert('XHR Error: ' + err);
				}
			});
		},

		/**
		 * refreshCart - refresh the cart
		 *
		 * @return void
		 * ---------------------------------------------------------
		 */
		refreshCart		: function(isTable)
		{
			// show spinner
			if (isTable == true)
			{
				$('.datagrid').html('<tr><td colspan="6" align="center" style="text-align: center;">' + JS_NETLASH.shopCore.loadingImage + '</td></tr>');
				$('#datagrid tfoot').css('visibility', 'hidden');
			}
			else
			{
				$('#cartContent').html(JS_NETLASH.shopCore.loadingImage);
			}

			// make the call
			$.ajax(
			{
				// URL to post to
				url: '/ajax.php?module=shop&action=refresh_cart' + '&language_id=' + JS_NETLASH.shopCore.languageId + '&method=ajax',

				// Method: get/post
				type: 'post',

				// What data do we expect to be returned?
				dataType: 'json',

				// Cache the url (best set to false!)
				cache: false,

				// the data to post
				data: '',

				// Success : call has been made and we have some data back!
				success:	function(json)
				{
					if (JS_NETLASH.main.parseint(json.status.code) >= 400)
					{
						alert("There was an error! : " + json.status.text);
						JS_NETLASH.shopCore.refreshCart(isTable);
					}
					else
					{
						JS_NETLASH.shopCore.rebuildCart(json.content, isTable);
					}
				},

				// Error : could not make the call
				error:		function(xhr,err,e)
				{
					alert("XHR Error: " + err);
				}
			});
		},

		/**
		 * rebuildCart - rebuilds the cart from an object with products in it
		 *
		 * @return void
		 * @param Object content
		 * @param bool isTable
		 * ---------------------------------------------------------
		 */
		rebuildCart		: function(content, isTable)
		{
			if (isTable == true)
			{
				JS_NETLASH.shopCore.rebuildShipping(content.shipping, false);
				JS_NETLASH.shopCore.rebuildCartTable(content);
			}
			else
			{
				JS_NETLASH.shopCore.rebuildCartSidebar(content);
			}
		},

		/**
		 * reformatPrice - reformat a price to 2 decimals
		 *
		 * @return string
		 * @param int value
		 * ---------------------------------------------------------
		 */
		reformatPrice	: function(value)
		{
			value 		= JS_NETLASH.main.parseint(value * 100) + '';
			if (value == 0) { return '0.00'; }
			beforeComma	= value.substring(0, value.length - 2);
			afterComma	= value.substring(value.length - 2, value.length) + '';

			// fix integer values and .x values to include zeroes after the comma
			while (afterComma.length < 2) afterComma += '0';

			// return it
			return beforeComma + '.' + afterComma;
		},

		/**
		 * extractShipping - etract shipping from the possible shiping options
		 *
		 * @return Object
		 * @param Object options
		 * @param Object option
		 * ---------------------------------------------------------
		 */
		extractShipping	: function (json, id)
		{
			// loop all shipping options
			for(ship in json)
			{
				// shipping matches? - return it!
				if (json[ship].id == id) return json[ship];
			}

			// no hit, return empty object!
			return {'id' : '0', 'title' : 'Unknown', 'code' : '?', 'price' : '0'};
		},

		/**
		 * rebuildCartSidebar - rebuilds the cart (sidebar version) from an object with products in it
		 *
		 * @return void
		 * @param Object content
		 * ---------------------------------------------------------
		 */
		rebuildCartSidebar		: function(json)
		{
			if ($('#cartContent'))
			{
				// start with 0
				var totalQuantity	= 0;
				var totalPrice		= 0;

				// no products in cart?
				if (json.quantity == 0)
				{
					// sidebar
					$('#cartContent').html('<p>' + json.text.products + '</p>');

					// sidebar overlay
					$('.datagrid').html('<tr><td colspan="6" align="center">' + json.text.products + '</td></tr>');
					$('#cartOverlay tfoot').css('display','none');
				}
				// products in cart
				else
				{
					// loop all products
					for (product in json.cart)
					{
						totalQuantity 	+= JS_NETLASH.main.parseint(json.cart[product].amount);
						totalPrice		+= JS_NETLASH.main.parseint(parseFloat(json.cart[product].price) * 100 * JS_NETLASH.main.parseint(json.cart[product].amount));
					};

					// extract the shipping from the json object
					shipping	= JS_NETLASH.shopCore.extractShipping(json.shipping.opts, json.shipping.id);

					// update the cart (right column)
					$('#cartContent').html(
						'<ul>' +
						'	<li id="basketProducts">' +
						'		<a title="" href="' + $('#shoppingCart form').attr('action') + '"><span id="basketProductsNum">' + totalQuantity + ' ' + json.text.products + '</span></a> <span id="basketProductsPrice">&euro; ' + JS_NETLASH.shopCore.reformatPrice(totalPrice / 100) + '</span>' +
						'	</li>' +
						'	<li id="basketShipping">' +
						'		<a title="" href="' + $('#shoppingCart form').attr('action') + '"><span class="float">' + json.text.shipping + ' <span id="basketShippingCountry">' + shipping.code + '</span></span></a> <span id="basketShippingPrice">&euro; ' + JS_NETLASH.shopCore.reformatPrice(shipping.price) + '</span>' +
						'	</li>' +
						'	<li id="basketTotal">' +
						'		<span id="basketTotalPrice">&euro; ' + JS_NETLASH.shopCore.reformatPrice(parseFloat(totalPrice / 100) + parseFloat(shipping.price)) + '</span><span class="float">'+ json.text.total +'</span>'+
						'	</li>' +
						'</ul>');

					// update the shipping first
					$('#shippingImage').attr('src', '/modules/core/layout/images/flags/' + shipping.id + '.gif');
					$('#shippingTitle').html(shipping.title);
					$('#shippingCost span').html(JS_NETLASH.shopCore.reformatPrice(shipping.price));

					// update the overlay table (entries + totals)
					JS_NETLASH.shopCore.rebuildCartTable(json, true);

					// rehook the events
					JS_NETLASH.shopCore.hookEvents(0);
				}
			}
		},

		/**
		 * updateCartTableRow - updates a single row of the cart table (after a delete or update)
		 *
		 * @return void
		 * @param int id - The productId
		 * @param int property - The propertyId
		 * @param int qty - The new quantity
		 * ---------------------------------------------------------
		 */
		updateCartTableRow	: function (id, property, qty)
		{
			// remove or update the row?
			if (qty == 0)
			{
				// remove the product row
				$('#row-' + id + (property != undefined ? '-' + property : '')).remove();

				// remove the property row
				$('#property-' + id + (property != undefined ? '-' + property : '')).remove();
			}
			else
			{
				// show the delete button and remove the spinner
				$('#row-' + id + (property != undefined ? '-' + property : '') + ' .datagridRemoveRow').css('display', 'block').next().remove();

				// set subtotal price
				$('#row-' + id + (property != undefined ? '-' + property : '') + ' .subtotal span').html(JS_NETLASH.shopCore.reformatPrice(parseFloat($('#row-' + id + (property != undefined ? '-' + property : '') + ' .price span').html()) * qty));

				// update qty in input
				$('#amount-' + id + (property != undefined ? '-' + property : '')).attr('value', qty).attr('disabled', '').focus();

				// set subtotal price for property
				$('#property-' + id + (property != undefined ? '-' + property : '') + ' .subtotal span').html(JS_NETLASH.shopCore.reformatPrice(parseFloat($('#property-' + id + (property != undefined ? '-' + property : '') + ' .price span').html()) * qty));

				// update qty for property
				$('#property-' + id + (property != undefined ? '-' + property : '') + ' .amount').html(qty);
			}

			// if no items, hide the footer and refresh the cart (and get the correct "empty cart text" with it)
			if ($('.subtotal span').size() == 0)
			{
				JS_NETLASH.shopCore.refreshCart();
				$('#datagrid tfoot').css('visibility', 'hidden');
			}
			// has items : update the totals / prices
			else
			{
				JS_NETLASH.shopCore.updateCouponcode('/ajax.php?module=shop&action=update_couponcode', $('#couponcode').val());
				JS_NETLASH.shopCore.updateClientcode('/ajax.php?module=shop&action=update_clientcode', $('#clientcode').val());
				// JS_NETLASH.shopCore.updateTotals();
			}
		},

		/**
		 * updateTotals - update the totals in the table
		 *
		 * @return void
		 * @param string txt - text for empty cart
		 * ---------------------------------------------------------
		 */
		updateTotals		: function(txt, isOverlay)
		{
			// update the totals
			var total = 0;

			// loop all shoppingcart entries
			$('.subtotal span').each(function(i)
			{
				total += parseFloat($(this).html());
			});

			// empty cart : give notice and return
			if (JS_NETLASH.main.parseint(total) == 0)
			{
				$('.datagrid').html('<tr><td colspan="6" align="center">' + (txt || 'Empty Cart') + '</td></tr>');
				if (isOverlay === true) $('#cartOverlay tfoot').css('visibility','hidden');
				else $('#datagrid tfoot').css('visibility','hidden');
				return;
			}

			// include couponcode
			if ($('#couponCodeReduction span').size() > 0) total -= parseFloat($('#couponCodeReduction span').html());
			if ($('#clientCodeReduction span').size() > 0) total -= parseFloat($('#clientCodeReduction span').html());

			// include shippingcost
			total += parseFloat($('#shippingCost span').html());

			// set final price
			$('#cTotal span').html(JS_NETLASH.shopCore.reformatPrice(total));

			// set footer visible
			if (isOverlay === true) $('#cartOverlay tfoot').css('visibility', 'visible');
			else $('#datagrid tfoot').css('visibility', 'visible');
		},

		/**
		 * refreshCartTable - rebuilds the cart (table version) from an object with products in it
		 *
		 * @return void
		 * @param Object content
		 * ---------------------------------------------------------
		 */
		rebuildCartTable	: function(content, isOverlay)
		{
			if ($('.datagrid'))
			{
				// start with 0
				var totalQuantity	= 0;
				var totalPrice		= 0;

				// no products in cart?
				if (content.quantity == 0)
				{
					$('.datagrid').html('<tr><td colspan="6" align="center">' + (content.text.products || 'Empty Cart') + '</td></tr>');
					if (isOverlay === true) $('#cartOverlay tfoot').css('visibility', 'hidden');
					else $('#datagrid tfoot').css('visibility', 'hidden');
				}
				// products in cart
				else
				{
					// clear datagrid
					$('.datagrid').empty();

					// counter (rowOdd, rowEven?)
					counter = 0;

					// loop all products
					for (product in content.cart)
					{
						var property = (content.cart[product].params != '' && content.cart[product].params != 'null' && content.cart[product].params != null);

						$('.datagrid').append(
							'<tr id="row-' + content.cart[product].product_id + (property ? '-' + content.cart[product].params : '') + '" class="' + (counter % 2 == 0 ? 'rowOdd' : 'rowEven') + '">' + '\n' +
								'<td class="image" style="background: transparent url(/modulefiles/products/products/small/' + content.cart[product].image + ') no-repeat 50% 50%;">&nbsp;</td>' +
								'<td class="amount">' + (isOverlay == true ? content.cart[product].amount : '<input type="text" id="amount-' + content.cart[product].product_id + (property ? '-' + content.cart[product].params : '') + '" name="amount-'+ content.cart[product].product_id + (property ? '-' + content.cart[product].params : '') + '" value="' + content.cart[product].amount + '" style="width: 40px;" />') + '</td>\n' +
								'<td class="title">' + content.cart[product].title + (isOverlay == true && property ? ' (<em>' + content.cart[product].property_name.replace(new RegExp('&', 'g'), ' - ').replace(new RegExp('=', 'g'), ' : ') + '</em>)' : '') + '</td>' + '\n' +
								'<td class="price">&euro; <span>' + JS_NETLASH.shopCore.reformatPrice(content.cart[product].price) + '</span></td>' + '\n' +
								'<td class="subtotal">&euro; <span>' + JS_NETLASH.shopCore.reformatPrice(content.cart[product].price * content.cart[product].amount)+ '</span></td>' + '\n' +
								'<td class="action">' + (isOverlay == true ? '&nbsp;' : '<a href="/ajax.php?module=shop&action=delete_product&amp;language_id=' + JS_NETLASH.shopCore.languageId + '" class="datagridRemoveRow" id="product-' + content.cart[product].product_id + (property ? '-' + content.cart[product].params : '') +'"><img src="/modules/shop/layout/images/btnCartDelete.gif" alt="" title="" height="17" width="16" /></a>') + '</td>\n' +
							'</tr>'
						);

						// @todo: dit werkt nog niet
						if (isOverlay == false && property)
						{
							$('.datagrid').append(
								'<tr id="property-' + content.cart[product].product_id + '-' + content.cart[product].params + '" class="' + (counter % 2 == 0 ? 'rowOdd' : 'rowEven') + '">' + '\n' +
									'<td class="image" style="background: transparent url(' + content.cart[product].property_image + ') no-repeat 50% 50%;">&nbsp;</td>' +
									'<td class="amount"><input type="text" id="amount-' + content.cart[product].product_id + '-' + content.cart[product].params + '" name="amount-'+ content.cart[product].product_id + '-' + content.cart[product].params + '" value="' + content.cart[product].amount + '" style="width: 40px;" /></td>' + '\n' +
									'<td class="title">' + content.cart[product].property_title + '</td>' + '\n' +
									'<td class="price">&euro; <span>' + JS_NETLASH.shopCore.reformatPrice(content.cart[product].property_price) + '</span></td>' + '\n' +
									'<td class="subtotal">&euro; <span>' + JS_NETLASH.shopCore.reformatPrice(content.cart[product].property_price * content.cart[product].amount)+ '</span></td>' + '\n' +
									'<td class="action">&nbsp;</td>' + '\n' +
								'</tr>'
							);
						}

						counter++;
					};

					// Set couponcode text (with some logic)
					JS_NETLASH.shopCore.rebuildCouponcode(content.couponcode);
					JS_NETLASH.shopCore.rebuildClientcode(content.clientcode);

					// recalc the totals / prices
					JS_NETLASH.shopCore.updateTotals(content.text.products, isOverlay);

					// rehook the events
					if (isOverlay !== true) JS_NETLASH.shopCore.hookEvents(1, true);
				}
			}
		}
	};

	/**
	 * JS_NETLASH - start your engines!
	 * -------------------------------------------------------------
	 */
	// define the languageId
	jQuery(document).ready(function($) { JS_NETLASH.shopCore.languageId = JS_NETLASH.languages[$('body').attr("class")]; });
	
	
/* @projectDescription jQuery Serialize Anything - Serialize anything (and not just forms!)
 * @author Bramus! (Bram Van Damme)
 * @version 1.0
 * @website: http://www.bram.us/
 * @license : CC 3.0 BY-SA
*/
(function($) {
	$.fn.serializeAnything = function()
	{
		var toReturn	= [];
		var els 		= $(this).find(':input').get();

		$.each(els, function()
		{
			if (this.name && !this.disabled && (this.checked || /select|textarea/i.test(this.nodeName) || /text|hidden|password/i.test(this.type)))
			{
				var val = $(this).val();
				toReturn.push(encodeURIComponent(this.name) + "=" + encodeURIComponent(val));
			}
		});

		return toReturn.join("&").replace(/%20/g, "+");
	}
})(jQuery);
