window.site_root = "http://"+JMDWiki_HTTP_HOST+"/";

window.gallery_onloadOld = window.onload?window.onload:function(){};
window.onload = gallery_onload;

window.accesslevel = "default";
window.Darkroom_slideshow_gallery_current = 0;










window.Darkroom = (function() {
	var obj = {
		galleryViewSensitivity : 20,
		ringSize : 5/8,		// 5/8
		ringHeight : 3/4,	// 3/4
		ViewGallery : function(gid,galleryElement) {
			if(this.galleryRequest) {
				this.galleryRequest.cancel();
				this.galleryRequest = null;
			}
			function onComplete() {
				this.parentGallery.ViewGalleryCallback(this,this.gid,this.galleryElement);
				this.parentGallery.galleryRequest = null;
			}
			this.galleryRequest = this.HttpRequest.create("./?ext=json_image_list&full=true&galleryid="+gid,null,onComplete,onComplete,true).send();
			this.galleryRequest.gid = gid;
			this.galleryRequest.galleryElement = galleryElement;
			this.galleryRequest.parentGallery = this;
		},
		ViewGalleryCallback : function(request,gid,galleryElement) {
			var response = {result:false,message:"Error parsing gallery data."};
			try{ response=eval("(" + request.data + ")"); }catch(e){ repsonse={result:false,message:"Error parsing gallery data."}; }
			if(response.result!=true) {
				if(response.message)
					this.ShowMessage(response.message, "Error");
				else
					this.ShowMessage("There was a strange error.", "Error");
				return false;
			}
			
			//this._oldBodyOverflow = document.body.style.overflow;
			//document.body.style.overflow = "hidden";
			
			this._style = document.createElement("style");
			this._style.setAttribute("type","text/css");
			this._style.setAttribute("id","gallery_style1");
			this._style.innerHTML = "html,body{overflow:hidden;}";
			document.getElementsByTagName("head")[0].appendChild(this._style);
			
			// view the gallery
			var galleryThumb = galleryElement.getElementsByTagName("img")[0];
			this.gallery_open_startWidth = galleryThumb.offsetWidth;
			this.gallery_open_startHeight = galleryThumb.offsetHeight;
			this.gallery_open_startX = galleryThumb.offsetLeft;
			this.gallery_open_startY = galleryThumb.offsetTop;
			var f = galleryThumb;
			while(f=f.parentNode) {
				if(f.offsetLeft==parseInt(f.offsetLeft))
					this.gallery_open_startX += f.offsetLeft;
				if(f.offsetTop==parseInt(f.offsetTop))
					this.gallery_open_startY += f.offsetTop;
				
				if(f.parentNode && f.parentNode.scrollLeft)
					this.gallery_open_startX -= f.parentNode.scrollLeft;
				if(f.parentNode && f.parentNode.scrollTop)
					this.gallery_open_startY -= f.parentNode.scrollTop;
			}
			
			var count = 0;
			for(var x in response.images)
				count++;
			
			this.imagesLoaded = 0;
			this.gallery_images = [];
			for(var x in response.images) {
				var img = response.images[x];
				this.gallery_images.push( this.createImage({
					src  : img.src,
					name : img.name,
					desc : img.desc,
					onload : (this.gallery_images.length==0?function(){ this.parentGallery.AnimateGalleryOpen(); }:function(){})  //function(){ this.parentGallery.imagesLoaded++; if(!this.parentGallery.ganim && this.parentGallery.gallery_images[0]._img.origHeight && this.parentGallery.imagesLoaded>=this.parentGallery.gallery_images.length/2) this.parentGallery.AnimateGalleryOpen(); }
				}) );
			}
		},
		AnimateGalleryOpen : function() {
			if(this.ganim && this.ganim.timer)
				try{ window.clearInterval(this.ganim.timer); }catch(e){}
			// animate
			var winW = window.innerWidth ? window.innerWidth : ((document.documentElement?document.documentElement:document.body).offsetWidth);
			var winH = window.innerHeight ? window.innerHeight : ((document.documentElement?document.documentElement:document.body).offsetHeight);
			var dsocTop = navigator.appName.indexOf("Microsoft")>-1 ? document.body.scrollTop : window.pageYOffset;
			
			var d=document.createElement("div");
			d.id = "darkroom_gallery_fader_"+Math.round(Math.random()*1000,0);
			d.className = "darkroom_gallery_dimmer";
			d.style.cssText = "position:absolute; width:100%; height:"+winH+"px; left:0; top:0;";
			d.onclick = function(){ Darkroom.AnimateGalleryClose(); };
			document.body.appendChild(d);
			if(document.all	&& document.body.filters && !window.opera && parseFloat(navigator.appVersion)<8)
				d.style.filter = "alpha(opacity=20)";
			else
				d.style.opacity = 0.2;
			
			var size = 5/8;
			this.ganim = {
				size	: size,
				step	: 0,
				maxstep	: 40,
				minHeight	: 50,
				minWidth	: 50,
				closing	: false,
				startX	: this.gallery_open_startX,
				startY	: this.gallery_open_startY,
				startWidth	: this.gallery_open_startWidth,
				startHeight	: this.gallery_open_startHeight,
				maxWidth	: winW *size,
				maxHeight	: winH *size,
				winW	: winW,
				winH	: winH,
				fader	: d,
				faded	: false,
				//timers	: []
				timer	: window.setInterval("Darkroom.AnimateGalleryStep();", 30)
			};
			//for(var x=0; x<5; x++)
			//	window.setTimeout("Darkroom.ganim.timers.push(window.setInterval('Darkroom.AnimateGalleryStep();',50));", 15*x + 15);
		},
		AnimateGalleryClose : function() {
			if(this.mouseisdown==true)
				return false;
			//if(this.ganim && this.ganim.timer)
			//	try{ window.clearInterval(this.ganim.timer); }catch(e){}
			
			document.onmousedown = this._galleryview_onmousedownold;
			//if(document.detachEvent)
			//	document.detachEvent("onmousedown", function(){window.onmousedown(window.event)});
			
			Darkroom.ganim.manuallymoving = false;
			
			if(this.ganim) {
				this.ganim.closing=true;
				this.ganim.loaded = false;
				
				for(var x=0; x<this.gallery_images.length; x++)
					this.gallery_images[x]._img.style.cssText = this.gallery_images[x]._img._movingCSS;
				
				if(this.ganim.step > this.ganim.maxstep)
					this.ganim.step = this.ganim.step - Math.floor(this.ganim.step/this.ganim.maxstep/2)*this.ganim.maxstep*2;
				this.ganim.currentClosingStep = this.ganim.step;
				//this.ganim.timer = window.setInterval("Darkroom.AnimateGalleryStep();", 20);
				
				/*
				this.ganim.fader.style.backgroundColor = "#000";
				this.ganim.fader.style.backgroundImage = "";
				if(document.all	&& document.body.filters && !window.opera && parseFloat(navigator.appVersion)<8)
					this.ganim.fader.style.filter = "alpha(opacity=60)";
				else
					this.ganim.fader.style.opacity = "0.6";
				*/
			}
		},
		createImage : function(params) {
			if(params==null || params=="undefined")
				return false;
			
			var imgwrap = document.createElement("div");
			imgwrap.className = "darkroom_image_wrap"+(params.name?"":" darkroom_image_untitled")+(params.desc?"":" darkroom_image_nodescription");
			imgwrap.style.visibility = "hidden";
			document.body.appendChild(imgwrap);
			
			var name = document.createElement("span");
			imgwrap._name = name;
			name.className = "darkroom_image_name";
			name.innerHTML = params.name ? params.name : "Untitled";
			imgwrap.appendChild(name);
			
			var desc = document.createElement("span");
			imgwrap._desc = desc;
			desc.className = "darkroom_image_desc";
			desc.innerHTML = params.desc ? params.desc : "";
			imgwrap.appendChild(desc);
			
			var img = document.createElement("img");
			imgwrap._img = img;
			img.className = "darkroom_image";
			img.onerror = function(){ this.onload(true); };
			img.usingNoimage = false;
			if(params.onload!=null && params.onload!="undefined")
				img._onload = params.onload;
			img.onload = function(error){
				if(error==true) {
					this.usingNoimage = true;
					this.src = this.parentGallery.noImageSrc;
					return true;
				}
				this.origWidth = this.width+0;
				this.origHeight = this.height+0;
				this.parentNode.style.width = "0";
				this.parentNode.style.height = "0";
				this._defaultCSS = "position:absolute; left:0; top:0; width:100%; height:100%; margin:0; padding:0;";
				
				if(this.origWidth*this.origHeight > 1500000)
					this._movingCSS = "position:absolute; top:50%; left:50%; margin:auto; margin-left:-"+(this.origWidth/2)+"px; margin-top:-"+(this.origHeight/2)+"px; padding:0;";
				else
					this._movingCSS = this._defaultCSS;
				
				if(this.parentGallery.ganim && this.parentGallery.ganim.loaded==true)
					this.style.cssText = this._defaultCSS;
				else
					this.style.cssText = this._movingCSS;
				this.parentNode.style.visibility = "visible";
				this._onload();
			};
			img.src = params.src ? params.src : this.noImageSrc;
			imgwrap.appendChild(img);
			
			img.parentGallery = this;
			return imgwrap;
		},
		AnimateGalleryStep : function() {
			if(this.ganim.step<20 && this.ganim.faded!=true) {
				if(this.ganim.step<=5) {
					this.ganim.fader.style.backgroundColor = "#000";
					this.ganim.fader.style.backgroundImage = "";
					if(document.all	&& document.body.filters && !window.opera && parseFloat(navigator.appVersion)<8)
						this.ganim.fader.style.filter = "alpha(opacity="+(10 + this.ganim.step*10)+")";
					else
						this.ganim.fader.style.opacity = Math.round(10 + this.ganim.step*10)/100;
				}
				/*
				else if(this.ganim.step==6) {
					if(document.all	&& document.body.filters && !window.opera && parseFloat(navigator.appVersion)<8)
						this.ganim.fader.style.filter = "alpha(opacity=100)";
					else
						this.ganim.fader.style.opacity = 1;
					this.ganim.fader.style.backgroundColor = "";
					this.ganim.fader.style.background = "url('images/gallery-cover.png') center repeat";
				}
				*/
				if(this.ganim.step==19) {
					this.ganim.faded = true;
					this.ganim.step = 0;
				}
				else
					this.ganim.step++;
				return false;
			}
			var ratio,progress,ringTop;
			ringTop = this.ganim.winH * (1 - this.ringHeight)/2;
			for(var x=0; x<this.gallery_images.length; x++) {
				var image = this.gallery_images[x];
				if(image._img.origWidth && image._img.origHeight)
					ratio = image._img.origWidth / image._img.origHeight;
				else
					ratio = 1;
				
				//if(this.ganim.closing!=true) {
					//progress = (this.ganim.step-this.ganim.step%this.ganim.maxstep)/this.ganim.maxstep + (this.ganim.step.this.ganim.maxstep - Math.floor(this.ganim.step/this.ganim.maxstep)) * (this.gallery_images.length-x)/this.gallery_images.length;
					progress = -x/this.gallery_images.length*2	// adjust for images in a circle
							 + this.ganim.step/this.ganim.maxstep;
					if(progress<0)
						progress = 0;
				//}
				//else
				//	progress = (this.ganim.maxstep*2-this.ganim.step)/this.ganim.maxstep * (this.gallery_images.length-x)/this.gallery_images.length;
				
				if(progress>0 && image.style.display=="none")
					image.style.display = "block";
				else if(progress==0 && image.style.display!="none")
					image.style.display = "none";
				
				if(progress%2 > 1)
					dimProgress = 2-(progress%2);
				else
					dimProgress = progress%2;
				
				//if(dimProgress<0)
				//	dimProgress=0;
				
				// improves speed, looks weird:
				/*
				if(dimProgress < 0.5 && image.style.display!="none") {
					image.style.display = "none";
				}
				else if(dimProgress >= 0.5 && image.style.display=="none")
					image.style.display = "block";
				*/
				
				
				
				if(ratio>=1) {
					//image.style.width = (((this.ganim.maxWidth-this.ganim.startWidth) * dimProgress) + this.ganim.startWidth) + "px";
					//image.style.width = (this.ganim.maxWidth * dimProgress) + "px";
					image.style.width = Math.round((((this.ganim.maxWidth-this.ganim.minWidth) * dimProgress) + this.ganim.minWidth) * 10)/10 + "px";
					image.style.height = Math.round(image.offsetWidth/ratio * 10)/10 + "px";
					
					//image._img.style.width = Math.round((((this.ganim.maxWidth-this.ganim.minWidth) * dimProgress) + this.ganim.minWidth) * 10)/10 + "px";
					//image._img.style.height = Math.round(image.offsetWidth/ratio * 10)/10 + "px";
				}
				else {
					//image.style.height = (((this.ganim.maxHeight-this.ganim.startHeight) * dimProgress) + this.ganim.startHeight) + "px";
					//image.style.height = (this.ganim.maxHeight * dimProgress) + "px";
					image.style.height = Math.round((((this.ganim.maxHeight-this.ganim.minHeight) * dimProgress) + this.ganim.minHeight) * 10)/10 + "px";
					image.style.width = Math.round(image.offsetHeight/ratio * 10)/10 + "px";
					
					//image._img.style.height = Math.round((((this.ganim.maxHeight-this.ganim.minHeight) * dimProgress) + this.ganim.minHeight) * 10)/10 + "px";
					//image._img.style.width = Math.round(image.offsetHeight/ratio * 10)/10 + "px";
				}
				
				//image.style.top = ( this.ganim.startY + ((this.ganim.winH-this.ganim.maxHeight)/2 - this.ganim.startY) * dimProgress ) + "px";
				//image.style.left = (this.ganim.startX - Math.sin(this.ganim.step/this.ganim.maxstep*Math.PI)*((this.ganim.winW-this.ganim.maxWidth)/2 /*+ this.ganim.startX*/) ) + "px";
				
				var widthMod = (this.ganim.maxWidth*dimProgress-image.offsetWidth)/2;
				var heightMod = (this.ganim.maxHeight*dimProgress-image.offsetHeight)/2;
				
				image.style.top = Math.round(( ringTop + heightMod - this.ganim.maxHeight*dimProgress + (this.ganim.winH - ringTop*2) * dimProgress )*10)/10 + "px";
				image.style.left = Math.round(( 
					this.ganim.winW/2 + widthMod
					- (this.ganim.maxWidth/2)*dimProgress 
					- Math.sin( progress * Math.PI )*this.ganim.winW * this.ringSize/2 
					) *10)/10 + "px";
				
				image.style.zIndex = 990-Math.ceil((1-dimProgress)*this.gallery_images.length);
				
				// cool, but too slow:
				/*
				if(document.all	&& document.body.filters && !window.opera && parseFloat(navigator.appVersion)<8)
					image.style.filter = "alpha(opacity="+Math.round(20 + dimProgress*80)+")";
				else
					image.style.opacity = Math.round(20 + dimProgress*80)/100;
				*/
			}
			
			
			if(this.ganim.manuallymoving==true || this.mouseisdown==true)
				return true;
			
			/*
			if(this.ganim.closing!=true) {
				if(this.ganim.step+3<this.ganim.maxstep*(this.gallery_images.length>3?3:1))
					this.ganim.step += 2 + 3*(this.ganim.step/this.ganim.maxstep/3);//- 3*(this.ganim.step/this.ganim.maxstep/3);
				else {
					window.clearInterval(this.ganim.timer);
					this.ganim.step = this.ganim.maxstep*3;
					window.setTimeout("Darkroom.AnimateGalleryStep(); Darkroom._onGalleryOpened();",10);
					//this._onGalleryOpened();
				}
			}
			*/
			if(this.ganim.closing!=true) {
				if(this.ganim.step+2<this.ganim.maxstep)
					this.ganim.step += 4 - 2*(this.ganim.step/this.ganim.maxstep);
				else {
					//window.clearInterval(this.ganim.timer);
					this.ganim.step = this.ganim.maxstep*33333;
					window.setTimeout("Darkroom.AnimateGalleryStep(); Darkroom._onGalleryOpened();",10);
				}
			}
			else {
				var wall = Math.floor(this.ganim.currentClosingStep/this.ganim.maxstep)*this.ganim.maxstep-this.ganim.maxstep;
				if(this.ganim.step > wall)
					this.ganim.step -= 3;
				else {
					window.clearInterval(this.ganim.timer);
					for(var x=0; x<this.gallery_images.length; x++)
						this.gallery_images[x].parentNode.removeChild(this.gallery_images[x]);
					this.ganim.fader.parentNode.removeChild(this.ganim.fader);
					try{ delete this.ganim; }catch(e){ this.ganim = null; }
					
					this._style.parentNode.removeChild(this._style);
					//document.body.style.overflow = this._oldBodyOverflow;
				}
			}
		},
		_onGalleryOpened : function(){
			if(this.ganim  && this.ganim.loaded==true)
				return false;
			
			this.ganim.loaded=true;
			
			if(document.all	&& document.body.filters && !window.opera && parseFloat(navigator.appVersion)<8) {
				this.ganim.fader.style.filter = "alpha(opacity=100)";
				//this.ganim.fader.style.filter = null;
			}
			else {
				this.ganim.fader.style.opacity = 1;
				//this.ganim.fader.style.opacity = null;
			}
			this.ganim.fader.style.backgroundColor = null;
			this.ganim.fader.style.background = "url('darkroom/images/gallery-cover.png')";
			
			for(var x=0; x<this.gallery_images.length; x++) {
				this.gallery_images[x]._img.style.cssText = this.gallery_images[x]._img._defaultCSS;
			}
			
			Darkroom.ganim.manuallymoving = true;
			
			this._galleryview_onmousedownold = window.onmousedown ? window.onmousedown : function(){};
			document.onmousedown = function(e) {
				if(!e)
					e = window.event;
				var mx = (e.pageX?e.pageX:(e.clientX+(document.documentElement?document.documentElement:document.body).scrollLeft)) + (document.getElementById("w3c")?document.getElementById("w3c").scrollLeft:0);
				var my = (e.pageY?e.pageY:(e.clientY+(document.documentElement?document.documentElement:document.body).scrollTop)) + (document.getElementById("w3c")?document.getElementById("w3c").scrollTop:0);
				Darkroom.mousereversed = my<Darkroom.ganim.winH*3/8;
				Darkroom.mouseisdown = true;
				//Darkroom.ganim.manuallymoving = true;
				
				Darkroom.ganim.mdx = mx;
				Darkroom.ganim.mdy = my;
				Darkroom.ganim.mdOrigStep = Darkroom.ganim.step;
				
				Darkroom._galleryview_onmousemoveold = window.onmousemove ? window.onmousemove : function(){};
				window.onmousemove = function(e) {
					if(!e)
						e = window.event;
					if(!Darkroom || !Darkroom.ganim)
						return true;
					
					var mx = (e.pageX?e.pageX:(e.clientX+(document.documentElement?document.documentElement:document.body).scrollLeft)) + (document.getElementById("w3c")?document.getElementById("w3c").scrollLeft:0);
					var my = (e.pageY?e.pageY:(e.clientY+(document.documentElement?document.documentElement:document.body).scrollTop)) + (document.getElementById("w3c")?document.getElementById("w3c").scrollTop:0);
					
					//Darkroom.ganim.step = Math.round(( Darkroom.ganim.mdOrigStep + Math.round((mx-Darkroom.ganim.mdx)/Darkroom.galleryViewSensitivity*10)/10 * (Darkroom.mousereversed==true?-1:1) )*2)/2;
					//Darkroom.AnimateGalleryStep();
					
					Darkroom.ganim.step = Math.round(( Darkroom.ganim.mdOrigStep + Math.round((mx-Darkroom.ganim.mdx)/Darkroom.galleryViewSensitivity*10)/10 * (Darkroom.mousereversed==true?-1:1) )*10)/10;
					
					return Darkroom.NullFunction(e);
				};
				Darkroom._galleryview_onmouseupold = window.onmouseup ? window.onmouseup : function(){};
				window.onmouseup = function(e) {
					if(!e)
						e = window.event;
					
					//Darkroom.ganim.manuallymoving = false;
					Darkroom.mouseisdown = false;
					
					if(document.detachEvent) {
						document.detachEvent("onmousemove", function(){window.onmousemove(window.event)});
						document.detachEvent("onmouseup", function(){window.onmouseup(window.event)});
					}
					window.onmousemove = Darkroom._galleryview_onmousemoveold;
					window.onmouseup = Darkroom._galleryview_onmouseupold;
					
					return Darkroom.NullFunction(e);
				};
				
				if(document.attachEvent) {
					document.attachEvent("onmousemove", function(){window.onmousemove(window.event)});
					document.attachEvent("onmouseup", function(){window.onmouseup(window.event)});
				}
				return Darkroom.NullFunction(e);
			};
			//if(document.attachEvent)
			//	document.attachEvent("onmousedown", function(){window.onmousedown(window.event)});
		},
		ShowMessage : function(text,title,textIsEscaped) {
			if(textIsEscaped==true) {
				text = unescape(text);
				title = unescape(title);
			}
			
			if(this.currentMessageBox) {
				this.currentMessageBox.Close();
				this.currentMessageBox = null;
			}
			
			if(title!=null && title!="undefined")
				title = "<div class=\"gallery_title\">" + title + "</div>";
			else
				title = "";
			
			var winH = (window.innerHeight&&window.innerHeight>document.body.offsetHeight)?window.innerHeight:((document.documentElement&&document.documentElement.offsetHeight>document.body.offsetHeight)?document.documentElement.offsetHeight-5:document.body.offsetHeight);
			var dsocTop = navigator.appName.indexOf("Microsoft")>-1?document.body.scrollTop:window.pageYOffset;
			
			var d=document.createElement("div");
			d.id = "darkroom_fader_"+Math.round(Math.random()*1000,0);
			d.className = "darkroom_dimmer gallery_dimmer";
			d.style.cssText = "z-index:899; position:absolute; width:100%; height:"+winH+"px; left:0; top:0;";
			d.setAttribute("style",d.style.cssText);
			d.onclick = function(){ this.alert.Close(); };
			document.body.appendChild(d);
			
			var a=document.createElement("div");
			a.id = "darkroom_"+Math.round(Math.random()*1000,0);
			a.className = "darkroom_messagebox gallery_alertbox";
			a.style.cssText = "z-index:900;";
			a.setAttribute("style",a.style.cssText);
			a.innerHTML = title + text + "<span onclick=\"document.getElementById('"+a.id+"').Close();\" style=\"position:absolute; right:-10px; top:-10px; padding:0px 4px 0px 4px; font-weight:bold; color:#FFFFFF; background:#BB0000; border:1px solid #FF0000; cursor:pointer;\">&times;</span>";
			document.body.appendChild(a);
			a.fader = d;
			d.alert = a;
			a.Close = function() {
				document.body.removeChild(this.fader);
				document.body.removeChild(this);
				this.parentGallery.currentMessageBox = null;
			};
			
			a.parentGallery = this;
			this.currentMessageBox = a;
		},
		NullFunction : function(e,doBubbleCancel) {
			if(!e) e = window.event;
			if(doBubbleCancel===false) {
				if(e.preventBubble)
					e.preventBubble();
				if(e.StopPropagation)
					e.StopPropagation();
			}
			if(e.preventDefault)
				e.preventDefault();
			e.returnValue=false;
			return false;
		},
		HttpRequest : {
			"create"	: function(url,postdata,oncomplete,onerror,autoDelete){ return new this._obj(this,url,postdata,oncomplete,onerror,autoDelete); },
			"stop"		: function(request){ if(request&&request.type=="request") request.abort(); },
			"init"		: function(request,url){ if(request&&request.type=="request"){ request.get(url); } },
			"close"		: function(request){
				if(!request||request.type!="request")
					return false;
				try{ request.abort(); }catch(err){}
				for(var x=0; x<this._requests.length; x++)
					if(this._requests[x]==request)
						this._requests = this._requests.splice(x,1);
				try{ delete request; } catch(err){ request = null; }
			},
			"_obj"		: function(parent,url,postdata,oncomplete,onerror,autoDelete) {
				this.type = "request";
				this.parent = parent;
				this.url = url;
				this.postdata = (postdata && postdata.constructor && (postdata.constructor==Object || postdata.constructor==Function)) ? postdata : {};
				this.poststring = (postdata && typeof(postdata)==String) ? postdata : "";
				this.autoDelete = autoDelete!=false;
				this.data = null;
				this.async = true;
				this.http_code = null;
				this.status = null;
				this.responseText = null;
				this.responseXML = null;
				this.complete = false;
				this.oncomplete = oncomplete?oncomplete:function(){};
				this.onerror = onerror?onerror:function(){};
				this.request = this.parent.XMLHttpRequestObj();
				this.request.parent = this;
				this.request.onreadystatechange = function() {
					this.parent.readyState = this.readyState;
					if(this.readyState==4) {
						this.parent.http_code = this.status;
						this.parent.status = this.status;
						this.parent.responseText = this.responseText;
						this.parent.responseXML = this.responseXML;
						this.parent.data = this.responseText;
						this.parent.complete = true;
						this.parent.error = this.status!=200;
						if(this.status==200 && this.parent.oncomplete)
							try{ this.parent.oncomplete(); }catch(err){ window.setTimeout("throw(new Error(\"request.oncomplete > "+err.toString()+"\"));",1); }
						else if(this.status!=200 && this.parent.onerror)
							try{ this.parent.onerror(); }catch(err){ window.setTimeout("throw(new Error(\"request.oncomplete > "+err.toString()+"\"));",1); }
						if(this.parent.autoDelete==true)
							this.parent.parent.close(this);
					}
				};
				this.get = function(url){
					if(url!=null && url==url+"" && url.length>0)
						this.url = url;
					this._poststring = this.poststring?this.poststring:"";
					for(var x in this.postdata)
						this._poststring += "&" + escape(x) + "=" + escape(this.postdata[x]);
					if(this._poststring.indexOf("&")==0)
						this._poststring = this._poststring.substring(1,this._poststring.length);
					this.request.open(this._poststring.length>2?"POST":"GET", this.url, this.async);
					this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					try {
						this.request.setRequestHeader("Content-length", this._poststring.length);
						this.request.setRequestHeader("Connection", "close");
					}catch(e){}
					this.request.send(this._poststring.length>2?this._poststring:null);
					return this;
				};
				this.send = this.get;
				this.Send = this.get;
				this.Get = this.get;
				this.abort = function(){
					if(this.readyState!=4)
						return this;
					this._oldOnError = this.onerror;
					this.onerror = function(){};
					this.request.abort();
					this.onerror = this._oldOnError;
					try{ delete this._oldOnError; }catch(e){ this._oldOnError=null; }
					return this;
				};
				this.Abort = this.abort;
				this.cancel = this.abort;
				this.Cancel = this.abort;
				this.stop = this.abort;
				this.Stop = this.abort;
				this.parent._requests.push(this);
			},
			"XMLHttpRequestObj" : function() {
				var xmlHttp=null;
				try { xmlHttp=new XMLHttpRequest(); }
				catch (e) {
					try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
					catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
				}
				return xmlHttp;
			},
			"_requests" : new Array()
		}
	};
	return obj;
})();













