/*
 * defines the user interface components for the shop.
 */

/**
 * constructor.
 */
ShopUserInterface = function(config) {
	this.configuration = config;
	this.init();
	$(".jsShow").show();
};

/**
 * class definition
 */
ShopUserInterface.prototype = {

	connectionTimeout : 10000,

	/**
	 * applies the configuration.
	 */
	init : function() {
		if (typeof(this.configuration.compactCartEl) == 'string') {
			this.compactCartEl = $(this.configuration.compactCartEl);
		} else {
			this.compactCartEl = null;
		}
		if (typeof(this.configuration.cartEl) == 'string') {
			this.cartEl = $(this.configuration.cartEl);
		} else {
			this.cartEl = null;
		}
	},

	/**
	 * increases the numerical value by one.
	 * 
	 * @param sii
	 *            the shop-item-id
	 */
	increase : function(sii, absMax, boolUpdatePrice) {
		field = $('#' + sii + '_amount');
		amount = parseInt(field[0].value.replace(/\D/g, ''), 10);
		if ((typeof(absMax) == 'number') && (absMax <= amount)) {
			return false;
		}
		amount++;
		field[0].value = amount.toString();
		if (false !== boolUpdatePrice) {
			this.updatePrice(sii, this.configuration.getPriceURL);
		}
		return false;
	},

	/**
	 * increases the numerical value by one.
	 * 
	 * @param sii
	 *            the shop-item-id
	 */
	decrease : function(sii, absMin, boolUpdatePrice) {
		field = $('#' + sii + '_amount');
		amount = parseInt(field[0].value.replace(/\D/g, ''), 10);
		if ((typeof(absMin) == 'number') && (absMin >= amount)) {
			return false;
		}
		amount--;
		if (amount < 1) {
			amount = 1;
		}
		field[0].value = amount.toString();
		if (false !== boolUpdatePrice) {
			this.updatePrice(sii, this.configuration.getPriceURL);
		}
		return false;
	},

	/**
	 * returns the updated price.
	 * 
	 * @param {}
	 *            sii
	 */
	getPrice : function(sii) {
		if (parseInt($('#' + sii + '_amount')[0].value, 10).toString() != 'NaN') {
			if (parseInt($('#' + sii + '_amount')[0].value, 10) > 0) {
				this.updatePrice(sii, this.configuration.getPriceURL);
			}
		}
	},

	/**
	 * modifies the position.
	 * 
	 * @param {}
	 *            sii
	 */
	modifyPosition : function(sii) {
		if (parseInt($('#' + sii + '_amount')[0].value, 10).toString() != 'NaN') {
			if (parseInt($('#' + sii + '_amount')[0].value, 10) < 1) {
				$('#' + sii + '_amount')[0].value = '1';
			}
			this.updatePrice(sii, this.configuration.modifyCartPositionURL);
		}
	},

	/**
	 * updates the price.
	 * 
	 * @param sii
	 *            the shop-item-id
	 * @param url
	 *            the ajax targete
	 */
	updatePrice : function(sii, url) {
		field = $('#' + sii + '_amount');
		field[0].value = field[0].value.replace(/\D/g, '');
		data = {};
		data[sii] = field[0].value;
		$.ajax({
					url : url,
					type : 'POST',
					dataType : 'json',
					data : data,
					price : $('#' + sii + '_price'),
					sum : $('#' + sii + '_sum'),
					amount : $('#' + sii + '_amount'),
					grossTotal : $('#' + this.configuration.bundle
							+ "_grossTotal"),
					netTotal : $('#' + this.configuration.bundle + "_netTotal"),
					timeout : this.connectionTimeout,
					error : function(req, msg) {
						status = "";
						statusText = "";
						try {
							status = req.status;
							statusText = req.statusText;
						} catch (e) {
						}
						alert("Communication Error:\n" + status + " "
								+ statusText + " (" + msg + ")");
					},
					success : function(json) {
						if (json !== false) {
							this.price.html(json.amount);
							this.sum.html(json.sum);
							if (typeof(json.itemAmount) != 'undefined') {
								$(this.amount)[0].value = json.itemAmount;
							}
							if (json.total) {
								this.grossTotal.html(json.total.grossSum);
								this.netTotal.html(json.total.netSum);
							}
						}
					}
				});
	},

	/**
	 * increases the numerical value by one.
	 * 
	 * @param sii
	 *            the shop-item-id
	 */
	increaseModify : function(sii) {
		field = $('#' + sii + '_amount');
		amount = parseInt(field[0].value.replace(/\D/g, ''), 10);
		amount++;
		field[0].value = amount.toString();
		this.updatePrice(sii, this.configuration.modifyCartPositionURL);
		return false;
	},

	/**
	 * increases the numerical value by one.
	 * 
	 * @param sii
	 *            the shop-item-id
	 */
	decreaseModify : function(sii) {
		field = $('#' + sii + '_amount');
		amount = parseInt(field[0].value.replace(/\D/g, ''), 10);
		amount--;
		if (amount < 1) {
			amount = 1;
		}
		field[0].value = amount.toString();
		this.updatePrice(sii, this.configuration.modifyCartPositionURL);
		return false;
	},

	/**
	 * updates the price.
	 * 
	 * @param sii
	 *            the shop-item-id
	 * @param url
	 *            the ajax target
	 */
	modifyCartPosition : function(sii, url) {
		field = $('#' + sii + '_amount');
		field[0].value = field[0].value.replace(/\D/g, '');
		data = {};
		data[sii] = field[0].value;
		$.ajax({
					url : url,
					type : 'POST',
					dataType : 'json',
					data : data,
					grossPrice : $('#' + sii + '_gross'),
					netPrice : $('#' + sii + '_net'),
					grossSum : $('#' + sii + '_grossSum'),
					netSum : $('#' + sii + '_netSum'),
					timeout : this.connectionTimeout,
					error : function(req, msg) {
						status = "";
						statusText = "";
						try {
							status = req.status;
							statusText = req.statusText;
						} catch (e) {
						}
						alert("Communication Error:\n" + status + " "
								+ statusText + " (" + msg + ")");
					},
					success : function(json) {
						if (json !== false) {
							this.grossPrice.html(json.grossAmount);
							this.netPrice.html(json.netAmount);
							this.grossSum.html(json.grossSum);
							this.netSum.html(json.netSum);
						}
					}
				});
	},

	/**
	 * adds the given position to the cart.
	 * 
	 * @param sii
	 *            the shop-item-id
	 */
	addCartPosition : function(sii) {
		field = $('#' + sii + '_amount');
		field[0].value = field[0].value.replace(/\D/g, '');
		data = {};
		data[sii] = field[0].value;
		data.mode = "compact";
		ajaxURL = this.configuration.addCartPositionURL;
		_this = this;
		$("#" + this.configuration.compactCartId).slideUp("normal", function() {
			$.ajax({
						url : ajaxURL,
						type : 'POST',
						dataType : 'html',
						data : data,
						timeout : this.connectionTimeout,
						error : function(req, msg) {
							status = "";
							statusText = "";
							try {
								status = req.status;
								statusText = req.statusText;
							} catch (e) {
							}
							alert("Communication Error:\n" + status + " "
									+ statusText + " (" + msg + ")");
						},
						success : function(html) {
							_this.processResponseAddCartPosition(sii, html);
						}
					});
		});
		return false;
	},

	/**
	 * removes a position from the cart.
	 * 
	 * @param sii
	 *            the shop-item-id
	 */
	removeCartPosition : function(sii) {
		data = {};
		data.mode = "json";
		data[sii] = 0;

		ajaxURL = this.configuration.removeCartPositionURL;
		_this = this;
		$.ajax({
					url : ajaxURL,
					type : 'POST',
					dataType : 'json',
					data : data,
					sii : sii,
					grossTotal : $('#' + this.configuration.bundle
							+ "_grossTotal"),
					netTotal : $('#' + this.configuration.bundle + "_netTotal"),
					timeout : this.connectionTimeout,
					error : function(req, msg) {
						status = "";
						statusText = "";
						try {
							status = req.status;
							statusText = req.statusText;
						} catch (e) {
						}
						alert("Communication Error:\n" + status + " "
								+ statusText + " (" + msg + ")");
					},
					success : function(json) {
						if (json !== false) {
							if (json.isEmpty === true) {
								data = {};
								data.mode = 'long';
								$.ajax({
											url : _this.configuration.displayCartURL,
											type : 'POST',
											dataType : 'html',
											data : data,
											timeout : this.connectionTimeout,
											error : function(req, msg) {
												status = "";
												statusText = "";
												try {
													status = req.status;
													statusText = req.statusText;
												} catch (e) {
												}
												alert("Communication Error:\n"
														+ status + " "
														+ statusText + " ("
														+ msg + ")");
											},
											success : function(html) {
												$('#'
														+ _this.configuration.bundle
														+ "_cart")
														.replaceWith(html);
											}
										});
							} else {
								$('#' + this.sii + "_position").fadeOut("fast",
										function() {
											$(this).remove();
										});
								this.grossTotal.html(json.grossSum);
								this.netTotal.html(json.netSum);
							}
						}
					}
				});
		return false;
	},

	/**
	 * processes the response of the addCartPosition AJAX call.
	 * 
	 * @param sii
	 *            the shop-item-id
	 * @param html
	 *            the rerendered cart
	 */
	processResponseAddCartPosition : function(sii, html) {
		cartDiv = $("#" + this.configuration.compactCartId);
		cartDiv.replaceWith(html);
	}
};

