dojo.require("dojo.back");
dojo.require("dojo.fx");
dojo.require("dojox.fx");

// Globals
var PHOTO_DIR = "photos";
var SLIDESHOW_TIME_TO_SHOW = 3000;
var NICE_ROLLOVER_OPACITY = 0.9;
var THUMB_ROLLOVER_OPACITY = 0.7;
var SPINNER_OPACITY = 0.5;
var IS_DEV = location.hostname.indexOf("bychristina.com") == -1;
var ONLOAD_HISTORY = false || IS_DEV; // set to false to prevent bookmarking (for production)

// Stuff
var Scenes = null, TheShow = null, Tree = null, Tracker = null;
var RunFirst = new dojo.Deferred();

function ID(id) { return dojo.byId(id); }

dojo.addOnLoad(function() {
	Tracker = new dojox.analytics.Urchin({ acct:"UA-4308761-2" });
});

RunFirst.addCallback(function() {
	dojo.back.init(); // back button support

	// preload useful images
	new Imager("addons/img/blank.gif");
	new Imager("addons/img/loading.gif");
	new Imager("addons/img/loader-box.png");
	new Imager("addons/img/nothumb40.gif");
});

RunFirst.addCallback(function init() {
	if(dojo.isIE && dojo.isIE < 7) {
		dojo.query("#ThumbBox, #Viewer, body").addClass("ie6");
	}

	Scenes = new SceneSelector("X");

	var splash = ID("SplashImg");
	if(splash) {
		new Imager(splash.src).onload(function(e) {
			splash.style.visibility = "visible";
		});
	}

	var t = TreeNode.types;
	Tree = new TreeNode("Home");
	Tree.add("Contact").down()
			.add("Pricelist").up()
		.add("Portraits2", t.GALLERY)
		.add("PR", t.GALLERY)
		.add("Weddings").down()
			.add("Dianne&Paul", t.GALLERY).add("Emily&Jason", t.GALLERY)
			.add("Hannah&Nathan", t.GALLERY).add("Julie&Clark", t.GALLERY)
			.add("Miriam&Mark", t.GALLERY).add("Sampler", t.GALLERY)
			.add("Stephanie&Ian", t.GALLERY).add("Trisha&Yemmi", t.GALLERY);

	dojo.query("a[href*=#], area[href*=#]").onclick(function(e) {
		e.preventDefault();
		var name = this.href.split('#')[1];
		var node = null;

		if(name == "go-back") {
			name = Scenes._current.name == "Gallery" ? Scenes._current.data : Scenes._current.name;
			node = Tree.find(name).parent;
			name = node.name;
		} else {
			node = Tree.find(name);
		}

		if(node) {
			if(node.type == t.PAGE) {
				launchPage(name);
				var url = name == "Home" ? "index.html" : name;
				googleTracker(url);
			} else {
				launchGallery(name);
				googleTracker("gallery/" + name);
			}
		} else {
			throw new Error("Cannot find page: '" + name + "'");
		}
	});

	dojo.query(".galleryItem").forEach("niceRollover(item)");

	Scroller.init();

	dojo.setSelectable(dojo.body(), false);
});

function googleTracker(url) {
	if(typeof Tracker != "undefined") {
		Tracker.trackPageView(url);
	}
	//console.log("Tracked view:", url);
}

RunFirst.addCallback(function initHistory() {
	var h = location.hash;
	if(!h) { h = "Home"; } else { h = unescape(h.substring(1)); }
	var name = h, data = null, ctor = AppState, setScene = false;

	switch(h) {
		case "Home":
			break;
		case "Galleries":
		case "Contact":
		case "Pricelist":
		case "Weddings":
			setScene = true;
			break;
		default: // assume it's a gallery
			name = "Gallery";
			data = h;
			ctor = GalleryAppState;
			setScene = true;
			break;

	}
	dojo.back.setInitialState(new ctor(name, data));

	if(ONLOAD_HISTORY && setScene) {
		dojo.addOnLoad(function() {
			Scenes.select(name, data);
		});
	}
});

