Portfolio = {
	items: [],
	nr: 0,
	autoplay: 1,
	timeoutId: null,

	showItem: function() {
		with (Portfolio) {
			$('#portfolio-display').html(items[nr]).fadeIn('normal');
			// if (this.autoplay) this.timeoutId = setTimeout('this.nextItem()', 5000);
			if (autoplay) { 
				timeoutId = setTimeout('Portfolio.nextItem()', 4500);
			} else {
				timeoutId = setTimeout('Portfolio.resume()', 10000);
			}
		};
	},
	
	prevItem: function(cancelAutoplay) {
		with (Portfolio) {
			nr--;
			if (nr < 0) nr = items.length - 1;
			hideItem();
		};
	},
	
	nextItem: function() {
		with (Portfolio) {
			nr++;
			if (nr >= items.length) nr = 0;
			hideItem();
		};
	},
	
	hideItem: function() {
		$('#portfolio-display').fadeOut('normal', this.showItem);
	},
	
	cancelAutoplay: function() {
		with (Portfolio) {
			clearTimeout(timeoutId);
			autoplay = 0;
		}
	},
	
	resume: function() {
		with (Portfolio) {
			autoplay = 1;
			nextItem();
		}
	},
	
	init: function() {
		with (Portfolio) {
			$('.portfolio-item').each(function(e) {
					items[items.length] = $(this).html();
			});
			
			$('#portfolio-prev').bind("mouseup", function(e) {
				Portfolio.cancelAutoplay();
				Portfolio.prevItem();
			});
			
			$('#portfolio-next').bind("mouseup", function(e) {
				Portfolio.cancelAutoplay();
				Portfolio.nextItem();
			});
			
			nr = Math.floor(items.length * Math.random());
			
			showItem();
		};
	}
};

$(document).ready(function() {
	Portfolio.init();
});