if (typeof Prototype != "undefined") {
	document.observe("dom:loaded", function() {
		function FeaturedProductsPaginator(elemContainer)
		{
			this.state = -1;
			
			if (elemContainer) {
				this.container = $(elemContainer);
				
				this.products = this.container.select(".product");
				//alert(this.products.length);
				
				if (this.products.length > 1) {
					this.lastIndex = -1;
					this.currentIndex = 0;
					
					// In case of CSS-fail
					for (var i = 0; i < this.products.length; i++) {
						if (i == this.currentIndex) {
							this.products[i].style.display = "block";
						} else {
							this.products[i].style.display = "none";
						}
					}
					
					this.state = 0;
					
					this.createNavigation();
					
					this.setUpAutoChange();
				}
			}
		}
		FeaturedProductsPaginator.prototype.animate = function() {
			var animateAgain = true;
			var animationDuration = 0.4;
			var currentItem;
			var lastItem;
			var paginator = this;
			var progress;
			
			// Calculate and cap progress
			progress = (this.getNowAsDecimalSeconds() - this.timeStart) / animationDuration;
			if (progress >= 1.0) {
				progress = 1.0;
				animateAgain = false;
			}
			
			// Animate
			currentItem = this.products[this.currentIndex];
			lastItem = this.products[this.lastIndex];
			
			var shiftDirection = -1;
			if (this.lastIndex == this.products.length - 1 && this.currentIndex == 0) {
				shiftDirection = 1;
			} else {
				if (this.lastIndex == 0 && this.currentIndex == this.products.length - 1) {
					// No change
				} else {
					shiftDirection = (this.currentIndex > this.lastIndex) ? 1 : -1;
				}
			}
			
			//if (this.currentIndex > this.lastIndex || (this.lastIndex == this.products.length - 1))
			if (shiftDirection > 0) {
				// Shift left
				currentItem.style.left = (100 - 100*progress) + "%";
				lastItem.style.left = (-100 * progress) + "%";
			} else {
				currentItem.style.left = (-100 + 100*progress) + "%";
				lastItem.style.left = (100 * progress) + "%";
			}
			
			// Animate again, or mark that the paginator is idle
			if (animateAgain) {
				setTimeout(function() {
					paginator.animate();
				}, 25);
			} else {
				this.state = 0;
				
				this.products[this.lastIndex].style.display = "none";
				//this.products[this.currentIndex].style.display = "block";
			}
		};
		FeaturedProductsPaginator.prototype.createElementFancy = function(tagName, objAttributes, arrChildren) {
			var elem = $(document.createElement(tagName));
			var key;
			var i, il;
			
			if (elem && objAttributes) {
				for (key in objAttributes) {
					// BECAUSE IE IS SUPER DUMB >:O
					if (key == "className") {
						elem.className = objAttributes[key];
					} else {
						// I CAN'T JUST DO THIS
						elem.setAttribute(key, objAttributes[key]);
					}
				}
			}
			
			if (elem && arrChildren) {
				//alert("Appending " + arrChildren.length + " children to " + elem);
				for (i = 0, il = arrChildren.length; i < il; i++) {
					//alert("Appending " + arrChildren[i] + " to " + elem);
					elem.appendChild(arrChildren[i]);
				}
			}
			
			return elem;
		};
		FeaturedProductsPaginator.prototype.createNavigation = function() {
			var next;
			var previous;
			var paginator;
			
			paginator = this;
			
			/*
			next = this.createElementFancy("a", {className: "next"}, [
				document.createTextNode("next <")
			]);
			if (next) {
				this.container.appendChild(next);
				next.observe("click", function() {
					paginator.goNext();
				});
			}
			
			previous = this.createElementFancy("a", {className: "previous"}, [
				document.createTextNode("> prev")
			]);
			if (previous) {
				this.container.appendChild(previous);
				previous.observe("click", function() {
					paginator.goPrevious();
				});
			}
			*/
			
			// Dots
			var header = $(this.container.parentNode).down("h1");
			var dotSpan;
			for (var i = 0; i < this.products.length; i++) {
				var dotClass = (i == this.currentIndex) ? "controlDot current" : "controlDot";
				dotSpan = this.createElementFancy("span", {className: dotClass}, [
					document.createTextNode("•")
				]);
				header.appendChild(dotSpan);
				dotSpan.clickIndex = i;
				dotSpan.observe("click", function() {
					paginator.go(this.clickIndex);
					
					/*
					var clickedDot = this;
					$(this.parentNode).select(".controlDot").each(function(item) {
						if (item == clickedDot) {
							item.addClassName("current");
						} else {
							item.removeClassName("current");
						}
					});
					*/
				});
			}
		};
		FeaturedProductsPaginator.prototype.getNowAsDecimalSeconds = function() {
			return (new Date()).getTime() / 1000;
		};
		FeaturedProductsPaginator.prototype.go = function(newIndex) {
			var paginator = this;
			
			newIndex = newIndex ? (newIndex * 1) : 0;
			
			//alert("go(" + newIndex + ")");
			
			// Ignore go() calls when busy animating
			if (this.state == 0 && newIndex != this.currentIndex) {
				// Update the animation indexes
				this.lastIndex = this.currentIndex;
				this.currentIndex = newIndex;
				
				//this.products[this.lastIndex].style.display = "block";
				this.products[this.currentIndex].style.display = "block";
				
				// Set the correct dot
				var dots = $(this.container.parentNode).select(".controlDot");
				for (var i = 0; i < dots.length; i++) {
					if (this.currentIndex == i) {
						dots[i].addClassName("current");
					} else {
						dots[i].removeClassName("current");
					}
				}
				
				// Start the animation
				this.state++;
				this.timeStart = this.getNowAsDecimalSeconds();
				this.animate();
			}
		};
		FeaturedProductsPaginator.prototype.goNext = function() {
			var nextIndex = (this.currentIndex + 1) % this.products.length;
			
			this.go(nextIndex);
		};
		FeaturedProductsPaginator.prototype.goPrevious = function() {
			var nextIndex = this.currentIndex - 1;
			
			if (nextIndex < 0) {
				nextIndex = this.products.length - 1;
			}
			
			this.go(nextIndex);
		};
		FeaturedProductsPaginator.prototype.setUpAutoChange = function() {
			var paginator = this;
			
			//this.autoChange = true;
			
			this.autoChangeIntervalHandler = function() {
				//if (paginator.autoChange) {
					paginator.goNext();
				//}
			};
			
			$(this.container.parentNode).observe("mouseover", function() {
				//paginator.autoChange = false;
				clearInterval(paginator.autoChangeIntervalHandle);
				paginator.autoChangeIntervalHandle = false;
			});
			
			$(this.container.parentNode).observe("mouseout", function() {
				//paginator.autoChange = true;
				if (!paginator.autoChangeIntervalHandle) {
					paginator.autoChangeIntervalHandle = setInterval(paginator.autoChangeIntervalHandler, 6000);
				}
			});
			
			this.autoChangeIntervalHandle = setInterval(this.autoChangeIntervalHandler, 6000);
		};
		
		$$(".inner.featured_products").each(function(item) {
			item.paginator = new FeaturedProductsPaginator(item);
		});
		
	});
}