function centerGallery(wh) {
	centerIt(wh, null, { y : -30 });
}

function playPause(node) {
	TheShow.playPause();
	node.blur();
}

function launchPage(name) {
	dojo.back.addToHistory(new AppState(name));
	Scenes.select(name);
}

function launchGallery(name) {
	dojo.back.addToHistory(new GalleryAppState(name));
	Scenes.select("Gallery", name);
}

dojo.subscribe("SceneSelector_select", function(e) {
	var type = e.type, scene = e.scene, data = e.data;
	switch(scene) {
		case "Home":
			switch(type) {
				case "beforeLoad":
					centerIt("Splash");
					break;
			}
			break;
		case "Contact":
			switch(type) {
				case "beforeLoad":
					centerIt("ContactMe");
					break;
			}
			break;
		case "Pricelist":
			switch(type) {
				case "beforeLoad":
					centerIt("Prices");
					break;
			}
			break;
		case "Galleries":
			switch(type) {
				case "beforeLoad":
					centerGallery("Pics");
					break;
			}
			break;
		case "Weddings":
			switch(type) {
				case "beforeLoad":
					centerGallery("WeddingPicker");
					break;
			}
			break;
		case "Gallery":
			switch(type) {
				case "beforeLoad":
					TheShow = Slideshow.get(data);
					dojo.query(".thumbStrip", "ThumbBox").forEach("item.style.display = 'none'");
					break;
				case "afterLoad":
					TheShow.load();
					break;
				case "beforeUnload":
					if(TheShow) { TheShow.reset(); }
					break;
			}
			break;
	}
});

dojo.subscribe("SceneSelector_resize", function(e) {
	switch(e.name) {
		case "Home":
			centerIt("Splash");
			break;
		case "Contact":
			centerIt("ContactMe");
			break;
		case "Pricelist":
			centerIt("Prices");
			break;
		case "Galleries":
			centerGallery("Pics");
			break;
		case "Weddings":
			centerGallery("WeddingPicker");
			break;
		case "Gallery":
			break;
	}
});

function niceRollover(node, opac) {
	node = ID(node);
	opac = opac || NICE_ROLLOVER_OPACITY;
	var dur = 200;
	var anim;

	dojo.style(node, "opacity", opac);
	dojo.attr(node, "niceRollover", "enabled");

	dojo.connect(node, "onmouseover", function() {
		if(anim) { anim.stop(); }
		if(dojo.attr(node, "niceRollover") == "enabled" && !dojo.hasClass(node, "disabled")) {
			anim = dojo.fadeIn({node: this, duration: dur}).play();
		}
	});

	dojo.connect(node, "onmouseout", function() {
		if(anim) { anim.stop(); }
		if(dojo.attr(node, "niceRollover") == "enabled" && !dojo.hasClass(node, "disabled")) {
			anim = dojo.fadeOut({node: this, duration: dur, end: opac}).play();
		}
	});
}

function centerIt(node, container, options) {
	node = dojo.byId(node);
	container = dojo.byId(container);
	if(!container) { container = node.parentNode; }

	var opts = dojo.mixin({x:0, y:0}, options), s = {};

	if(!node || !container) { return; }

	var B = dojo.coords(container);
	var b = dojo.coords(node);
	var W = B.w, H = B.h, w = b.w, h = b.h;
	if(!W && !H) { return; }

	var left = ((W-w)/2 + opts.x),
		top  = ((H-h)/2 + opts.y);

	if(!opts.notX) { s.left = left + "px"; }
	if(!opts.notY) { s.top = top + "px"; }
	dojo.style(node, s);
}

/* Classes, etc...
***************************************/

