	
	$numworks = $("#thecarousel ul li").length; // how many works?
	$("#thecarousel").width(62 * $numworks - 4); // sets the designercontent div width to hold all the works
	
	$("a#prevbutton").css("background-position", "-100px");	
	$("a#prevbutton").click(function() { return false; }); //stops the link being followed
	$("a#nextbutton").click(function() { return false; });	
	
	$("#thecarousel ul li a").click(function() { return false; });
	
	$("a#nextbutton").click(function(){
		$current = $("#thecarousel").css("left").replace(/[^\d\.]/g, '');
		if($current < 59 * $numworks + 10 - 373) {			
			$('#thecarousel').animate({
				'left' : "-=373px"
			})			
		} 
		if($current > 59 * $numworks + 10 - 473) {			
			$("a#nextbutton").css("background-position", "-100px");	
		}
		$current = $("#thecarousel").css("left").replace(/[^\d\.]/g, '');
		
		$("a#prevbutton").css("background-position", "13px 15px");
	});
	
	$("a#prevbutton").click(function(){
		$current = $("#thecarousel").css("left").replace(/[^\d\.]/g, '');
		if($current > 372) {			
			$('#thecarousel').animate({
				'left' : "+=373px"
			})			
		}
		$current = $("#thecarousel").css("left").replace(/[^\d\.]/g, '');
		if($current == 373) {			
			$("a#prevbutton").css("background-position", "-100px");		
		}
		$("a#nextbutton").css("background-position", "13px 15px");	
	});
	
	$("#thecarousel li a").click(function(){
			$imgsrc = $(this).attr("href");
			$imgclass = $(this).attr("rel");
			$imgalt = $(this).attr("title");
			$("#mainimage").fadeOut("slow",function() {
				$("#mainimage").html("<img src='"+ $imgsrc +"' class='"+ $imgclass +"'><p class='caption'>"+ $imgalt +"</p>");									
     		});			
			$("#mainimage").fadeIn("slow");
		$("#thecarousel li a").removeClass("theone");
		$(this).addClass("theone");

	});
	
	function is_int(value){
		if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
			return true;
		} else {
			return false;
		}
	}
	
	// the magic timer!
	$(function() {	
		var timer = setInterval( showSrc, 5000);		
		var counter = 0;
		$items = $("#thecarousel li a");
		
		function showSrc() {
			if(counter==$numworks-1) {
				$('#thecarousel').animate({
					'left' : "0"
				})
				$("a#prevbutton").css("background-position", "-100px");
				$("a#nextbutton").css("background-position", "13px 15px");	
			}
			counter == $numworks-1? counter = 0 : counter++;
			if(is_int(counter/6) && counter!=0) {
				if(counter < $numworks) {			
					$('#thecarousel').animate({
						'left' : "-=373px"
					})			
				}
				if($numworks < counter+7) {	
					$("a#nextbutton").css("background-position", "-100px");		
				}
				$("a#prevbutton").css("background-position", "13px 15px");
			}
			$imgsrc = $($items[counter]).attr("href");
			$imgclass = $($items[counter]).attr("rel");
			$imgalt = $($items[counter]).attr("title");
			$("#mainimage").fadeOut("slow",function() {
				$("#mainimage").html("<img src='"+ $imgsrc +"' class='"+ $imgclass +"'><p class='caption'>"+ $imgalt +"</p>");									
     		});			
			$("#mainimage").fadeIn("slow");
			$("#thecarousel li a").removeClass("theone");
			$($items[counter]).addClass("theone");
			
		}
		$("#thecarousel li a").click(function(){									
			clearInterval(timer);		  
		 });
		
	});	
	
	
	
