function loadBits(bits) {
	segs = bits.split(',');

	if(segs.length != 3) {
		return false;
	}

	for(i=0 ; i < segs.length ; i++) {
		segs[i] = parseInt(segs[i]);
	}

	// load a new page if we need to
	if(segs[0] > 1) {
		h = '/portfolio/thumbnails/P' + (10 * (segs[0] - 1) );
		$("#thumbs").block().load(h,function(){
			$("#thumbs").unblock();

			$("#thumb"+segs[1]).addClass("hi");

			// load the detail
			th = $("#thumb"+segs[1]).find('a').attr("href");
			$("#detail").block().load(th,function(){ $("#detail").unblock();slideshow.init();});

		});

	// or sort out the same page
	} else {

		// highlight the correct thumbnail
		$("#thumb"+segs[1]).addClass("hi");

		// load the detail
		th = $("#thumb"+segs[1]).find('a').attr("href");
		$("#detail").block().load(th,function(){ $("#detail").unblock();slideshow.init();});
	}

	return true;

}

$(function(){

	// thumbnail pagination
	$("#pagination a").live("click",function(){

		$("#thumbs").block().load($(this).attr("href"),function(){ $("#thumbs").unblock()});
		return false;
	})

	// thumbnail clicking
	$("#thumbs li a").live("click",function(){
		$("#detail").block().load($(this).attr("href"),function(){ $("#detail").unblock();slideshow.init();});
		$("#thumbs li").removeClass("hi");
		$(this).closest("li").addClass("hi");
		return false;
	})

	// image nav clicking
	$("#project-pagination a").live("click",function(){
		slideshow.nextSlide($(this).text());
		return false;
	});


	// go direct to a thumbnail if we have one
	bits = window.location.toString().split('#');
	if(typeof bits[1] != 'undefined') {

		if(!loadBits(bits[1])) {
			$("#thumbs li:first").addClass("hi");
		}

	// initial thumbnail highlighting
	} else {
		$("#thumbs li:first").addClass("hi");
		slideshow.init();
	}

})


var slideshow = {

	st: null, // timer

	sd: 5000, // delay

	cs: 0, // current slide

	init: function(n) {		
		clearTimeout ( this.st);
		slideshow.pics = new Array();
		 $("#project-images img").each(function(){
			slideshow.pics.push($(this).attr("src"));
		});

		if(this.pics.length < 2) {
			$("#project-images").html('<img src="'+this.pics[0]+'" alt="" id="slimg1"/>');
			$("#slimg1").show();
			return;
		}
		this.cs = 0;
		this.buildnav();

		$("#project-images").html('<img src="'+this.pics[0]+'" alt="" id="slimg1"/><img src="'+this.pics[0]+'" alt="" id="slimg2"/>');

		$("#slimg1").show();

		this.st = setTimeout ( "slideshow.nextSlide()", this.sd );
	},

	buildnav: function() {
		$("#project-pagination").html("");
		str = '';
		for(i = 1 ; i <= this.pics.length ; i++) {
			str = str + '<a href="#">'+i+"</a>";
		}
		$("#project-pagination").html('<p>'+str+'</p>');
		$("#project-pagination a:first").addClass("activeSlide");
	},

	nextSlide: function(forceNav) {
		clearTimeout ( this.st);

		if(typeof forceNav != "undefined") {
			this.cs = forceNav - 1;
		} else {
			this.cs ++;
			if(this.cs == this.pics.length) {
				this.cs = 0;
			}
		}

		$("#slimg2").attr({src:slideshow.pics[slideshow.cs]}).fadeIn(1000,function(){
			$("#slimg1").attr({src:slideshow.pics[slideshow.cs]});
			$("#slimg2").hide();
			$("#project-pagination a").removeClass("activeSlide");
			$("#project-pagination a").eq(slideshow.cs).addClass("activeSlide");
			slideshow.st = setTimeout ( "slideshow.nextSlide()", slideshow.sd );
		});

	}

}
