window.LB || (LB={});
LB.slideshow2 = (function($){ return function(id,opt){
	// Default options
	opt || (opt = {});
	opt.showTime = opt.showTime || 5000;
	opt.slideTime = opt.slideTime || 700;
	opt.slideSteps = opt.slideSteps || 10;
	
	// Setup
	if(id.charAt(0)=='#') id = id.substring(1);
	var container = document.getElementById(id);
	var nex, step = 1, totalSlides = 1, firstSlide, nodes = container.childNodes, x=-1;
	while(!firstSlide && ++x<nodes.length) { if(nodes[x].nodeType==1) firstSlide = nodes[x]; }
	while(++x<nodes.length) { if(nodes[x].nodeType==1) ++totalSlides; }
	if(totalSlides < 2) return;
	var width = $(firstSlide).width();
	var cur = opt.random ? Math.floor(Math.random()*totalSlides) : 0;
	container.style.marginLeft = '-' + cur * width + 'px';
	container.appendChild(firstSlide.cloneNode(true));

	// Pause state
	var blocked = false;
	var paused = false;

	// Private functions
	var doSlide = function(){
		container.style.marginLeft = -1 * (width * cur + (width * step / opt.slideSteps))+'px';
		if (step++ < opt.slideSteps) 
			setTimeout(doSlide, opt.slideTime / opt.slideSteps);
		else {
			step = 1;
			cur = nex;
			setTimeout(doShow, opt.showTime);
		}
	};
	var doShow = function(){
		if (!paused) {
			nex = (cur + 1) % totalSlides;
			doSlide();
		} else {
			blocked = true;
		}
	};
	setTimeout(doShow, opt.showTime);
	
	var pub = {
		play: function(delay) {
			// delay function doesn't quite work
			delay || (delay = 200);
			paused = false;
			if(blocked) {
				blocked = false;
				setTimeout(doShow,delay);
			}
		},
		pause: function() {
			paused = true;
		}
	};
	
	// Pause on hover
	$(container).hover(pub.pause,pub.play);
	// Backdoor DOM access to slideshow 
	container.LB_slideshow2 = pub;
	
	return pub;
}; })(jQuery);