var clicked = false;
function loadImage(url) {
	$("#loader a").html("");
	$("#loader a").addClass('loading');

	var img = new Image();

	// wrap our new image in jQuery, then:
	$(img)
	// once the image has loaded, execute this code
	.load(function () {
	// set the image hidden by default
	$(this).hide();

	// with the holding div #loader, apply:
	$('#loader a')
	// remove the loading class (so no background spinner),
	.removeClass('loading')
	// then insert our image
	.append(this);

	// fade our image in to create a nice effect
	$(this).fadeIn();
	clicked = false;
	})

	// if there was an error loading the image, react accordingly
	.error(function () {
	// notify the user that the image could not be loaded
	})

	// *finally*, set the src attribute of the new image to our image
	.attr('src', url);
} 
$(function(){
	$(".slideshow a").live("click",function(){
		if( clicked ) return false;
		clicked = true;
		$(".inner_slide a").removeClass("selected");
		$(this).addClass("selected");
		loadImage($(this).attr("href"));
		var url = $("> a.siteurl",$(this).next()).attr("href");
		$("#loader a").attr("href",url);
		$(".description").html( $(this).next().html() );
		return false;
	});

	var curPos = 0;
	var size = $(".inner_slide a:visible").size();
	$(".next").click(function(){
		if(curPos <= (7-size)*91) return false;
		curPos -=91;
		$(".inner_slide").animate({left: curPos},500)
		return false;
	})
	$(".prev").click(function(){
		if(curPos >= 0) return false;
		curPos +=91;
		$(".inner_slide").animate({left: curPos},500)
		return false;
	})

	
	var element = $('.inner_slide'); // all the list elements
	var offset = 0;             // angle offset for animation
	var stepping =0;        // how fast the list rotates
	var list = $('.slideshow');      // the list wrapper
	var $list = $(list);

	$list.mousemove(function(e){
		var leftOfList = $list.eq(0).offset().left
		var listWidth = $list.width()
	   
		// Sets variable that controls the speed of rotation.
		stepping = (e.clientX - leftOfList) /  listWidth * 0.2 - 0.1;
	   
	});

	// call render function over and over
	setInterval(render, 20);

	var clonedRight = false;
	var clonedLeft = false;
	// Handles how and where each element will be rendered.
	function render(){
		var x = offset*10;
		var leftValue = (($list.width()/2) *  x / 100)-89;
		var floor =  Math.floor(leftValue / 89);

		if( floor >= -1 && !clonedLeft){
			$("> div",element).eq(6).clone().prependTo(element);
			clonedLeft = true;
		}
		if( floor >= 0){
			$("> div:last",element).remove();
			clonedLeft = false;
			leftValue = 0;
			offset = 0;
		}

		if( floor < -2){
			$("> div:first",element).remove();
			clonedRight = false;
			leftValue = -89;
			offset = 0;
		}
		if( floor <= -1 && !clonedRight){
			$("> div",element).eq(1).clone().appendTo(element);
			clonedRight = true;
		}

		$(element).css("left" ,leftValue);
		// Rotate the circle
		offset += stepping;
	}
});