(function() {
	$.product = {
		_container: null,
		_module: null,
		_args: [],
		_method: null,
		_focus: null,
		
		set_focus: function(element) {
			$.product._focus = element;
		},
		
		set_module: function(module) {
			$.product._module = module;
			$.product._container = null;
		},
		
		set_container: function(container) {
			$.product._container = container;
		},
		
		create: function(label, callback) {
			callback = callback || function(){};
			$.ajax({url: '/' + $.product._module + '/create/' + encodeURIComponent(label),
					data: {},
					dataType: 'json', 
					success: function(json) {
						if (typeof json == 'number') {
							callback();
							$.product.popup_select($.product._method, $.product._callback);
						} else {
							if (json.message) {
								$('#' + $.product._module + '-select-creator-error').text(json.message).show();
							} else {
								$('#' + $.product._module + '-select-creator-error').hide();
							}
							
							($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
						}
					}
				}
			);
		},
		
		popup_select: function(method, callback) {
			$.product._method = method;
			$.product._callback = callback || function(){};
			
			if ($.product._container) {
				$.product.do_callback();
			} else {
				var popup = $('#' + $.product._module + '-select-popup');
				if (popup.length) {
					popup.popup('/' + encodeURIComponent($.product._module) + '/select');
				} else {
					$.product._callback('/' + $.map($.merge([$.product._module, $.product._method], $.product._args), encodeURIComponent).join('/'));
				}
			}
		},
		
		do_callback: function() {
			$.product._callback('/' + $.map($.merge([$.product._module, $.product._method, $.product._container], $.product._args), encodeURIComponent).join('/'));
		},
		
		check_product: function(item_id, fastentry, callback) {
			if ($.product._last_item_id && $.product._last_item_id == item_id && $.product._last_fastentry.get(0) == fastentry.get(0)) {
				return;
			}
			$.product._last_item_id = item_id;
			$.product._last_fastentry = fastentry;
			callback = callback || function(){};
			$.ajax({url: '/' + encodeURIComponent($.product._module) + '/check_product/' + encodeURIComponent(item_id),
					dataType: 'json', 
					success: function(json) {
						if (json) {
							if (json.item_id) {
								$.product._checked_item_id = json.item_id;
							} else {
								$.product._checked_item_id = item_id;
							}
							$.product._is_reel = !!json.is_reel;
							$.product._is_divers = !!json.is_divers;
							$.product._no_stock_has_alternatives = !!json.no_stock_has_alternatives;
							fastentry.find('.shop-fastentry-label').text(json.label).show();
							if(json.packaging_quantity > ' ')
								fastentry.find('.shop-fastentry-ve').text('VE:' + json.packaging_quantity).show();
							fastentry.find('.shop-fastentry-error').hide();
							fastentry.find('input.shop-fastentry-qty:last').focus();
						} else {
							fastentry.find('.shop-fastentry-label').hide();
							fastentry.find('.shop-fastentry-ve').hide();
							fastentry.find('.shop-fastentry-error').show();
							if (!($.product._check_item_id && $.product._check_item_id == item_id)) {
								fastentry.find('input.shop-fastentry-item-id:last').focus();
							}
							$.product._check_item_id = item_id;
						}
						callback(json);
					}
				}
			);
		},
		
		set_package: function(package_id, callback) {
			callback = callback || function() {};
			$.ajax({url: '/' + encodeURIComponent($.product._module) + '/set_package/' + encodeURIComponent(package_id),
					data: {},
					dataType: 'json', 
					success: function(json) {
						($[$.product._module] && $[$.product._module].set_package || function(){})(json);
						($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
						callback(json);
					}
				}
			);
		},
		
		delete_package: function(package_id, callback) {
			callback = callback || function() {};
			$.ajax({url: '/' + encodeURIComponent($.product._module) + '/delete_package/' + encodeURIComponent(package_id),
					data: {},
					dataType: 'json', 
					success: function(json) {
						($[$.product._module] && $[$.product._module].delete_package || function(){})(json);
						($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
						callback(json);
					}
				}
			);
		},
		
		rename_package: function(package_id, label, callback) {
			callback = callback || function() {};
			$.ajax({url: '/' + encodeURIComponent($.product._module) + '/rename_package/' + encodeURIComponent(package_id),
					data: {label: label},
					dataType: 'json', 
					success: function(json) {
						($[$.product._module] && $[$.product._module].rename_package || function(){})(json);
						($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
						callback(json);
					}
				}
			);
		},
		
		create_package: function(label, callback) {
			callback = callback || function() {};
			$.ajax({url: '/' + encodeURIComponent($.product._module) + '/create_package',
					data: {label: label},
					dataType: 'json', 
					success: function(json) {
						($[$.product._module] && $[$.product._module].create_package || function(){})(json);
						($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
						callback(json);
					}
				}
			);
		},
		
		recalculate: function(callback) {
			callback = callback || function() {};
			$.product._args = [];
			$.product.popup_select('recalculate', function(url) {
				$.ajax({url: url,
						data: {},
						dataType: 'json', 
						success: function(json) {
							($[$.product._module] && $[$.product._module].recalculate || function(){})(json);
							($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
							callback(json);
						}
					}
				);
			});
		},
		
		set_object: function(object_id, callback) {
			callback = callback || function() {};
			$.product._args = [object_id];
			$.product.popup_select('set_object', function(url) {
				$.ajax({url: url,
						data: {},
						dataType: 'json', 
						success: function(json) {
							($[$.product._module] && $[$.product._module].set_object || function(){})(json);
							($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
							callback(json);
						}
					}
				);
			});
		},
		
		set_association: function(association_id, callback) {
			callback = callback || function() {};
			$.product._args = [association_id];
			$.product.popup_select('set_association', function(url) {
				$.ajax({url: url,
						data: {},
						dataType: 'json', 
						success: function(json) {
							($[$.product._module] && $[$.product._module].set_association || function(){})(json);
							($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
							callback(json);
						}
					}
				);
			});
		},
		
		clear: function(callback) {
			callback = callback || function() {};
			$.product._args = [];
			$.product.popup_select('clear', function(url) {
				$.ajax({url: url,
						data: {},
						dataType: 'json', 
						success: function(json) {
							($[$.product._module] && $[$.product._module].set_association || function(){})(json);
							($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
							callback(json);
						}
					}
				);
			});
		},
		
		add_reel: function(item_id, quantity, positiontype, callback, force) {
			$.product._item_id = item_id;
			$.product._quantity = Math.max(parseFloat(quantity) || 1, 1)
			$.product._positiontype = positiontype || 0;
			$.product._callback = callback || function(){};
			$.product._force = !!force;
			$('#shop-reel-select-popup').popup('/itemlist/reels/' + encodeURIComponent($.product._item_id) + '/' + encodeURIComponent($.product._quantity));
		},
		
		add_alternative: function(item_id, quantity, positiontype, callback, force) {
			$.product._item_id = item_id;
			$.product._quantity = Math.max(parseFloat(quantity) || 1, 1)
			$.product._positiontype = positiontype || 0;
			$.product._callback = callback || function(){};
			$.product._force = !!force;
			$('#shop-alternative-select-popup').popup('/itemlist/alternatives/' + encodeURIComponent($.product._item_id) + '/' + encodeURIComponent($.product._quantity));
		},
		
		add_diversitem: function(item_id, label, subtitle, quantity, callback) {
			if (!label) {
				$.product._quantity = quantity;
				$('#shop-diversitem-label').val('');
				$('#shop-diversitem-subtitle').val('');
				$('#shop-diversitem-popup').popup();
				$('#shop-diversitem-label:last').focus();
				$('#shop-diversitem-item-id').val(item_id);
			} else {
				$.product.add_position(item_id, quantity || $.product._quantity || 1, 0, callback || function() {}, true, label, subtitle);
				$.product._quantity = null;
			}
		},
		
		add_position: function(item_id, quantity, positiontype, callback, force, label, subtitle, reel_select) {
			var oitem_id = item_id,
				oquantity = quantity;
			if (typeof item_id.push == 'function') {
				$.product._args = [item_id.shift(), Math.max(parseFloat(quantity.shift()) || 1, 1), positiontype || 0];
				$.product._data = {
					sub_item_ids: item_id,
					sub_quantities: $.map(quantity, function(qty) {return Math.max(parseFloat(qty) || 1, 1);}),
					force: (force ? '1' : '0'),
					label: label || '',
					subtitle: subtitle || '',
					reel_select: (reel_select ? '1' : '0')
				};
			} else {
				$.product._args = [item_id, Math.max(parseFloat(quantity) || 1, 1), positiontype || 0];
				$.product._data = {
					force: (force ? '1' : '0'),
					label: label || '',
					subtitle: subtitle || '',
					reel_select: (reel_select ? '1' : '0')
				};
			}
			
			callback = callback || function(){};
			$.product.popup_select('add_position', function(url) {
				$.ajax({url: url,
						data: $.product._data || {},
						dataType: 'json', 
						success: function(json) {
							if (json.ask) {
								var pos = json.pos;
								$('#' + $.product._module + '-ask-add-popup').popup();
								$('#' + $.product._module + '-ask-add-popup a.' + $.product._module + '-ask-add-no').unbind('click').bind('click', function(event) {
									event.preventDefault();
									event.stopPropagation();
									$.product.update_position(pos, oquantity, callback, true);
									$('#' + $.product._module + '-ask-add-popup').popdown();
								});
								$('#' + $.product._module + '-ask-add-popup a.' + $.product._module + '-ask-add-yes').unbind('click').bind('click', function(event) {
									event.preventDefault();
									event.stopPropagation();
									$.product.add_position(oitem_id, oquantity, positiontype, callback, true);
									$('#' + $.product._module + '-ask-add-popup').popdown();
								});
							} else {
								$.removeUpdaterEvents();
								($[$.product._module] && $[$.product._module].add_position || function(){})(json);
								($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
								if (json.message) {
									$.removeUpdaterEvents();
									var autoclose = 2000;
									var div = document.createElement('div');
									div.className = $.product._module + '-addposition-message';
									var popup = $('#' + $.product._module + '-addposition-popup');
									if (popup.css('display') == 'none') popup.find('div.shop-popup-content').html('');
									popup.find('div.shop-popup-content').append($(div).text(json.message));
									if ($.product._module == 'cart' && json.pos) {
										var msgs = json.positions[json.pos].calculation.messages;
										if (msgs.length) {
											for (var i = 0, l = msgs.length; i < l; i++) {
												var div2 = document.createElement('div');
												if (msgs[i].messagetype == 'warning') {
													autoclose = false;
													div2.className = $.product._module + '-addposition-message shop-alert-2';
												} else if (msgs[i].messagetype == 'error') {
													autoclose = false;
													div2.className = $.product._module + '-addposition-message shop-alert-3';
												} else {
													autoclose = 2000;
													div2.className = $.product._module + '-addposition-message shop-info-1';
												}
												$(div).append($(div2).text(msgs[i].content));
											}
										}
									}
									if (autoclose) {
										setTimeout(function() {
											$(div).remove();
											if (popup.find('div.' + $.product._module + '-addposition-message').length == 0) {
												popup.popdown();
											}
										}, autoclose);
									}
									
									popup.simple_popup();
								}
								$.updateEvents();
								callback(json);
								if ($.product._focus) {
									$($.product._focus).val('').select().focus();
									$.product._focus = null;
								}
							}
						}
					}
				);
			});
		},
		
		update_position: function(position, quantity, callback, messages, reel) {
			$.product._args = [position];
			if (quantity !== null) $.product._args.push(Math.max(parseFloat(quantity) || 1, 1));
			$.product._data = messages ? {messages: '1'} : {};
			if (reel) $.product._data.reel = reel;
			callback = callback || function(){};
			$.product.popup_select('update_position', function(url) {
				$.ajax({url: url,
						dataType: 'json', 
						data: $.product._data || {},
						success: function(json) {
							($[$.product._module] && $[$.product._module].update_position || function(){})(json);
							($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
							if (messages && json.message) {
								$.removeUpdaterEvents();
								var autoclose = 2000;
								var div = document.createElement('div');
								div.className = $.product._module + '-addposition-message';
								var popup = $('#' + $.product._module + '-addposition-popup');
								popup.find('div.shop-popup-content').append($(div).text(json.message));
								if ($.product._module == 'cart') {
									var msgs = json.positions[json.pos].calculation.messages;
									if (msgs.length) {
										for (var i = 0, l = msgs.length; i < l; i++) {
											var div2 = document.createElement('div');
											if (msgs[i].messagetype == 'warning') {
												autoclose = false;
												div2.className = $.product._module + '-addposition-message shop-alert-2';
											} else if (msgs[i].messagetype == 'error') {
												autoclose = false;
												div2.className = $.product._module + '-addposition-message shop-alert-3';
											} else {
												autoclose = 2000;
												div2.className = $.product._module + '-addposition-message shop-info-1';
											}
											$(div).append($(div2).text(msgs[i].content));
										}
									}
								}
								
								if (autoclose) {
									setTimeout(function() {
										$(div).remove();
										if (popup.find('div.' + $.product._module + '-addposition-message').length == 0) {
											popup.popdown();
										}
									}, autoclose);
									popup.simple_popup();
								} else {
									popup.popup();
								}
								$.updateEvents();
							}
							callback(json);
							if ($.product._focus) {
								$($.product._focus).val('').select().focus();
								$.product._focus = null;
							}
						}
					}
				);
			});
		},
		
		copy_positions: function(target_module, positions, callback) {
			var source_module = $.product._module;
			$.product._module = target_module;
			if (positions.length) {
				$.product._args = [positions.join(',')];
				callback = callback || function(){};
				$.product.popup_select('copy_positions', function(url) {
					$.ajax({url: url,
							dataType: 'json', 
							success: function(json) {
								($[target_module] && $[target_module].copy_positions || function(){})(json);
								($['portlet_' + target_module] && $['portlet_' + target_module].update || function(){})(json);
								if (json.message) {
									$.removeUpdaterEvents();
									var div = document.createElement('div');
									div.className = $.product._module + '-addposition-message';
									var popup = $('#' + $.product._module + '-addposition-popup');
									popup.find('div.shop-popup-content').append($(div).text(json.message));
									setTimeout(function() {
										$(div).remove();
										if (popup.find('div.' + $.product._module + '-addposition-message').length == 0) {
											popup.popdown();
										}
									}, 3000);
									popup.simple_popup();
									$.updateEvents();
								}
								callback(json);
							}
						}
					);
				});
			}
		},
		
		move_positions: function(target_module, positions, callback) {
			var source_module = $.product._module;
			$.product._module = target_module;
			$.product._args = [positions.join(',')];
			callback = callback || function(){};
			$.product.popup_select('move_positions', function(url) {
				$.ajax({url: url,
						dataType: 'json', 
						success: function(json) {
							($[target_module] && $[target_module].move_positions || function(){})(json);
							($['portlet_' + target_module] && $['portlet_' + target_module].update || function(){})(json);
							if (json.message) {
								$.removeUpdaterEvents();
								var div = document.createElement('div');
								div.className = $.product._module + '-addposition-message';
								var popup = $('#' + $.product._module + '-addposition-popup');
								popup.find('div.shop-popup-content').append($(div).text(json.message));
								setTimeout(function() {
									$(div).remove();
									if (popup.find('div.' + $.product._module + '-addposition-message').length == 0) {
										popup.popdown();
									}
								}, 3000);
								popup.simple_popup();
								$.updateEvents();
							}
							callback(json);
						}
					}
				);
			});
		},
		
		copy_container: function(from_container, callback) {
			$.product._args = [from_container];
			callback = callback || function(){};
			$.product.popup_select('copy_container', function(url) {
				$.ajax({url: url,
						dataType: 'json', 
						success: function(json) {
							($[$.product._module] && $[$.product._module].copy_container || function(){})(json);
							($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
							if (json.message) {
								$.removeUpdaterEvents();
								var div = document.createElement('div');
								div.className = $.product._module + '-addposition-message';
								var popup = $('#' + $.product._module + '-addposition-popup');
								popup.find('div.shop-popup-content').append($(div).text(json.message));
								setTimeout(function() {
									$(div).remove();
									if (popup.find('div.' + $.product._module + '-addposition-message').length == 0) {
										popup.popdown();
									}
								}, 3000);
								popup.simple_popup();
								$.updateEvents();
							}
							callback(json);
						}
					}
				);
			});
		},
		
		move_container: function(from_container, callback) {
			$.product._args = [from_container];
			callback = callback || function(){};
			$.product.popup_select('move_container', function(url) {
				$.ajax({url: url,
						dataType: 'json', 
						success: function(json) {
							($[$.product._module] && $[$.product._module].move_container || function(){})(json);
							($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
							if (json.message) {
								$.removeUpdaterEvents();
								var div = document.createElement('div');
								div.className = $.product._module + '-addposition-message';
								var popup = $('#' + $.product._module + '-addposition-popup');
								popup.find('div.shop-popup-content').append($(div).text(json.message));
								setTimeout(function() {
									$(div).remove();
									if (popup.find('div.' + $.product._module + '-addposition-message').length == 0) {
										popup.popdown();
									}
								}, 3000);
								popup.simple_popup();
								$.updateEvents();
							}
							callback(json);
						}
					}
				);
			});
		},
		
		add_order: function(order_id, callback) {
			$.product._args = [order_id];
			callback = callback || function(){};
			$.product.popup_select('add_order', function(url) {
				$.ajax({url: url,
						dataType: 'json', 
						success: function(json) {
							($[$.product._module] && $[$.product._module].copy_container || function(){})(json);
							($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
							if (json.message) {
								$.removeUpdaterEvents();
								var div = document.createElement('div');
								div.className = $.product._module + '-addposition-message';
								var popup = $('#' + $.product._module + '-addposition-popup');
								popup.find('div.shop-popup-content').append($(div).text(json.message));
								setTimeout(function() {
									$(div).remove();
									if (popup.find('div.' + $.product._module + '-addposition-message').length == 0) {
										popup.popdown();
									}
								}, 3000);
								popup.simple_popup();
								$.updateEvents();
							}
							callback(json);
						}
					}
				);
			});
		},
		
		remove_position: function(position, callback) {
			$.product._args = [position];
			callback = callback || function(){};
			$.product.popup_select('remove_position', function(url) {
				$.ajax({url: url,
						dataType: 'json', 
						success: function(json) {
							($[$.product._module] && $[$.product._module].remove_position || function(){})(json);
							($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
							callback(json);
						}
					}
				);
			});
		},
		
		remove_positions: function(positions, callback) {
			if (positions) {
				$.product._args = [positions.join(',')];
				callback = callback || function(){};
				$.product.popup_select('remove_positions', function(url) {
					$.ajax({url: url,
							dataType: 'json', 
							success: function(json) {
								($[$.product._module] && $[$.product._module].remove_positions || function(){})(json);
								($['portlet_' + $.product._module] && $['portlet_' + $.product._module].update || function(){})(json);
								callback(json);
							}
						}
					);
				});
			}
		}
	};
	
	$.attachUpdater(function() {
		$('table.shop-container-select tbody tr').bind('click.update', function(event) {
			$(this).closest('.shop-popup').popdown();
			$.product.set_container(this.id.split('-')[1]);
			$.product.do_callback();
		});
		
		var ddto = null;
		var ddti = null;
		var ddactive = null;
		$('input.shop-button-pos-ekl-dd').bind('mouseover.update', function(event) {
			if (ddti) clearTimeout(ddti);
			if (ddto) clearTimeout(ddto);
			if (ddactive && ddactive.parent().get(0) != this.parentNode) ddactive.hide();
			ddactive = $(this).parent().find('.shop-pos-dd');
			ddti = setTimeout(function() {
				ddactive.show();
			}, 300);
		});
		
		$('input.shop-button-pos-ekl-dd').bind('mouseout.update', function(event) {
			if (ddti) clearTimeout(ddti);
			if (ddto) clearTimeout(ddto);
			ddto = setTimeout(function() {
				ddactive.hide();
			}, 300);
		});
		
		$('div.shop-pos-dd').bind('mouseover.update', function (event) {
			if (ddto) clearTimeout(ddto);
		});
		
		$('div.shop-pos-dd').bind('mouseout.update', function (event) {
			if (ddti) clearTimeout(ddti);
			if (ddto) clearTimeout(ddto);
			ddto = setTimeout(function() {
				ddactive.hide();
			}, 300);
		});
		
		$('div.shop-pos-dd a').bind('click.update', function(event) {
			event.preventDefault();
			if (ddti) clearTimeout(ddti);
			if (ddto) clearTimeout(ddto);
			ddactive.hide();
		});
		
		$('input.shop-button-list-ekl-dd').bind('mouseover.update', function(event) {
			if (ddti) clearTimeout(ddti);
			if (ddto) clearTimeout(ddto);
			if (ddactive && ddactive.parent().get(0) != this.parentNode) ddactive.hide();
			ddactive = $(this).parent().find('.shop-list-dd');
			ddti = setTimeout(function() {
				ddactive.show();
			}, 300);
		});
		
		$('input.shop-button-list-ekl-dd').bind('mouseout.update', function(event) {
			if (ddti) clearTimeout(ddti);
			if (ddto) clearTimeout(ddto);
			ddto = setTimeout(function() {
				ddactive.hide();
			}, 300);
		});
		
		$('div.shop-list-dd').bind('mouseover.update', function (event) {
			if (ddto) clearTimeout(ddto);
		});
		
		$('div.shop-list-dd').bind('mouseout.update', function (event) {
			if (ddti) clearTimeout(ddti);
			if (ddto) clearTimeout(ddto);
			ddto = setTimeout(function() {
				ddactive.hide();
			}, 300);
		});
		
		$('div.shop-list-dd a').bind('click.update', function(event) {
			event.preventDefault();
			if (ddti) clearTimeout(ddti);
			if (ddto) clearTimeout(ddto);
			ddactive.hide();
		});
		
		$('a.shop-diversitem-add').bind('click.update', function(event) {
			event.preventDefault();
			event.stopPropagation();
			var id = $(this).attr('href');
			id = id.split('#');
			id[0] = id[0].split('/').pop();
			$.product.set_module(id[0] + '');
			$.product.add_diversitem(id[1] + '');
		});
		
		$('#shop-diversitem-form').bind('submit.update', function(event) {
			event.preventDefault();
			event.stopPropagation();
			$('#shop-diversitem-popup').popdown();
			var label = $.trim($('#shop-diversitem-label').val());
			var subtitle = $.trim($('#shop-diversitem-subtitle').val());
			if (label == '') {
				label = subtitle;
				subtitle = '';
			}
			if (label) {
				$.product.add_diversitem($('#shop-diversitem-item-id').val(), label, subtitle);
			}
		});
		
		$('form.shop-fastentry').each(function() {
			var self = $(this);
			self.find('input.shop-fastentry-item-id').bind('change.update, blur.update', function(event) {
				if (this.value != '' && this.value != this.defaultValue) {
					$.product.set_module(self.attr('action').substr(1));
					$.product.check_product(this.value, self);
				} else {
					self.find('.shop-fastentry-label,.shop-fastentry-ve,.shop-fastentry-error').hide();
				}
			}).find('input.shop-fastentry-qty').bind('change.update', function(event) {
				if (this.value != '' && this.value != this.defaultValue) {
					this.value = Math.max(parseFloat(this.value) || 1, 1);
				}
			});
			self.find('input.shop-fastentry-item-id').bind('keypress.update', function(event) {
				if ((event.keyCode || event.which) == 13) {
					event.preventDefault();
					event.stopPropagation();
					$(this).trigger('change');
				}
			});
		}).bind('submit.update', function(event) {
			event.preventDefault();
			event.stopPropagation();
			var self = $(this);
			if (self.find('.shop-fastentry-label').css('display') != 'none') {
				var p = self;
				while (!p.attr('id')) {
					p = p.parent();
				}
				if (p && p.attr('id')) {
					$.product.set_focus('#' + p.attr('id') + ' form.shop-fastentry input.shop-fastentry-item-id:last');
				} else {
					$.product.set_focus('form.shop-fastentry input.shop-fastentry-item-id:last');
				}
				$.product.set_module(self.attr('action').substr(1));
				var container = $('#' + self.attr('action').substr(1) + '-container-id').val();
				if (container && (self.attr('action').substr(1) == 'shoppinglist' || self.attr('action').substr(1) == 'projects' || self.attr('action').substr(1) == 'etiquettes')) $.product.set_container(container);
				var _qty = self.find('input.shop-fastentry-qty');
				var _item_id = self.find('input.shop-fastentry-item-id');
				
				if ($.product._is_reel) {
					$.product.add_reel($.product._checked_item_id, _qty.val(), 0, function() {
						self.find('.shop-fastentry-label,.shop-fastentry-ve,.shop-fastentry-error').hide();
						_qty.val(_qty.get(0).defaultValue);
					});
				} else {
					if ($.product._is_divers) {
						$.product.add_diversitem($.product._checked_item_id, null, null, _qty.val(), 0, function() {
							self.find('.shop-fastentry-label,.shop-fastentry-ve,.shop-fastentry-error').hide();
							_qty.val(_qty.get(0).defaultValue);
						});
					} else if ($.product._no_stock_has_alternatives) {
						$.product.add_alternative($.product._checked_item_id, _qty.val(), 0, function() {
							self.find('.shop-fastentry-label,.shop-fastentry-ve,.shop-fastentry-error').hide();
							_qty.val(_qty.get(0).defaultValue);
						});
					} else {
						$.product.add_position($.product._checked_item_id, _qty.val(), 0, function() {
							self.find('.shop-fastentry-label,.shop-fastentry-ve,.shop-fastentry-error').hide();
							_qty.val(_qty.get(0).defaultValue);
						});
					}
				}
			} else {
				self.find('input.shop-fastentry-item-id:last').select().focus();
			}
		});
		
		$('form.shop-package-create').bind('submit.update', function(event) {
			event.preventDefault();
			event.stopPropagation();
			var self = $(this);
			if ($.trim(self.find('input[type="text"]').val())) {
				$.product.set_module(self.attr('action').substr(1));
				$.product.create_package($.trim(self.find('input[type="text"]').val()), function(json) {
					var error = self.closest('form').find('.shop-package-create-error').hide();
					self.find('input[type="text"]:first').select().focus();
					if (json.message) error.text(json.message).show();
				});
			}
		});
		
		$('form.shop-package-select li').bind('click.update', function(event) {
			event.preventDefault();
			event.stopPropagation();
			var self = $(this);
			$.product.set_module(self.closest('form').attr('action').substr(1));
			$.product.set_package(self.find('input[name="package_id"]:first').val());
		});
		
		$('a.shop-copy-container').bind('click.update', function(event) {
			var target = $(this).attr('href').split('#')[1].split('-');
			$.product.set_module(target[0]);
			target.shift();
			target = target.join('-');
			$.product.copy_container(target);
		});
		
		$('input.shop-copy-container').bind('click.update', function(event) {
			var target = $(this).attr('name').split('-');
			$.product.set_module(target[0]);
			target.shift();
			target = target.join('-');
			$.product.copy_container(target);
		});
		
		$('input.shop-add-order').bind('click.update', function(event) {
			var target = $(this).attr('name').split('-');
			$.product.set_module(target[0]);
			target.shift();
			target = target.join('-');
			$.product.add_order(target);
		});
		
		$('#shop-reel-select-popup input.shop-button-pos-cart').bind('click.update', function(event) {
			event.preventDefault();
			event.stopPropagation();
			var self = $(this);
			var item_id = self.attr('name');
			var quantity = self.closest('tr').find('input.shop-itemlist-reel-qty,select.shop-itemlist-reel-qty').val();
			if ($.trim(item_id) == '') {
				quantity = quantity.split('$');
				item_id = quantity[0] + '$' + quantity[1];
				quantity = quantity[2];
			}
			var positiontype = $.product._positiontype;
			var callback = $.product._callback;
			var force = $.product._force;
			var label = $.product._label;
			var subtitle = $.product._subtitle;
			var reel_select = 1;
			$('#shop-reel-select-popup').popdown();
			$.product.add_position(item_id, quantity, positiontype, callback, force, label, subtitle, reel_select);
		});
		
		
		$('#shop-alternative-select-popup input.shop-button-pos-cart').bind('click.update', function(event) {
			event.preventDefault();
			event.stopPropagation();
			var self = $(this);
			var item_id = self.attr('name');
			var quantity = self.closest('tr').find('input.shop-itemlist-alternative-qty,select.shop-itemlist-alternative-qty').val();
			if ($.trim(item_id) == '') {
				quantity = quantity.split('$');
				item_id = quantity[0] + '$' + quantity[1];
				quantity = quantity[2];
			}
			var positiontype = $.product._positiontype;
			var callback = $.product._callback;
			var force = $.product._force;
			var label = $.product._label;
			var subtitle = $.product._subtitle;
			var reel_select = 1;
			$('#shop-alternative-select-popup').popdown();
			$.product.add_position(item_id, quantity, positiontype, callback, force, label, subtitle, reel_select);
		});
		
		
		
		$('table.shop-itemlist').each(function() {
			var il = $(this), prefix = '';
			if (!il.hasClass('shop-itemlist-qty')) prefix = 'table.shop-itemlist-qty ';
			il.find(prefix + '.shop-itemlist-qty-basket input.shop-itemlist-qty-button').bind('click.update', function(event) {
				var self = $(this);
				var qty = self.closest('.shop-itemlist-qty').find('input.shop-itemlist-qty');
				var val = Math.max(parseFloat(qty.val()) || 1, 1);
				if (val != parseFloat(qty.val())) qty.val(val);
				$.product.set_module('cart');
				if (self.closest('tr.shop-itemlist-item-reel').length) {
					$.product.add_reel(self.attr('name'), qty.val());
				} else {
					if (self.closest('tr.shop-itemlist-item-divers').length) {
						$.product.add_diversitem(self.attr('name'), null, null, qty.val());
					} else {
						$.product.add_position(self.attr('name'), qty.val());
					}
				}
			});
			
			il.find(prefix + 'input.shop-button-pos-ekl').bind('click.update', function(event) {
				var self = $(this);
				var id = self.closest('tbody.container-position').find('input.shop-checkbox').attr('name');
				var mod = self.attr('id').length ? self.attr('id').split('-').pop(): 'shoppinglist';
				if (id) {
					$.product.set_module(self.closest('tbody.container-position').attr('id').split('-')[0]);
					$.product.copy_positions(mod, [id]);
				} else {
					var qty = self.closest('.shop-itemlist-qty').find('input.shop-itemlist-qty');
					var val = Math.max(parseFloat(qty.val()) || 1, 1);
					if (val != parseFloat(qty.val())) qty.val(val);
					$.product.set_module(mod);
					if (self.closest('tr.shop-itemlist-item-reel').length) {
						$.product.add_reel(self.attr('name'), qty.val());
					} else {
						if (self.closest('tr.shop-itemlist-item-divers').length) {
							$.product.add_diversitem(self.attr('name'), null, null, qty.val());
						} else {
							$.product.add_position(self.attr('name'), qty.val());
						}
					}
				}
			});
			
			il.find(prefix + 'a.shop-button-add-shoppinglist, ' + prefix + 'a.shop-button-add-projects').bind('click.update', function(event) {
				var self = $(this);
				var id = self.closest('tbody.container-position').find('input.shop-checkbox').attr('name');
				var mod = self.attr('class').split('-').pop();
				if (id) {
					$.product.set_module(self.closest('tbody.container-position').attr('id').split('-')[0]);
					$.product.copy_positions(mod, [id]);
				} else {
					var qty = self.closest('.shop-itemlist-qty').find('input.shop-itemlist-qty');
					var val = Math.max(parseFloat(qty.val()) || 1, 1);
					if (val != parseFloat(qty.val())) qty.val(val);
					$.product.set_module(mod);
					if (self.closest('tr.shop-itemlist-item-reel').length) {
						$.product.add_reel(self.attr('name'), qty.val());
					} else {
						if (self.closest('tr.shop-itemlist-item-divers').length) {
							$.product.add_diversitem(self.attr('name'), null, null, qty.val());
						} else {
							$.product.add_position(self.attr('name'), qty.val());
						}
					}
				}
			});
			
			il.find(prefix + 'a.shop-button-add-quoterequest').bind('click.update', function(event) {
				var self = $(this);
				var id = self.closest('tbody.container-position').find('input.shop-checkbox').attr('name');
				if (id) {
					$.product.set_module(self.closest('tbody.container-position').attr('id').split('-')[0]);
					$.product.copy_positions('quoterequest', [id]);
				} else {
					var qty = self.closest('.shop-itemlist-qty').find('input.shop-itemlist-qty');
					var val = Math.max(parseFloat(qty.val()) || 1, 1);
					if (val != parseFloat(qty.val())) qty.val(val);
					$.product.set_module('quoterequest');
					if (self.closest('tr.shop-itemlist-item-reel').length) {
						$.product.add_reel(self.attr('name'), qty.val());
					} else {
						if (self.closest('tr.shop-itemlist-item-divers').length) {
							$.product.add_diversitem(self.attr('name'), null, null, qty.val());
						} else {
							$.product.add_position(self.attr('name'), qty.val());
						}
					}
				}
			});
			
			il.find(prefix + 'a.shop-button-add-etiquettes').bind('click.update', function(event) {
				var self = $(this);
				var id = self.closest('tbody.container-position').find('input.shop-checkbox').attr('name');
				if (id) {
					$.product.set_module(self.closest('tbody.container-position').attr('id').split('-')[0]);
					$.product.copy_positions('etiquettes', [id]);
				} else {
					$.product.set_module('etiquettes');
					$.product.add_position(self.attr('name'), '1');
				}
			});
			
			il.find(prefix + 'input.shop-button-pos-cart').bind('click.update', function(event) {
				var self = $(this);
				var id = self.closest('tbody.container-position').find('input.shop-checkbox').attr('name');
				if (id) {
					$.product.set_module(self.closest('tbody.container-position').attr('id').split('-')[0]);
					$.product.copy_positions('cart', [id]);
				} else {
					var qty = self.closest('.shop-itemlist-qty').find('input.shop-itemlist-qty');
					var val = Math.max(parseFloat(qty.val()) || 1, 1);
					if (val != parseFloat(qty.val())) qty.val(val);
					$.product.set_module('cart');
					$.product.add_position(self.attr('name'), val);
				}
			});
			
			il.find(prefix + '.shop-itemlist-qty-basket input.shop-itemlist-qty').bind('keypress.update', function(event) {
				if ((event.keyCode || event.which) == 13) {
					event.preventDefault();
					event.stopPropagation();
					$(this).siblings('input.shop-itemlist-qty-button').trigger('click');
				}
			});
			
			il.find(prefix + '.shop-itemlist-qty-delete input.shop-itemlist-qty').bind('keypress.update', function(event) {
				if ((event.keyCode || event.which) == 13) {
					event.preventDefault();
					event.stopPropagation();
					$(this).trigger('change');
				}
			});
			
			il.find(prefix + ' a.shop-itemlist-button-overwrite-position').bind('click.update', function(event) {
				event.preventDefault();
				event.stopPropagation();
				var href = $(this).attr('href').split('#')[1].split('-');
				$.product.set_module(href[0]);
				if ($('#shoppinglist-container-id').length && $('#shoppinglist-container-id').val()) {
					$.product.set_container($('#shoppinglist-container-id').val());
				}
				$.product.update_position(href[1], null, null, false, $(this).attr('target'));
			});
			
			il.find(prefix + ' a.shop-itemlist-button-overwrite-position').bind('click.update', function(event) {
				event.preventDefault();
				event.stopPropagation();
				var href = $(this).attr('href').split('#')[1].split('-');
				$.product.set_module(href[0]);
				if ($('#projects-container-id').length && $('#projects-container-id').val()) {
					$.product.set_container($('#projects-container-id').val());
				}
				$.product.update_position(href[1], null, null, false, $(this).attr('target'));
			});
		});
		
		var pr = $('td.shop-itemlist-price');
		
		var _show_graduated = null;
		pr.find('div.shop-itemlist-price-graduated-popup').bind('mouseover.update', function(event) {
			var e = $(this);
			var position = e.offset();
			var size = e.outerWidth();
			var item = encodeURIComponent(e.find('input').val());
			if (typeof e.get(0)._target == 'undefined') {
				var t = e.find('.shop-itemlist-price-graduated-box');
				e.get(0)._target = t;
			} else {
				var t = e.get(0)._target;
			}
			
			var p = t.parent();
			
			if (t.html().length == 0) {
				_show_graduated = item;
				t.load('/catalog/graduated_prices/' + item, function() {
					if (_show_graduated == item) {
						$('body').append(p);
						p.css('top', position.top + 'px').css('left', position.left + 'px').css('width', size + 'px').show();
						_show_graduated = null;
					}
				});
			} else {
				p.show();
				p.css('top', position.top + 'px').css('left', position.left + 'px').css('width', size + 'px').show();
			}
		}).bind('mouseout.update', function(event) {
			var e = $(this);
			var item = encodeURIComponent(e.find('input').val());
			if (_show_graduated == item) {
				if (typeof e.get(0)._target == 'undefined') {
					var t = e.find('.shop-itemlist-price-graduated-box');
					e.get(0)._target = t;
				} else {
					var t = e.get(0)._target;
				}
				
				var p = t.parent();
				if (p.css('display') == 'none') {
					_show_graduated = null;
				}
			}
		});
		
		var _show_associations = null;
		pr.find('div.shop-itemlist-price-associations-popup').bind('mouseover.update', function(event) {
			var e = $(this);
			var position = e.offset();
			var size = e.outerWidth();
			var item = encodeURIComponent(e.find('input').val());
			if (typeof e.get(0)._target == 'undefined') {
				var t = e.find('.shop-itemlist-price-associations-box');
				e.get(0)._target = t;
			} else {
				var t = e.get(0)._target;
			}
			
			var p = t.parent();
			
			if (t.html().length == 0) {
				_show_associations = item;
				t.load('/catalog/association_prices/' + item, function() {
					if (_show_associations == item) {
						$('body').append(p);
						p.css('top', position.top + 'px').css('left', position.left + 'px').css('width', size + 'px').show();
						_show_associations = null;
					}
				});
			} else {
				p.show();
				p.css('top', position.top + 'px').css('left', position.left + 'px').css('width', size + 'px').show();
			}
		}).bind('mouseout.update', function(event) {
			var e = $(this);
			var item = encodeURIComponent(e.find('input').val());
			if (_show_associations == item) {
				if (typeof e.get(0)._target == 'undefined') {
					var t = e.find('.shop-itemlist-price-associations-box');
					e.get(0)._target = t;
				} else {
					var t = e.get(0)._target;
				}
				
				var p = t.parent();
				if (p.css('display') == 'none') {
					_show_associations = null;
				}
			}
		});
		
		pr.find('.shop-itemlist-price-graduated-box-container,.shop-itemlist-price-associations-box-container').bind('mouseleave.update', function(eve){
			$(this).hide();
		});
	
		$('#etiquettes-lists-select-creator').bind('submit.update', function(event) {
			event.preventDefault();
			event.stopPropagation();
			var label = $('#etiquettes-lists-select-creator-label');
			if ($.trim(label.val()) == '') {
				label.focus();
				return;
			}
			
			$.product.set_module('etiquettes');
			$.product.create(label.val());
		});
		
		$('#shoppinglist-select-creator').bind('submit.update', function(event) {
			event.preventDefault();
			event.stopPropagation();
			var label = $('#shoppinglist-select-creator-label');
			if ($.trim(label.val()) == '') {
				label.focus();
				return;
			}
			
			$.product.set_module('shoppinglist');
			$.product.create(label.val());
		});
		
		$('#projects-select-creator').bind('submit.update', function(event) {
			event.preventDefault();
			event.stopPropagation();
			var label = $('#projects-select-creator-label');
			if ($.trim(label.val()) == '') {
				label.focus();
				return;
			}
			
			$.product.set_module('projects');
			$.product.create(label.val());
		});
	});
})();