function upload(form, file, name, description, realname, galleryid) {
	var framenumber = 1;
	while(document.getElementById("upload_fileframe"+framenumber))
		framenumber++;
	
	var upload_frame = document.createElement("iframe");
	upload_frame.setAttribute('src', 'about:blank');
	upload_frame.setAttribute("name", "upload_fileframe"+framenumber);
	upload_frame.name = "upload_fileframe"+framenumber;
	upload_frame.id = "upload_fileframe"+framenumber;
	form.target = upload_frame.name;
	
	
	upload_frame.style.cssText = "position:absolute; visibility:hidden; display:none; left:-999em; height:0px; width:0px;";
	upload_frame.style.display = "none";
	upload_frame.height = "0";
	upload_frame.width = "0";
	upload_frame.style.left = "-999em";
	upload_frame.params = "image_file="+file+"&image_description="+description+"&image_name="+name+"&image_realname="+realname+"&image_galleryid="+galleryid;
	document.body.appendChild(upload_frame);
	upload_frame.onload = function() {
		xmlhttp_post(site_root+"gallery/?ext=upload_check&sid="+Math.random(), this.params, upload_frame.id, "upload_completed(\""+upload_frame.id+"\");");
	};
	
	var iFrameID = upload_frame.id;
	//if(document.all)
		//captureEvent('load', xmlhttp_post(site_root+"gallery/?ext=upload_check&sid="+Math.random(), this.params, upload_frame.id, "upload_completed(\""+upload_frame.id+"\");") , upload_frame); 
	if(self.frames[iFrameID].name != iFrameID) {
		/* *** IMPORTANT: This is a BUG FIX for Internet Explorer *** */
		self.frames[iFrameID].name = iFrameID;
	}
	
	if(framenumber==1)
		document.getElementById('image_statustext').innerHTML = 'Uploading image '+(document.getElementById('image_name').value.length>0?document.getElementById('image_name').value:document.getElementById('image_realname').value)+', please wait...';
	else {
		var cnt=0;
		var iframes = document.getElementsByTagName('iframe');
		for(var i in iframes)
			if(iframes[i] && iframes[i].id && iframes[i].id.indexOf('upload_fileframe')==0)
				cnt++;
		document.getElementById('image_statustext').innerHTML = "Please Wait, uploading "+cnt+" images...";
	}
	
	/*
	document.getElementById('image_name').disabled = true;
	document.getElementById('image_description').disabled = true;
	*/
	setTimeout("document.getElementById('image_name').value = ''; document.getElementById('image_description').value = '';", 100);
}

