var gallery = function(idd, initn) {

	this.mingalw = 500;
	this.mingalh = 600;
	this.id = idd;
	this.initn = initn;
	this.nav_height = 145;
	this.nav_jump = 20;
	this.bigh = 360;
	this.smalln = 0;
	this.open = false;

	this.load_smalls = function() {
		var gallery = document.getElementById(this.id);
		var smalls = getElementsByClassName('small', null, gallery);
		var w = 0;
		this.smalln = 0;
		for (var i = 0; i < smalls.length; i++) {
			var img = smalls[i].getElementsByTagName('img')[0];
			if (img.src == '') {
				var filename = getElementsByClassName('filename', null, smalls[i])[0].childNodes[0].nodeValue;
				img.src = filename;
				current_gallery = this;
				img.n = i;
				img.onclick = function(){current_gallery.set_img(this.n)};
			}
			w += img.offsetWidth + 10;
			this.smalln++;
		}
		var wrap = getElementsByClassName('smalls_wrap', null, gallery)[0];
		wrap.style.width = (w+10) + 'px';
	}
	
	this.open_gallery = function() {
		if (this.open) {
			return;
		}
		this.open = true;
		var windoww = window.innerWidth;
		if (!windoww) {
			windoww = document.documentElement.clientWidth;
		}
		var windowh = window.innerHeight;
		if (!windowh) {
			windowh = document.documentElement.clientHeight;
		}
		galleryw = windoww;
		galleryh = windowh;
		if (galleryw < this.mingalw) {
			galleryw = this.mingalw;
		}
		if (galleryh < this.mingalh) {
			galleryh = this.mingalh;
		}
		var bodyw = document.body.offsetWidth;
		var bodyh = document.body.offsetHeight;
		if (galleryh < bodyh) {
			galleryh = bodyh;
		}
		if (galleryw < bodyw) {
			galleryw = bodyw;
		}
		var gallery = document.getElementById(this.id);
		gallery.style.visibility = 'visible';
		gallery.style.left = parseInt(galleryw/2) + 'px';
		gallery.style.top = parseInt(galleryh/2) + 'px';
		gallery.style.width = '10px';
		this.showbg();
	}

	this.showbg = function() {
		var gallery = document.getElementById(this.id);
		var bigs = getElementsByClassName('bigs', null, gallery)[0];
		var w = gallery.offsetWidth;
		var h = gallery.offsetHeight;
		if (h == galleryh) {
			w += 80;
			if (w > galleryw) {
				w = galleryw;
			}
		}
		h += 15;
		if (h > galleryh) {
			h = galleryh;
		}
		gallery.style.width = w + 'px';
		gallery.style.height = h + 'px';
		var left = parseInt(galleryw/2 - w/2);
		var top = parseInt(galleryh/2 - h/2);
		gallery.style.left = left + 'px';
		gallery.style.top = top + 'px';
		current_gallery = this;
		if (w < galleryw) {
			setTimeout('current_gallery.showbg()', 1);
		} else {
			this.show_photos();
		}
	}
	
	this.hidebg = function() {
		var gallery = document.getElementById(this.id);
		var bigs = getElementsByClassName('bigs', null, gallery)[0];
		var w = gallery.offsetWidth;
		var h = gallery.offsetHeight;
		if (h == galleryh) {
			w -= 80;
			if (w < 10) {
				w = 10;
			}
		}
		if (w == 10) {
			h -= 15;
		}
		if (h < 0) {
			h = 0;
		}
		gallery.style.width = w + 'px';
		gallery.style.height = h + 'px';
		var left = parseInt(galleryw/2 - w/2);
		var top = parseInt(galleryh/2 - h/2);
		gallery.style.left = left + 'px';
		gallery.style.top = top + 'px';
		current_gallery = this;
		if (h > 0) {
			setTimeout('current_gallery.hidebg()', 1);
		} else {
			this.open = false;
			document.body.style.overflowX = 'auto';
			gallery.style.width = '0px';
			gallery.style.height = '0px';
			gallery.style.visibility = 'hidden';
		}
	}
	
	this.show_photos = function() {
		document.body.style.overflowX = 'hidden';
		var gallery = document.getElementById(this.id);
		var bigs = getElementsByClassName('bigs', null, gallery)[0];
		var big = getElementsByClassName('big', null, bigs)[this.initn];
		var img = big.getElementsByTagName('img')[0];
		img.style.height = this.bigh + 'px';
		if (img.src == '') {
			var filename = getElementsByClassName('filename', null, big)[0].childNodes[0].nodeValue;
			img.style.visibility = 'hidden';
			img.onload = function(){this.style.padding = '4px'; this.style.visibility = 'inherit'};
			img.src = filename;
		}
		bigs.style.visibility = 'visible';
		bigs.style.width = 'auto';
		bigs.style.height = (this.bigh+18) + 'px';
		bigs.style.position = 'static';
		bigs.style.marginTop = parseInt((galleryh - this.mingalh)/2) + 'px';
		big.style.height = (this.bigh+18) + 'px';
		big.style.position = 'static';
		big.style.visibility = 'visible';
		var smalls = getElementsByClassName('smalls', null, gallery)[0];
		smalls.style.width = galleryw + 'px';
		smalls.style.height = this.nav_height + 'px';
		smalls.style.position = 'static';
		smalls.style.visibility = 'visible';
		var smalls2 = getElementsByClassName('small', null, smalls);
		this.smalln = smalls2.length;
		var w = 0;
		for (var i = 0; i < this.smalln; i++) {
			w += smalls2[i].offsetWidth + 10;
		}
		var wrap = getElementsByClassName('smalls_wrap', null, gallery)[0];
		wrap.style.width = (w+10) + 'px';
		if (w < smalls.offsetWidth + 31) {
			wrap.style.marginLeft = parseInt((smalls.offsetWidth-18 - w)/2) + 'px';
		} else {
			var nav = getElementsByClassName('nav', null, gallery)[0];
			nav.style.width = 'auto';
			nav.style.height = 'auto';
			nav.style.position = 'static';
			nav.style.visibility = 'visible';
		}
		var close = getElementsByClassName('close', null, gallery)[0];
		close.style.width = 'auto';
		close.style.height = 'auto';
		close.style.position = 'static';
		close.style.visibility = 'visible';
	}
	
	this.scrolling = null;
	this.nav_jump2 = 0;
	this.navn = 0;
	this.scroll_timer = null;
	
	this.nav_left = function() {
		if (this.navn == 0 || this.scrolling) {
			return;
		}
		var gallery = document.getElementById(this.id);
		var smalls = getElementsByClassName('smalls_wrap', null, gallery)[0];
		var small = getElementsByClassName('small', null, smalls)[this.navn-1];
		var img = small.getElementsByTagName('img')[0];
		this.nav_jump2 = this.nav_jump2 + img.width + 10;
		if (this.nav_jump2 > 0) {
			this.nav_jump2 = 0;
		}
		this.navn--;
		this.scrolling = true;
		this.nav_left1();
	}
	
	this.nav_left1 = function() {
		var gallery = document.getElementById(this.id);
		var smalls = getElementsByClassName('smalls_wrap', null, gallery)[0];
		var marg = smalls.style.marginLeft;
		if (marg == '') {
			marg = 0;
		} else {
			marg = parseInt(marg.substr(0, marg.length-2));
		}
		marg += this.nav_jump;
		if (marg >= this.nav_jump2) {
			marg = this.nav_jump2;
			if (this.scrolling && marg < 0) {
				this.scroll_timer = setTimeout(function(){current_gallery.nav_left()}, 10);
			}
			this.scrolling = false;
		} else {
			current_gallery = this;
			setTimeout(function(){current_gallery.nav_left1()}, 10);
		}
		smalls.style.marginLeft = marg + 'px';
	}
	
	this.nav_right = function() {
		if (this.navn == this.smalln || this.scrolling) {
			return;
		}
		var gallery = document.getElementById(this.id);
		var smalls = getElementsByClassName('smalls_wrap', null, gallery)[0];
		var small = getElementsByClassName('small', null, smalls)[this.navn];
		var img = small.getElementsByTagName('img')[0];
		var maxw = smalls.offsetWidth - galleryw;
		if (this.nav_jump2 < -maxw) {
			return;
		}
		this.nav_jump2 = this.nav_jump2 - img.width - 10;
		this.navn++;
		this.scrolling = true;
		this.nav_right1();
	}
	
	this.nav_right1 = function() {
		var gallery = document.getElementById(this.id);
		var smalls = getElementsByClassName('smalls_wrap', null, gallery)[0];
		var marg = smalls.style.marginLeft;
		if (marg == '') {
			marg = 0;
		} else {
			marg = parseInt(marg.substr(0, marg.length-2));
		}
		marg -= this.nav_jump;
		var wrap = getElementsByClassName('smalls_wrap', null, gallery)[0];
		var maxw = wrap.offsetWidth - galleryw;
		if (marg <= this.nav_jump2) {
				marg = this.nav_jump2;
				if (this.scrolling && marg > -maxw) {
					this.scroll_timer = setTimeout(function(){current_gallery.nav_right()}, 10);
				}
				this.scrolling = false;
		} else {
			current_gallery = this;
			setTimeout(function(){current_gallery.nav_right1()}, 10);
		}
		smalls.style.marginLeft = marg + 'px';
	}
	
	this.nav_stop = function() {
		this.scrolling = false;
		clearTimeout(this.scroll_timer);
	}
	
	this.oldn = this.initn;
	
	this.set_img = function(n) {
		var gallery = document.getElementById(this.id);
		var bigs = getElementsByClassName('bigs', null, gallery)[0];
		var big = getElementsByClassName('big', null, bigs)[this.oldn];
		var img = big.getElementsByTagName('img')[0];
		img.style.height = '0px';
		big.style.height = '0px';
		big.style.position = 'absolute';
		big.style.visibility = 'hidden';
		this.oldn = n;
		big = getElementsByClassName('big', null, bigs)[n];
		img = big.getElementsByTagName('img')[0];
		img.style.height = '360px';
		if (img.src == '') {
			var filename = getElementsByClassName('filename', null, big)[0].childNodes[0].nodeValue;
			img.style.visibility = 'hidden';
			img.onload = function(){this.style.padding = '4px'; this.style.visibility = 'inherit'};
			img.src = filename;
			//img.onload = img_loaded;
		}
		big.style.height = 'auto';
		big.style.position = 'static';
		big.style.visibility = 'visible';
	}
	
	this.gal_close = function() {
		var gallery = document.getElementById(this.id);
		var bigs = getElementsByClassName('bigs', null, gallery)[0];
		var big = getElementsByClassName('big', null, bigs)[this.oldn];
		big.style.height = '0px';
		big.style.position = 'absolute';
		big.style.visibility = 'hidden';
		bigs.style.visibility = 'hidden';
		bigs.style.width = '0px';
		bigs.style.height = '0px';
		bigs.style.position = 'absolute';
		var smalls = getElementsByClassName('smalls', null, gallery)[0];
		smalls.style.width = '0px';
		smalls.style.height = '0px';
		smalls.style.position = 'absolute';
		smalls.style.visibility = 'hidden';
		var nav = getElementsByClassName('nav', null, gallery)[0];
		nav.style.width = '0px';
		nav.style.height = '0px';
		nav.style.position = 'absolute';
		nav.style.visibility = 'hidden';
		var close = getElementsByClassName('close', null, gallery)[0];
		close.style.width = '0px';
		close.style.height = '0px';
		close.style.position = 'absolute';
		close.style.visibility = 'hidden';
		var wrap = getElementsByClassName('smalls_wrap', null, gallery)[0];
		wrap.style.marginLeft = '0px';
		this.hidebg();
	}
	
	function img_loaded(e) {
		e = e || window.event;
		var img = e.target || e.srcElement;
		img.style.padding = '4px'; 
		img.style.visibility = 'inherit';
	}

}