function ScrollWheel(node) {
	if(arguments.length == 0) { node = window; }
	node = dojo.byId(node);

	this._scroll = function(e) {
		var dir = e.detail * -1 || e.wheelDelta;
		e.wheel = dir < 0 ? "down" : dir > 0 ? "up" : dir;

		switch(dir) {
			case "up":
				this._fire("onWheelUp", e);
				break;
			case "down":
				this._fire("onWheelDown", e);
				break;
		}
		this._fire("onMouseWheel", e);
	}

	var a = dojo.connect(node, "DOMMouseScroll", this, "_scroll");
	var b = dojo.connect(node, "onmousewheel", this, "_scroll");

	this._fire = function(name, e) {
		if(typeof this[name] == "function") {
			this[name].apply(node, [e]);
		}
	}

	this.destroy = function() {
		dojo.disconnect(a);
		dojo.disconnect(b);
	}
}

dojo.declare("TreeNode", null, {
	constructor: function(name, type, parent) {
		this.name = name;
		this.type = type || TreeNode.types.PAGE;
		this.parent = parent;
		this.children = {};
	},

	add: function(name, type) {
		var child = new window[this.declaredClass](name, type, this);
		this.children[name] = child;
		this._lastChild = name;
		return this;
	},

	down: function(name) {
		name = name||this._lastChild;
		if(name) {
			return this.children[name];
		} else {
			throw new Error("No child named '" + name + "'");
		}
	},

	up: function() {
		if(this.parent) {
			return this.parent;
		} else {
			throw new Error("No parent for '" + this.name + "'");
		}
	},

	toObject: function() {
		var obj = { name: this.name, children: {} };
		for(var child in this.children) {
			obj.children[child] = this.children[child].toObject();
		}
		return obj;
	},

	find: function(name) {
		if(this.name == name) {
			return this;
		} else {
			for(var child in this.children) {
				var node = this.children[child].find(name);
				if(node) { return node; }
			}
		}
		return null;
	}
});
TreeNode.types = {
	PAGE : 1,
	GALLERY : 2
};

dojo.declare("Imager", null, {
	_src: null,
	_img: null,
	_dfr: null,
	ready: false,

	constructor: function(url, delay) {
		this._dfr = new dojo.Deferred();
		this._src = url;
		this._img = new Image();
		dojo.connect(this._img, "onload", this, "_load");
		dojo.connect(this._img, "onerror", this, "_error");

		if(delay) {
			setTimeout(dojo.hitch(this, function() {
				this._img.src = this._src;
			}), delay);
		} else {
			this._img.src = this._src;
		}
	},

	_load: function(e) {
		this._dfr.callback(dojo.mixin({img: this._img}, e));
		this.ready = true;
	},

	_error: function(e) {
		this._dfr.errback(dojo.mixin({img: this._img}, e));
		this.ready = false;
	},

	onload: function(a, b) {
		if(arguments.length == 1) {
			this._dfr.addCallback(function(r){ a(arguments); return r; });
		} else {
			this._dfr.addCallback(function(r){ b.apply(a, arguments); return r; });
		}
		return this;
	},

	img: function() {
		return this._img;
	}
});

dojo.declare("AppState", null, {
	constructor: function(name, data) {
		this.scene = this.changeUrl = name;
		this.data = data;
	},

	handle: function(kind) {
		Scenes.select(this.scene, this.data);
	}
});

dojo.declare("GalleryAppState", [AppState], {
	constructor: function(name) {
		AppState.apply(this, ["Gallery", name]);
		this.changeUrl = name;
	}
});

