

window.FXdisableIEHeightFix = true;


function Gallery(galleryname) {
	this.galleryname = (galleryname) ? galleryname.toLowerCase() : 'gallery';

	/*** default options ***/
	/* these are default options, do not modify directly
	 * (unless changing the default value is your intention)
	 * rather, override these options when creating your gallery
	 * ex: var g = new Gallery('mygal'); g.foo = bar; etc;
	*/
	this.options = new Object();

	this.options.imageDir 					= 'images/';
	this.options.loadingImage 				= 'loading.gif';
	this.options.resizeSpeed 				= 6;	// controls the speed of the image resizing (1=slowest and 10=fastest)

	this.options.overlay = new Object();
	this.options.overlay.enabled			= true;
	this.options.overlay.backgroundColor 	= '#000000'; // overlay color
	this.options.overlay.opacity 			= 50;  // 0-100 (%)
	this.options.overlay.duration 			= 500;  // milliseconds

	this.options.loading = new Object();
	this.options.loading.enabled			= true;
	this.options.loading.image				= '';
	this.options.loading.text				= 'Loading photos<br>Please wait...';
	this.options.loading.fontSize			= '24px';
	this.options.loading.backgroundColor 	= '#000000'; // overlay color
	this.options.loading.fontColor		 	= '#FFFFFF';
	this.options.loading.opacity 			= 50;  // 0-100 (%)
	this.options.loading.duration 			= 300;  // milliseconds


	this.options.frame = new Object();
	this.options.frame.backgroundColor 		= 'white'; // frame color
	this.options.frame.width 				= 10;

	this.options.data = new Object();
	this.options.data.backgroundColor		 = 'white'; // frame color
	this.options.data.fontSize 				= '14px';
	this.options.data.fontColor 			= 'blue';

	this.options.navigate = new Object();
	this.options.navigate.enabled 			= true;
	this.options.navigate.prevImage 		= 'prev.gif';
	this.options.navigate.prevImageOver 	= 'prev-over.gif';
	this.options.navigate.nextImage 		= 'next.gif';
	this.options.navigate.nextImageOver 	= 'next-over.gif';
	this.options.navigate.closeImage 		= 'close.gif';
	this.options.navigate.closeImageOver 	= 'close-over.gif';
	this.options.navigate.lockImage 		= 'lock.gif';
	this.options.navigate.lockImageOn 		= 'lock-on.gif';

	this.options.slideshow = new Object();
	this.options.slideshow.enabled 			= true;
	this.options.slideshow.interval 		= 8; // seconds
	this.options.slideshow.playImage 		= 'play.gif';
	this.options.slideshow.playImageOver 	= 'play-over.gif';
	this.options.slideshow.pauseImage 		= 'pause.gif';
	this.options.slideshow.pauseImageOver 	= 'pause-over.gif';

	this.options.keyboard = new Object();
	this.options.keyboard.enabled 			= true;
	this.options.keyboard.prevKey 			= 'P';
	this.options.keyboard.nextKey 			= 'N';
	this.options.keyboard.closeKey 			= 'X';
	this.options.keyboard.playKey 			= 'A';
	this.options.keyboard.lockKey 			= 'L';



	/*** /default options ***/

	/***  ***/
	this.initialized = false;
	this.galleryimages = new Array();
	this.obj = new Array();
	this.currentImageIndex = -1;
	this.isActiveGallery = false;
	this.isSlideshowPlaying = false;
	this.photoCount = 0;
	this.lockNav = false;
	this.manualLockNav = false;
	this.preloadImageList = new Array();


	/*** preinitialize ***/
	this.preinitialize = function() {
		if(!window.galleries) window.galleries = new Array();
		window.galleries[this.galleryname] = this;
	}

	/*** initialize ***/
	this.initialize = function() {


		if (!document.getElementsByTagName) return;
		var alist = document.getElementsByTagName('a');

		// find gallery links with a rel attribute of gallery[galleryname]
		for (var i=0; i<alist.length; ++i) {
			var rel = String(alist[i].getAttribute('rel'));
			
			if (alist[i].getAttribute('href') && (rel.toLowerCase() == 'gallery['+this.galleryname+']')) {
				// check for dupe
				var thishref = alist[i].getAttribute('href');
				var isdupe = false;
				for(var p=0; p<this.galleryimages.length; ++p) {
					if(this.galleryimages[p].href == thishref) {
						isdupe = true;
						break;
					}
				}
				if(!isdupe) {
					var imgindex = this.galleryimages.length;
					this.galleryimages[imgindex] = new this.GalleryImage(this, alist[i], thishref, alist[i].getAttribute('title'), alist[i].getAttribute('description'));
				}
			}
		}
		this.loadOptions();

		/********** create gallery objects ***********/
		
		/**** overlay ****/
		this.obj['overlay'] = $('gallery_overlay');
		if(!this.obj['overlay']) {
			this.obj['overlay'] = document.createElement('div');
			this.obj['overlay'].setAttribute('id', 'gallery_overlay');
			this.obj['overlay'].style.position = 'absolute';
			this.obj['overlay'].style.top = 0;
			this.obj['overlay'].style.left = 0;
			this.obj['overlay'].style.zIndex = 90;
			this.obj['overlay'].style.width = '100%';
			this.obj['overlay'].style.height = '300px';
			this.obj['overlay'].style.backgroundColor = this.options.overlay.backgroundColor;
			this.obj['overlay'].gallery = this;
			this.obj['overlay'].onclick = function() {
				this.gallery.end();
				return false;
			};
			document.body.appendChild(this.obj['overlay']);
		}
		this.obj['overlay'].effect = new fx.Opacity(this.obj['overlay'], { duration: this.options.overlay.duration });	
		this.obj['overlay'].effect.hide();

		/**** gallery ****/
		this.obj['gallery'] = document.createElement('div');
		this.obj['gallery'].setAttribute('id','gallery_'+this.galleryname);
		this.obj['gallery'].style.display = 'none';
		this.obj['gallery'].style.position = 'absolute';
		this.obj['gallery'].style.top = '40px';
		this.obj['gallery'].style.left = '0';
		this.obj['gallery'].style.width = '100%';
		this.obj['gallery'].style.zIndex = '100';
		this.obj['gallery'].style.textAlign = 'center';
		this.obj['gallery'].style.lineHeight = '0';
		this.obj['gallery'].style.backgroundColor = 'transparent';
		document.body.appendChild(this.obj['gallery']);

		/**** outerimagecontainer ****/
		this.obj['outerimagecontainer'] = document.createElement('div');
		this.obj['outerimagecontainer'].setAttribute('id','galleryouterimagecontainer_'+this.galleryname);
		this.obj['outerimagecontainer'].setAttribute('galleryname', this.galleryname);
		this.obj['outerimagecontainer'].style.position = 'relative';
		this.obj['outerimagecontainer'].style.backgroundColor = this.options.frame.backgroundColor;
		this.obj['outerimagecontainer'].style.width = '250px';
		this.obj['outerimagecontainer'].style.height = '250px';
		this.obj['outerimagecontainer'].style.margin = '0 auto';

		this.obj['outerimagecontainer'].onmouseover = function() {
			window.galleries[this.getAttribute('galleryname')].showNav();
		}
		this.obj['outerimagecontainer'].onmouseout = function() {
			window.galleries[this.getAttribute('galleryname')].hideNav();
		}

		this.obj['gallery'].appendChild(this.obj['outerimagecontainer']);

		/**** imagecontainer ****/
		this.obj['imagecontainer'] = document.createElement('div');
		this.obj['imagecontainer'].setAttribute('id', 'galleryimagecontainer_'+this.galleryname);
		this.obj['imagecontainer'].setAttribute('galleryname', this.galleryname);
		this.obj['imagecontainer'].style.padding = this.options.frame.width + 'px';
		this.obj['outerimagecontainer'].appendChild(this.obj['imagecontainer']);

		/**** galleryimage ****/
		this.obj['galleryimage'] = document.createElement('img');
		this.obj['galleryimage'].setAttribute('id','galleryimage_'+this.galleryname);
		this.obj['galleryimage'].setAttribute('galleryname', this.galleryname);
		this.obj['imagecontainer'].appendChild(this.obj['galleryimage']);

		this.obj['galleryimage'].onclick = function() {
			window.galleries[this.getAttribute('galleryname')].photoClick();
		}

		this.obj['galleryimage'].effect = new fx.Opacity(this.obj['galleryimage'], { duration: 350, onComplete: function() {  }});
		this.obj['galleryimage'].effect.hide();


		/**** loading ****/
		this.obj['loading'] = document.createElement('div');
		this.obj['loading'].setAttribute('id', 'galleryloading_'+this.galleryname);

		this.obj['loading'].style.position = 'absolute';
		this.obj['loading'].style.top = '40%';
		this.obj['loading'].style.left = '0%';
		this.obj['loading'].style.height = '25%';
		this.obj['loading'].style.width = '100%';
		this.obj['loading'].style.textAlign = 'center';
		this.obj['loading'].style.lineHeight = '0';
		this.obj['imagecontainer'].appendChild(this.obj['loading']);


		/**** loadinglink ****/
		this.obj['loadinglink'] = document.createElement('a');
		this.obj['loadinglink'].setAttribute('id', 'loadinglink_'+this.galleryname);
		this.obj['loadinglink'].setAttribute('galleryname', this.galleryname);
		this.obj['loadinglink'].setAttribute('href', 'javascript:void(0)');
		this.obj['loadinglink'].style.textDecoration = 'none';
		this.obj['loadinglink'].style.color = '#555555';
		this.obj['loadinglink'].style.fontSize = '20px';
		this.obj['loadinglink'].style.fontWeight = 'bold';
		this.obj['loadinglink'].onclick = function() { window.galleries[this.getAttribute('galleryname')].end(); return false; }

		this.obj['loading'].appendChild(this.obj['loadinglink']);

		/**** loadingimage ****/
		this.obj['loadingimage'] = document.createElement("img");
		this.obj['loadingimage'].style.border = 'none';

		if(this.options.loadingImage) {
			this.obj['loadingimage'].setAttribute('src', this.options.imageDir + this.options.loadingImage);
			this.obj['loadinglink'].appendChild(this.obj['loadingimage']);
		} else {
			this.obj['loadinglink'].innerHTML = 'Loading ...';
		}

		/**** hovernav ****/
		this.obj['hovernav'] = document.createElement('div');
		this.obj['hovernav'].setAttribute('id', 'navlinks_'+this.galleryname);
		this.obj['hovernav'].style.position = 'absolute';
		this.obj['hovernav'].style.width = '100%';
		this.obj['hovernav'].style.height = '100%';
		this.obj['hovernav'].style.zIndex = '10';
		this.obj['hovernav'].style.top = '0';
		this.obj['hovernav'].style.left = '0';

		var overcode = ''; var outcode = '';
		if(this.options.navigate.prevImageOver) {
			overcode = "this.src='"+this.options.imageDir + this.options.navigate.prevImageOver+"'";
			outcode = "this.src='"+this.options.imageDir + this.options.navigate.prevImage+"'";
		}
		var prev = '<img src="'+this.options.imageDir + this.options.navigate.prevImage+'" style="border:none; cursor:pointer;" onclick="window.galleries[\''+this.galleryname+'\'].slideShowStop(); window.galleries[\''+this.galleryname+'\'].prevImage()" onmouseover="'+overcode+'" onmouseout="'+outcode+'">';

		var overcode = ''; var outcode = '';
		if(this.options.navigate.nextImageOver) {
			overcode = "this.src='"+this.options.imageDir + this.options.navigate.nextImageOver+"'";
			outcode = "this.src='"+this.options.imageDir + this.options.navigate.nextImage+"'";
		}
		var next = '<img src="'+this.options.imageDir + this.options.navigate.nextImage+'" style="border:none; cursor:pointer;" onclick="window.galleries[\''+this.galleryname+'\'].slideShowStop(); window.galleries[\''+this.galleryname+'\'].nextImage()" onmouseover="'+overcode+'" onmouseout="'+outcode+'">';


		var overcode = ''; var outcode = '';
		if(this.options.navigate.closeImageOver) {
			overcode = "this.src='"+this.options.imageDir + this.options.navigate.closeImageOver+"'";
			outcode = "this.src='"+this.options.imageDir + this.options.navigate.closeImage+"'";
		}
		var close = '<img src="'+this.options.imageDir + this.options.navigate.closeImage+'" style="border:none; cursor:pointer;" onclick="window.galleries[\''+this.galleryname+'\'].end()" onmouseover="'+overcode+'" onmouseout="'+outcode+'">'; 



		this.obj['imagecontainer'].appendChild(this.obj['hovernav']);

		var buf = ''
			+ '<table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0">'
			+ ''
			+ '<tr>'
			+ '<td align="left" valign="top" style="white-space:nowrap;">'+prev+'</td>'
			+ '<td align="right" valign="top" style="white-space:nowrap;">'+next+'</td>'
			+ '</tr>'
			+ ''
			+ '<tr>'
			+ '<td align="left" valign="bottom" style="white-space:nowrap;">&nbsp;</td>'
			+ '<td align="right" valign="bottom" style="white-space:nowrap;">'+close+'</td>'
			+ '</tr>'
			+ ''
			+ '</table>'
			+ ''
		;
		this.obj['hovernav'].innerHTML = buf;

		this.obj['hovernav'].effect = new fx.Opacity(this.obj['hovernav'], { duration: 200, onComplete: function() {  } });
		this.obj['hovernav'].effect.hide();


		/**** imagedatacontainer ****/
		this.obj['imagedatacontainer'] = document.createElement('div');
		this.obj['imagedatacontainer'].setAttribute('id','imagedatacontainer_'+this.galleryname);
		this.obj['imagedatacontainer'].setAttribute('galleryname', this.galleryname);
		this.obj['imagedatacontainer'].style.backgroundColor = this.options.data.backgroundColor;
		this.obj['imagedatacontainer'].style.margin = '0 auto';
		this.obj['imagedatacontainer'].style.lineHeight = '1.4em';

		this.obj['imagedatacontainer'].onmouseover = function() {
			window.galleries[this.getAttribute('galleryname')].showNav();
		}
		this.obj['imagedatacontainer'].onmouseout = function() {
			window.galleries[this.getAttribute('galleryname')].hideNav();
		}
		this.obj['gallery'].appendChild(this.obj['imagedatacontainer']);


		var playpause = '';
		if(this.photoCount > 1 && this.options.slideshow.enabled && this.options.slideshow.interval > 0) {

			var overcode = ''; var outcode = '';
			if(this.options.slideshow.playImageOver) {
				overcode = "this.src='"+this.options.imageDir + this.options.slideshow.playImageOver+"'";
				outcode = "this.src='"+this.options.imageDir + this.options.slideshow.playImage+"'";
			}
			playpause = '<img id="galleryslideshowstartstop_'+this.galleryname+'" src="'+this.options.imageDir + this.options.slideshow.playImage+'" style="cursor:pointer; border:none;" onclick="window.galleries[\''+this.galleryname+'\'].slideShowStartStop()" onmouseover="'+overcode+'" onmouseout="'+outcode+'">';
		}

		var locknav = '';
		if(this.options.navigate.enabled && this.options.navigate.lockImage && this.options.navigate.lockImageOn) {
			var imgsrc = (this.manualLockNav) ? this.options.navigate.lockImageOn : this.options.navigate.lockImage;
			locknav = '<img id="gallerylocknavimage_'+this.galleryname+'" src="'+this.options.imageDir + imgsrc+'" style="cursor:pointer; border:none;" onclick="window.galleries[\''+this.galleryname+'\'].setManualLockNav()">';
		}

		var buf = ''
			+ '<table width="100%" cellpadding="0" cellspacing="0" border="0">'
			+ ''
			+ '<tr>'
			+ '<td align="left" valign="top" style="white-space:nowrap;">'+prev+'</td>'
			+ '<td align="left" valign="top" style="width:99%; font-size:12px;" id="gallerydatatext_'+this.galleryname+'">&nbsp;</td>'
			+ '<td align="right" valign="top" style="white-space:nowrap;">'+next+'</td>'
			+ '</tr>'
			+ ''
			+ '</table>'
			+ ''
		;
		var buf = ''
			+ '<table width="100%" cellpadding="0" cellspacing="0" border="0">'
			+ ''
			+ '<tr>'
			+ '<td align="left" valign="top" style="width:99%; padding:0px; font-size:12px;" id="gallerydatatext_'+this.galleryname+'">&nbsp;</td>'
			+ '<td align="right" valign="bottom" style="padding:0px; width:1%;">'
			+  locknav + '<br>' + playpause
			+ '</td>'
			+ '</tr>'
			+ ''
			+ '</table>'
			+ ''
		;
		this.obj['imagedatacontainer'].innerHTML = buf;

		this.obj['imagedatacontainer'].effect = new fx.Opacity(this.obj['imagedatacontainer'],
			{
				duration: 200,
				onComplete: function() {
					//if(this.now == 0) window.galleries[this.el.getAttribute('galleryname')].clearNavText();
					//else  window.galleries[this.el.getAttribute('galleryname')].setNavText();
				}
			}
		); 
		this.obj['imagedatacontainer'].effect.hide();


		/********** done with gallery objects ***********/


		this.loadOptions();


		this.hideLoadingScreenTimer = setTimeout("window.galleries['"+this.galleryname+"'].hideLoadingScreen()", 50);

		this.initialized = true;
	}

	this.hideLoadingScreen = function(n) {

		n = (n) ? n * 1 : 0;

		var divloading = $('gallery_loading');

		if(divloading && divloading.effect) {
			divloading.effect.clearTimer();
			divloading.effect.custom(divloading.effect.now, 0);
			return;
		}
		else if(++n > 10) {
			document.body.removeChild(divloading);
			return;
		}
		else {
			this.hideLoadingScreenTimer = setTimeout("window.galleries['"+this.galleryname+"'].hideLoadingScreen("+n+")", 50);
		}

	}

	this.displayLoadingScreen = function() {

		if(!this.options.loading.enabled) return;

		 if(document.body) {
			with(this.obj['loading'] = document.createElement('div')) {
				clearTimeout(this.displayLoadingScreenTimer);

				setAttribute('id', 'gallery_loading');
				style.position = 'absolute';
				style.top = 0;
				style.left = 0;
				style.zIndex = 90;
				style.width = '100%';
				style.height = '300px';
				style.textAlign = 'center';
				style.backgroundColor = this.options.loading.backgroundColor;
				gallery = this;

			}
			this.obj['loading'].effect = new fx.Opacity(this.obj['loading'], { duration: this.options.loading.duration });	
			document.body.appendChild(this.obj['loading']);
			this.obj['loading'].effect.hide();




			with(this.obj['loadingtext'] = document.createElement('div')) {
				style.fontWeight = 'bold';
				style.fontSize = this.options.loading.fontSize;
				style.color = this.options.loading.fontColor;
				style.position = 'absolute';
				style.textAlign = 'center';
				style.borderStyle = 'solid';
				style.borderWidth = '2px';
				style.borderColor = this.options.loading.fontColor;
				style.padding = '10px';
				//style.width = '100%';

			}
			this.obj['loading'].appendChild(this.obj['loadingtext']);

			if(this.options.loading.image) {
				var img;
				with(img = document.createElement('img')) {
					alt = 'Loading ...';
				}
				this.obj['loadingtext'].appendChild(img);
				img.src = this.options.imageDir + this.options.loading.image;

			} else {
				this.obj['loadingtext'].innerHTML = this.options.loading.text;
			}





			this.obj['loading'].style.height = getPageSize()[1] + 'px';
			this.obj['loadingtext'].style.top = ( (getPageSize()[3] / 2) - this.obj['loadingtext'].offsetHeight) + 'px';
			this.obj['loadingtext'].style.left = ( (getPageSize()[0] / 2) - (this.obj['loadingtext'].offsetWidth / 2) ) + 'px';
			this.obj['loading'].effect.custom(0, this.options.loading.opacity / 100);

		} else {
			this.displayLoadingScreenTimer = setTimeout("window.galleries['"+this.galleryname+"'].displayLoadingScreen();", 50);
		}

	}

	this.loadOptions = function() {
		this.photoCount = this.galleryimages.length;

		if(this.options.resizeSpeed > 10) this.options.resizeSpeed = 10;
		else if(this.options.resizeSpeed < 1) this.options.resizeSpeed = 1;
		this.options.resizeDuration = (11 - this.options.resizeSpeed) * 100;

		this.options.keyboard.prevKey = this.options.keyboard.prevKey.toLowerCase();
		this.options.keyboard.nextKey = this.options.keyboard.nextKey.toLowerCase();
		this.options.keyboard.closeKey = this.options.keyboard.closeKey.toLowerCase();
		this.options.keyboard.playKey = this.options.keyboard.playKey.toLowerCase();
		this.options.keyboard.lockKey = this.options.keyboard.lockKey.toLowerCase();

	}


	this.set = function(what, val) {
		if(!what) return;

		var bits = what.split(/\./);
		switch(bits.length) {
			case 1: this.options[bits[0]] = val; break;
			case 2: this.options[bits[0]][bits[1]] = val; break;
			case 3: this.options[bits[0]][bits[1]][bits[2]] = val; break;
		}
	}

	this.setManualLockNav = function() {
		this.manualLockNav = (this.manualLockNav) ? false : true;

		var img = $('gallerylocknavimage_'+this.galleryname);
		if(this.manualLockNav) {
			this.showNav();
			img.src = this.options.imageDir + this.options.navigate.lockImageOn;
		} else {
			this.hideNav();
			img.src = this.options.imageDir + this.options.navigate.lockImage;
		}
	}



	this.slideShowStartStop = function() {
		if(this.isSlideshowPlaying) this.slideShowStop();
		else this.slideShowStart();
	}

	this.slideShowStart = function() {
		var img = $('galleryslideshowstartstop_'+this.galleryname);
		if(img) {
			img.setAttribute('galleryname', this.galleryname);
			img.src = g.options.imageDir + this.options.slideshow.pauseImage;
			img.onmouseover = function() {
				var g = window.galleries[this.getAttribute('galleryname')];
				this.src = g.options.imageDir + g.options.slideshow.pauseImageOver;
			}
			img.onmouseout = function() {
				var g = window.galleries[this.getAttribute('galleryname')];
				this.src = g.options.imageDir + g.options.slideshow.pauseImage;
			}
		}
		this.isSlideshowPlaying = true;

		this.nextImage();
		this.slideshowtimer = setInterval("window.galleries['"+this.galleryname+"'].nextImage()", this.options.slideshow.interval * 1000);
	}

	this.slideShowStop = function() {
		var img = $('galleryslideshowstartstop_'+this.galleryname);
		if(img) {
			img.setAttribute('galleryname', this.galleryname);
			img.src = this.options.imageDir + this.options.slideshow.playImage;
			img.onmouseover = function() {
				var g = window.galleries[this.getAttribute('galleryname')];
				this.src = g.options.imageDir + g.options.slideshow.playImageOver;
			}
			img.onmouseout = function() {
				var g = window.galleries[this.getAttribute('galleryname')];
				this.src = g.options.imageDir + g.options.slideshow.playImage;
			}
		}
		this.isSlideshowPlaying = false;
		clearInterval(this.slideshowtimer);
	}

	this.photoClick = function() {
		//this.end();

	}


	this.clearNavText = function() {
		var navtext = $('gallerydatatext_'+this.galleryname);
		navtext.innerHTML = '&nbsp;';
	}
	this.setNavText = function() {
		var navtext = $('gallerydatatext_'+this.galleryname);
		var img = this.galleryimages[this.currentImageIndex];

		var imginfo = new Array();

		if(this.photoCount > 1) {
			imginfo[imginfo.length] = 'Image ' + (this.currentImageIndex + 1) + ' of ' + this.photoCount;
		}

		imginfo[imginfo.length] = '' + this.obj['galleryimage'].offsetWidth + ' x ' + this.obj['galleryimage'].offsetHeight;

//		var fsize = ''; var ftype = '';
//		if(this.obj['galleryimage'].fileSize) {
//			var fs = Math.ceil(this.obj['galleryimage'].fileSize / 1024);
//			if(fs > 1) fsize = fs  + 'kb ';
//		}
//		imginfo[imginfo.length] = fsize + ftype;



		var imginfostr = '<b>'+img.title+'</b><br>' + '<span style="font-size:80%;">' + imginfo.join('<br>') + '</span>';

		var buf = ''
			+	'<table width="100%" cellpadding="0" cellspacing="0" style="font-size:'+this.options.data.fontSize+'; color:'+this.options.data.fontColor+';">'
			+	'<tr>'
			+	'<td valign="top" width="1%" style="white-space:nowrap; padding: 0px '+this.options.frame.width+'px '+this.options.frame.width+'px '+this.options.frame.width+'px;">'
			+	imginfostr
			+	'</td>'
			+	'<td valign="top" style="padding: 0px '+this.options.frame.width+'px '+this.options.frame.width+'px '+this.options.frame.width+'px;">'
			+	img.description
			+	'</td>'
			+	'</tr>'
			+	'</table>'
			+	''
		;

		navtext.innerHTML = buf;
	}

	this.showNav = function() {
		clearTimeout(this.obj['hovernav'].timer);
		if(this.lockNav) return;

		if(this.obj['hovernav'].effect.now == 0) {
			if(this.options.navigate.enabled && this.photoCount > 1) {
				this.obj['hovernav'].style.width='100%';
				this.obj['hovernav'].effect.custom(0,1);
			}
		}

		this.setNavText();

		if(this.obj['imagedatacontainer'].effect.now == 0) {
			this.obj['imagedatacontainer'].style.width = this.obj['outerimagecontainer'].offsetWidth + 'px';
			this.obj['imagedatacontainer'].effect.custom(0, 1);
		}

	}
	this.hideNav = function() {
		clearTimeout(this.obj['hovernav'].timer);
		this.obj['hovernav'].timer = setTimeout("window.galleries['"+this.galleryname+"'].doHideNav()", 100);
	}

	this.doHideNav = function(force) {
		if((this.lockNav || this.manualLockNav) && !force) return;

		if(force) {
			this.obj['hovernav'].effect.hide();
			this.obj['imagedatacontainer'].effect.hide(1, 0);
			this.clearNavText();
		} else {
			if(this.obj['hovernav'].effect.now > 0) this.obj['hovernav'].effect.custom(1, 0);
			if(this.obj['imagedatacontainer'].effect.now > 0) this.obj['imagedatacontainer'].effect.custom(1, 0);
		}
	}



	this.prevImage = function() {
		var i = this.getPrevImageIndex();
		this.startShowGalleryImage(i);
	}
	this.nextImage = function() {
		var i = this.getNextImageIndex();
		this.startShowGalleryImage(i);
	}

	this.start = function(galleryimage) {
		this.isActiveGallery = true;

		hideSelectBoxes();

		this.obj['hovernav'].effect.hide();
		this.obj['imagedatacontainer'].effect.hide();

		// start show overlay
		if(this.options.overlay.enabled) {
			this.obj['overlay'].style.height = getPageSize()[1] + 'px';
			this.obj['overlay'].style.backgroundColor = this.options.overlay.backgroundColor;
			this.obj['overlay'].effect.custom(0, this.options.overlay.opacity / 100);
		}

		// calculate top offset for the gallery display
		var pagesize = getPageSize();
		var pagescroll = getPageScroll();
		var gallerytop = pagescroll[1] + (pagesize[3] / 15);

		// show gallery
		this.obj['gallery'].style.top = gallerytop + 'px';
		this.obj['gallery'].style.display = '';


		this.startShowGalleryImage(galleryimage.index);
	}

	this.startShowGalleryImage = function(imageindex) {
		this.currentImageIndex = imageindex;

		this.disableKeyboardNav();

		// hide elements during transition
		this.obj['loadinglink'].style.display = '';
		this.obj['galleryimage'].effect.hide();
		this.doHideNav(true);
		this.lockNav = new Date().getTime();

		// preload image
		var imgPreloader = new Image();

		// once image is preloaded, resize image container
		imgPreloader.setAttribute('galleryname', this.galleryname);
		imgPreloader.onload=function() {
			var g = window.galleries[this.getAttribute('galleryname')];
			g.obj['galleryimage'].src = g.galleryimages[g.currentImageIndex].href;
			g.resizeImageContainer(this.width, this.height);
		}
		if(this.preloadImageList[imageindex]) {
			imgPreloader.src = this.preloadImageList[imageindex].src;
		} else {
			imgPreloader.src = this.galleryimages[imageindex].href;
		}
	}

	this.resizeImageContainer = function(w, h) {

		// get current height and width
		this.hCur = this.obj['outerimagecontainer'].offsetHeight;
		this.wCur = this.obj['outerimagecontainer'].offsetWidth;

		// calculate size difference between new and old image, and resize if necessary
		var wDiff = (this.wCur - this.options.frame.width * 2) - w;
		var hDiff = (this.hCur - this.options.frame.width * 2) - h;

		// Resize the outerImageContainer very sexy like
		var reHeight = new fx.Height(this.obj['outerimagecontainer'], { duration: this.options.resizeDuration });
		reHeight.custom(this.hCur, h + (this.options.frame.width * 2)); 

		reWidth = new fx.Width(this.obj['outerimagecontainer'], {
				duration: this.options.resizeDuration,
				onComplete: function() {
					var g = window.galleries[this.galleryname];
					g.obj['galleryimage'].effect.custom(0, 1);
					g.lockNav = 0;
					g.enableKeyboardNav();
					if(g.manualLockNav) g.showNav();
				}
			}
		);

		reWidth.galleryname = this.galleryname;
		reWidth.custom(this.wCur, w + (this.options.frame.width*2));

		// if new and old image are same size and no scaling transition is necessary, 
		// do a quick pause to prevent image flicker.
		if((hDiff == 0) && (wDiff == 0)){
			if (navigator.appVersion.indexOf("MSIE")!=-1){ pause(250); } else { pause(100);} 
		}


		this.obj['loadinglink'].style.display = 'none';
		this.preloadNextImage();
	}

	this.preloadNextImage = function() {
		var i = this.getNextImageIndex();
		this.preloadImageList[i] = new Image();
		this.preloadImageList[i].src = this.galleryimages[i].href;

	}

	this.getPrevImageIndex = function() {
		var i = this.currentImageIndex - 1;
		if(i < 0) i = this.galleryimages.length - 1;
		return i;
	}
	this.getNextImageIndex = function() {
		var i = this.currentImageIndex + 1;
		if(i >= this.galleryimages.length) i = 0;
		return i;
	}


	this.end = function() {
		showSelectBoxes();

		this.disableKeyboardNav();
		this.slideShowStop();

		// hide nav
		this.doHideNav();

		// hide gallery
		this.obj['gallery'].style.display = 'none';

		// start hide overlay
		this.obj['overlay'].effect.custom(this.options.overlay.opacity / 100, 0);

		this.isActiveGallery = false;
	}

	this.enableKeyboardNav = function() {
		if(!this.options.keyboard.enabled) return this.disableKeyboardNav();
		document.onkeydown = this.keyboardAction; 
	}

	this.disableKeyboardNav = function() {
		document.onkeydown = ''; 
	}

	this.keyboardAction = function(e) {
		var keycode;
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}
		var key = String.fromCharCode(keycode).toLowerCase();

		var gal = false;
		for(var n in window.galleries) {
			if(window.galleries[n].isActiveGallery) {
				gal = window.galleries[n];
				break;
			}
		}
		if(!gal) return;

		switch(key) {
			case gal.options.keyboard.prevKey: gal.prevImage(); break;
			case gal.options.keyboard.nextKey: gal.nextImage(); break;
			case gal.options.keyboard.closeKey: gal.end(); break;
			case gal.options.keyboard.playKey: gal.slideShowStartStop(); break;
			case gal.options.keyboard.lockKey: gal.setManualLockNav(); break;
			default: return;
		}

	}

	/*** GalleryImage object ***/
	this.GalleryImage = function(gallery, anchor, href, title, description) {
		this.gallery = gallery;
		this.anchor = anchor;
		this.href = (href) ? href : '';
		this.title = (title) ? title : '';
		this.description = (description) ? description : '';

		this.index = this.gallery.galleryimages.length;
		this.anchor.galleryImage = this;

		if(this.anchor) {
			this.anchor.onclick = function() {
				this.galleryImage.show();
				return false;
			};
		}

		this.show = function() {
			this.gallery.start(this);

		}
	}

	this.preinitialize();
}




/*************** utility functions ***************/
//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}



function showSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}


function hideSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