function upload_completed(frameid) {
	var JSONtext = xmlhttp_postdata[frameid].responseText;
	var JSON = eval('('+xmlhttp_postdata[frameid].responseText+')');
	
	setTimeout("document.body.removeChild(document.getElementById('"+frameid+"'));",700);

	document.getElementById('image_statustext').innerHTML = JSON["message"];
	//var iframes = document.getElementsByTagName('iframe'); var cnt=0; for(var i in iframes) { if(iframes[i] && iframes[i].id && iframes[i].id.indexOf('upload_fileframe')==0) cnt++; }
	setTimeout("var iframes = document.getElementsByTagName('iframe'); var cnt=0; for(var i in iframes) { if(iframes[i] && iframes[i].id && iframes[i].id.indexOf('upload_fileframe')==0) cnt++; } if(cnt>0) { document.getElementById('image_statustext').innerHTML = 'Please Wait, uploading '+cnt+' images...'; } else { document.getElementById('image_statustext').innerHTML = ''; }", 1050);
	
	//alert(JSONtext);
	//alert(document.getElementById('gallery_display_image_'+JSON['galleryid']).src);
	if(document.getElementById('gallery_display_image_'+JSON['galleryid']) && document.getElementById('gallery_display_image_'+JSON['galleryid']).className=="gallery_display_image_noimages") {
		document.getElementById('gallery_display_image_'+JSON['galleryid']).className = "gallery_display_image";
		document.getElementById('gallery_display_image_'+JSON['galleryid']).src = "?ext=thumb&size=80&id="+JSON['id'];
	}
	/*
	document.getElementById('image_name').disabled = false;
	document.getElementById('image_description').disabled = false;
	document.getElementById('image_name').value = '';
	document.getElementById('image_description').value = '';
	*/
}



function newgallery(iname, idescription) {
	var unique = Math.floor(Math.random()*999999);
	var params = "newgallery_name="+escape(iname)+"&newgallery_description="+escape(idescription)+"&ext=new_gallery";
	xmlhttp_post(site_root+"gallery/?ext=new_gallery&sid="+Math.random(), params, unique, "gallery_created("+unique+");");
	document.getElementById('newgallery_statustext').innerHTML = "Creating Gallery, Please Wait...";
	document.getElementById('newgallery_name').disabled = "disabled";
	document.getElementById('newgallery_description').disabled = "disabled";
}