dojo.declare("SceneSelector", null, {
	constructor: function(box) {
		this._box = ID(box);
		dojo.connect(window, "onresize", this, "_resize");
		this._resize();
	},

	_current: null,

	_sceneChangeDuration: 350,

	select: function(name, data) {
		var scene = ID(name);
		var currentScene = dojo.query(".scene.current", this._box)[0];
		if(currentScene != scene || (this._current && this._current.name == name && this._current.data != data)) {
			var pubevt = { scene: name, data: data };
			if(this._current) {
				dojo.publish("SceneSelector_select",
					[{type:"beforeUnload", scene:this._current.name, data:this._current.data}]);
			}
			this._current = { name:name, data:data };
			dojo.style(scene, "opacity", 0);
			dojo.addClass(scene, "current");
			var anim = dojox.fx.crossFade({
				nodes: [currentScene, scene],
				duration: this._sceneChangeDuration
			});
			anim.onEnd = function() {
				try {
					dojo.removeClass(currentScene, "current");
					dojo.publish("SceneSelector_select", [dojo.mixin({type:"afterLoad"}, pubevt)]);
				} catch(E) {
					console.error("select onEnd error", E);
				}
			};
			dojo.publish("SceneSelector_select", [dojo.mixin({type:"beforeLoad"}, pubevt)]);
			anim.play();
		}
	},

	getName: function() {
		var scene = dojo.query(".scene.current", this._box)[0];
		return scene.id;
	},

	_resize: function(e) {
		var name = this.getName();
		if(name) {
			dojo.publish("SceneSelector_resize", [{name:name}]);
		}
	}
});

