window.gallery_onloadold = window.onload ? window.onload : function(){};
window.onload = function() {
	if(Gallery.loaded!=true)
		Gallery.Load();
	window.gallery_onloadold();
};



window.Gallery = {
	viewerPadding	: 30,
	Load	: function(secondrun) {
		this.loaded = true;
		if(secondrun!==true) {
			window.setTimeout(function(){
				Gallery.Load(true)
			},100);
			return true;
		}
		var li = [];
		var listItems = document.getElementsByTagName("*");
		for(var p=0; p<listItems.length; p++)
			if( (listItems[p].className+"").match(/\bgallery_image\b/mgi,'') )
				li.push(listItems[p]);
		try{ delete listItems; }catch(err){ listItems=null; }
		var ti;
		for(var x=0; x<li.length; x++) {
			i = li[x].childNodes[0];
			while((i.nodeName+"").toLowerCase()!="a" && i.nextSibling)
				i = i.nextSibling;
			if(i) {
				t = i.childNodes[0];
				while((t.nodeName+"").toLowerCase()!="img" && t.nextSibling)
					t = t.nextSibling;
				if(t) {
					this.images.push({
						title	: i.getAttribute("alt")?i.getAttribute("alt"):"Untitled",
						thumb	: t.src,
						image	: i.href,
						thumbel	: t,
						imageel	: i
					});
					i._galleryItem = this.images[this.images.length-1];
					i.onclick = function() {
						if(this.className && this.className.match(/gallery_image_size_[0-9]+x[0-9]+/mgi))
							Gallery.View( this.href , parseInt(this.className.substring(19,this.className.lastIndexOf("x"))) , parseInt(this.className.substring(this.className.lastIndexOf("x")+1,this.className.length)) );
						else
							Gallery.View(this.href);
						return false;
					};
				}
			}
		}
		
		this.cover = document.createElement("div");
		this.cover._g = this;
		this.cover.innerHTML = "&nbsp;";
		this.cover.className = "gallery_cover";
		this.cover.style.cssText = "position:absolute; position:fixed; display:none; left:0; top:0; width:100%; height:100%; margin:0; padding:0; border-width:0; z-index:998; overflow:hidden;";
		this.cover.onclick = function() {
			this._g.CloseView();
		};
		document.body.appendChild(this.cover);
		
		this.display = document.createElement("div");
		this.cover._g = this;
		this.display.className = "gallery_display";
		this.display.style.cssText = "position:absolute; display:none; z-index:999; overflow:visible;";
		document.body.appendChild(this.display);
		
		this.throbber = document.createElement("span");
		this.throbber._g = this;
		this.throbber.className = "gallery_throbber";
		this.throbber.style.visibility = "hidden";
		this.display.appendChild(this.throbber);
		
		this.currentText = document.createElement("span");
		this.currentText.innerHTML = "<span class=\"gallery_current_text_inner\"><span class=\"gallery_current_text_progress\">1</span><span class=\"gallery_current_text_separator\"> of </span><span class=\"gallery_current_text_total\">1</span></span>";
		this.currentText._g = this;
		this.currentText.className = "gallery_current_text";
		this.display.appendChild(this.currentText);
		
		this.closebtn = document.createElement("span");
		this.closebtn.innerHTML = "<span class=\"innerText\">&times;</span>";
		this.closebtn._g = this;
		this.closebtn.onmouseover = function(){this.className=this.className.replace(/\shover/mgi,"")+" hover";};
		this.closebtn.onmouseout = function(){this.className=this.className.replace(/\shover/mgi,"");};
		this.closebtn.onmousedown = function(){return false;};
		this.closebtn.onselectstart = function(){return false;};
		this.closebtn.onclick = function() { this._g.CloseView(); };
		this.closebtn.className = "gallery_closebutton";
		this.display.appendChild(this.closebtn);
		
		this.nextbtn = document.createElement("span");
		this.nextbtn.innerHTML = "<span class=\"innerText\">Next</span>";
		this.nextbtn._g = this;
		this.nextbtn.onmouseover = function(){this.className=this.className.replace(/\shover/mgi,"")+" hover";};
		this.nextbtn.onmouseout = function(){this.className=this.className.replace(/\shover/mgi,"");};
		this.nextbtn.onmousedown = function(){return false;};
		this.nextbtn.onselectstart = function(){return false;};
		this.nextbtn.onclick = function() { this._g.ViewNext(); };
		this.nextbtn.className = "gallery_nextbutton";
		this.display.appendChild(this.nextbtn);
		
		this.prevbtn = document.createElement("span");
		this.prevbtn.innerHTML = "<span class=\"innerText\">Prev</span>";
		this.prevbtn._g = this;
		this.prevbtn.onmouseover = function(){this.className=this.className.replace(/\shover/mgi,"")+" hover";};
		this.prevbtn.onmouseout = function(){this.className=this.className.replace(/\shover/mgi,"");};
		this.prevbtn.onmousedown = function(){return false;};
		this.prevbtn.onselectstart = function(){return false;};
		this.prevbtn.onclick = function() { this._g.ViewPrev(); };
		this.prevbtn.className = "gallery_prevbutton";
		this.display.appendChild(this.prevbtn);
		
		this.cover2 = document.createElement("div");
		this.cover2.innerHTML = "&nbsp;";
		this.cover2.className = "gallery_cover gallery_open";
		this.cover2.style.cssText = "position:absolute; display:block; left:0; top:-999em; width:1px; height:1px; margin:0; padding:0; border:0;";
		document.body.appendChild(this.cover2);
	},
	View	: function(url, /*opt.*/ width, /*opt.*/ height) {
		if(url==null || url=="undefined" || (url.length && url.length<1) || this._changing==true )
			return false;
		
		if(!width)
			width = 200;
		if(!height)
			height = 200;
		
		if(this._opened==false) {
			Gallery.cover.className = Gallery.cover.className.replace(/\sgallery_opening/mgi,'') + " gallery_opening";
			try{ window.clearInterval(this._cot); }catch(err){}
			this.setOpacity(this.cover,10);
			this._co = 10;
			this._cot = window.setInterval(function(){
				Gallery._co = Gallery._co+10;
				if(Gallery._co>=50) {
					try{ window.clearInterval(Gallery._cot); }catch(err){}
					Gallery._cot = null;
					Gallery.cover.style.filter = null;
					Gallery.cover.style.opacity = null;
					Gallery.cover.className = Gallery.cover.className.replace(/\sgallery_opening/mgi,'') + " gallery_open";
					if(Gallery.cover2) {
						Gallery.cover2.parentNode.removeChild(Gallery.cover2);
						Gallery.cover2 = null;
					}
				}
				else
					Gallery.setOpacity(Gallery.cover,Gallery._co);
			}, 15);
			this._opened = true;
			this._firstload = true;
		}
		else {
			if(this._firstrun==true)
				this._firstrun = false;
		}
		
		if(this._firstrun==true) {
			this._adjustViewDims(width, height, null, true);
			this._firstrun = false;
		}
		
		this.HardResize( this._width?this._width:width , this._height?this._height:height );
		
		this.display.style.display = "block";
		this.cover.style.display = "block";
		
		this.throbber.style.visibility = "visible";
		
		this.currenturl = url;
		
		var load;
		if(this.img && this.img.src) {
			load = this.img;
			load.onload = null;
			load.onerror = null;
			//load.src = "about:blank";
		}
		else {
			load = document.createElement("img");
			this.img = load;
			this.display.appendChild(load);
		}
		load.style.visibility = "hidden";
		load.style.position = "absolute";
		load.style.width = "auto";
		load.style.height = "auto";
		load.style.top = "-999em";
		load.style.right = "0";
		load._g = this;
		load.onload = function() {
			this._g.viewloaded(true);
		};
		load.onerror = function() {
			this._g.viewloaded(false);
		};
		load.src = url;
		
		
		var cur = -1;
		for(var x=0; x<this.images.length; x++) {
			if(this.images[x].image==url) {
				cur = x;
				break;
			}
		}
		
		if(cur==0)
			this.prevbtn.style.display = "none";
		else
			this.prevbtn.style.display = "block";
		
		if(cur==this.images.length-1)
			this.nextbtn.style.display = "none";
		else
			this.nextbtn.style.display = "block";
		
		this.currentText.innerHTML = "<span class=\"gallery_current_text_inner\"><span class=\"gallery_current_text_progress\">"+(cur+1)+"</span><span class=\"gallery_current_text_separator\"> of </span><span class=\"gallery_current_text_total\">"+this.images.length+"</span></span>";
	},
	Adjust	: function() {
		this._adjustViewDims(this._width, this._height);
	},
	_adjustViewDims	: function(width,height,forced,noshow) {
		var bod = document.documentElement?document.documentElement:document.body;
		var winW = window.innerWidth ? window.innerWidth : bod.offsetWidth;
		var winH = window.innerHeight ? window.innerHeight : bod.offsetHeight;
		if(winH!=this.currentWinH) {
			this.currentWinH = winH;
			this.cover.style.height = winH>document.body.offsetHeight ? "100%" : (document.body.offsetHeight+"px");
			forced = true;
		}
		if(winW!=this.currentWinW) {
			this.currentWinW = winW;
			this.cover.style.width = winW>document.body.offsetWidth ? "100%" : (document.body.offsetWidth+"px");
			forced = true;
		}
		
		if( width!=this._width || height!=this._height || forced===true ) {
			//var imgLeft = this.img?this.img.offsetLeft:(this.display.offsetWidth-this._width);
			//var imgTop = this.img?this.img.offsetTop:(this.display.offsetWidth-this._width);
			var p = this.viewerPadding*2;
			if( width+p>winW ) {
				if( height!=this._height || forced===true )
					height = height * (winW-p) / width;
				width = winW-p;
			}
			if( height+p>winH ) {
				if( width!=this._width || forced===true )
					width = width * (winH-p) / height;
				height = winH-p;
			}
		}
		
		if(width!=this._width || height!=this._height || forced===true) {
			if(noshow!==true)
				this.SmoothResize( width?width:this._width , height?height:this._height );
			this._width = width;
			this._height = height;
		}
		/*
		if(width!=this._width || forced===true) {
			var left = window.pageXOffset ? window.pageXOffset : bod.scrollLeft;
			if(left==0 && document.body.childNodes[0] && document.body.childNodes[0].scrollLeft)
				left = Math.round(document.body.childNodes[0].scrollLeft);
			this.display.style.width = width + "px";
			this.display.style.left = (left + winW/2 - width/2) + "px";
			this._width = width;
		}
		if(height!=this._height || forced===true) {
			var top = window.pageYOffset ? window.pageYOffset : bod.scrollTop;
			if(top==0 && document.body.childNodes[0] && document.body.childNodes[0].scrollTop)
				top = Math.round(document.body.childNodes[0].scrollTop);
			this.display.style.height = height + "px";
			this.display.style.top = (top + winH/2 - height/2) + "px";
			this._height = height;
		}
		*/
	},
	SmoothResize	: function(w,h) {
		if( w==this._srwn && h==this._srhn ) {
			this.img.style.width = w + "px";
			this.img.style.height = h + "px";
			this.img.style.position = "relative";
			this.img.style.top = "0";
			this.img.style.right = "0";
			this.img.style.visibility = "visible";
			this.HardResize(w,h);
			return false;
		}
		this._changing = true;
		var bod = document.documentElement?document.documentElement:document.body;
		if(this._srtimer)
			try{ window.clearInterval(this._srtimer); }catch(err){}
		if(!this._srw)
			this._srw = this._width + 0;
		if(!this._srh)
			this._srh = this._height + 0;
		this._srwn = w?w:this._width + 0;
		this._srhn = h?h:this._height + 0;
		this._sri = -1;
		
		if( Math.abs(this._srwn-this._srw)>Math.abs(this._srhn-this._srh) ) {
			var winW = window.innerWidth ? window.innerWidth : bod.offsetWidth;
			this._srm = Math.ceil(Math.abs(this._srwn-this._srw)/winW * 20);
			//this._srm = Math.ceil(Math.abs(this._srwn-this._srw)/20);
		}
		else {
			var winH = window.innerHeight ? window.innerHeight : bod.offsetHeight;
			this._srm = Math.ceil(Math.abs(this._srhn-this._srh)/winH * 20);
			//this._srm = Math.ceil(Math.abs(this._srhn-this._srh)/20);
		}
		
		this._srtimer = window.setInterval(function(){ Gallery._doSmoothResize(); }, 20);
		if(this.img) {
			this.img.style.position = "absolute";
			this.img.style.top = "-999em";
			this.img.style.right = "0";
			this.img.style.visibility = "hidden";
		}
	},
	_doSmoothResize	: function() {
		this._sri++;
		
		this.HardResize( this._srw+(Math.sin(Math.PI*(this._sri/this._srm-1/2))+1)*(this._srwn-this._srw)/2 , this._srh+(Math.sin(Math.PI*(this._sri/this._srm-1/2))+1)*(this._srhn-this._srh)/2 );
		
		if(this._sri==this._srm) {
			this.img.style.width = Math.round(this._srwn*10)/10 + "px";
			this.img.style.height = Math.round(this._srhn*10)/10 + "px";
			this.img.style.position = "relative";
			this.img.style.top = "0";
			this.img.style.right = "0";
			this.img.style.visibility = "visible";
			try{ window.clearInterval(this._srtimer); }catch(err){}
			try{ delete this._srtimer; }catch(err){ this._srtimer = null; }
			this._srw = null;
			this._srwn = null;
			this._srh = null;
			this._srhn = null;
			this._sri = null;
			this._srm = null;
			this._changing = false;
		}
	},
	HardResize	: function(w,h) {
		var bod = document.documentElement?document.documentElement:document.body;
		if( w ) {
			var winW = window.innerWidth ? window.innerWidth : bod.offsetWidth;
			var left = window.pageXOffset ? window.pageXOffset : bod.scrollLeft;
			if(left==0 && document.body.childNodes[0] && document.body.childNodes[0].scrollLeft)
				left = Math.round(document.body.childNodes[0].scrollLeft);
			//var imgLeft = this.img.offsetLeft;
			
			//this.img.style.width = w + "px";
			this.display.style.width = Math.round(w*10)/10 + "px";
			this.display.style.left = Math.round((left + winW/2 - w/2)*10)/10 + "px";
		}
		if( h ) {
			var winH = window.innerHeight ? window.innerHeight : bod.offsetHeight;
			var top = window.pageYOffset ? window.pageYOffset : bod.scrollTop;
			if(top==0 && document.body.childNodes[0] && document.body.childNodes[0].scrollTop)
				top = Math.round(document.body.childNodes[0].scrollTop);
			//var imgTop = this.img.offsetTop;
			
			//this.img.style.height = h + "px";
			this.display.style.height = Math.round(h*10)/10 + "px";
			this.display.style.top = Math.round((top + winH/2 - h/2)*10)/10 + "px";
		}
	},
	viewloaded	: function(result) {
		if(result==false) {
			this.ImageFailed(this.img.src);
			return false;
		}
		
		this.img.style.width = "auto";
		this.img.style.height = "auto";
		this.throbber.style.visibility = "hidden";
		this._adjustViewDims(this.img.width,this.img.height, true);
		//this.scrollCheckTimer = window.setInterval("Gallery.Adjust();",200);
	},
	ViewNext	: function() {
		var cur;
		for(var x=0; x<this.images.length; x++) {
			if(this.images[x].image==this.currenturl) {
				cur = x;
				break;
			}
		}
		if(cur==this.images.length-1)
			cur = -1;
		cur++;
		var ow = this._width;
		var oh = this._height;
		this.View(this.images[cur].image);
		//this._adjustViewDims(ow,oh,true);
	},
	ViewPrev	: function() {
		var cur;
		for(var x=0; x<this.images.length; x++) {
			if(this.images[x].image==this.currenturl)
				cur = x;
		}
		if(cur==0)
			cur = this.images.length;
		cur--;
		var ow = this._width;
		var oh = this._height;
		this.View(this.images[cur].image);
		//this._adjustViewDims(ow,oh,true);
	},
	setOpacity	: (
		(document.all&&navigator.userAgent.toLowerCase().indexOf("msie")>-1&&parseFloat(navigator.appVersion)<8) ? function(el,o) {
			el.style.filter = "alpha(opacity="+Math.round(o)+")";
		} : function(el,o) {
			el.style.opacity = Math.round(o)/100 + "";
		}
	),
	ImageFailed	: function(src) {
		this.CloseView();
	},
	CloseView	: function() {
		if(this._opened==false)
			return false;
		
		if(this._srtimer) {
			try{ window.clearInterval(this._srtimer); }catch(err){}
			try{ delete this._srtimer; }catch(err){ this._srtimer = null; }
		}
		
		if(this.img) {
			this.img.onload = null;
			this.img.onerror = null;
			this.display.removeChild(this.img);
			this.img = null;
		}
		this.display.style.display = "none";
		
		this.cover.className = this.cover.className.replace(/\sgallery_open/mgi,'');
		
		try{ window.clearInterval(this._cot); }catch(err){}
		this.setOpacity(this.cover,this._co=this._co-10);
		this._cot = window.setInterval(function(){
			Gallery._co = Gallery._co-10;
			Gallery.setOpacity(Gallery.cover,Gallery._co);
			if(Gallery._co<=10) {
				Gallery.cover.style.display = "none";
				window.clearInterval(Gallery._cot);
				Gallery._cot = null;
			}
		}, 15);
		
		this._opened = false;
		this._firstrun = true;
		this._changing = false;
		
		if(this.scrollCheckTimer)
			try{ window.clearInterval(this.scrollCheckTimer); }catch(err){}
		try {
			delete this._srw;
			delete this._srh;
			delete this._srwn;
			delete this._srhn;
		} catch(err) {
			this._srw = null;
			this._srh = null;
			this._srwn = null;
			this._srhn = null;
		}
	},
	_opened	: false,
	_firstrun : true,
	_changing : false,
	_width	: 200,
	_height	: 200,
	images	: [],
	loaded	: false
};