$(function(){
	var dir = 1; // increment in files array
	var timeout = 8000; // time to rotate in ms
	var throttle = 800; // time in ms before you can scroll again
	var lastScroll = 0;
	var cur = 0;
	var files = [
		{src: "http://swmontgomery.com/wp/wp-content/uploads/2010/10/slideshow32.jpg", link: "http://swmontgomery.com/work/965/holly-ivory"},
		{src: "http://swmontgomery.com/wp/wp-content/uploads/2010/11/slideshow4.jpg", link: "http://swmontgomery.com/work/135/luck-of-the-draw-promo"},
		{src: "http://swmontgomery.com/wp/wp-content/uploads/2010/11/slideshow5.jpg", link: "http://swmontgomery.com/work/266/reclamation-film-poster"},
		{src: "http://swmontgomery.com/wp/wp-content/uploads/2010/10/slideshow2.jpg", link: "http://swmontgomery.com/work/132/the-daydream"},
		{src: "http://swmontgomery.com/wp/wp-content/uploads/2010/10/slideshow.jpg", link: "http://swmontgomery.com/work/258/wcub-tv-rate-card"}	
	];
	var fileObjs = [];
	
	// no need to run if we don't have anything to showcase
	if (files.length < 2) {
		return;
	}

	var scrollShow = function(dir){
		// check scroll status
		var now = (new Date()).getTime();
		if (now-lastScroll < throttle) {
			return;
		}
		lastScroll = now;
		
		var next, open = fileObjs[cur];
		cur = (cur + dir) % files.length;
		if (cur < 0)
			cur = files.length + cur;
		next = fileObjs[cur];
		
		// is our current the stock slideshow image?
		if ($("#pres-default").is(":visible")) {
			open = $("#pres-default");
		}
		
		// start animation on current
		open.stop();
		open.clearQueue();
		open.css({
			left: 0,
			opacity: 1,
		});
		open.fadeOut(1000);
		
		// set position and start animation on next
		next.stop();
		next.clearQueue();
		next.css({
			left: 960*dir,
			opacity: 0,
		});
		next.show();
		next.animate({
			left: 0,
			opacity: 1,
		}, 800);
		
		// restart timer
		timer.start();
	};
	
	// pre-load images
	for(var i=0;i<files.length;i++){
		fileObjs[i] = $("#presentation").append("<a href='"+files[i].link+"'><img id='pres-"+i+"' src='"+files[i].src+"'></a>").find("#pres-"+i).hide();
	}
	
	// attach arrow buttons
	var leftButton = $("#presentation").append("<div id='pres-left'>").find("#pres-left").click(function(){
		scrollShow(dir=-1);
	});
	var rightButton = $("#presentation").append("<div id='pres-right'>").find("#pres-right").click(function(){
		scrollShow(dir=1);
	});
		
	// timer control
	var timer = {
		ref: null,
		start: function(callback){
			if (this.ref)
				clearTimeout(this.ref);
			
			this.ref = setTimeout(this.callback, timeout);
		},
		callback: function(){
			scrollShow(dir);
			timer.start();
		},
	};
	timer.start();
});