dojo.declare("Slideshow", null, {
	displayTime: SLIDESHOW_TIME_TO_SHOW, // ms before moving to the next img

	constructor: function(name) {
		this._gallery = name;
		this._items = G[name];
		dojo.connect(window, "onresize", this, "_resize");
	},

	_img: function(name) { return PHOTO_DIR + "/" + this._gallery + "/images/" + name; },

	_thumb: function(name) { return PHOTO_DIR + "/" + this._gallery + "/thumbnails/" + name; },

	_toId: function(name, thumb) {
		if(!dojo.isString(name)) {
			name = name.src.substring(name.src.lastIndexOf("/")+1);
		}
		return name.replace(/[^a-z0-9]/gi, "_") + (thumb ? "_thumb" : "_img");
	},

	_strip: function() {
		var id = this._gallery + "_thumbStrip";
		var strip = ID(id);
		if(!strip) {
			// holder
			strip = document.createElement("div");
			strip.id = id;
			strip.className = "thumbStrip";

			// thumbs
			var th = document.createElement("img");
			th.src = "addons/img/blank.gif";
			th.width = th.height = 40;
			th.className = "thumb";

			dojo.forEach(this._items, function(n, i) {
				var t = strip.appendChild(th.cloneNode(true));
				t.id = this._toId(n, true);

				var img = new Imager(this._thumb(n), 5);
				img.onload(this, function(e) {
					dojo.style(t, "backgroundImage", "url(" + e.img.src + ")");
				});

				dojo.connect(t, "onclick", this, function(e) {
					this.click(i);
				});
				niceRollover(t, THUMB_ROLLOVER_OPACITY);
			}, this);
		}
		ID("ThumbBox").appendChild(strip); // make sure it's the top one
		dojo.query(".thumbStrip", "ThumbBox").forEach(function(n) {
			n.style.display = n == strip ? "block" : "none";
			dojo[n == strip ? "addClass" : "removeClass"](n, "current");
		});
		return strip;
	},

	_active: false,

	load: function() {
		this._active = true;
		this._strip(); // make sure this exists
		this._resize();
		this.play();
	},

	play: function(goNext) {
		clearTimeout(this._slideTimer);
		this._stopping = false;
		this._active = true;
		if(this._idx < 0) {
			this.select(0);
			this._playing = true;
		} else {
			this._playing = true;
			this._setupNext(goNext);
		}
		dojo.removeClass("Controls", "paused");
	},

	_setupNext: function(goNext) {
		clearTimeout(this._slideTimer);
		this._loadNext(); // start loading img!
		if(this._playing) {
			this._slideTimer = setTimeout(dojo.hitch(this, "next"),
				goNext ? 0 : this.displayTime + 2 * this._fadeDuration);
		}
	},

	pause: function() {
		this._playing = false;
		clearTimeout(this._slideTimer);
		dojo.addClass("Controls", "paused");
	},

	playPause: function() {
		if(this._playing == false) {
			this.play(true);
		} else {
			this.pause();
		}
	},

	resume: function() {
	},

	next: function() {
		var len = this._items.length;
		var next = this._idx + 1;
		this.select(next % len);
	},

	prev: function() {
		var prev = this._idx - 1;
		if(prev < 0) { prev = this._items.length - 1; }
		this.select(prev);
	},

	_stopping: false,

	stop: function() {
		this._stopping = true;
		this._idx = -1;
		dojo.query("img", "PhotoBox").forEach(function(n) {
			n.parentNode.removeChild(n);
		}, this);
		this._hideSpinner();
	},

	reset: function() {
		this.stop();
		this._resetThumbs();
		Scroller.reset();
		this._active = false;
	},

	_idx: -1,

	click: function(idx) {
		this.pause();
		this._forceSlideThumb = true;
		this.select(idx);
	},

	select: function(idx) {
		if(idx == "first") {
			return this.select(0);
		} else if(idx == "last") {
			return this.select(this._items.length - 1);
		}

		if(this._idx == idx) { return; }

		this._showSpinner();

		var wh = this._items[idx];

		var img = new Imager(this._img(wh));
		this._loadingImg = img.img();
		img.onload(this, function(e) {
			if(e.img == this._loadingImg) {
				this._imgLoaded(e.img, idx);
				this._loadingImg = null;
			}
		});

		img.img().id = this._toId(wh);
		img.img().className = "photo";
	},

	_loadNext: function() {
		var len = this._items.length;
		var next = this._idx + 1;
		var img = new Image();
		img.src = this._img(this._items[next]);
	},

	_fadeDuration: 400,

	_imgLoaded: function(img, idx) {
		if(this._stopping) { return; }
		this._idx = idx;
		var fx = [];
		var lastImgs = dojo.query("img", "PhotoBox");
		var lastImg = lastImgs[lastImgs.length-1];
		lastImgs.slice(0, -1).forEach(function(node) {
			node.parentNode.removeChild(node);
			delete node;
		});

		if(lastImg) {
			var bye = dojo.fadeOut({ node: lastImg, duration: this._fadeDuration });
			bye.onEnd = function() {
				if(lastImg.parentNode) { lastImg.parentNode.removeChild(lastImg); }
				delete lastImg;
			}
			fx.push(bye);
		}

		dojo.style(img, "opacity", 0);
		dojo.connect(img, "onclick", this, function() { this.pause(); this.next(); });
		ID("PhotoBox").appendChild(img);
		centerIt(img, "Viewer");
		this._hideSpinner();
		fx.push( dojo.fadeIn({ node: img, duration: this._fadeDuration }) );
		this._highlightThumb(img, lastImg);
		dojo.fx.chain(fx).play();
		this._setupNext();
	},

	_resize: function(e) {
		var img = dojo.query("img", "PhotoBox")[0];
		if(img) {
			centerIt(img, "Viewer");
		}
		centerIt("Spinner");
		Scroller.update();
	},

	_spinAnim: null,

	_showSpinner: function(noPlay) {
		if(this._playing) { return; }
		if(this._spinAnim) { this._spinAnim.stop(); }
		centerIt("Spinner", "Gallery");
		this._spinAnim = dojo.fadeIn({node: "Spinner", duration: 100, end: SPINNER_OPACITY});
		if(!noPlay) { this._spinAnim.play(); }
		return this._spinAnim;
	},

	_hideSpinner: function(noPlay) {
		if(this._spinAnim) { this._spinAnim.stop(); }
		this._spinAnim = dojo.fadeOut({node: "Spinner", duration: this._fadeDuration});
		if(!noPlay) { this._spinAnim.play(); }
		return this._spinAnim;
	},

	_highlightThumb: function(curr, prev) {
		if(prev) {
			dojo.style(this._toId(prev, true), "opacity", THUMB_ROLLOVER_OPACITY);
			dojo.attr(this._toId(prev, true), "niceRollover", "enabled");
		}
		var x = this._toId(curr, true);
		dojo.style(this._toId(curr, true), "opacity", 1);
		dojo.attr(this._toId(curr, true), "niceRollover", "disabled");
		this._slideToThumb(curr);
	},

	_resetThumbs: function() {
		dojo.query("img[niceRollover]").forEach(function(item) {
			dojo.attr(item, "niceRollover", "enabled");
			dojo.style(item, "opacity", NICE_ROLLOVER_OPACITY);
		});
	},

	_forceSlideThumb: false,

	_slideToThumb: function(name) {
		if(!this._forceSlideThumb && Scroller.isActive()) { return; } // don't mess w/users!
		this._forceSlideThumb = false;

		var img = this._toId(name, true);
		var a = dojo.coords(img),
			c = dojo.coords("ThumbBox");
		var x0 = c.x, x1 = c.w + x0,
			a0 = a.x, a1 = a.w + a0;
		var tooLeft = a0 < x0,
			tooRight = a1 > x1;
		if(tooLeft) {
			Scroller.stop();
			dojox.fx.slideBy({
				node: this._strip(),
				left: x0 - a0,
				duration: this._fadeDuration,
				onEnd: function() {
					Scroller.update();
				}
			}).play();
		} else if(tooRight) {
			Scroller.stop();
			dojox.fx.slideBy({
				node: this._strip(),
				left: x1 - a1 - 5,
				duration: this._fadeDuration,
				onEnd: function() {
					Scroller.update();
				}
			}).play();
		}
	}
});
dojo.mixin(Slideshow, {
	_Shows: {},
	get: function(name) {
		if(!name) { return null; }
		if(!Slideshow._Shows[name]) {
			var ss = Slideshow._Shows[name] = new Slideshow(name);
		}
		return Slideshow._Shows[name];
	},
	keyhandler: function(e) {
		if(!TheShow || !TheShow._active) { return; }
		if(e.altKey || e.shiftKey || e.ctrlKey) { return; }

		var k = dojo.keys;
		switch(e.keyCode || e.charCode) {
			case k.DOWN_ARROW:
			case k.RIGHT_ARROW:
			case k.PAGE_DOWN:
			case k.ENTER:
				TheShow.pause();
				TheShow.next();
				break;
			case k.UP_ARROW:
			case k.LEFT_ARROW:
			case k.PAGE_UP:
				TheShow.pause();
				TheShow.prev();
				break;
			case k.SPACE:
				TheShow.playPause();
				break;
			case k.HOME:
				TheShow.select("first");
				break;
			case k.END:
				TheShow.select("last");
				break;
			default:
				return;
		}
		e.preventDefault(); // since we didn't return
	}
});
dojo.connect(document, "onkeypress", Slideshow, "keyhandler");

