var Gallery = {
	bg: null,
	bodyEle: null,
	resizeEvent: null,
	img: null,
	originalHeight: null,
	
	reposition: function() {
		var dims = HtmlDOM.getWindowInnerSize();
		this.bg.style.height = dims.y + "px";
		if (this.img.offsetHeight < dims.y) {
			this.img.style.marginTop = (dims.y - this.img.offsetHeight) / 2 + "px";
		} else {
			this.img.style.marginTop = "5px";
		}
	},
	
	closeLightbox: function() {
		if (this.resizeEvent) {
			EventMan.removeEvent(this.resizeEvent);
		}
		
		if (this.bg) {
			this.bodyEle.removeChild(this.bg);
		}
		
		if (this.container) {
			this.bodyEle.removeChild(this.container);
		}
	},

	imageLoaded: function() {
		this.reposition();
		this.container.style.visibility = "visible";
	},
	
	showLightbox: function(img) {
		var dims = HtmlDOM.getWindowInnerSize();
		this.bg = document.createElement("div");
		this.bg.className = "lightbox_bg";
		this.bg.style.height = dims.y + "px";
		this.container = document.createElement("div");
		this.container.className = "lightbox_container";
		this.img = document.createElement("img");
		this.container.appendChild(this.img);
		this.originalHeight = 0;
		EventMan.addEvent(this.img,"load",this.imageLoaded, {scope: this});
		this.img.src = "/images/gallery/" + img;
		this.bodyEle.appendChild(this.bg);
		this.bodyEle.appendChild(this.container);
		
		EventMan.addEvent(this.bg,"click",this.closeLightbox, {scope: this});
		EventMan.addEvent(this.container,"click",this.closeLightbox, {scope: this});
		this.resizeEvent = EventMan.addEvent(window,"resize",this.reposition, {scope: this});
	},
	
	doClick: function(e, ele, filename) {
		this.showLightbox(filename);
	},
	
	init: function() {
		this.bodyEle = document.getElementsByTagName("body")[0];
		var divs = $C("thumb");
		for (var i = 0, len = divs.length; i < len; i++) {
			var d = divs[i];
			var filename = divs[i].className.match(/file_(.*?)($|\s)/);
			if (filename) {
				filename = filename[1];
			}
			EventMan.addEvent(d,"click",this.doClick,{scope: this, userdata: filename});
			d.style.cursor = "pointer";
		}
	}
}