function gallery_created(xmlhttpid) {
	var JSONtext = xmlhttp_postdata[xmlhttpid].responseText;
	var JSON = eval('('+xmlhttp_postdata[xmlhttpid].responseText+')');
	document.getElementById('newgallery_statustext').innerHTML = JSON["message"];
	setTimeout("document.getElementById('newgallery_statustext').innerHTML = '';", 1000);
	if(JSON['result']=="failed" || JSON['result']==false) {
		document.getElementById('newgallery_name').disabled = null;
		document.getElementById('newgallery_description').disabled = null;
		var nameField = document.getElementById('newgallery_name');
		nameField.focus();
		try{nameField.select();}catch(e){}
		try{nameField.SelectAll();}catch(e){}
		return false;
	}
	//var newHTML = "<div id=\"gallery_display_row_"+JSON['id']+"\" class=\"gallery_display_row\" onmouseover=\"className+=' hover';\" onmouseout=\"className=className.substring(0, className.indexOf(' hover'));\">\n";
	var newDiv = document.createElement("div");
	newDiv.id = "gallery_display_row_"+JSON['galleryid'];
	newDiv.className = "gallery_display_row";
	newDiv.onmouseover = function() { this.className+=' hover'; };
	newDiv.onmouseout = function() { if(this.className.indexOf(' hover') > -1) this.className=this.className.substring(0, this.className.indexOf(' hover')); };
	var newHTML = "<span class=\"gallery_display_name\">"+JSON['name']+"</span>\n";
	newHTML += "<img class=\"gallery_display_image_noimages\" id=\"gallery_display_image_"+JSON['galleryid']+"\" src=\"gallery_noimages.gif\" />\n";
	newHTML += "<span class=\"gallery_display_created\">"+JSON['created']+"</span>";
	newHTML += "</div>\n";
	window.gallery_display.innerHTML += newHTML;
	newDiv.innerHTML += newHTML;
	window.gallery_display.getElementsByTagName("div")[0].appendChild(newDiv);
	
	document.getElementById("image_galleryid").innerHTML += "\t<option id=\"image_galleryid_option_"+JSON['galleryid']+"\" value=\""+JSON['galleryid']+"\">"+JSON['name']+"</option>\n";
	
	document.getElementById('newgallery_name').disabled = null;
	document.getElementById('newgallery_description').disabled = null;
	document.getElementById('newgallery_name').value = '';
	document.getElementById('newgallery_description').value = '';
	
	var nameField = document.getElementById('newgallery_name');
	nameField.focus();
	try{nameField.select();}catch(e){}
	try{nameField.SelectAll();}catch(e){}
	

	var c = document.getElementById("gallery_display_row_"+JSON['galleryid']);
	c.onclick = galleryItemClick;
	c.onmousedown = galleryItemMousedown;
	c.onmouseup = galleryItemMouseup;
	c.onmousemove = galleryItemMousemove;
	c.oncontextmenu = function(e) { return false; };
	
	window.gallery_resizeInnerWrap();
}


function deletegallery(galleryid) {
	if(window.accesslevel!="superadmin")
		return false;
	var x = confirm("Permanently delete this gallery and all contained photos?");
	if(x!=true)
		return false;
	var unique = galleryid+Math.random();
	var params = "id="+galleryid+"&ext=delete_gallery";
	xmlhttp_post(site_root+"gallery/?ext=delete_gallery&sid="+Math.random(), params, unique, "gallery_deleted(\""+unique+"\");");
}

function gallery_deleted(xmlhttpid) {
	var JSONtext = xmlhttp_postdata[xmlhttpid].responseText;
	var JSON = eval('('+xmlhttp_postdata[xmlhttpid].responseText+')');
	if( JSON && JSON.result && JSON.result==true ) {
		// remove the gallery
		document.getElementById("gallery_display_row_"+JSON['galleryid']).parentNode.removeChild(document.getElementById("gallery_display_row_"+JSON['galleryid']));
		// remove the <option> from <select>
		document.getElementById("image_galleryid").removeChild(document.getElementById("image_galleryid_option_"+JSON['galleryid']));
	}
	else {
		if(JSON && JSON.message)
			alert("Could not delete gallery. "+JSON.message);
		else
			alert("Could not delete gallery.");
	}
	
	window.gallery_resizeInnerWrap();
}





// for non-slideshow image deletion:

function deleteImage(imageID) {
	if(window.accesslevel!="admin" && window.accesslevel!="superadmin")
		return false;
	
	var xid = "deleteImage_"+Math.floor(Math.random()*99999999);
	var params = "id="+imageID+"&ext=delete_image";
	if(confirm("Delete this photo?"))
		xmlhttp_post(site_root+"gallery/?ext=delete_image&sid="+xid, params, xid, "image_deleted(\""+xid+"\","+imageID+");");
}

function image_deleted(xmlhttpid,imageID) {
	var item = document.getElementById("gallery_viewer_item_"+imageID);
	if(item && item.id) {
		item.parentNode.removeChild(item);
	}
}


function renameImage(imageID,newname,newdescription,process,xmlhttpid) {
	if(process!="update" && process!="visual") {
		var winH = (window.innerHeight&&window.innerHeight>document.body.offsetHeight)?window.innerHeight:((document.documentElement&&document.documentElement.offsetHeight>document.body.offsetHeight)?document.documentElement.offsetHeight-5:document.body.offsetHeight);
		var dsocTop = window.pageYOffset?window.pageYOffset:document.body.scrollTop;
	
		var d=document.createElement("div");
		d.id = "renameImage_fader_"+Math.round(Math.random()*1000,0);
		d.className = "gallery_dimmer";
		d.style.cssText = "z-index:999; position:absolute; width:100%; height:"+winH+"px; left:0; top:0;";
		d.setAttribute("style",d.style.cssText);
		d.onclick = function(){ this.renameImage.Close(); };
		document.body.appendChild(d);
	
		var a=document.createElement("div");
		a.imageID = imageID;
		a.id = "renameImage_box";
		a.className = "gallery_renamebox";
		a.style.cssText = "z-index:1000;";
		a.setAttribute("style",a.style.cssText);
		document.body.appendChild(a);
		a.fader = d;
		d.renameImage = a;
		a.Close = function() {
			document.body.removeChild(this.fader);
			document.body.removeChild(this);
		};
		a.onselectstart = null;
		a.onmousedown = null;
		a.onmouseup = null;
		
		var closer = document.createElement("span");
		closer.parentForm = a;
		closer.style.cssText = "position:absolute; right:-10px; top:-10px; padding:0px 4px 0px 4px; font-weight:bold; color:#FFF; background:#B00; border:1px solid #F00; cursor:pointer;";
		closer.setAttribute("style",closer.style.cssText);
		closer.onclick = function() {
			this.parentForm.Close();
		};
		closer.innerHTML = "&times;";
		a.appendChild(closer);
		
		var title = document.createElement("div");
		title.innerHTML = "Rename Photo";
		title.className = "gallery_title";
		a.appendChild(title);
		
		var nameWrap = document.createElement("div");
		nameWrap.className = "renameImage_input_wrap";
		a.appendChild(nameWrap);
		
		var nameTitle = document.createElement("span");
		nameTitle.className = "renameImage_input_title";
		nameTitle.innerHTML = "Name";
		nameWrap.appendChild(nameTitle);
		
		var nameField = document.createElement("input");
		nameField.type = "text";
		nameField.value = unescape(newname);	//oldname
		nameField.className = "renameImage_input text renameImage_newname";
		nameWrap.appendChild(nameField);
		a.nameField = nameField;
		nameField.focus();
		try{nameField.select();}catch(e){}
		try{nameField.SelectAll();}catch(e){}
		
		var descWrap = document.createElement("div");
		descWrap.className = "renameImage_input_wrap";
		a.appendChild(descWrap);
		
		var descTitle = document.createElement("span");
		descTitle.className = "renameImage_input_title";
		descTitle.innerHTML = "Description";
		descWrap.appendChild(descTitle);
		
		var descField = document.createElement("input");
		descField.type = "text";
		descField.value = unescape(newdescription);	//olddescription
		descField.className = "renameImage_input text renameImage_newname";
		descWrap.appendChild(descField);
		a.descField = descField;
		
		var buttonWrap = document.createElement("div");
		buttonWrap.className = "renameImage_input_wrap";
		a.appendChild(buttonWrap);
		
		var cancelBtn = document.createElement("input");
		cancelBtn.parentForm = a;
		cancelBtn.type = "reset";
		cancelBtn.value = "Cancel";
		cancelBtn.className = "renameImage_input button renameImage_cancel";
		cancelBtn.onclick = function() {
			this.parentForm.Close();
			return false;
		};
		buttonWrap.appendChild(cancelBtn);
		
		var okBtn = document.createElement("input");
		okBtn.parentForm = a;
		okBtn.type = "submit";
		okBtn.value = "Save Changes";
		okBtn.className = "renameImage_input button renameImage_ok renameImage_saveChanges";
		okBtn.onclick = function() {
			var imgname = this.parentForm.nameField.value;
			while(imgname!=(imgname=imgname.replace("\"","&quot;"))){}
			while(imgname!=(imgname=imgname.replace("\'","&apos;"))){}
			var imgdesc = this.parentForm.descField.value;
			while(imgdesc!=(imgdesc=imgdesc.replace("\"","&quot;"))){}
			while(imgdesc!=(imgdesc=imgdesc.replace("\'","&apos;"))){}
			renameImage(this.parentForm.imageID, imgname, imgdesc, "update");
			return false;
		};
		buttonWrap.appendChild(okBtn);
	}
	else if(process=="update") {
		var xid = "rename_image_"+Math.floor(Math.random()*99999999);
		var params = "id="+imageID+"&image_newname="+newname+"&image_newdescription="+newdescription+"&ext=rename_image";
		xmlhttp_post(site_root+"gallery/?ext=rename_image&sid="+xid, params, xid, "renameImage("+imageID+",null,null,\"visual\",\""+xid+"\");");
	}
	else if(process=="visual") {
		var box = document.getElementById("renameImage_box");
		var response = eval('('+xmlhttp_postdata[xmlhttpid].responseText+')');
		if(response.result == true) {
			var item = document.getElementById("gallery_viewer_item_"+imageID);
			if(item && item.id) {
				item.imgname.innerHTML = box.nameField.value;
				item.imgdesc.innerHTML = box.descField.value;
			}
		}
		else {
			gallery_alert("There was an error when Gallery2 tried to rename the requested photo.","Error - Photo Gallery");
		}
		if(box)
			box.Close();
	}
}


function setGalleryDisplayImage(imageID, galleryID) {
	var xid = "setdisplayimage_"+Math.floor(Math.random()*99999999);
	var params = "gallery_id="+galleryID+"&displayimage_id="+imageID+"&ext=set_display_image";
	xmlhttp_post(site_root+"gallery/?ext=set_display_image&sid="+xid, params, xid, "setGalleryDisplayImage_complete(\""+xid+"\","+imageID+","+galleryID+");");
}

function setGalleryDisplayImage_complete(xmlhttpid,imageID,galleryID) {
	var img = document.getElementById("gallery_display_image_"+galleryID);
	img.src = "?ext=thumb&size=80&id=" + imageID;
}







