if (typeof (AC) == "undefined") {
	AC = {}
}
AC.ProductBrowser = 
{
	productSlider : null, sliderVal : 0, animationId : false, viewportWidth : 946, contentWidth : 946, 
	categories : [ {
		id : "pb-cat1", offset : 0
	},
	{
		id : "pb-cat2", offset : 0.32
	}], isIPhone : AC.Detector.isiPhone(), iPhoneCategories : [ {
		id : "pb-cat1", offset : 0
	},
	{
		id : "pb-cat2", offset : 0.32
	}], arrowScrollAmount : 0.24, iPhoneScrollAmount : 0.22, iPhoneContainerWidth : 834, isSliding : false, 
	lastX : 0.32, isMouseDown : false, dif : 0, overlap : 0, offsetImageWidth : 127, sliderOffset : 291, 
	offsetContentWidth :- 946, clicked : false, startIndex : 0, isIpScroll : false, hasIpDragged : false, 
	init : function (g)
	{
		if (typeof (g.categories) != "undefined") {
			this.categories = g.categories
		}
		if (typeof (g.imageOverlap) != "undefined") {
			this.overlap = g.imageOverlap
		}
		if (typeof (g.sliderCentering) != "undefined") {
			this.sliderOffset = g.sliderCentering
		}
		if (typeof (g.initialCategory) != "undefined") {
			this.startIndex = g.initialCategory
		}
		if (typeof (g.arrowScrollAmount) != "undefined") {
			this.arrowScrollAmount = g.arrowScrollAmount
		}
		if (typeof (g.iPhoneCategories) != "undefined") {
			this.iPhoneCategories = g.iPhoneCategories
		}
		if (typeof (g.iPhoneScrollAmount) != "undefined") {
			this.iPhoneScrollAmount = g.iPhoneScrollAmount
		}
		if (typeof (g.iPhoneContainerWidth) != "undefined") {
			this.iPhoneContainerWidth = g.iPhoneContainerWidth
		}
		$("pb-productslidertrack").style.visibility = "visible";
		$("pb-productbrowsercontainer").style.overflow = "hidden";
		this.viewportWidth = $("pb-productbrowsercontainer").getWidth();
		this.offsetImageWidth = $$("#pb-productslider .pb-productimage")[0].getWidth() - this.overlap;
		this.contentWidth = this.offsetImageWidth * $$("#pb-productslider .pb-productimage").length;
		this.offsetContentWidth =- 1 * (this.contentWidth - this.viewportWidth);
		this.productSlider = new Control.Slider("pb-productsliderhandle", "pb-productslidertrack", {
			axis : "horizontal"
		});
		if (AC.ProductBrowser.isIPhone)
		{
			this.categories = this.iPhoneCategories;
			this.arrowScrollAmount = this.iPhoneScrollAmount;
			$("pb-productslidertrack").style.visibility = "hidden";
			$("pb-leftarrow").style.visibility = "hidden";
			$("pb-rightarrow").style.visibility = "hidden";
			$("pb-productbrowsercontainer").style.width = this.iPhoneContainerWidth + "px";
			var f = document.createElement("div");
			f.id = "pb-iphone-leftarrow";
			var b = document.createElement("div");
			b.id = "pb-iphone-rightarrow";
			$("productbrowser").appendChild(f);
			$("productbrowser").appendChild(b);
			Event.observe($(f), "click", function ()
			{
				AC.ProductBrowser.left()
			});
			Event.observe($(b), "click", function ()
			{
				AC.ProductBrowser.right()
			})
		}
		AC.ProductBrowser.animateSlide(this.categories[this.startIndex].offset);
		this.productSlider.options.onChange = function (i)
		{
			$("pb-productsliderhandleimage").style.left = $("pb-productsliderhandle").style.left;
			if (AC.ProductBrowser.isThrow && !AC.ProductBrowser.isSliding)
			{
				AC.ProductBrowser.isSliding = true;
				AC.ProductBrowser.isThrow = false;
				var h = i + AC.ProductBrowser.throwMod;
				if (h < 0) {
					h = 0
				}
				if (h > 1) {
					h = 1
				}
				AC.ProductBrowser.animateSlide(h)
			}
			else
			{
				if (!AC.ProductBrowser.isSliding && i) {
					AC.ProductBrowser.isSliding = true;
					AC.ProductBrowser.animateSlide(i)
				}
			}
		};
		this.productSlider.options.onSlide = function (i)
		{
			$("pb-productsliderhandleimage").style.left = $("pb-productsliderhandle").style.left;
			if (i && !AC.ProductBrowser.isSliding)
			{
				AC.ProductBrowser.isSliding = true;
				AC.ProductBrowser.isThrow = false;
				if (AC.ProductBrowser.isMouseDown)
				{
					AC.ProductBrowser.dif = i - AC.ProductBrowser.lastX;
					AC.ProductBrowser.lastX = i;
					if (AC.ProductBrowser.dif > 0.05) {
						AC.ProductBrowser.isThrow = true;
						AC.ProductBrowser.throwMod = 0.2
					}
					else
					{
						if (AC.ProductBrowser.dif <- 0.04) {
							AC.ProductBrowser.isThrow = true;
							AC.ProductBrowser.throwMod =- 0.2
						}
					}
				}
				var h = AC.ProductBrowser.offsetContentWidth;
				$("pb-productslider").style.left = h * i + "px";
				this.sliderVal = i;
				AC.ProductBrowser.lastX = i;
				AC.ProductBrowser.colorCats();
				AC.ProductBrowser.isSliding = false
			}
			Element.setStyle($("pb-productbrowsercontainer"), {
				overflow : "hidden"
			})
		};
		var a = "mousedown", e = "mouseup";
		if (AC.Detector.isMobile() || AC.Detector.isiPad())
		{
			a = "touchstart";
			e = "touchend";
			var d = {
				x : 0, y : 0
			},
			c = {
				x : 0, y : 0
			};
			Event.observe("pb-productslider", "touchstart", function (h)
			{
				d.x = event.targetTouches[0].pageX;
				d.y = event.targetTouches[0].pageY;
			});
			Event.observe("pb-productslider", "touchmove", function (h)
			{
				Event.stop(h);
				c.x = event.targetTouches[0].pageX;
				c.y = event.targetTouches[0].pageY;
			});
			Event.observe("pb-productslider", "touchend", function (h)
			{
				var i = d.y - c.y;
				if (i < 70 && i > -70)
				{
					changeX = d.x - c.x;
					if (changeX > 30) {
						AC.ProductBrowser.right()
					}
					if (changeX <- 30) {
						AC.ProductBrowser.left()
					}
				}
			})
		}
		Event.observe("pb-productslidertrack", a, function (i)
		{
			var j = (AC.Detector.isMobile() || AC.Detector.isiPad()) ? i.touches[0].pageX : i.offsetX || i.layerX, 
			h = Event.element(i).id || i.target.id;
			if (h == "pb-productslidertrack" && j < 100) {
				AC.ProductBrowser.animateSlide(0)
			}
		});
		Event.observe("pb-leftarrow", a, function ()
		{
			AC.ProductBrowser.left()
		});
		Event.observe("pb-rightarrow", a, function ()
		{
			AC.ProductBrowser.right()
		});
		Event.observe("pb-productsliderhandle", a, function ()
		{
			AC.ProductBrowser.isMouseDown = true;
			$("pb-productsliderhandle").style.zIndex = "5";
		});
		Event.observe("pb-productsliderhandle", e, function ()
		{
			AC.ProductBrowser.isMouseDown = false;
		});
		AC.ProductBrowser.categories.each(function (h)
		{
			Event.observe($(h.id), e, function (i)
			{
				AC.ProductBrowser.animateSlide(h.offset)
			})
		})
	},
	animateSlide : function (d)
	{
		if (d > 1) {
			d = 1
		}
		if (d < 0) {
			d = 0
		}
		AC.ProductBrowser.sliderVal = d;
		window.clearInterval(AC.ProductBrowser.animationId);
		var a = AC.ProductBrowser.offsetContentWidth;
		var c = a * d;
		var b = (Math.round(AC.ProductBrowser.viewportWidth - AC.ProductBrowser.sliderOffset) * d);
		AC.ProductBrowser.isSliding = true;
		AC.ProductBrowser.animationId = window.setInterval(function ()
		{
			var g = parseInt($("pb-productslider").getStyle("left")) || 0;
			var f = parseInt($("pb-productsliderhandle").getStyle("left")) || 0;
			var e = AC.ProductBrowser.calculateDecel(g, c);
			var h = AC.ProductBrowser.calculateDecel(f, b);
			$("pb-productslider").style.left = e + "px";
			$("pb-productsliderhandle").style.left = h + "px";
			$("pb-productsliderhandleimage").style.left = h + "px";
			AC.ProductBrowser.colorCats();
			if (e == c)
			{
				window.clearInterval(AC.ProductBrowser.animationId);
				AC.ProductBrowser.isSliding = false;
			}
		}, 30)
	},
	colorCats : function ()
	{
		var a = parseInt($("pb-productsliderhandle").getStyle("left")) + (($("pb-productsliderhandle").getWidth() - 20) / 2);
		AC.ProductBrowser.categories.each(function (e)
		{
			var d = parseInt($(e.id).getStyle("left"));
			var b = Math.ceil((Math.min(a, d) / Math.max(a, d)) * 10);
			$(e.id).className = "pb-catclass" + b;
		})
	},
	left : function ()
	{
		AC.ProductBrowser.animateSlide(AC.ProductBrowser.sliderVal - AC.ProductBrowser.arrowScrollAmount)
	},
	right : function ()
	{
		AC.ProductBrowser.animateSlide(AC.ProductBrowser.sliderVal + AC.ProductBrowser.arrowScrollAmount)
	},
	calculateDecel : function (c, b)
	{
		var a = c - Math.floor((c - b) * 0.4);
		if (Math.abs(c - b) < 4) {
			return b
		}
		else {
			return a;
		}
	}
};
Control.Slider.prototype.getEventStr = function (a)
{
	return this.mobile ? this.mobileEvents[a] : a;
};
Control.Slider.prototype.initialize = function (d, a, b)
{
	var c = this;
	if (Object.isArray(d)) {
		this.handles = d.collect(function (f)
		{
			return $(f);
		})
	}
	else {
		this.handles = [$(d)]
	}
	this.track = $(a);
	this.options = b || {};
	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 (e)
	{
		return $(e);
	}) : 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);
	if (this.mobile = ((AC.Detector.isMobile() || AC.Detector.isiPad()) ? true : false))
	{
		this.mobileEvents = {
			mousedown : "touchstart", mousemove : "touchmove", mouseup : "touchend"
		}
	}
	this.handles.each(function (f, e)
	{
		e = c.handles.length - 1 - e;
		c.setValue(parseFloat((Object.isArray(c.options.sliderValue) ? c.options.sliderValue[e] : c.options.sliderValue) || c.range.start), 
		e);
		f.makePositioned().observe(c.getEventStr("mousedown"), c.eventMouseDown)
	});
	this.track.observe(this.getEventStr("mousedown"), this.eventMouseDown);
	document.observe(this.getEventStr("mouseup"), this.eventMouseUp);
	document.observe(this.getEventStr("mousemove"), this.eventMouseMove);
	this.initialized = true;
};
Control.Slider.prototype.dispose = function ()
{
	var a = this;
	Event.stopObserving(this.track, this.getEventStr("mousedown"), this.eventMouseDown);
	Event.stopObserving(document, this.getEventStr("mouseup"), this.eventMouseUp);
	Event.stopObserving(document, this.getEventStr("mousemove"), this.eventMouseMove);
	this.handles.each(function (b)
	{
		Event.stopObserving(b, this.getEventStr("mousedown"), a.eventMouseDown)
	})
};
Control.Slider.prototype.startDrag = function (c)
{
	if (Event.isLeftClick(c) || this.mobile)
	{
		if (!this.disabled)
		{
			this.active = true;
			var d = Event.element(c);
			var e = (this.mobile) ? [c.touches[0].pageX, c.touches[0].pageY] : [Event.pointerX(c), Event.pointerY(c)];
			var a = d;
			if (a == this.track)
			{
				var b = Position.cumulativeOffset(this.track);
				this.event = c;
				this.setValue(this.translateToValue((this.isVertical() ? e[1] - b[1] : e[0] - b[0]) - (this.handleLength / 2)));
				var b = Position.cumulativeOffset(this.activeHandle);
				this.offsetX = (e[0] - b[0]);
				this.offsetY = (e[1] - b[1])
			}
			else
			{
				while ((this.handles.indexOf(d) ==- 1) && d.parentNode) {
					d = d.parentNode
				}
				if (this.handles.indexOf(d) !=- 1)
				{
					this.activeHandle = d;
					this.activeHandleIdx = this.handles.indexOf(this.activeHandle);
					this.updateStyles();
					var b = Position.cumulativeOffset(this.activeHandle);
					this.offsetX = (e[0] - b[0]);
					this.offsetY = (e[1] - b[1])
				}
			}
		}
		Event.stop(c)
	}
};
Control.Slider.prototype.draw = function (b)
{
	var c = (this.mobile) ? [b.touches[0].pageX, b.touches[0].pageY] : [Event.pointerX(b), Event.pointerY(b)];
	var a = Position.cumulativeOffset(this.track);
	c[0] -= this.offsetX + a[0];
	c[1] -= this.offsetY + a[1];
	this.event = b;
	this.setValue(this.translateToValue(this.isVertical() ? c[1] : c[0]));
	if (this.initialized && this.options.onSlide) {
		this.options.onSlide(this.values.length > 1 ? this.values : this.value, this)
	}
};

