if (!Control)
	var Control = {};
Control.Slider = Class
		.create( {
			initialize : function(handle, track, options) {
				var slider = this;
				if (Object.isArray(handle))
					this.handles = handle.collect(function(e) {
						return $(e)
					});
				else
					this.handles = [ $(handle) ];
				this.track = $(track);
				this.options = options || {};
				this.axis = this.options.axis || "horizontal";
				this.increment = this.options.increment || 1;
				this.step = parseInt(this.options.step || "1");
				this.range = this.options.range || $R(0, 1);
				this.value = 0;
				this.values = this.handles.map(function() {
					return 0
				});
				this.spans = this.options.spans ? this.options.spans
						.map(function(s) {
							return $(s)
						}) : false;
				this.options.startSpan = $(this.options.startSpan || null);
				this.options.endSpan = $(this.options.endSpan || null);
				this.restricted = this.options.restricted || false;
				this.maximum = this.options.maximum || this.range.end;
				this.minimum = this.options.minimum || this.range.start;
				this.alignX = parseInt(this.options.alignX || "0");
				this.alignY = parseInt(this.options.alignY || "0");
				this.trackLength = this.maximumOffset() - this.minimumOffset();
				this.handleLength = this.isVertical() ? this.handles[0].offsetHeight != 0 ? this.handles[0].offsetHeight
						: this.handles[0].style.height.replace(/px$/, "")
						: this.handles[0].offsetWidth != 0 ? this.handles[0].offsetWidth
								: this.handles[0].style.width
										.replace(/px$/, "");
				this.active = false;
				this.dragging = false;
				this.disabled = false;
				if (this.options.disabled)
					this.setDisabled();
				this.allowedValues = this.options.values ? this.options.values
						.sortBy(Prototype.K) : false;
				if (this.allowedValues) {
					this.minimum = this.allowedValues.min();
					this.maximum = this.allowedValues.max()
				}
				this.eventMouseDown = this.startDrag.bindAsEventListener(this);
				this.eventMouseUp = this.endDrag.bindAsEventListener(this);
				this.eventMouseMove = this.update.bindAsEventListener(this);
				this.handles
						.each(function(h, i) {
							i = slider.handles.length - 1 - i;
							slider
									.setValue(
											parseFloat((Object
													.isArray(slider.options.sliderValue) ? slider.options.sliderValue[i]
													: slider.options.sliderValue)
													|| slider.range.start), i);
							h.makePositioned().observe("mousedown",
									slider.eventMouseDown)
						});
				this.track.observe("mousedown", this.eventMouseDown);
				document.observe("mouseup", this.eventMouseUp);
				document.observe("mousemove", this.eventMouseMove);
				this.initialized = true
			},
			dispose : function() {
				var slider = this;
				Event.stopObserving(this.track, "mousedown",
						this.eventMouseDown);
				Event.stopObserving(document, "mouseup", this.eventMouseUp);
				Event.stopObserving(document, "mousemove", this.eventMouseMove);
				this.handles.each(function(h) {
					Event.stopObserving(h, "mousedown", slider.eventMouseDown)
				})
			},
			setDisabled : function() {
				this.disabled = true
			},
			setEnabled : function() {
				this.disabled = false
			},
			getNearestValue : function(value) {
				if (this.allowedValues) {
					if (value >= this.allowedValues.max())
						return this.allowedValues.max();
					if (value <= this.allowedValues.min())
						return this.allowedValues.min();
					var offset = Math.abs(this.allowedValues[0] - value);
					var newValue = this.allowedValues[0];
					this.allowedValues.each(function(v) {
						var currentOffset = Math.abs(v - value);
						if (currentOffset <= offset) {
							newValue = v;
							offset = currentOffset
						}
					});
					return newValue
				}
				if (value > this.range.end)
					return this.range.end;
				if (value < this.range.start)
					return this.range.start;
				return value
			},
			setValue : function(sliderValue, handleIdx) {
				if (!this.active) {
					this.activeHandleIdx = handleIdx || 0;
					this.activeHandle = this.handles[this.activeHandleIdx];
					this.updateStyles()
				}
				handleIdx = handleIdx || this.activeHandleIdx || 0;
				if (this.initialized && this.restricted) {
					if (handleIdx > 0
							&& sliderValue < this.values[handleIdx - 1])
						sliderValue = this.values[handleIdx - 1];
					if (handleIdx < this.handles.length - 1
							&& sliderValue > this.values[handleIdx + 1])
						sliderValue = this.values[handleIdx + 1]
				}
				sliderValue = this.getNearestValue(sliderValue);
				this.values[handleIdx] = sliderValue;
				this.value = this.values[0];
				this.handles[handleIdx].style[this.isVertical() ? "top"
						: "left"] = this.translateToPx(sliderValue);
				this.drawSpans();
				if (!this.dragging || !this.event)
					this.updateFinished()
			},
			setValueBy : function(delta, handleIdx) {
				this.setValue(this.values[handleIdx || this.activeHandleIdx
						|| 0]
						+ delta, handleIdx || this.activeHandleIdx || 0)
			},
			translateToPx : function(value) {
				return Math.round((this.trackLength - this.handleLength)
						/ (this.range.end - this.range.start)
						* (value - this.range.start))
						+ "px"
			},
			translateToValue : function(offset) {
				return offset / (this.trackLength - this.handleLength)
						* (this.range.end - this.range.start)
						+ this.range.start
			},
			getRange : function(range) {
				var v = this.values.sortBy(Prototype.K);
				range = range || 0;
				return $R(v[range], v[range + 1])
			},
			minimumOffset : function() {
				return this.isVertical() ? this.alignY : this.alignX
			},
			maximumOffset : function() {
				return this.isVertical() ? (this.track.offsetHeight != 0 ? this.track.offsetHeight
						: this.track.style.height.replace(/px$/, ""))
						- this.alignY
						: (this.track.offsetWidth != 0 ? this.track.offsetWidth
								: this.track.style.width.replace(/px$/, ""))
								- this.alignX
			},
			isVertical : function() {
				return this.axis == "vertical"
			},
			drawSpans : function() {
				var slider = this;
				if (this.spans)
					$R(0, this.spans.length - 1).each(function(r) {
						slider.setSpan(slider.spans[r], slider.getRange(r))
					});
				if (this.options.startSpan)
					this.setSpan(this.options.startSpan, $R(0,
							this.values.length > 1 ? this.getRange(0).min()
									: this.value));
				if (this.options.endSpan)
					this.setSpan(this.options.endSpan, $R(
							this.values.length > 1 ? this.getRange(
									this.spans.length - 1).max() : this.value,
							this.maximum))
			},
			setSpan : function(span, range) {
				if (this.isVertical()) {
					span.style.top = this.translateToPx(range.start);
					span.style.height = this.translateToPx(range.end
							- range.start + this.range.start)
				} else {
					span.style.left = this.translateToPx(range.start);
					span.style.width = this.translateToPx(range.end
							- range.start + this.range.start)
				}
			},
			updateStyles : function() {
				this.handles.each(function(h) {
					Element.removeClassName(h, "selected")
				});
				Element.addClassName(this.activeHandle, "selected")
			},
			startDrag : function(event) {
				if (Event.isLeftClick(event)) {
					if (!this.disabled) {
						this.active = true;
						var handle = Event.element(event);
						var pointer = [ Event.pointerX(event),
								Event.pointerY(event) ];
						var track = handle;
						if (track == this.track) {
							var offsets = this.track.cumulativeOffset();
							this.event = event;
							this.setValue(this.translateToValue((this
									.isVertical() ? pointer[1] - offsets[1]
									: pointer[0] - offsets[0])
									- this.handleLength / 2));
							var offsets = this.activeHandle.cumulativeOffset();
							this.offsetX = pointer[0] - offsets[0];
							this.offsetY = pointer[1] - offsets[1]
						} else {
							while (this.handles.indexOf(handle) == -1
									&& handle.parentNode)
								handle = handle.parentNode;
							if (this.handles.indexOf(handle) != -1) {
								this.activeHandle = handle;
								this.activeHandleIdx = this.handles
										.indexOf(this.activeHandle);
								this.updateStyles();
								var offsets = this.activeHandle
										.cumulativeOffset();
								this.offsetX = pointer[0] - offsets[0];
								this.offsetY = pointer[1] - offsets[1]
							}
						}
					}
					Event.stop(event)
				}
			},
			update : function(event) {
				if (this.active) {
					if (!this.dragging)
						this.dragging = true;
					this.draw(event);
					if (Prototype.Browser.WebKit)
						window.scrollBy(0, 0);
					Event.stop(event)
				}
			},
			draw : function(event) {
				var pointer = [ Event.pointerX(event), Event.pointerY(event) ];
				var offsets = this.track.cumulativeOffset();
				pointer[0] -= this.offsetX + offsets[0];
				pointer[1] -= this.offsetY + offsets[1];
				this.event = event;
				this.setValue(this
						.translateToValue(this.isVertical() ? pointer[1]
								: pointer[0]));
				if (this.initialized && this.options.onSlide)
					this.options.onSlide(this.values.length > 1 ? this.values
							: this.value, this)
			},
			endDrag : function(event) {
				if (this.active && this.dragging) {
					this.finishDrag(event, true);
					Event.stop(event)
				}
				this.active = false;
				this.dragging = false
			},
			finishDrag : function(event, success) {
				this.active = false;
				this.dragging = false;
				this.updateFinished()
			},
			updateFinished : function() {
				if (this.initialized && this.options.onChange)
					this.options.onChange(this.values.length > 1 ? this.values
							: this.value, this);
				this.event = null
			}
		});