function renameGallery(galleryID,newname,newdescription,process,xmlhttpid) {
	if(process!="update" && process!="visual") {
		var winH = (window.innerHeight&&window.innerHeight>document.body.offsetHeight)?window.innerHeight:((document.documentElement&&document.documentElement.offsetHeight>document.body.offsetHeight)?document.documentElement.offsetHeight-5:document.body.offsetHeight);
		var dsocTop = window.pageYOffset?window.pageYOffset:document.body.scrollTop;
	
		var d=document.createElement("div");
		d.id = "renameGallery_fader_"+Math.round(Math.random()*1000,0);
		d.className = "gallery_dimmer";
		d.style.cssText = "z-index:995; position:absolute; width:100%; height:"+winH+"px; left:0; top:0;";
		d.setAttribute("style",d.style.cssText);
		d.onclick = function(){ this.renameGallery.Close(); };
		document.body.appendChild(d);
	
		var a=document.createElement("div");
		a.galleryID = galleryID;
		a.id = "renameGallery_box";
		a.className = "gallery_renamebox";
		a.style.cssText = "z-index:999;";
		a.setAttribute("style",a.style.cssText);
		document.body.appendChild(a);
		a.fader = d;
		d.renameGallery = a;
		a.Close = function() {
			document.body.removeChild(this.fader);
			document.body.removeChild(this);
		};
		a.onselectstart = null;
		a.onmousedown = null;
		a.onmouseup = null;
		
		var closer = document.createElement("span");
		closer.parentForm = a;
		closer.style.cssText = "position:absolute; right:-10px; top:-10px; padding:0px 4px 0px 4px; font-weight:bold; color:#FFFFFF; background:#BB0000; border:1px solid #FF0000; cursor:pointer;";
		closer.setAttribute("style",closer.style.cssText);
		closer.onclick = function() {
			this.parentForm.Close();
		};
		closer.innerHTML = "&times;";
		a.appendChild(closer);
		
		var title = document.createElement("div");
		title.innerHTML = "Rename Gallery";
		title.className = "gallery_title";
		a.appendChild(title);
		
		var nameWrap = document.createElement("div");
		nameWrap.className = "renameGallery_input_wrap";
		a.appendChild(nameWrap);
		
		var nameTitle = document.createElement("span");
		nameTitle.className = "renameGallery_input_title";
		nameTitle.innerHTML = "Name";
		nameWrap.appendChild(nameTitle);
		
		var nameField = document.createElement("input");
		nameField.type = "text";
		nameField.className = "renameGallery_input text renameImage_newname";
		nameWrap.appendChild(nameField);
		nameField.value = unescape(newname);	//oldname
		a.nameField = nameField;
		nameField.focus();
		try{nameField.select();}catch(e){}
		try{nameField.SelectAll();}catch(e){}
		
		var descWrap = document.createElement("div");
		descWrap.className = "renameGallery_input_wrap";
		a.appendChild(descWrap);
		
		var descTitle = document.createElement("span");
		descTitle.className = "renameGallery_input_title";
		descTitle.innerHTML = "Description";
		descWrap.appendChild(descTitle);
		
		var descField = document.createElement("textarea");
		descField.className = "renameGallery_input text renameGallery_newname";
		descWrap.appendChild(descField);
		descField.value = unescape(newdescription);	//olddescription
		a.descField = descField;
		
		var buttonWrap = document.createElement("div");
		buttonWrap.className = "renameGallery_input_wrap";
		a.appendChild(buttonWrap);
		
		var cancelBtn = document.createElement("input");
		cancelBtn.parentForm = a;
		cancelBtn.type = "reset";
		cancelBtn.value = "Cancel";
		cancelBtn.className = "renameGallery_input gallery_button renameGallery_cancel";
		cancelBtn.onclick = function() {
			this.parentForm.Close();
			return false;
		};
		buttonWrap.appendChild(cancelBtn);
		
		var okBtn = document.createElement("input");
		okBtn.parentForm = a;
		okBtn.type = "submit";
		okBtn.value = "Save Changes";
		okBtn.className = "renameGallery_input gallery_button renameGallery_ok renameGallery_saveChanges";
		okBtn.onclick = function() {
			var galname = this.parentForm.nameField.value;
			//while(galname!=(galname=galname.replace("\"","&quot;"))){}
			//while(galname!=(galname=galname.replace("\'","&apos;"))){}
			var galdesc = this.parentForm.descField.value;
			//while(galdesc!=(galdesc=galdesc.replace("\"","&quot;"))){}
			//while(galdesc!=(galdesc=galdesc.replace("\'","&apos;"))){}
			renameGallery(this.parentForm.galleryID, galname, galdesc, "update");
			return false;
		};
		buttonWrap.appendChild(okBtn);
	}
	else if(process=="update") {
		var xid = "rename_gallery_"+Math.floor(Math.random()*99999999);
		var params = "id="+galleryID+"&gallery_newname="+escape(newname)+"&gallery_newdescription="+escape(newdescription)+"&ext=rename_gallery";
		xmlhttp_post(site_root+"gallery/?ext=rename_gallery&sid="+xid, params, xid, "renameGallery("+galleryID+",null,null,\"visual\",\""+xid+"\");");
	}
	else if(process=="visual") {
		var box = document.getElementById("renameGallery_box");
		var response = eval('('+xmlhttp_postdata[xmlhttpid].responseText+')');
		if(response.result == true) {
			var item = document.getElementById("gallery_display_row_"+galleryID);
			if(item && item.id) {
				var gi_contents = item.getElementsByTagName("*");
				for(var x in gi_contents) {
					if(gi_contents[x].className && gi_contents[x].className=="gallery_display_name")
						gi_contents[x].innerHTML = box.nameField.value;
					if(gi_contents[x].className && gi_contents[x].className=="gallery_display_description")
						gi_contents[x].innerHTML = box.descField.value;
				}
			}
		}
		else
			gallery_alert("There was an error when Gallery2 tried to rename the requested photo gallery.<br />"+response.message,"Error - Photo Gallery");
		if(box)
			box.Close();
	}
}





window.currentKeyDowns = new Array();

function gallery_onload() {
		// CTRL+G = show/hide gallery creation dialog
	window.onKeyDownEvents.push(function(newkey){ if(window.isKeyDown(17) && newkey==71) { showHideCreateGalleryBox(); return "preventDefault"; } });
		// CTRL+I = show/hide gallery creation dialog
	window.onKeyDownEvents.push(function(newkey){ if(window.isKeyDown(17) && newkey==73) { showHideImageUploadBox(); return "preventDefault"; } });
		// CTRL+J = open image selection dialog
	window.onKeyDownEvents.push(function(newkey){ if(window.isKeyDown(17) && newkey==74 && window.imageUploadBox_visible==true) { document.getElementById('image_file').focus(); return "preventDefault"; } });
		// CTRL+ENTER = submit image/gallery form
	window.onKeyDownEvents.push(function(newkey){ if(window.isKeyDown(17) && newkey==13) { if(window.createGalleryBox_visible==true) document.getElementById('newgallery_submit').onclick(); else if(window.imageUploadBox_visible==true) document.getElementById('image_submit').click();  return "preventDefault"; } });
		// [right arrow key] = next image
	window.onKeyDownEvents.push(function(newkey){ if(newkey==39 && document.getElementById("slideshow_showpic_frame") && !slideshow_isPreloading && !setting_image) { md_bounceicon("Next Image",1,"name"); nextimage(); return "preventDefault"; } });
		// [left arrow key] = previous image
	window.onKeyDownEvents.push(function(newkey){ if(newkey==37 && document.getElementById("slideshow_showpic_frame") && !slideshow_isPreloading && !setting_image) { md_bounceicon("Previous Image",1,"name"); previmage(); return "preventDefault"; } });
		// [esc] = close slideshow
	window.onKeyDownEvents.push(function(newkey){ if(newkey==27 && document.getElementById("slideshow_showpic_frame") && !slideshow_isPreloading && !setting_image) { showpic_stop(); return "preventDefault"; } });
		// [v] = download iamge
	window.onKeyDownEvents.push(function(newkey){ if(newkey==86 && document.getElementById("slideshow_showpic_frame") && !slideshow_isPreloading && !setting_image) { imagePOP(); return "preventDefault"; } });
		// [d] = download iamge
	window.onKeyDownEvents.push(function(newkey){ if(newkey==68 && document.getElementById("slideshow_showpic_frame") && !slideshow_isPreloading && !setting_image) { slideshow_imageDL(); return "preventDefault"; } });
	
	window.gallery_display = document.getElementById("gallery_display");
	
	//document.oncontextmenu = function(e) { return false; };
	//document.onmousedown = function(e) { if(!e) e=window.event; if(e.returnValue) e.returnValue=false; return false; };
	gallery_display.oncontextmenu = window.gallery_NullFunction;
	window.gallery_display.onselectstart = window.gallery_NullFunction;
	window.gallery_display.onmousedown = window.galleryDisplayMousedown;
	
	xmlhttp_get(site_root+"gallery/?ext=accesslevel&sid="+Math.random(), "accesslevel", "setAccessLevel();");
	window.setAccessLevel = function() {
		window.accesslevel = xmlhttp_data["accesslevel"].responseText;
	};
	
	var current_file_input = null;
	var divs = document.getElementsByTagName("*");
	for(var n=0; n<divs.length; n++) {
		var c = divs[n];
		if(c.className && c.className.indexOf("gallery_display_row") > -1) {
			c.gid = Math.round( c.id.substring(c.id.lastIndexOf("_")+1,c.id.length) );
			c.onclick = galleryItemClick;
			c.onmouseover = function(){ this.className=this.className.replace(/\shover/mgi,"")+" hover"; };
			c.onmouseout = function(){ this.className=this.className.replace(/\s(hover|pressed)/mgi,""); };
			c.onmousedown = galleryItemMousedown;
			c.onmouseup = galleryItemMouseup;
			c.onmousemove = galleryItemMousemove;
			c.oncontextmenu = window.gallery_NullFunction;
		}
		else if(c.id && c.id.indexOf("gallery_display_image") > -1) {
			c.onclick = galleryImageClick;
			c.onmousedown = galleryImageMousedown;
			c.onmouseup = galleryImageMouseup;
			c.onmousemove = galleryImageMousemove;
			c.oncontextmenu = window.gallery_NullFunction;
		}
		else if(c.className && c.className.match(/\bgallery_button\b/mgi)!=null) {
			c.onmouseover = function(){ this.className=this.className.replace(/\shover/mgi,"")+" hover"; };
			c.onmouseout = function(){ this.className=this.className.replace(/\s(hover|pressed)/mgi,""); };
			c.onmousedown = function(){ this.className=this.className.replace(/\spressed/mgi,"")+" pressed"; };
			c.onmouseup = function(){ this.className=this.className.replace(/\s(hover|pressed)/mgi,"")+" pressed hover"; };
			c.oncontextmenu = window.gallery_NullFunction;
		}
		else if(c.className && c.className.match(/\bgallery_filebox\b/mgi)!=null) {
			c.onmouseover = function(){ this.i.className=this.i.className.replace(/\shover/mgi,"")+" hover"; };
			c.onmouseout = function(){ this.i.className=this.i.className.replace(/\s(hover|pressed)/mgi,""); };
			c.onmousedown = function(){ this.i.className=this.i.className.replace(/\spressed/mgi,"")+" pressed"; };
			c.onmouseup = function(){ this.i.className=this.i.className.replace(/\s(hover|pressed)/mgi,"")+" pressed hover"; };
			current_file_input = c;
		}
		if(current_file_input && c.className && c.className.match(/\binput_file_cover\b/mgi)!=null)
			current_file_input.i = c;
	}
	
	
	window.gallery_resizeInnerWrap();
	
	window.gallery_onloadOld();
}



