/* modevoorkinderen.nl - Winkelmandje
 *
 * @author <vincent@comwave.nl>
 * @requires jQuery (scrollPane)
 */

var Winkelmandje = function($)
{
	var _handler = devpath + 'winkelmandje/';
	var _count   = 0;
	var _total   = 0.00;
	var _product = {};

	function _call(method, data, callback) {
		method = method + '?t=' + (new Date).getTime();

		$.ajax({
			cache: false,
			type: 'POST',
			url: _handler + method,
			dataType: 'json',
			data: data,
			success: callback,
			failure: callback
		});
	}

	function _update() {
		$('#winkelmandje .totaal').removeClass('cart-loader').html('');

		if( _count > 0 ) {
			$('#emptycart').css('display', 'none');
			if( $('#winkelmandje .afrekenen').css('visibility') == 'hidden' ) $('#winkelmandje .afrekenen').css('visibility', 'visible');
			$('#winkelmandje .totaal').html(_count + ' artikel' + (_count == 1 ? '' : 'en') + ' (totaal: &euro; ' + _currency(_total) + ')');
		}
		else {
			$('#emptycart').css('display', 'block');
		}

		return true;
	}

	function _loader() {
		$('#winkelmandje .totaal').addClass('cart-loader').html('mandje wordt bijgewerkt&hellip;');
		$('#winkelmandje .afrekenen').css('visibility', 'hidden');
	}

	function _currency(amount) {
		var formattedAmount = (Math.round(amount * 100) / 100).toString();
		formattedAmount = ( formattedAmount.indexOf('.') > -1 ? formattedAmount.replace(/\./, ',') : formattedAmount + ',' ) + '00';
		displayAmount = formattedAmount.substring(0, formattedAmount.indexOf(',')) + formattedAmount.substring(formattedAmount.indexOf(','), formattedAmount.indexOf(',') + 3);
		
		return displayAmount;
	}

	return {
		toevoegen: function(productId, maatId, aantal) {
			_loader();
			var data = {product: productId, maat: maatId, aantal: aantal};
			if( current = _product[maatId] ) return Winkelmandje.aanpassen(maatId, current.aantal + aantal);

			return _call('productToevoegen', data, Winkelmandje.verwerken);
		},

		verwijderen: function(maatId) {
			_loader();
			var data = {maat: maatId};

			return _call('productVerwijderen', data, Winkelmandje.verwerken);
		},

		aanpassen: function(maatId, aantal) {
			_loader();
			var data    = {maat: maatId, aantal: aantal};
			var current = _product[data.maat];
			
			return (current.aantal != data.aantal ? _call('productBijwerken', data, Winkelmandje.verwerken) : _update());
		},

		verwerken: function(data) {
			if( !data || !data.result ) return false;

			switch( data.action )
			{
				case 'add':
					var phtml = [];
					phtml.push('<div class="winkelmandjeproduct product-box" id="productId_%maatid%">');
					phtml.push('	<div class="product-top">');
					phtml.push('		<button title="product verwijderen" class="btnVerwijderWinkelmandjeproduct" onclick="Winkelmandje.verwijderen(%maatid%)" id="product_%maatid%">&nbsp;</button>');
					phtml.push('	</div>');
					phtml.push('	<div class="product-body">');
					phtml.push('		<img width="45" height="45" src="%miniatuur%" class="product-thumbnail" />');
					phtml.push('		<div class="product-amount">');
					phtml.push('			<label for="productAantal_0">aantal:</label>');
					phtml.push('			<input type="text" class="winkelmandjeproductAantalInput" onfocus="clearText(this)" onblur="addText(this); Winkelmandje.aanpassen(%maatid%, this.value);" value="%aantal%" id="productAantal_%maatid%" name="aantal[]">');
					phtml.push('		</div>');
					phtml.push('	</div>');
					phtml.push('	<div class="product-name"><strong>%naam%</strong> (%maat%)</div>');
					phtml.push('	<div class="product-price">prijs: <em>&euro; %prijs%</em></div>');
					phtml.push('</div>');

					var details = data.product;
					var product = phtml.join("\n");

					for( var veld in details ) {
						var value = details[veld];
						if( veld == 'prijs' ) value = _currency(value);
						product = product.replace(new RegExp('%' + veld + '%', 'g'), value);
					}
					
					$('#inside').append(product);

					Winkelmandje.bijwerken(data);
					break;

				case 'remove':
					data.product.aantal = $('#productAantal_' + data.product.maatid).val();
					$('#productId_' + data.product.maatid).remove();
					Winkelmandje.bijwerken(data);
					break;

				case 'update':
					$('#productAantal_' + data.product.maatid).val(data.product.aantal);
					Winkelmandje.bijwerken(data);
					break;
			}

			return true;
		},

		bijwerken: function(data) {
			switch( data.action )
			{
				case 'add':
					_product[data.product.maatid] = {aantal: data.product.aantal, prijs: data.product.prijs};

					_count += data.product.aantal;
					_total += (data.product.aantal * data.product.prijs);
					break;

				case 'remove':
					var current = _product[data.product.maatid];
					_count -= current.aantal;
					_total -= (current.aantal * current.prijs);

					delete _product[data.product.maatid];
					break;

				case 'update':
					var current = _product[data.product.maatid];

					_count = (_count - current.aantal) + data.product.aantal;
					_total = (_total - (current.aantal * current.prijs)) + (data.product.aantal * current.prijs);
					_product[data.product.maatid].aantal = data.product.aantal;
					break;
			}

			return _update();
		}
	};
}(jQuery);