var ProductFinder1700 = Class.create();
ProductFinder1700.prototype = {
	allProducts : [],
	fadeEffects : [],
	morphEffects : [],
	appearEffects : [],
	selectedProducts : [],
	hideThese : [],
	imageSizes : $w("category search small"),
	imageSize : "small",
	slider : null,
	viewport : null,
	productDetailsTimer : null,
	resultContainerOffset : null,
	resultContainerSize : null,
	productTextIds : [],
	isBusy : false,
	formChangedDuringTransistion : false,
	url : "/index.php",
	initialize : function(b) {
		this.working();
		this.slider = b;
		this.readState();
		new Ajax.Request(this.url, {
			method : "get",
			parameters: {main_page: 'product_finder_ajax', for_page: 'bargain_search' },
			onSuccess : function(a) {
				
				this.allProducts = a.responseText.evalJSON(true);
				//alert('Claudiu Test' + this.allProducts.toSource());
				//alert('Claudiu Test' + a.toSource());
				this.createDivs();
				this.formChanged()
			}.bind(this)
		})
	},
	working : function() {
		$("pf_idle").hide();
		$("pf_working").show()
	},
	idle : function() {
		$("pf_working").hide();
		$("pf_idle").show();
		this.selectedProducts.size() === 0 && $("no_products_found").show();
		if (this.formChangedDuringTransistion) {
			this.formChangedDuringTransistion = false;
			this.updateFromForm()
		}
		this.isBusy = false
	},
	makeProductUrl : function(b) {
		return "/index.php?main_page=" + b.mainPage + "&products_id=" + b.productId + ""
	},
	createDivs : function() {
		var b = $("results_container");
		this.allProducts.each(function(a) {
			var c = this.makeProductUrl(a);
			c = new Element("a", {
				href : c
			});
			var d = new Element("img", {
				id : "_product_" + a.productId,
				style : "position: absolute; display: none;",
				alt : a.name,
				title : ""
			});
			d.observe("mousemove", function(h) {
				this.showDetails(h, a)
			}.bind(this));
			d.observe("mouseout", function() {
				this.productDetailsTimer = setTimeout(function() {
					Element.hide("product_details")
				}, 2E2)
			}.bind(this));
			c.insert(d);
			b.insert(c)
		}.bind(this))
	},
	showDetails : function(b, a) {
		if (this.productDetailsTimer) {
			clearTimeout(this.productDetailsTimer);
			this.productDetailsTimer = null
		}
		var c = $("product_details"), d = "";
		if (this.imageSize === "search")
			c.hide();
		else {
			
			$("pdb_product_name").update(a.name);
			if (a.specialPrice !== a.normalPrice && a.specialPrice !== 0){
				
				$("pdb_normalprice").update(CURRENCY_SYMBOL + a.normalPrice);
				$("pdb_normalprice").removeClassName("price");
				$("pdb_normalprice").addClassName("normalprice");
				$("pdb_specialprice").update(CURRENCY_SYMBOL + a.specialPrice);
				$("pdb_specialprice").show();
			}else{
				$("pdb_normalprice").update(CURRENCY_SYMBOL + a.normalPrice);
				$("pdb_normalprice").removeClassName("normalprice");
				$("pdb_normalprice").addClassName("price");
				$("pdb_specialprice").hide();
			}

			a = b.pointer();
			if (!this.resultContainerOffset) {
				this.resultContainerOffset = $("results_container")
						.cumulativeOffset();
				this.resultContainerSize = $("results_container")
						.getDimensions()
			}
			b = a.x - this.resultContainerOffset.left + 3;
			d = c.getWidth();
			if (b + d > this.resultContainerSize.width)
				b = this.resultContainerSize.width
						- (this.resultContainerSize.width - b) - d - 3;
			a = a.y - this.resultContainerOffset.top + 3;
			c.style.left = b + "px";
			c.style.top = a + "px";
			c.show()
		}
	},
	saveState : function() {
		var b = String.interpret(document.location), a = $("controls_form")
				.serialize();
		b = b.gsub(/(\#.*)$/, "");
		if (a.length > 0) {
			b = b + "#" + a;
			document.location = b;
			document.cookie = "product_finder_local_state=" + a + "; path=/"
		}
	},
	readState : function() {
		var b = String.interpret(document.location);
		b = $A(b.match(/\#(.+)$/));
		var a;
		if (b.size() === 0)
			document.cookie.split("; ").each(function(d) {
				if (d.startsWith("product_finder_local_state")) {
					a = d.match(/^product_finder_local_state\=(.+)$/)[1];
					hide_help_bubble()
				}
			});
		else
			a = b[1];
		if (a) {
			var c = a.toQueryParams();
			if (c.recipients) {
				if (Object.isString(c.recipients))
					c.recipients = [ c.recipients ];
				$("controls_form").getInputs("radio", "recipients").each(
						function(d) {
							if (c.recipients.include(parseInt(d.value, 10))) {
								d.checked = true;
								$(d.id + "_display").addClassName("radio_on")
							}
						})
			}
			if (c.categories) {
				if (Object.isString(c.categories))
					c.categories = [ c.categories ];
				$("controls_form").getInputs("checkbox", "categories").each(
						function(d) {
							if (c.categories.include(parseInt(d.value, 10))) {
								d.checked = true;
								$(d.id + "_display").addClassName(
										"check_checked")
							}
						})
			}
			if (c.min_price && c.max_price) {
				c.min_price = parseFloat(c.min_price);
				c.max_price = parseFloat(c.max_price);
				if (c.min_price < c.max_price) {
					this.slider.setValue(price_to_slider(c.max_price), 1);
					this.slider.setValue(price_to_slider(c.min_price), 0)
				}
			}
		}
	},
	updateResults : function() {
		var b = this.selectedProducts.size(), a, c, d, h = 0, i = 0, l = 0, k = 0, m = 0, hpw = 4/3, u = document.viewport
				.getDimensions();
		this.resultContainerOffset = null;
		this.viewport = document.viewport.getScrollOffsets();
		this.viewport.bottom = this.viewport.top + u.height;
		this.viewport.right = this.viewport.left + u.width;
		this.morphEffects = [];
		this.appearEffects = [];
		this.fadeEffects = [];
		this.hideEffects = [];
		this.showEffects = [];
		this.productTextIds.each(function(e) {
			$(e).hide()
		});
		if (b < 49) {
			this.imageSize = "search";
			a = 180;
			c = 4;
			d = 8;
			m = 60
		} else if (b < 150) {
			this.imageSize = "category";
			a = 99;
			c = 7;
			d = 8
		} else {
			this.imageSize = "small";
			a = 48;
			c = 15;
			d = 2
		}
		
		this.selectedProducts.each(function(e) {
			var f = $("_product_" + e.productId), n = MEDIA_SERVER
					+ e[this.imageSize + "_image"], g = false, o = f
					.cumulativeOffset();
			
			i = k * a + (k + 1) * d;
			h = l * (a*hpw + d) + d;
			if (m > 0 && l !== 0)
				h += l * m;
			if (this.imageSize === "search") {
				var p = "product_text_" + e.productId, q = "";
				
				f.style.border = "1px solid #C4C4C4";
				$("pf_fade").hide();
				if (this.productTextIds.include(p))
					e = $(p);
				else {
					var j = new Element("div", {
						id : p,
						"class" : "product_finder_product_text",
						style : "display: none;"
					}), r = this.makeProductUrl(e), 
					v1 = new Element("span", {
						"class" : "prod_more_info"
					}),v2 = new Element("span", {
						"class" : "prod_title"
					});
					r = new Element("a", {
						href : r
					});
					v1.update("... more info");
					v2.update(e.name);
					//v = v1 + v2;
					
					
					r.insert(v1);
					r.insert(v2);
					
					if (e.specialPrice !== e.normalPrice && e.specialPrice !== 0 ){
						q1 = new Element("span", {
							"class" : "normalprice"
						});
						q2 = new Element("span", {
							"class" : "specialprice"
						});
						q2.update(CURRENCY_SYMBOL + e.specialPrice);
					}else{
						q1 = new Element("span", {
							"class" : "price"
						});
						q2 = "";
					}
					q1.update(CURRENCY_SYMBOL + e.normalPrice);
					
					j.style.width = a + "px";
					j.style.height = m + "px";
					j.insert(r);
					j.insert('<div class="clearElement"></div>');
					j.insert(q1);
					j.insert(q2);
					
					$("results_container").insert(j);
					e = j;
					this.productTextIds.push(p)
				}
				e.style.top = h + a*hpw + "px";
				e.style.left = i + "px";
				e.hide();
				this.appear(e)
			} else {
				f.style.border = "0px none";
				$("pf_fade").show();
			}
			if (f.visible() && o.top < this.viewport.bottom
					&& o.top >= this.viewport.top
					&& o.left < this.viewport.right
					&& o.left >= this.viewport.left)
				g = true;
			if (!g && h < this.viewport.bottom && h >= this.viewport.top
					&& i < this.viewport.right && i >= this.viewport.left)
				g = true;
			if (f.visible())
				if (g) {
					g = {};
					g.style = "width: " + a + "px; height: " + a*hpw + "px; top:"
							+ h + "px; left: " + i + "px;";
					if (f.src !== n) {
						g.newImageSrc = n;
						if (f.width > a)
							g.afterFinish = function(s) {
								s.element.src = this.newImageSrc
							};
						else
							g.afterSetup = function(s) {
								s.element.src = this.newImageSrc
							}
					}
					this.morph(f, g)
				} else {
					f.style.top = h + "px";
					f.style.left = i + "px";
					f.style.width = a + "px";
					f.style.height = a*hpw + "px";
					f.src = n;
					this.show(f)
				}
			else {
				f.style.top = h + "px";
				f.style.left = i + "px";
				f.style.width = a + "px";
				f.style.height = a*hpw + "px";
				if (f.src === "")
					this.show(f);
				else
					g ? this.appear(f) : this.show(f);	
				f.src = n
			}
			
			
			k++;
			if (k === c) {
				k = 0;
				l++
			}
			
		}.bind(this));
		lhi = h + (a*hpw) + m;
		$("results_container").style.height = lhi + "px";
		this.hideThese.each(function(e) {
			e = $("_product_" + e.productId);
			if (e.visible()) {
				var f = e.cumulativeOffset();
				f.top < this.viewport.bottom && f.top >= this.viewport.top
						&& f.left < this.viewport.right
						&& f.left >= this.viewport.left ? this.fade(e) : this
						.hide(e)
			}
		}.bind(this));
		this.cancelIdleEffects("pf_parallel");
		var t = [ this.fadeEffects, this.morphEffects, this.appearEffects ]
				.findAll(function(e) {
					return e.size() > 0
				});
		t.size() === 0 && this.idle();
		t.each(function(e) {
			new Effect.Parallel(e, {
				queue : {
					position : "end",
					scope : "pf_parallel",
					limit : 4
				},
				duration : 0.5,
				lastEffect : t.last(),
				thisEffect : e,
				giftFinder : this,
				afterFinish : function() {
					this.lastEffect === this.thisEffect
							&& this.giftFinder.idle()
				}
			})
		}.bind(this))
	},
	cancelIdleEffects : function(b) {
		Effect.Queues.get(b).each(function(a) {
			a.state === "idle" && a.cancel()
		})
	},
	defaultOptions : function() {
		return {
			duration : 0.5,
			sync : true
		}
	},
	show : function(b) {
		var a = this.defaultOptions(b);
		a.transition = Effect.Transitions.full;
		this.appearEffects.push(new Effect.Appear(b, a))
	},
	hide : function(b) {
		var a = this.defaultOptions(b);
		a.transition = Effect.Transitions.full;
		this.fadeEffects.push(new Effect.Fade(b, a))
	},
	appear : function(b) {
		var a = this.defaultOptions(b);
		this.appearEffects.push(new Effect.Appear(b, a))
	},
	fade : function(b) {
		var a = this.defaultOptions(b);
		this.fadeEffects.push(new Effect.Fade(b, a))
	},
	morph : function(b, a) {
		a = $H(a).merge($H(this.defaultOptions(b))).toObject();
		this.morphEffects.push(new Effect.Morph(b, a))
	},
	highlight : function(b) {
		var a = "queueName" + $(b).id;
		this.cancelIdleEffects(a);
		var c = this.defaultOptions(b);
		c.queue = {
			position : "end",
			scope : a,
			limit : 1
		};
		c.duration = 2;
		return new Effect.Highlight(b, c)
	},
	getPriceRange : function() {
		var b = parseFloat($F("min_price")), a = parseFloat($F("max_price"));
		return $R(b, a)
	},
	filterByPrice : function() {
		var b = this.getPriceRange();
		//alert('Claudiu Test' + b.toSource());
		
		this.selectedProducts = this.selectedProducts.findAll(function(a) {
			var c = a.normalPrice >= b.start && a.normalPrice <= b.end;
			//alert('Claudiu Test' + a.toSource());
			return a.specialPrice >= b.start && a.specialPrice <= b.end || c
		})
		
	},
	getSelectedCategories : function() {
		var b = $("controls_form").getInputs("checkbox", "categories").collect(
				function(a) {
					return $(a).getValue()
				}).compact();
		return b = b.collect(function(a) {
			return parseInt(a, 10)
		})
	},
	filterByCategories : function() {
		var b = this.getSelectedCategories();
		if (b.size() > 0)
			this.selectedProducts = this.selectedProducts.findAll(function(a) {
				return b.any(function(c) {
					return a.categories.include(c)
				})
			})
	},
	getSelectedRecipients : function() {
		var b = $("controls_form").getInputs("radio", "recipients").collect(
				function(a) {
					return $(a).getValue()
				}).compact();
		return b = b.collect(function(a) {
			return parseInt(a, 10)
		})
	},
	filterByRecepients : function() {
		var b = this.getSelectedRecipients();
		if (b.size() > 0) {
			var a = b[0];
			this.selectedProducts = this.selectedProducts.findAll(function(c) {
				return c.categories.include(a)
			})
		}
	},
	formChanged : function() {
		if (this.isBusy)
			this.formChangedDuringTransistion = true;
		else
			this.updateFromForm()
	},
	updateFromForm : function() {
		this.isBusy = true;
		this.working();
		$("product_count").update("Finding products...");
		this.saveState();
		if (this.productDetailsTimer) {
			clearTimeout(this.productDetailsTimer);
			this.productDetailsTimer = null
		}
		$("product_details").hide();
		this.selectedProducts = this.allProducts;
		this.filterByPrice();
		this.filterByRecepients();
		var b = $H();
		this.selectedProducts.each(function(a) {
			a.categories.each(function(c) {
				b.get(c) ? b.set(c, b.get(c) + 1) : b.set(c, 1)
			})
		});
		$("pf_cats").select("a").each(function(a) {
			a.hide()
		});
		b.each(function(a) {
			var c = $("category_count_" + a.key);
			if (c) {
				$("pf_category_" + a.key + "_link").show();
				c.update("(" + a.value + ")")
			}
		});
		this.filterByCategories();
		this.hideThese = this.allProducts.findAll(function(a) {
			return !this.selectedProducts.include(a)
		}.bind(this));
		$("product_count").update(
				"Found " + this.selectedProducts.size() + " Products!");
		this.selectedProducts.size() > 0 && $("no_products_found").hide();
		this.updateResults()
	}
};
