GalleryApplication = function () {
	this.buttons = [];
	this.dock = null;
	this.current_gallery = null;
	this.gallery_list = null;
	this.interface_buttons = {};
	this.slideshow = false;
	this.current_slide = 0;
	this.timer_handle;
/*	
	this.center = function (inner, outer) {
		var inElement = document.getElementById(inner);
		var iw = inElement.offsetWidth;
		var ih = inElement.offsetHeight;
	
		var ow;
		var oh;
	
		if (outer) {
			var outElement = document.getElementById(outer);
			ow = outElement.offsetWidth;
			oh = outElement.offsetHeight;
		} else {
			var dimensions = this.get_browser_dimensions();
			ow = dimensions.w;
			oh = dimensions.h;
		}
		
		var woff = (ow - iw) / 2;
		var hoff = (oh - ih) / 2;
		
		inElement.style.left = woff + "px";
		inElement.style.top = hoff + "px";
	};
*/	
	this.scale_to_fit = function (inner) {
		var inElement = document.getElementById(inner);
		YAHOO.util.Dom.removeClass(inElement, "wide");
		YAHOO.util.Dom.removeClass(inElement, "high");
		var iw = inElement.offsetWidth;
		var ih = inElement.offsetHeight;
	
		var outElement = document.getElementById("main");
		var ow = outElement.offsetWidth;
		var oh = outElement.offsetHeight;
	
		var scale = 1.0;
		var new_class = null;
		var rescale = false;
		
		if (iw > ow) {
			rescale = true;
			scale = ow / iw;
			new_class = "wide";
		}
		if (ih > oh) {
			rescale = true;
			var s = oh / ih;
			if (s < scale) {
				new_class = "high";
			}
		}
		
		if (rescale) {
			YAHOO.util.Dom.addClass(inElement, new_class);
		}
	};

	this.show_image = function (from_button) {
		document.getElementById("placeholder").src = from_button.getAttribute("large");
		YAHOO.util.Event.on(document.getElementById("placeholder"), "load", (function (this_obj) {
			return function () {
				this_obj.scale_to_fit("placeholder");
//				this_obj.center("placeholder", "main");
			};
		})(this));
	}

	this.make_gallery_button = function (button) {
		button.select = function () {
			button.setAttribute("selected", "true");
		};
		button.unselect = function () {
			button.setAttribute("selected", "false");
			button.do_mouseout();
		};
		button.is_selected = function () {
			return button.getAttribute("selected");
		};
		button.do_mouseover = function () {
			button.setAttribute("mouseover", "true");
			if (button.is_selected() === "true") {
				return;
			}
			if (button.anim) {
				button.anim.stop();
				button.anim = null;
			}
			button.anim = new YAHOO.util.Anim(button, {
				width: {to: '36'}, 
				height: {to: '36'}, 
				left: {to: '0'},
				top: {to: '0'},
				opacity: {to: 1}
			}, 0.1);
			button.anim.animate();
		};
		button.do_mouseout = function () {
			button.setAttribute("mouseover", "false");
			if (button.is_selected() === "true") {
				return;
			}
			if (button.anim) {
				button.anim.stop();
				button.anim = null;
			}
			button.anim = new YAHOO.util.Anim(button, {
				width: {to: '20'},
				height: {to: '20'}, 
				left: {to: '8'},
				top: {to: '8'},
				opacity: {to: 0.4}
				}, 0.1);
			button.anim.animate();
		};
		button.set_default = function () {
			button.do_mouseover();
			button.select();
			button.do_mouseout();
		};
		YAHOO.util.Event.on(button, "mouseover", button.do_mouseover);
		YAHOO.util.Event.on(button, "mouseout", button.do_mouseout);
		button.unselect();
	};

	this.make_scroll_button = function (button, scroll_target, offset, max_scroll) {
		YAHOO.util.Event.purgeElement(button, true);
		if (max_scroll <= 0) {
			YAHOO.util.Dom.setStyle(button, "display", "none");
			return;
		}
		YAHOO.util.Dom.setStyle(button, "display", "inline");
		button.max_scroll = -max_scroll;
		button.get_max_move = function (move_by) {
			var x = parseInt(YAHOO.util.Dom.getStyle(scroll_target, "left"));
			if (move_by > 0) {
				x = Math.min(x + move_by, 0);
			} else if (move_by < 0) {
				x = Math.max(x + move_by, button.max_scroll);
			}
			return x;
		};
		button.do_mouseover = function () {
			button.setAttribute("mouseover", "true");
			if (button.anim) {
				button.anim.stop();
				button.anim = null;
			}
			if (scroll_target.interval) {
				clearInterval(scroll_target.interval);
				scroll_target.interval = null;
			}
			button.anim = new YAHOO.util.ColorAnim(button, {
				backgroundColor: { to: '#FFA' }
			}, 0.05);
			button.anim.animate();
			scroll_target.interval = setInterval(function () {
				if (button.getAttribute("mouseover") !== "true") {
					clearInterval(scroll_target.interval);
					scroll_target.interval = null;
				}
				var x = button.get_max_move(offset);
				YAHOO.util.Dom.setStyle(scroll_target, "left", x + "px");
			}, 7);
		};
		button.do_mouseout = function () {
			button.setAttribute("mouseover", "false");
			if (button.anim) {
				button.anim.stop();
				button.anim = null;
			}
			if (scroll_target.interval) {
				clearInterval(scroll_target.interval);
				scroll_target.interval = null;
			}
			button.anim = new YAHOO.util.ColorAnim(button, {
				backgroundColor: { to: '#888' }
				}, 0.05);
			button.anim.animate();
		};
		button.do_click = function () {
			var jump = parseInt(YAHOO.util.Dom.getStyle(scroll_target, "left")) % 45;
			jump = (offset < 0) ? (jump + (offset * 45)) : ((offset * 45) - jump);
			jump = button.get_max_move(jump);
			if (scroll_target.anim) {
				scroll_target.anim.stop();
				scroll_target.anim = null;
			}
			scroll_target.anim = new YAHOO.util.Anim(scroll_target, {
				left: {to: jump }
			}, 0.1, YAHOO.util.Easing.bounceBoth);
			scroll_target.anim.animate();
		};
		YAHOO.util.Event.on(button, "mouseover", button.do_mouseover);
		YAHOO.util.Event.on(button, "mouseout", button.do_mouseout);
		YAHOO.util.Event.on(button, "click", button.do_click);
	};
	
	this.clear_scroll_button = function (button) {
		YAHOO.util.Event.purgeElement(button, true);
		YAHOO.util.Dom.setStyle(button, "display", "none");
	}

	this.clear_dock = function () {
		if (this.timer_handle) {
			clearTimeout(this.timer_handle);
			this.timer_handle = false;
		}
		this.dock = this.dock || document.getElementById("dock");
		var children = YAHOO.util.Dom.getChildren(this.dock);
		var child;
		var count = 0;
		var length = children.length;
		YAHOO.util.Dom.setStyle(this.dock, "left", "0px");

		YAHOO.util.Event.purgeElement(this.dock, true);	//	remove all listeners from dock & children
		for (count = 0; count < length; count++) {
			child = children[count];
			this.dock.removeChild(child);
		}
		document.getElementById("placeholder").src = "images/blank.gif";
	}
	
	this.build_gallery = function () {
		var i = 0;
		var start_x = 0;
		var start_y = 0;
		var x = start_x;
		var y = start_y;
		var row_count = 1;
		var col_count = 1;
		var w = 36;
		var h = 36;
		var max_rows = 1;
		var max_cols = 5000;
		var dock_item;
		var dock_image;
		var list = this.gallery_list[this.current_gallery];
		var len = list.length;
		this.clear_dock();		
		for ( i = 0 ; i < len ; i++ ) {
			dock_item = document.createElement("div");
			YAHOO.util.Dom.addClass(dock_item, "dock_item");
			YAHOO.util.Dom.setStyle(dock_item, "left", x + "px");
			YAHOO.util.Dom.setStyle(dock_item, "top", y + "px");
			dock_image = document.createElement("img");
			YAHOO.util.Dom.addClass(dock_image, "button");
			dock_image.src = list[i]["thumb"];
			dock_image.setAttribute("large", list[i]["large"]);
			dock_item.appendChild(dock_image);
			this.dock.appendChild(dock_item);
			this.make_gallery_button(dock_image);
			(function (this_obj, gallery_obj) {
				this_obj.do_click = function () {
					gallery_obj.show_image(this_obj);
					this_obj.select();
				};
				YAHOO.util.Event.on(this_obj, "click", this_obj.do_click);
			})(dock_image, this);
			y += h;
			row_count++;
			if (row_count > max_rows) {
				y = start_y;
				row_count = 1;
				col_count++;
				if (col_count > max_cols) {
					var panels = Math.floor((i + 1) / (max_rows * max_cols));
					var panel_width = w * max_cols;
					x = start_x + (panels * panel_width);
					col_count = 1;
				} else {
					x += w;
				}
			}
		}
		this.buttons = YAHOO.util.Dom.getElementsByClassName("button");
		YAHOO.util.Dom.setStyle(this.dock, "width", x + "px");
		var max_scroll = x - parseInt(YAHOO.util.Dom.getStyle("dock_container", "width"));
		if (max_scroll < 0) {
			max_scroll = 0;
		}
		this.make_scroll_button(this.interface_buttons.button_left, this.dock, 1, max_scroll);
		this.make_scroll_button(this.interface_buttons.button_right, this.dock, -1, max_scroll);
		(function (this_obj, parent_obj) {
			var do_click = function () {
				var count = parent_obj.buttons.length;
				for (var i = 0; i < count; i++) {
					if (parent_obj.buttons[i].getAttribute("mouseover") === "false") {
						parent_obj.buttons[i].unselect();
					}
				}
				if (parent_obj.timer_handle) {
					parent_obj.current_slide--;
					parent_obj.buttons[parent_obj.current_slide].unselect();
					parent_obj.buttons[parent_obj.current_slide].do_mouseout();
					clearTimeout(parent_obj.timer_handle);
					parent_obj.timer_handle = false;
				}
			};
			YAHOO.util.Event.on(this_obj, "click", do_click);
		})(this.dock, this);
		this.buttons[0].set_default();
		this.show_image(this.buttons[0]);
		this.slideshow = this.current_gallery;
		this.current_slide = 0;
		this.do_slideshow(this.buttons[0]);
	}

	this.do_slideshow = function (btn) {
		this.current_slide++;
		this.current_slide = this.current_slide % this.buttons.length;
		this.timer_handle = setTimeout((function (button, this_obj) {
			return function () { 
				if (btn) {
					btn.unselect();
					btn.do_mouseout();
				}
				button.unselect();
				button.do_mouseover();
				button.do_click();
				this_obj.do_slideshow(button);
			};
		})(this.buttons[this.current_slide], this), 5000);
	};
	
	this.set_current_gallery = function (gallery) {
		this.clear_dock();
		this.clear_scroll_button(this.interface_buttons.button_left);
		this.clear_scroll_button(this.interface_buttons.button_right);
		if (this.gallery_list[gallery] && this.gallery_list[gallery].length) {
			this.current_gallery = gallery;
			this.build_gallery();
		} else {
			document.getElementById("placeholder").src = "images/blank.gif";
		}
	};

	this.on_dom_ready = function () {
		this.interface_buttons.button_left = document.getElementById("scroll_left");
		this.interface_buttons.button_right = document.getElementById("scroll_right");
		this.set_current_gallery("home");
	};
};

var gapp = new GalleryApplication();

(function () {
	YAHOO.util.Event.onDOMReady(function () {
		gapp.on_dom_ready();
	});
})();