var gallery = {

	currentSlide : 1,

	// distance to move slide
	slideDistance : 600,

	init : function() {
		this.wrapper = $("#project-strip");
		this.addSlideControls();
	},

	getSlideCount : function() {
		var width = $(this.wrapper).width();
		this.slideCount = Math.floor(width / this.slideDistance);
		return this.slideCount;
	},

	moveSlide : function(num) {
		this.getSlideCount();

		switch(num) {
			case "right":
				if( this.currentSlide < this.slideCount ) {
					this.currentSlide++;
				}
			break;
			case "left":
				if( this.currentSlide > 1 ) {
					this.currentSlide--;
				}
			break;
			default:
				// this would only happen with scrollies
				if( num > this.slideCount ) {
					this.currentSlide = this.slideCount;
				} else {
					this.currentSlide = num;
				}
			break;
		}

		$(this.wrapper).animate({ left : -((this.currentSlide - 1) * this.slideDistance) }, 500, null, function() {
			gallery.activateSlideControls();
			gallery.slideScrollies();
		});
		return false;
	},

	clearSlideControls : function() {
		$("#slide-controls").remove();
	},

	addSlideControls : function() {
		this.getSlideCount();

		this.clearSlideControls();

		// add slide controls over images
		$(this.wrapper).before('<div id="slide-controls"><span id="back"><a href="#prev-slide" title="<< Previous slide">&nbsp;</a></span> <span id="next"><a href="#next-slide" title="Next slide >>">&nbsp;</a></span></div>');

		// add control handlers
		$("#back a").click(function(e) {
			e.preventDefault();
			gallery.moveSlide("left");
			$(this).removeClass("hover");
		});
		$("#next a").click(function(e) {
			e.preventDefault();
			gallery.moveSlide("right");
			$(this).removeClass("hover");
		});

		$("#back a, #next a").hover(function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		});

		this.activateSlideControls();
		this.slideScrollies();
		this.slideScrollies();
	},

	activateSlideControls : function() {
		if( this.currentSlide === 1 ) {
			$("#back a").hide();
		} else {
			$("#back a").show();
		}

		if( this.currentSlide === this.slideCount ) {
			$("#next a").hide();
		} else {
			$("#next a").show();
		}
	},

	slideScrollies : function() {
		var imgs = "";
		$("#scrollies").html("");
		if( this.slideCount > 1 ) {
			for(i=1; i <= this.slideCount; i++) {
				if( this.currentSlide === i ) {
					imgs += '<a class="here">' + i + '</a>';
				} else {
					imgs += '<a href="" onclick="return gallery.moveSlide(' + i + ');">' + i + '</a>';
				}
			}
			$("#scrollies").html(imgs);
		}
	}

};

$(function() {

	gallery.init();

	// convert email addresses
	$("span.mail").each(function() {
		var address = $(this).text();
		address = address.replace(/ at /, "@");
		address = address.replace(/ dot /g, ".");
		var link = $(this).attr("title") || address;
		$(this).html('<a href="mailto:'+address+'">'+link+'</a>');
	});
});