
function gallery() {
	this.showTime = 3500;
	this.base = 'http://www.lauranidrayoga.com/';
	this.nextFn = function() {myGallery.playNext()};
	this.galleries = {};
	this.wd = '675';
	this.newGallery('lauranidra','LauraNidra');
}

gallery.prototype.hideImg = function() {
	var el;
	el = document.getElementById('photoVert');
	el.style.left = '-1000px';
	el = document.getElementById('photoHorz');
	el.style.left = '-1000px';
	this.didVert = false;
	this.didHorz = false;
}

gallery.prototype.cancelTimer = function() {
	if (this.timer) clearTimeout(this.timer);
	this.timer = null;
}

gallery.prototype.startTimer = function(fn,tm) {
	if (this.timer) {
		this.cancelTimer();
		setTimeout(function(){myGallery.startTimer(fn,tm)},13);
		return;
	}
	this.timer = setTimeout(fn,tm);
}

gallery.prototype.set = function(idx) {
	var el,wd;
	var nm = this.arr[idx];
	var elVert = document.getElementById('photoVert');
	var elHorz = document.getElementById('photoHorz');
	if (this.arr[idx].indexOf('_V_') != -1) {
		wd = '400';
		el = document.getElementById('photoVertImg');
		el.src = this.base+'gallery/'+this.photos.dirpath+'/'+this.arr[idx];
		if (!this.didVert) elVert.style.left = '248px';
		this.didVert = true;
	} else {
		wd = '675';
		el = document.getElementById('photoHorzImg');
		el.src = this.base+'gallery/'+this.photos.dirpath+'/'+this.arr[idx];
		if (!this.didHorz) elHorz.style.left = '31px';
		this.didHorz = true;
	}

	this.wd = wd;
	el.width = wd;

	//el.src = this.base+'gallery/'+this.photos.dirpath+'/'+this.arr[idx];
	this.idx = idx;

	if (window.console) console.log('set() idx=' + idx + ' src=' + el.src);

	// Set the photo title
	el = document.getElementById('photoTitle');
	if (el) el.innerHTML = this.photoTitle(this.arr[idx]);

	// Set the phototographer
	el = document.getElementById('photographer');
	if (el) {
		if (nm.indexOf('_C_') != -1)
			el.innerHTML = '<a href="http://www.cam2yogi.com/" target="_blank">Cameron Karsten Photography</a>';
		else
			el.innerHTML = '';
	}

	// Preload the next image
	this.preloadImg();
}

gallery.prototype.preloadImg = function() {
	var el, src, i = this.idx + 1;
	if (i >= this.arr.length) i = 0;
	if (!this.preloads) this.preloads = {};
	src = this.base+'gallery/'+this.photos.dirpath+'/'+this.arr[i];
	if (!this.preloads[src]) {
		el = document.createElement('img');
		el.style.position = 'absolute';
		el.style.left = '-3000px';
		el.src = this.base+'gallery/'+this.photos.dirpath+'/'+this.arr[i];
		el = document.getElementsByTagName('body')[0].appendChild(el);
		this.preloads[src] = el;
	}
}

gallery.prototype.photoTitle = function(nm) {
	if (nm.substr(0,2) == 'xx') return '';
	nm = nm.replace(/\d*/g,'');
	nm = nm.replace(/\.jpg/g,'');
	nm = nm.replace(/\.JPG/g,'');
	nm = nm.replace(/^\s+|\s+$/g,'');
	nm = nm.replace(/_C_|_H_|_V_/ig,'');
	return nm;
}

gallery.prototype.showPauseBtn = function(json) {
	var el = document.getElementById('playBtn');
	if (el) {
		el.src = this.base + 'img/gallerynav_pause.gif';
		el.title = 'Pause';
	}
}

gallery.prototype.showPlayBtn = function(json) {
	var el = document.getElementById('playBtn');
	if (el) {
		el.src = this.base + 'img/gallerynav_play.gif';
		el.title = 'Play';
	}
}

gallery.prototype.newGallery = function(nm,title) {
	this.playing = false;
	this.hideImg();
	this.cancelTimer();
	if (this.fading) {
		this.startTimer(function(){myGallery.newGallery(nm,title)},13);
		return;
	}
	$('#photoVert').stop(true,true);
	$('#photoHorz').stop(true,true);
	$('#photoVert').fadeOut('fast');
	$('#photoHorz').fadeOut('fast');
	this.startTimer(function(){myGallery.newGallery2(nm,title)},300);
}