window.gallery_resizeInnerWrap = function(countOffset) {
	window.gallery_display.getElementsByTagName("*")[0].style.width = "9999em";
	var divs = document.getElementsByTagName("*");
	var sample1 = null;
	var sample2 = null;
	var count = 0;
	for(var n=0; n<divs.length; n++) {
		var c = divs[n];
		if(c.className && c.className.indexOf("gallery_display_row") > -1) {
			if(!sample1)
				sample1 = c;
			else if(!sample2)
				sample2 = c;
			count++;
		}
	}
	if(countOffset!=null && countOffset!="undefined" && parseInt(countOffset) && (countOffset>0 || countOffset<0) )
		count += countOffset;
	window.gallery_display.getElementsByTagName("*")[0].style.width = ((sample2.offsetLeft-sample1.offsetLeft)*count + sample1.offsetLeft)+"px";
	var oldscroll = window.gallery_display.scrollLeft;
	window.gallery_display.scrollLeft = 0;
	window.gallery_display.scrollLeft = oldscroll;
};





window.gallery_NullFunction = function(e,doBubbleCancel) {
	if(!e) e = window.event;
	if(doBubbleCancel===false) {
		if(e.preventBubble)
			e.preventBubble();
		if(e.StopPropagation)
			e.StopPropagation();
	}
	if(e.preventDefault)
		e.preventDefault();
	e.returnValue=false;
	return false;
};










function galleryDisplayMousedown(e) {
	if(!e) e=window.event;
	if(this.flickTimer) {
		try{ window.clearTimeout(this.flickTimer); }catch(e){}
		this.flickTimer = null;
		this.isFlicking = false;
		this.isDragging = false;
		this.mousedown = false;
	}
	this.mdx = (e.pageX?e.pageX:(e.clientX+(document.documentElement?document.documentElement:document.body).scrollLeft)) + (document.getElementById("w3c")?document.getElementById("w3c").scrollLeft:0);
	this.mdy = (e.pageY?e.pageY:(e.clientY+(document.documentElement?document.documentElement:document.body).scrollTop)) + (document.getElementById("w3c")?document.getElementById("w3c").scrollTop:0);
	this.mousedown_scrollLeft = this.scrollLeft;
	this.mousedown_scrollTop = this.scrollTop;
	this.mousedown = true;
	this.hasDragged = false;
	this.isFlicking = false;
	this.lastDiffX = null;
	this.lastDiffY = null;
	this.justPressed = true;
	this.className = this.className.replace(/\s(mousedown|dragging|flicking)/mgi,"") + " mousedown";
	window.gallery_current = this;
	this._oldwinmousemove = window.onmousemove ? window.onmousemove : function(){};
	window.onmousemove = function(e){
		if(!e) e=window.event;
		return window.galleryDisplayMousemove.apply(window.gallery_current, arguments);
	};
	this._oldwinmouseup = window.onmouseup ? window.onmouseup : function(){};
	window.onmouseup = function(e){
		if(!e) e=window.event;
		
		window.onmousemove = window.gallery_current._oldwinmousemove;
		window.onmouseup = window.gallery_current._oldwinmouseup;
		if(document.attachEvent) {
			document.detachEvent("onmousemove", function(){window.onmousemove(window.event)});
			document.detachEvent("onmouseup", function(){window.onmouseup(window.event)});
		}
		
		return window.galleryDisplayMouseup.apply(window.gallery_current, arguments);
	};
	if(document.attachEvent) {
		document.attachEvent("onmousemove", function(){window.onmousemove(window.event)});
		document.attachEvent("onmouseup", function(){window.onmouseup(window.event)});
	}
	return window.gallery_NullFunction(e);
}



function galleryDisplayMouseup(e) {
	if(!e) e=window.event;
	if(this.flickTimer) {
		try{ window.clearTimeout(this.flickTimer); }catch(e){}
		this.flickTimer = null;
	}
	this.mousedown = false;
	this.hasDragged = false;
	this.isFlicking = false;
	this.className = this.className.replace(/\s(mousedown|dragging|flicking)/mgi,"");
	
	var date = new Date();
	var time = date.getMinutes()*60000 + date.getSeconds()*1000 + date.getMilliseconds();
	
	if( time-this.lastMoved < 100 && (Math.abs(this.lastDiffX)>1 || Math.abs(this.lastDiffY)>1) && (Math.abs(this.lastX-this.mdx)>20 || Math.abs(this.lastY-this.mdy)>20) ) {
		this.flickDiffX = Math.abs(this.lastDiffX)<1 ? 1 : this.lastDiffX;
		this.flickDiffY = Math.abs(this.lastDiffY)<1 ? 1 : this.lastDiffY;
		this.flickRatioX = Math.abs(this.flickDiffX) / Math.sqrt(Math.pow(this.flickDiffX,2)+Math.pow(this.flickDiffY,2));
		this.flickRatioY = Math.abs(this.flickDiffY) / Math.sqrt(Math.pow(this.flickDiffX,2)+Math.pow(this.flickDiffY,2));
		this.flickTimer = window.setInterval("galleryDisplayFlick.apply(document.getElementById('"+this.id+"'));", 30);
		this.isFlicking = true;
		this.className += " flicking";
	}
	this.lastDiffX = null;
	this.lastDiffY = null;
	return window.gallery_NullFunction(e);
}

function galleryDisplayFlick() {
	if(Math.abs(this.flickDiffY)>=1 || Math.abs(this.flickDiffX)>=1) {
		this.flickDiffY -= this.flickDiffY/Math.abs(this.flickDiffY) * 3/2 * this.flickRatioY;
		this.flickDiffX -= this.flickDiffX/Math.abs(this.flickDiffX) * 3/2 * this.flickRatioX;
		var ot = this.scrollTop+0;
		var ol = this.scrollLeft+0;
		this.scrollTop = this.scrollTop + this.flickDiffY;
		this.scrollLeft = this.scrollLeft + this.flickDiffX;
		if(this.scrollTop==ot && this.scrollLeft==ol) {
			try{ window.clearTimeout(this.flickTimer); }catch(e){}
			this.flickTimer = null;
			this.isFlicking = false;
			this.mousedown = false;
			this.className = this.className.replace(/\s(flicking|mousedown|dragging)/mgi,"");
		}
	}
	else {
		try{ window.clearTimeout(this.flickTimer); }catch(e){}
		this.flickTimer = null;
		this.isFlicking = false;
		this.className = this.className.replace(/\sflicking/mgi,"");
	}
}


function galleryDisplayMousemove(e) {
	if(!e) e=window.event;
	var mx = (e.pageX?e.pageX:(e.clientX+(document.documentElement?document.documentElement:document.body).scrollLeft)) + (document.getElementById("w3c")?document.getElementById("w3c").scrollLeft:0);
	var my = (e.pageY?e.pageY:(e.clientY+(document.documentElement?document.documentElement:document.body).scrollTop)) + (document.getElementById("w3c")?document.getElementById("w3c").scrollTop:0);
	
	if(Math.abs(this.lastX-mx)<1 && Math.abs(this.lastY-my)<1)
		return false;
	
	if(this.mousedown==true) {
		if(this.flickTimer) {
			try{ window.clearTimeout(this.flickTimer); }catch(e){}
			this.flickTimer = null;
			this.isFlicking = false;
		}
		var date = new Date();
		this.lastMoved = date.getMinutes()*60000 + date.getSeconds()*1000 + date.getMilliseconds();
		
		if(this.justPressed==true) {
			this.mdx = mx;
			this.mdy = my;
		}
		this.justPressed = false;
		
		if( this.hasDragged!=true && (Math.abs(mx-this.mdx)>20 || Math.abs(my-this.mdx)>20) ) {
			this.className = this.className.replace(/\sdragging/mgi,"") + " dragging";
			this.hasDragged = true;
		}
		
		var newLeft = this.mousedown_scrollLeft - (mx-this.mdx);
		var newTop = this.mousedown_scrollTop - (my-this.mdy);
		
		this.lastDiffX = newLeft - this.scrollLeft;
		this.lastDiffY = newTop - this.scrollTop;
		
		this.scrollLeft = Math.round(newLeft*10)/10;
		this.scrollTop = Math.round(newTop*10)/10;
		
		this.lastX = mx;
		this.lastY = my;
	}
	return window.gallery_NullFunction(e);
}











// gallery displays:

function galleryItemClick(e) {
	if(!e) e=window.event;
	return window.gallery_NullFunction(e);
}

function galleryItemMousedown(e) {
	if(!e) e=window.event;
	this._wasFlicking = (window.gallery_display.isFlicking==true);
	this._wasDragging = (window.gallery_display.hasDragged==true);
	return window.gallery_NullFunction(e);
}

