var Gallery = new Class({
	initialize : function(e, g, c, f, d, a, b) {
		this.preview = $(e);
		this.number = $(g);
		this.title = $(c);
		this.container = $(f);
		this.scroller = $(d);
		this.scroller_id = d;
		this.back_link = $(a);
		this.forward_link = $(b);
		this.step = 35;
		this.timer = null;
		this.timeout = 10;
		this.setActionHandlers();
		this.initScroller()
	}
});
Gallery.implement({
	initScroller : function() {
		var a = this;
		var b = a.container.getCoordinates();
		new Slider("scroll-ctrl-cont", "scroll-ctrl", {
			range : [ 0, 1000 ],
			onChange : function(c) {
				var d = parseInt((a.getScrollerSize() - b.width) / 1000 * c);
				a.scroller.set("styles", {
					left : -d
				})
			}
		})
	},
	setActionHandlers : function() {
		var a = this;
		$$("#" + a.scroller_id + " a").each(function(b, c) {
			b.addEvent("click", function(g) {
				new Event(g).stop();
				a.number.set("html", c + 1);
				a.title.set("html", b.title);
				a.title.href = b.href;
				a.preview.href = b.href;
				var f = a.preview.getElement("img");
				var d = new Fx.Morph(f);
				d.set({
					opacity : 0
				});
				new Asset.image(b.rel, {
					onload : function() {
						f.src = b.rel;
						d.start({
							opacity : [ 0, 1 ]
						})
					}
				})
			})
		});
		a.back_link.addEvent("click", function(b) {
			new Event(b).stop()
		});
		a.forward_link.addEvent("click", function(b) {
			new Event(b).stop()
		});
		a.back_link.addEvent("mousedown", function(b) {
			a.start(a.step)
		});
		a.back_link.addEvent("mouseup", function(b) {
			a.stop()
		});
		a.back_link.addEvent("mouseout", function(b) {
			a.stop()
		});
		a.forward_link.addEvent("mousedown", function() {
			a.start(-a.step)
		});
		a.forward_link.addEvent("mouseup", function() {
			a.stop()
		});
		a.forward_link.addEvent("mouseout", function() {
			a.stop()
		})
	},
	stop : function() {
		if (this.timer) {
			clearInterval(this.timer)
		}
	},
	start : function(a) {
		var b = this;
		b.timer = setInterval(function() {
			var f = b.scroller.getCoordinates();
			var e = b.container.getCoordinates();
			var d = f.left - e.left + a;
			if (d > 0) {
				d = 0
			} else {
				var c = -(b.getScrollerSize() - e.width);
				if (d < c) {
					d = c
				}
			}
			b.scroller.set("styles", {
				left : d
			})
		}, b.timeout)
	},
	getScrollerSize : function() {
		var a = 0;
		var b = this;
		this.scroller.getChildren().each(function(c) {
			var d = c.getCoordinates();
			a += d.width
		});
		return a
	}
});