gallery.prototype.newGallery2 = function(nm,title) {
	this.gallery = nm;
	var gg = this.galleries;
	var el = document.getElementById('galleryTitle');
	if (el) el.innerHTML = title || nm;
	this.idx = 0;
	// If gallery is not loaded then load the list of image names for the gallery
	if (!gg[nm]) return this.doScript('gallery/lcl.photolist.php?callbackName=myGallery.play&path='+nm);
	this.startTimer(function(){myGallery.play()},300);
}

gallery.prototype.play = function(json) {
	this.cancelTimer();
	var gg = this.galleries;
	if (json) gg[this.gallery] = eval('('+json+')');
	this.showPauseBtn();
	this.photos = gg[this.gallery];
	this.photo = this.photos.files[0];
	this.arr = this.photos.files;
	this.set(this.idx);
	this.playing = true;
	this.fading = true;
	switch (this.wd) {
	case '400':
		$('#photoVert').fadeIn('slow', function(){myGallery.nextHold()});
		break;
	case '675':
		$('#photoHorz').fadeIn('slow', function(){myGallery.nextHold()});
		break;
	}
}

gallery.prototype.previous = function() {
	this.cancelTimer();
	if (this.fading) {
		this.startTimer(function(){myGallery.previous()},13);
		return;
	}
	var max = this.arr.length;
	var nxt = this.idx - 1;
	if (nxt < 0) nxt = max-1;
	this.idx = nxt;
	this.showPlayBtn();
	this.playing = false;
	this.fadeOut();
}

gallery.prototype.pause = function() {
	this.cancelTimer();
	if (this.fading) {
		this.startTimer(function(){myGallery.pause()},1);
		return;
	}
	if (!this.playing) {
		this.playing = true;
		this.showPauseBtn();
		return this.nextOut();
	}
	this.showPlayBtn();
	this.playing = false;
	this.fadeIn();
}

gallery.prototype.next = function() {
	this.cancelTimer();
	if (this.fading) {
		this.startTimer(function(){myGallery.next()},13);
		return;
	}
	this.startTimer(function(){myGallery.nextOut()},100);
}


gallery.prototype.nextOut = function() {
	this.fading = true;
	switch (this.wd) {
	case '400':
		$('#photoVert').fadeOut('slow', function(){myGallery.nextIn()});
		break;
	case '675':
		$('#photoHorz').fadeOut('slow', function(){myGallery.nextIn()});
		break;
	}
}

gallery.prototype.nextIn = function() {
	var max = this.arr.length;
	var nxt = this.idx + 1;
	if (nxt >= max) nxt = 0;
	this.idx = nxt;
	this.set(nxt);
	this.fading = true;
	switch (this.wd) {
	case '400':
		$('#photoVert').fadeIn('slow', function(){myGallery.nextHold()});
		break;
	case '675':
		$('#photoHorz').fadeIn('slow', function(){myGallery.nextHold()});
		break;
	}
}

gallery.prototype.nextHold = function() {
	this.fading = false;
	if (!this.playing) return;
	this.startTimer(function(){myGallery.nextOut()},this.showTime);
}

gallery.prototype.fadeIn = function() {
	this.set(this.idx);
	switch (this.wd) {
	case '400':
		$('#photoVert').fadeIn('slow', function(){myGallery.nextHold()});
		break;
	case '675':
		$('#photoHorz').fadeIn('slow', function(){myGallery.nextHold()});
		break;
	}
}

gallery.prototype.fadeOut = function() {
	switch (this.wd) {
	case '400':
		$('#photoVert').fadeOut('slow', function(){myGallery.fadeIn()});
		break;
	case '675':
		$('#photoHorz').fadeOut('slow', function(){myGallery.fadeIn()});
		break;
	}
}

gallery.prototype.close = function() {
	window.close();
	this.next();
	this.play();
}

gallery.prototype.doScript = function(u,tg) {
	var scp = document.createElement('script');
	scp.setAttribute('language',"JavaScript");
	document.getElementsByTagName(tg||'body')[0].appendChild(scp);
	scp.setAttribute('src',u);
}


window.myGallery = new gallery();

//document.write('<xmp>'+myGallery.photos.files+'</xmp>');