function galleryItemMouseup(e) {
	if(!e) e=window.event;
	
	if(window.gallery_display.hasDragged==true || window.gallery_display.isFlicking==true || this._wasFlicking==true || this._wasDragging==true)
		return false;
	
	if(window.isKeyDown([16,17,18,224,91]) || ((e.which&&e.which==2)||(e.button&&e.button==2))) {
		/*
		if(window.gallery_contextmenu_current != null) {
			document.body.removeChild(window.gallery_contextmenu_current);
			document.onmousedown = window.gallery_contextmenu_current.onmousedownOld;
			window.gallery_contextmenu_current = null;
		}
		*/
		
		var mx = (e.pageX?e.pageX:(e.clientX+document.body.scrollLeft)) + (document.getElementById("w3c")?document.getElementById("w3c").scrollLeft:0);
		var my = (e.pageY?e.pageY:(e.clientY+document.body.scrollTop)) + (document.getElementById("w3c")?document.getElementById("w3c").scrollTop:0);
		
		var idParts = this.id.split("_");
		this.itemId = parseInt(idParts[idParts.length-1]);
		
		var galleryItem = document.getElementById("gallery_display_row_"+this.itemId);
		var gi_contents = galleryItem.getElementsByTagName("*");
		var gallery_name = "";
		var gallery_desc = "";
		for(var x in gi_contents) {
			if(gi_contents[x].className && gi_contents[x].className=="gallery_display_name") {
				gallery_name = gi_contents[x].innerHTML;
				galleryItem.galname = gi_contents[x];
			}
			if(gi_contents[x].className && gi_contents[x].className=="gallery_display_description") {
				gallery_desc = gi_contents[x].innerHTML;
				galleryItem.galdesc = gi_contents[x];
			}
		}

		this.contextMenu = document.createElement("div");
		this.contextMenu.id = "gallery_contextMenu_"+this.itemId;
		this.contextMenu.className = "galleryItemContextMenu";
		this.contextMenu.style.position = "absolute";
		this.contextMenu.style.left = mx+"px";
		this.contextMenu.style.top = my+"px";
		window.gallery_contextmenu_current = this.contextMenu;
		window.gallery_contextMenu_onmousedownOld = document.onmousedown ? document.onmousedown : function(e) {};
		setTimeout("document.onmousedown = function() { setTimeout(\"if(document.getElementById('gallery_contextMenu_"+this.itemId+"')) { document.getElementById('gallery_contextMenu_"+this.itemId+"').location_parentElement.removeChild(document.getElementById('gallery_contextMenu_"+this.itemId+"')); } document.onmousedown = window.gallery_contextMenu_onmousedownOld;\", 200); return false; };", 200);
		var iHTML = "<div class=\"contextMenuItem\" onclick=\"window.Darkroom.ViewGallery("+this.gid+", document.getElementById('"+this.id+"'));\" onmouseover=\"className+=' hover';\" onmouseout=\"if(className.indexOf(' hover') > -1) className=className.substring(0, className.indexOf(' hover'));\">View Slideshow</div>";
		iHTML += "<div class=\"contextMenuItem\" onclick=\"gallery_about("+this.itemId+");\" onmouseover=\"className+=' hover';\" onmouseout=\"if(className.indexOf(' hover') > -1) className=className.substring(0, className.indexOf(' hover'));\">About this Gallery</div>";
		if(window.accesslevel=="superadmin") {
			iHTML += "<div class=\"contextMenuItem\" onclick=\"showHideImageUploadBox("+this.itemId+");\" onmouseover=\"className+=' hover';\" onmouseout=\"if(className.indexOf(' hover') > -1) className=className.substring(0, className.indexOf(' hover'));\">Add Photos to this Gallery</div>";
			iHTML += "<div class=\"contextMenuItem\" onclick=\"renameGallery("+this.itemId+",'"+escape(gallery_name)+"','"+escape(gallery_desc)+"');\" onmouseover=\"className+=' hover';\" onmouseout=\"if(className.indexOf(' hover') > -1) className=className.substring(0, className.indexOf(' hover'));\">Rename this Gallery</div>";
			iHTML += "<div class=\"contextMenuItem\" onclick=\"deletegallery("+this.itemId+");\" onmouseover=\"className+=' hover';\" onmouseout=\"if(className.indexOf(' hover') > -1) className=className.substring(0, className.indexOf(' hover'));\">Delete this Gallery</div>";
		}
		this.contextMenu.innerHTML = iHTML;
		
		this.contextMenu.location_parentElement = document.getElementById("w3c")?document.getElementById("w3c"):document.body;
		this.contextMenu.location_parentElement.appendChild(this.contextMenu);
		//document.body.appendChild(this.contextMenu);
	}
	else {
		window.setTimeout("window.Darkroom.ViewGallery("+this.gid+", document.getElementById('"+this.id+"'));",1);
		/*
		Darkroom_slideshow( parseInt(idParts[idParts.length-1]) );
		window.Darkroom_slideshow_gallery_current = parseInt(idParts[idParts.length-1]);
		*/
	}
	return window.gallery_NullFunction(e);
}

function galleryItemMousemove(e) {
	if(!e) e=window.event;
	return window.gallery_NullFunction(e);
}


// gallery images:

function galleryImageClick(e) {
	if(!e) e=window.event;
	//if(window.gallery_display.hasDragged==true || window.gallery_display.isFlicking==true || this._wasFlicking==true || this._wasDragging==true)
	//	return false;
	
	if(window.isKeyDown([16,17,18,224,91]) || ((e.which&&e.which==2)||(e.button&&e.button==2))) {
	}
	else {
		//Darkroom.ViewImage(this.imageid);
	}
}

function galleryImageMousedown(e) {
	if(!e) e=window.event;
	
}

function galleryImageMouseup(e) {
	if(!e) e=window.event;
	
}

function galleryImageMousemove(e) {
	if(!e) e=window.event;
	
}



window.imageUploadBox_visible = false;
function showHideImageUploadBox(forceGalleryid) {
	//window.showHideImageUploadBox_bodyOverflowOld = document.documentElement ? document.documentElement.style.overflow : document.body.style.overflow;
	//document.documentElement ? document.documentElement.style.overflow = "hidden" : document.body.style.overflow = "hidden";
	
	if(window.imageUploadBox_visible==false) {
		window.imageUploadBox_visible = true;
		if(window.createGalleryBox_visible==true)
			showHideCreateGalleryBox();
		
		//window.gallery_display.style.overflow = "hidden";
		window.showHideImageUploadBox_dimmer = document.createElement("div");
		window.showHideImageUploadBox_dimmer.style.cssText = "position:absolute; top:0; left:0; width:100%; height:"+document.body.offsetHeight+"px; display:block; z-index:890;";
		window.showHideImageUploadBox_dimmer.className = "gallery_dimmer gallery_upload_image_dimmer";
		window.showHideImageUploadBox_dimmer.onclick = showHideImageUploadBox;
		document.body.appendChild(window.showHideImageUploadBox_dimmer);
		var box = document.getElementById('create_image');
		if(box.moved != true) {
			box.parentNode.removeChild(box);
			document.body.appendChild(box);
			box.moved = true;
		}
		box.style.display = "block";
		var bWidth = Math.round(box.offsetWidth);
		box.style.marginLeft = "-"+Math.round(bWidth/2)+"px";
		if(forceGalleryid!=null) {
			var opts = document.getElementById("image_galleryid").getElementsByTagName("option")
			for(var i in opts) {
				if(opts[i] && opts[i].value && opts[i].value==forceGalleryid)
					opts[i].selected = true;
				else
					opts[i].selected = false;
			}
		}
				
		var nameField = document.getElementById("image_name");
		nameField.focus();
		try{nameField.select();}catch(e){}
		try{nameField.SelectAll();}catch(e){}
	}
	else {
		window.imageUploadBox_visible = false;
		//window.gallery_display.style.overflow = "auto";
		document.body.removeChild(window.showHideImageUploadBox_dimmer);
		
		var box = document.getElementById('create_image');
		box.style.display="none";
	}
}


window.createGalleryBox_visible = false;
function showHideCreateGalleryBox() {
	//window.showHideCreateGalleryBox_bodyOverflowOld = document.documentElement ? document.documentElement.style.overflow : document.body.style.overflow;
	//document.documentElement ? document.documentElement.style.overflow = "hidden" : document.body.style.overflow = "hidden";
	
	if(window.createGalleryBox_visible==false) {
		window.createGalleryBox_visible = true;
		if(window.imageUploadBox_visible==true)
			showHideImageUploadBox();

		//window.gallery_display.style.overflow = "hidden";
		window.showHideCreateGalleryBox_dimmer = document.createElement("div");
		window.showHideCreateGalleryBox_dimmer.style.cssText = "position:absolute; top:0; left:0; width:100%; height:"+document.body.offsetHeight+"px; display:block; z-index:890;";
		window.showHideCreateGalleryBox_dimmer.className = "gallery_dimmer gallery_create_gallery_dimmer";
		window.showHideCreateGalleryBox_dimmer.onclick = showHideCreateGalleryBox;
		document.body.appendChild(window.showHideCreateGalleryBox_dimmer);
		var box = document.getElementById('create_gallery');
		if(box.moved != true) {
			box.parentNode.removeChild(box);
			document.body.appendChild(box);
			box.moved = true;
		}
		box.style.display="block";
		var bWidth = Math.round(box.offsetWidth);
		box.style.marginLeft = "-"+Math.round(bWidth/2)+"px";
		//box.style.display = box.style.display=="block" ? "none" : "block";
		
		var nameField = document.getElementById("newgallery_name");
		nameField.focus();
		try{nameField.select();}catch(e){}
		try{nameField.SelectAll();}catch(e){}
	}
	else {
		window.createGalleryBox_visible = false;
		//window.gallery_display.style.overflow = "auto";
		document.body.removeChild(window.showHideCreateGalleryBox_dimmer);
		
		var box = document.getElementById('create_gallery');
		box.style.display="none";
	}
}











// Begin Gallery2