var Scroller = {
	timer: null,
	ms: 45,
	amt: 7,
	wheelAmt: 30,
	_isScrolling: false,
	_isHover: false,
	_isWheel: false,

	init: function() {
		dojo.connect(dojo.byId("ThumbNext"), "onmouseover", function() { Scroller.left(); });
		dojo.connect(dojo.byId("ThumbNext"), "onmouseout", function() { Scroller.stop(); });
		niceRollover("ThumbNext", 0.4);

		dojo.connect(dojo.byId("ThumbPrev"), "onmouseover", function() { Scroller.right(); });
		dojo.connect(dojo.byId("ThumbPrev"), "onmouseout", function() { Scroller.stop(); });
		niceRollover("ThumbPrev", 0.4);

		dojo.connect(dojo.byId("Thumbs"), "onmouseover", function() { Scroller._isHover = true; });
		dojo.connect(dojo.byId("Thumbs"), "onmouseout", function() { Scroller._isHover = false; });

		dojo.connect(new ScrollWheel("Thumbs"), "onMouseWheel", Scroller, "mousewheel");
	},

	mousewheel: function(e) {
		this._isWheel = true;
		switch(e.wheel) {
			case "up":
				this.right(true);
				break;
			case "down":
				this.left(true);
				break;
		}
		this._isWheel = false;
	},

	amount: function() {
		return Scroller._isWheel ? Scroller.wheelAmt : Scroller.amt;
	},

	isActive: function() {
		// so we don't auto-slide to thumbs if our mouse is on the thumbs!
		return Scroller._isHover || Scroller._isScrolling;
	},

	_leftStuff: function() {
		var thumbs = dojo.query(".thumbStrip.current", "ThumbBox")[0];
		if(!thumbs) { return 0; }
		var s = dojo.style;
		var W = s("ThumbBox", "width");
		var w = s(thumbs, "width");
		if(w <= W) { return 0; }
		var left = s(thumbs, "left");
		var amt = Math.min(w - W + left, Scroller.amount());
		return amt;
	},

	left: function(runOnce) {
		var thumbs = dojo.query(".thumbStrip.current", "ThumbBox")[0];
		if(!thumbs) { return; }

		Scroller.stop();

		var s = dojo.style;
		var W = s("ThumbBox", "width");
		var w = s(thumbs, "width");

		if(w > W) {
			Scroller._isScrolling = true;

			function fcn() {
				var left = s(thumbs, "left");
				var amt = Scroller._leftStuff();

				if(amt > 0) {
					s(thumbs, "left", left - amt + "px");
				} else {
					Scroller.stop();
				}

				Scroller.updateArrows();
			}
			if(runOnce) {
				fcn();
			} else {
				Scroller.timer = setInterval(fcn, Scroller.ms);
			}
		}
	},

	_rightStuff: function() {
		var thumbs = dojo.query(".thumbStrip.current", "ThumbBox")[0];
		if(!thumbs) { return 0; }
		var left = dojo.style(thumbs, "left");
		if(left > 0) { return 0; }
		var amt = Math.min(Math.abs(left), Scroller.amount());
		return amt;
	},

	right: function(runOnce) {
		var thumbs = dojo.query(".thumbStrip.current", "ThumbBox")[0];
		if(!thumbs) { return; }

		Scroller.stop();

		Scroller._isScrolling = true;

		function fcn() {
			var left = dojo.style(thumbs, "left");
			var amt = Scroller._rightStuff();

			if(amt > 0) {
				dojo.style(thumbs, "left", left + amt + "px");
			} else {
				Scroller.stop();
			}

			Scroller.updateArrows();
		}
		if(runOnce) {
			fcn();
		} else {
			Scroller.timer = setInterval(fcn, Scroller.ms);
		}
	},

	stop: function() {
		clearTimeout(Scroller.timer);
		Scroller._isScrolling = false;
	},

	updateArrows: function() {
		var thumbs = dojo.query(".thumbStrip.current", "ThumbBox")[0];
		if(thumbs) {
			var W = dojo.style("ThumbBox", "width");
			var w = dojo.style(thumbs, "width");
			if(W > w) {
				centerIt(thumbs, null, { notY: true });
			} else if(dojo.coords(thumbs).l > 0) {
				dojo.style(thumbs, "left", 0);
			}
		}

		dojo[Scroller._leftStuff() >= Scroller.amount() ? "removeClass" : "addClass"]("ThumbNext", "disabled");
		dojo[Scroller._rightStuff() > 0? "removeClass" : "addClass"]("ThumbPrev", "disabled");
	},

	update: function() {
		Scroller.updateArrows();
	},

	reset: function() {
		var thumbs = dojo.query(".thumbStrip.current", "ThumbBox")[0];
		if(!thumbs) { return; }

		dojo.style(thumbs, "left", 0);
		Scroller.update();
		Scroller.stop();
		Scroller._isHover = false;
	}
};

RunFirst.callback();