function gallery_viewGalleryContents(gid,e,xid) {
	if(!e) e=window.event;
	if(xid==null || xid=="undefined") {
		var xid = "gallery2_viewgallery_"+gid;
		xmlhttp_get("?ext=json_image_list&galleryid="+gid+"&full=true&xid="+xid, xid, "gallery_viewGalleryContents("+gid+",null,'"+xid+"');");
	}
	else {
		var JSONtext = xmlhttp_data[xid].responseText;
		var response = eval('('+JSONtext+')');
		
		if(response.result==true || response.result=="successful") {
			var imgs = response.images;

			var winH = (window.innerHeight&&window.innerHeight>document.body.offsetHeight)?window.innerHeight:((document.documentElement&&document.documentElement.offsetHeight>document.body.offsetHeight)?document.documentElement.offsetHeight-5:document.body.offsetHeight);
			var dsocTop = navigator.appName.indexOf("Microsoft")>-1?document.body.scrollTop:window.pageYOffset;
			
			var d=document.createElement("div");
			d.id = "gallery_viewer_fader_"+Math.round(Math.random()*1000,0);
			d.style.cssText = "position:absolute; width:100%; height:"+winH+"px; left:0px; top:0px;";
			d.detectInterval = setInterval("var winH = (window.innerHeight&&window.innerHeight>document.body.offsetHeight)?window.innerHeight:((document.documentElement&&document.documentElement.offsetHeight>document.body.offsetHeight)?document.documentElement.offsetHeight-5:document.body.offsetHeight); var d=document.getElementById('"+d.id+"'); if(d && d.offsetHeight!=winH) d.style.height=winH+'px';",1000);
			d.setAttribute("style",d.style.cssText);
			d.className = "gallery_viewer_fader";
			d.onclick = function(){ this.viewer.Close(); };
			d.onmouseover = function() {
				this.viewer.scrollAmount = 0;
			};
			document.body.appendChild(d);

			var gallery_viewer = document.createElement("div");
			d.viewer = gallery_viewer;
			gallery_viewer.fader = d;
			gallery_viewer.id = "gallery_viewer";
			gallery_viewer.className = "gallery_viewer";
			for(var x in imgs) {
				if(imgs[x] && imgs[x].id) {
					var item = document.createElement("span");
					item.parentViewer = gallery_viewer;
					item.galleryID = gid;
					item.imageID = imgs[x].id;
					item.id = "gallery_viewer_item_"+imgs[x].id;
					item.className = "gallery_viewer_item";
					item.onclick = function() { /* moved to mouseup event */ };
					item.onmouseover = function(e) {
						this.className += " hover"
					};
					item.onmouseout = function(e) {
						this.className = this.className.substring(0,this.className.indexOf(" hover"));
					};
					
					item.img = document.createElement("img");
					item.img.className = "gallery_viewer_item_img";
					item.img.src = "?ext=thumb&size=80&id="+imgs[x].id;
					item.appendChild(item.img);
					
					item.imgname = document.createElement("span");
					item.imgname.className = "gallery_viewer_item_name gallery_viewer_item_imgname";
					item.imgname.innerHTML = imgs[x].name;
					item.appendChild(item.imgname);
					
					item.imgdesc = document.createElement("span");
					item.imgdesc.className = "gallery_viewer_item_desc gallery_viewer_item_imgdesc";
					item.imgdesc.innerHTML = imgs[x].description;
					item.appendChild(item.imgdesc);
					
					item.onmousedown = window.gallery_NullFunction;
					item.oncontextmenu = window.gallery_NullFunction;
					item.onselectstart = window.gallery_NullFunction;
					item.onmouseup = function(e) {
						if(!e) e=window.event;
						if(window.isKeyDown(17) || ((e.which&&e.which==2)||(e.button&&e.button==2))) {
							var mx = (e.pageX?e.pageX:(e.clientX+document.body.scrollLeft)) + (this.parentViewer.parentNode?this.parentViewer.parentNode.scrollLeft:0);
							var my = (e.pageY?e.pageY:(e.clientY+document.body.scrollTop)) + (this.parentViewer.parentNode?this.parentViewer.parentNode.scrollTop:0);
		
							var idParts = this.id.split("_");
							this.itemId = parseInt(idParts[idParts.length-1]);
							this.contextMenu = document.createElement("div");
							this.contextMenu.id = "gallery_contextMenu_"+this.itemId;
							this.contextMenu.className = "galleryImageContextMenu";
							this.contextMenu.style.position = "absolute";
							this.contextMenu.style.left = mx+"px";
							this.contextMenu.style.top = my+"px";
							window.gallery_contextmenu_current = this.contextMenu;
							window.gallery_contextMenu_onmousedownOld = document.onmousedown ? document.onmousedown : function(e) {};
							setTimeout("document.onmousedown = function() { setTimeout(\"if(document.getElementById('gallery_contextMenu_"+this.itemId+"')) { document.getElementById('gallery_contextMenu_"+this.itemId+"').location_parentElement.removeChild(document.getElementById('gallery_contextMenu_"+this.itemId+"')); } document.onmousedown = window.gallery_contextMenu_onmousedownOld;\", 200); return false; };", 200);
							var iHTML = "<div class=\"contextMenuItem\" onclick=\"Darkroom_slideshow("+this.galleryID+","+this.imageID+"); window.Darkroom_slideshow_gallery_current="+this.galleryID+";\" onmouseover=\"className+=' hover';\" onmouseout=\"if(className.indexOf(' hover') > -1) className=className.substring(0, className.indexOf(' hover'));\">View Image</div>";
							if(window.accesslevel=="superadmin") {
								iHTML += "<div class=\"contextMenuItem\" onclick=\"deleteImage("+this.imageID+");\" onmouseover=\"className+=' hover';\" onmouseout=\"if(className.indexOf(' hover') > -1) className=className.substring(0, className.indexOf(' hover'));\">Delete Image</div>";
								iHTML += "<div class=\"contextMenuItem\" onclick=\"renameImage("+this.imageID+",'"+escape(this.imgname.innerHTML)+"','"+escape(this.imgdesc.innerHTML)+"');\" onmouseover=\"className+=' hover';\" onmouseout=\"if(className.indexOf(' hover') > -1) className=className.substring(0, className.indexOf(' hover'));\">Rename Image</div>";
								iHTML += "<div class=\"contextMenuItem\" onclick=\"setGalleryDisplayImage("+this.imageID+","+this.galleryID+");\" onmouseover=\"className+=' hover';\" onmouseout=\"if(className.indexOf(' hover') > -1) className=className.substring(0, className.indexOf(' hover'));\">Set as Gallery Thumb</div>";
							}
							this.contextMenu.innerHTML = iHTML;
		
							this.contextMenu.location_parentElement = this.parentViewer.parentNode;
							this.contextMenu.location_parentElement.appendChild(this.contextMenu);
						}
						else {
							//setTimeout("document.getElementById(\""+this.id+"\").onmouseout();", 1000);
							Darkroom_slideshow(this.galleryID,this.imageID);
						}
						return window.gallery_NullFunction(e)
					};
					
					gallery_viewer.appendChild(item);
				}
			}
			gallery_viewer.onmousemove = function(e) {
				if(!e) e=window.event;
				var target = e.srcElement ? e.srcElement : e.target;
				var mx = (e.pageX?e.pageX:(document.body.scrollLeft+e.clientX)) - this.offsetLeft;
				var my = (e.pageY?e.pageY:(document.body.scrollTop+e.clientY)) - this.offsetTop;
				var scrolldir = 0;
				var third = this.offsetHeight/3;
				if(my<third)
					scrolldir = -Math.pow( ((third-my)/third)*6 , 2 );
				if(my>third*2)
					scrolldir = Math.pow( ((my-(third*2))/third)*6 , 2 );
				this.scrollAmount = scrolldir;
			};
			gallery_viewer.Close = function() {
				try{ window.clearInterval(this.scrollInterval); } catch(err){}
				window.clearInterval(this.fader.detectInterval);
				this.fader.parentNode.removeChild(this.fader);
				this.closeBtn.parentNode.removeChild(this.closeBtn);
				this.parentNode.removeChild(this);
			};

			var c=document.createElement("span");
			c.innerHTML = "<span class=\"innerText\">&times;</span>";
			c.viewer = gallery_viewer;
			gallery_viewer.closeBtn = c;
			c.className = "gallery2_closeButton galleryViewer";
			c.onclick = function(){ this.viewer.Close(); };
			c.onmouseover = function(){ this.className="gallery2_closeButton galleryViewer hover"; };
			c.onmouseover = function(){ this.className="gallery2_closeButton galleryViewer"; };
			document.body.appendChild(c);
	

			document.body.appendChild(gallery_viewer);
			gallery_viewer.focus();
			window._gallery2_gallery_viewer = gallery_viewer;
			window.setTimeout("window._gallery2_gallery_viewer.blur();", 1);
			gallery_viewer.scrollInterval = window.setInterval("var gv=window._gallery2_gallery_viewer; if(gv && gv.scrollAmount) gv.scrollTop += gv.scrollAmount;",20);
		}
		else {
			gallery_alert("There are no photos in this gallery","Empty Gallery");
		}
	}
}



















function gallery_about(galleryid, xmlhttpid) {
	if(!xmlhttpid) {
		xmlhttp_get(site_root+"gallery/?ext=json_gallery_list&sid="+Math.random(), "gallery_about_"+galleryid, "gallery_about("+galleryid+",\"gallery_about_"+galleryid+"\");");
		return true;
	}
	var JSONtext = xmlhttp_data[xmlhttpid].responseText;
	var JSON = eval('('+JSONtext+')');
	var gallery = JSON["galleries"][galleryid];
	var desc = gallery['description'];
	while(desc.indexOf("\\'") > -1)
		desc = desc.replace("\\'","'");
	gallery_alert(desc, gallery['name']);
}










function gallery_alert(text,title) {
	text = unescape(text);
	if(title!=null && title!="undefined")
		title = "<div class=\"gallery_title\">" + unescape(title) + "</div>";
	else
		title = "";
	
	var winH = (window.innerHeight&&window.innerHeight>document.body.offsetHeight)?window.innerHeight:((document.documentElement&&document.documentElement.offsetHeight>document.body.offsetHeight)?document.documentElement.offsetHeight-5:document.body.offsetHeight);
	var dsocTop = navigator.appName.indexOf("Microsoft")>-1?document.body.scrollTop:window.pageYOffset;
	
	var d=document.createElement("div");
	d.id = "alert2_fader_"+Math.round(Math.random()*1000,0);
	d.className = "gallery_dimmer";
	d.style.cssText = "z-index:999; position:absolute; width:100%; height:"+winH+"px; left:0; top:0;";
	d.setAttribute("style",d.style.cssText);
	d.onclick = function(){ this.alert.Close(); };
	document.body.appendChild(d);
	
	var a=document.createElement("div");
	a.id = "alert2_"+Math.round(Math.random()*1000,0);
	a.className = "gallery_alertbox";
	a.style.cssText = "z-index:1000;";
	a.setAttribute("style",a.style.cssText);
	a.innerHTML = title + text + "<span onclick=\"document.getElementById('"+a.id+"').Close();\" style=\"position:absolute; right:-10px; top:-10px; padding:0px 4px 0px 4px; font-weight:bold; color:#FFFFFF; background:#BB0000; border:1px solid #FF0000; cursor:pointer;\">&times;</span>";
	document.body.appendChild(a);
	a.fader = d;
	d.alert = a;
	a.Close = function() {
		document.body.removeChild(this.fader);
		document.body.removeChild(this);
	};
}




















// control keys:

if(!window.controlClickIsIncluded) {
	window.controlClickIsIncluded = true;
	window.controlClickVersion = "1.1";

	window.currentKeyDowns = new Array();
	
	window.onKeyDownEvents = new Array();
	window.onKeyUpEvents = new Array();

	document.onkeydown = function(e) {
		if(!e) e=window.event;
		var key = e.keyCode ? e.keyCode : e.which;
		var nA = new Array();
		for(var i in window.currentKeyDowns)
			if(window.currentKeyDowns[i] != key)
				nA.push(window.currentKeyDowns[i]);
		nA.push(key);
		window.currentKeyDowns = nA;

		preventDefault = false;
		// if you want something to happen when a key is pressed, 
		// register an event in the window.onKeyDownEvents[] array.
		for(var i in window.onKeyDownEvents)
			if(window.onKeyDownEvents[i](key) == "preventDefault")
				preventDefault = true;
		if(preventDefault==true) {
			if(e.returnValue)
				e.returnValue=false;
			if(e.preventDefault)
				e.preventDefault();
		}
		return preventDefault?false:true;
	};

	document.onkeyup = function(e) {
		if(!e) e=window.event;
		var key = e.keyCode ? e.keyCode : e.which;
		var nA = new Array();
		for(var i in window.currentKeyDowns)
			if(window.currentKeyDowns[i] != key)
				nA.push(window.currentKeyDowns[i]);
		window.currentKeyDowns = nA;

		preventDefault = false;
		// if you want something to happen when a key is UNpressed, 
		// register an event in the window.onKeyUpEvents[] array.
		for(var i in window.onKeyUpEvents)
			if(window.onKeyUpEvents[i](key) == "preventDefault")
				preventDefault = true;
		return preventDefault?false:true;
	};
	
	window.controlKeys_onblurold = window.onblur?window.onblur:function(){};	
	window.onblur = function() {
		for(var i in window.onKeyUpEvents)
			for(var key=1; key<230; key++)
				try{ window.onKeyUpEvents[i](key); }catch(err){}
		window.currentKeyDowns = new Array();
		window.controlKeys_onblurold();
	};

	window.isKeyDown = function(key) {
		if(key==null || key=="undefined")
			return false;
		if(key.constructor && key.constructor!=Array)
			key = new Array(key);
		for(var i=0; i<window.currentKeyDowns.length; i++)
			for(var x=0; x<key.length; x++)
				if(window.currentKeyDowns[i]==key[x])
					return true;
		return false;
	};
}













